9 "github.com/vapor/encoding/blockchain"
10 "github.com/vapor/encoding/bufpool"
11 "github.com/vapor/errors"
12 "github.com/vapor/protocol/bc"
20 func (p *Proof) readFrom(r *blockchain.Reader) (err error) {
21 if p.Sign, err = blockchain.ReadVarstr31(r); err != nil {
24 if p.ControlProgram, err = blockchain.ReadVarstr31(r); err != nil {
30 func (p *Proof) writeTo(w io.Writer) error {
31 if _, err := blockchain.WriteVarstr31(w, p.Sign); err != nil {
35 if _, err := blockchain.WriteVarstr31(w, p.ControlProgram); err != nil {
41 // BlockHeader defines information about a block and is used in the Bytom
42 type BlockHeader struct {
43 Version uint64 // The version of the block.
44 Height uint64 // The height of the block.
45 PreviousBlockHash bc.Hash // The hash of the previous block.
46 Timestamp uint64 // The time of the block in seconds.
51 // Time returns the time represented by the Timestamp in block header.
52 func (bh *BlockHeader) Time() time.Time {
53 return time.Unix(int64(bh.Timestamp), 0).UTC()
56 // Hash returns complete hash of the block header.
57 func (bh *BlockHeader) Hash() bc.Hash {
58 h, _ := mapBlockHeader(bh)
62 // MarshalText fulfills the json.Marshaler interface. This guarantees that
63 // block headers will get deserialized correctly when being parsed from HTTP
65 func (bh *BlockHeader) MarshalText() ([]byte, error) {
67 defer bufpool.Put(buf)
69 if _, err := bh.WriteTo(buf); err != nil {
73 enc := make([]byte, hex.EncodedLen(buf.Len()))
74 hex.Encode(enc, buf.Bytes())
78 // UnmarshalText fulfills the encoding.TextUnmarshaler interface.
79 func (bh *BlockHeader) UnmarshalText(text []byte) error {
80 decoded := make([]byte, hex.DecodedLen(len(text)))
81 if _, err := hex.Decode(decoded, text); err != nil {
85 _, err := bh.readFrom(blockchain.NewReader(decoded))
89 func (bh *BlockHeader) readFrom(r *blockchain.Reader) (serflag uint8, err error) {
91 io.ReadFull(r, serflags[:])
94 case SerBlockHeader, SerBlockFull:
96 return 0, fmt.Errorf("unsupported serialization flags 0x%x", serflags)
99 if bh.Version, err = blockchain.ReadVarint63(r); err != nil {
102 if bh.Height, err = blockchain.ReadVarint63(r); err != nil {
105 if _, err = bh.PreviousBlockHash.ReadFrom(r); err != nil {
108 if bh.Timestamp, err = blockchain.ReadVarint63(r); err != nil {
111 if _, err = blockchain.ReadExtensibleString(r, bh.BlockCommitment.readFrom); err != nil {
114 if _, err = blockchain.ReadExtensibleString(r, bh.Proof.readFrom); err != nil {
120 // WriteTo writes the block header to the input io.Writer
121 func (bh *BlockHeader) WriteTo(w io.Writer) (int64, error) {
122 ew := errors.NewWriter(w)
123 if err := bh.writeTo(ew, SerBlockHeader); err != nil {
126 return ew.Written(), ew.Err()
129 func (bh *BlockHeader) writeTo(w io.Writer, serflags uint8) (err error) {
130 w.Write([]byte{serflags})
131 if _, err = blockchain.WriteVarint63(w, bh.Version); err != nil {
134 if _, err = blockchain.WriteVarint63(w, bh.Height); err != nil {
137 if _, err = bh.PreviousBlockHash.WriteTo(w); err != nil {
140 if _, err = blockchain.WriteVarint63(w, bh.Timestamp); err != nil {
143 if _, err = blockchain.WriteExtensibleString(w, nil, bh.BlockCommitment.writeTo); err != nil {
146 if _, err = blockchain.WriteExtensibleString(w, nil, bh.Proof.writeTo); err != nil {