9 "github.com/vapor/encoding/blockchain"
10 "github.com/vapor/encoding/bufpool"
11 "github.com/vapor/errors"
12 "github.com/vapor/protocol/bc"
21 func (p *Proof) readFrom(r *blockchain.Reader) (err error) {
22 if p.Sign, err = blockchain.ReadVarstr31(r); err != nil {
25 if p.ControlProgram, err = blockchain.ReadVarstr31(r); err != nil {
28 if p.Address, err = blockchain.ReadVarstr31(r); err != nil {
34 func (p *Proof) writeTo(w io.Writer) error {
35 if _, err := blockchain.WriteVarstr31(w, p.Sign); err != nil {
39 if _, err := blockchain.WriteVarstr31(w, p.ControlProgram); err != nil {
42 if _, err := blockchain.WriteVarstr31(w, p.Address); err != nil {
48 // BlockHeader defines information about a block and is used in the Bytom
49 type BlockHeader struct {
50 Version uint64 // The version of the block.
51 Height uint64 // The height of the block.
52 PreviousBlockHash bc.Hash // The hash of the previous block.
53 Timestamp uint64 // The time of the block in seconds.
60 // Time returns the time represented by the Timestamp in block header.
61 func (bh *BlockHeader) Time() time.Time {
62 return time.Unix(int64(bh.Timestamp), 0).UTC()
65 // Hash returns complete hash of the block header.
66 func (bh *BlockHeader) Hash() bc.Hash {
67 h, _ := mapBlockHeader(bh)
71 // MarshalText fulfills the json.Marshaler interface. This guarantees that
72 // block headers will get deserialized correctly when being parsed from HTTP
74 func (bh *BlockHeader) MarshalText() ([]byte, error) {
76 defer bufpool.Put(buf)
78 if _, err := bh.WriteTo(buf); err != nil {
82 enc := make([]byte, hex.EncodedLen(buf.Len()))
83 hex.Encode(enc, buf.Bytes())
87 // UnmarshalText fulfills the encoding.TextUnmarshaler interface.
88 func (bh *BlockHeader) UnmarshalText(text []byte) error {
89 decoded := make([]byte, hex.DecodedLen(len(text)))
90 if _, err := hex.Decode(decoded, text); err != nil {
94 _, err := bh.readFrom(blockchain.NewReader(decoded))
98 func (bh *BlockHeader) readFrom(r *blockchain.Reader) (serflag uint8, err error) {
100 io.ReadFull(r, serflags[:])
101 serflag = serflags[0]
103 case SerBlockHeader, SerBlockFull:
105 return 0, fmt.Errorf("unsupported serialization flags 0x%x", serflags)
108 if bh.Version, err = blockchain.ReadVarint63(r); err != nil {
111 if bh.Height, err = blockchain.ReadVarint63(r); err != nil {
114 if _, err = bh.PreviousBlockHash.ReadFrom(r); err != nil {
117 if bh.Timestamp, err = blockchain.ReadVarint63(r); err != nil {
120 if bh.Coinbase, err = blockchain.ReadVarstr31(r); err != nil {
123 if _, err = blockchain.ReadExtensibleString(r, bh.BlockCommitment.readFrom); err != nil {
126 if _, err = blockchain.ReadExtensibleString(r, bh.Proof.readFrom); err != nil {
129 if bh.Extra, err = blockchain.ReadVarstr31(r); err != nil {
135 // WriteTo writes the block header to the input io.Writer
136 func (bh *BlockHeader) WriteTo(w io.Writer) (int64, error) {
137 ew := errors.NewWriter(w)
138 if err := bh.writeTo(ew, SerBlockHeader); err != nil {
141 return ew.Written(), ew.Err()
144 func (bh *BlockHeader) writeTo(w io.Writer, serflags uint8) (err error) {
145 w.Write([]byte{serflags})
146 if _, err = blockchain.WriteVarint63(w, bh.Version); err != nil {
149 if _, err = blockchain.WriteVarint63(w, bh.Height); err != nil {
152 if _, err = bh.PreviousBlockHash.WriteTo(w); err != nil {
155 if _, err = blockchain.WriteVarint63(w, bh.Timestamp); err != nil {
158 if _, err := blockchain.WriteVarstr31(w, bh.Coinbase); err != nil {
161 if _, err = blockchain.WriteExtensibleString(w, nil, bh.BlockCommitment.writeTo); err != nil {
164 if _, err = blockchain.WriteExtensibleString(w, nil, bh.Proof.writeTo); err != nil {
167 if _, err = blockchain.WriteVarstr31(w, bh.Extra); err != nil {