OSDN Git Service

ts to ms (#71)
[bytom/vapor.git] / protocol / bc / types / block_header.go
1 package types
2
3 import (
4         "encoding/hex"
5         "fmt"
6         "io"
7         "time"
8
9         "github.com/vapor/encoding/blockchain"
10         "github.com/vapor/encoding/bufpool"
11         "github.com/vapor/errors"
12         "github.com/vapor/protocol/bc"
13 )
14
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 milliseconds.
21         BlockCommitment
22         BlockWitness
23 }
24
25 // Time returns the time represented by the Timestamp in block header.
26 func (bh *BlockHeader) Time() time.Time {
27         return time.Unix(0, int64(bh.Timestamp)*int64(time.Millisecond)).UTC()
28 }
29
30 // Hash returns complete hash of the block header.
31 func (bh *BlockHeader) Hash() bc.Hash {
32         h, _ := mapBlockHeader(bh)
33         return h
34 }
35
36 // MarshalText fulfills the json.Marshaler interface. This guarantees that
37 // block headers will get deserialized correctly when being parsed from HTTP
38 // requests.
39 func (bh *BlockHeader) MarshalText() ([]byte, error) {
40         buf := bufpool.Get()
41         defer bufpool.Put(buf)
42
43         if _, err := bh.WriteTo(buf); err != nil {
44                 return nil, err
45         }
46
47         enc := make([]byte, hex.EncodedLen(buf.Len()))
48         hex.Encode(enc, buf.Bytes())
49         return enc, nil
50 }
51
52 // UnmarshalText fulfills the encoding.TextUnmarshaler interface.
53 func (bh *BlockHeader) UnmarshalText(text []byte) error {
54         decoded := make([]byte, hex.DecodedLen(len(text)))
55         if _, err := hex.Decode(decoded, text); err != nil {
56                 return err
57         }
58
59         _, err := bh.readFrom(blockchain.NewReader(decoded))
60         return err
61 }
62
63 func (bh *BlockHeader) readFrom(r *blockchain.Reader) (serflag uint8, err error) {
64         var serflags [1]byte
65         io.ReadFull(r, serflags[:])
66         serflag = serflags[0]
67         switch serflag {
68         case SerBlockHeader, SerBlockFull:
69         default:
70                 return 0, fmt.Errorf("unsupported serialization flags 0x%x", serflags)
71         }
72
73         if bh.Version, err = blockchain.ReadVarint63(r); err != nil {
74                 return 0, err
75         }
76         if bh.Height, err = blockchain.ReadVarint63(r); err != nil {
77                 return 0, err
78         }
79         if _, err = bh.PreviousBlockHash.ReadFrom(r); err != nil {
80                 return 0, err
81         }
82         if bh.Timestamp, err = blockchain.ReadVarint63(r); err != nil {
83                 return 0, err
84         }
85         if _, err = blockchain.ReadExtensibleString(r, bh.BlockCommitment.readFrom); err != nil {
86                 return 0, err
87         }
88
89         if _, err = blockchain.ReadExtensibleString(r, bh.BlockWitness.readFrom); err != nil {
90                 return 0, err
91         }
92         return
93 }
94
95 // WriteTo writes the block header to the input io.Writer
96 func (bh *BlockHeader) WriteTo(w io.Writer) (int64, error) {
97         ew := errors.NewWriter(w)
98         if err := bh.writeTo(ew, SerBlockHeader); err != nil {
99                 return 0, err
100         }
101         return ew.Written(), ew.Err()
102 }
103
104 func (bh *BlockHeader) writeTo(w io.Writer, serflags uint8) (err error) {
105         w.Write([]byte{serflags})
106         if _, err = blockchain.WriteVarint63(w, bh.Version); err != nil {
107                 return err
108         }
109         if _, err = blockchain.WriteVarint63(w, bh.Height); err != nil {
110                 return err
111         }
112         if _, err = bh.PreviousBlockHash.WriteTo(w); err != nil {
113                 return err
114         }
115         if _, err = blockchain.WriteVarint63(w, bh.Timestamp); err != nil {
116                 return err
117         }
118         if _, err = blockchain.WriteExtensibleString(w, nil, bh.BlockCommitment.writeTo); err != nil {
119                 return err
120         }
121
122         if _, err = blockchain.WriteExtensibleString(w, nil, bh.BlockWitness.writeTo); err != nil {
123                 return err
124         }
125         return nil
126 }