9 "github.com/bytom/encoding/blockchain"
10 "github.com/bytom/encoding/bufpool"
11 "github.com/bytom/errors"
12 "github.com/bytom/protocol/bc"
15 // BlockHeader defines information about a block and is used in the Bytom
16 type BlockHeader struct {
17 Version uint64 // The version of the block.
18 Height uint64 // The height of the block.
19 PreviousBlockHash bc.Hash // The hash of the previous block.
20 Timestamp uint64 // The time of the block in seconds.
21 Nonce uint64 // Nonce used to generate the block.
22 Bits uint64 // Difficulty target for the block.
26 // Time returns the time represented by the Timestamp in block header.
27 func (bh *BlockHeader) Time() time.Time {
28 return time.Unix(int64(bh.Timestamp), 0).UTC()
31 // Hash returns complete hash of the block header.
32 func (bh *BlockHeader) Hash() bc.Hash {
33 h, _ := mapBlockHeader(bh)
37 // MarshalText fulfills the json.Marshaler interface. This guarantees that
38 // block headers will get deserialized correctly when being parsed from HTTP
40 func (bh *BlockHeader) MarshalText() ([]byte, error) {
42 defer bufpool.Put(buf)
44 if _, err := bh.WriteTo(buf); err != nil {
48 enc := make([]byte, hex.EncodedLen(buf.Len()))
49 hex.Encode(enc, buf.Bytes())
53 // UnmarshalText fulfills the encoding.TextUnmarshaler interface.
54 func (bh *BlockHeader) UnmarshalText(text []byte) error {
55 decoded := make([]byte, hex.DecodedLen(len(text)))
56 if _, err := hex.Decode(decoded, text); err != nil {
60 _, err := bh.readFrom(blockchain.NewReader(decoded))
64 func (bh *BlockHeader) readFrom(r *blockchain.Reader) (serflag uint8, err error) {
66 io.ReadFull(r, serflags[:])
69 case SerBlockHeader, SerBlockFull:
71 return 0, fmt.Errorf("unsupported serialization flags 0x%x", serflags)
74 if bh.Version, err = blockchain.ReadVarint63(r); err != nil {
77 if bh.Height, err = blockchain.ReadVarint63(r); err != nil {
80 if _, err = bh.PreviousBlockHash.ReadFrom(r); err != nil {
83 if bh.Timestamp, err = blockchain.ReadVarint63(r); err != nil {
86 if _, err = blockchain.ReadExtensibleString(r, bh.BlockCommitment.readFrom); err != nil {
89 if bh.Nonce, err = blockchain.ReadVarint63(r); err != nil {
92 if bh.Bits, err = blockchain.ReadVarint63(r); err != nil {
98 // WriteTo writes the block header to the input io.Writer
99 func (bh *BlockHeader) WriteTo(w io.Writer) (int64, error) {
100 ew := errors.NewWriter(w)
101 if err := bh.writeTo(ew, SerBlockHeader); err != nil {
104 return ew.Written(), ew.Err()
107 func (bh *BlockHeader) writeTo(w io.Writer, serflags uint8) (err error) {
108 w.Write([]byte{serflags})
109 if _, err = blockchain.WriteVarint63(w, bh.Version); err != nil {
112 if _, err = blockchain.WriteVarint63(w, bh.Height); err != nil {
115 if _, err = bh.PreviousBlockHash.WriteTo(w); err != nil {
118 if _, err = blockchain.WriteVarint63(w, bh.Timestamp); err != nil {
121 if _, err = blockchain.WriteExtensibleString(w, nil, bh.BlockCommitment.writeTo); err != nil {
124 if _, err = blockchain.WriteVarint63(w, bh.Nonce); err != nil {
127 if _, err = blockchain.WriteVarint63(w, bh.Bits); err != nil {