OSDN Git Service

fix conflict
[bytom/bytom.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/bytom/bytom/encoding/blockchain"
10         "github.com/bytom/bytom/encoding/bufpool"
11         "github.com/bytom/bytom/errors"
12         "github.com/bytom/bytom/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 seconds.
21         BlockWitness
22         BlockCommitment
23 }
24
25 // Hash returns complete hash of the block header.
26 func (bh *BlockHeader) Hash() bc.Hash {
27         h, _ := mapBlockHeader(bh)
28         return h
29 }
30
31 // Time returns the time represented by the Timestamp in block header.
32 func (bh *BlockHeader) Time() time.Time {
33         return time.Unix(int64(bh.Timestamp/1000), 0).UTC()
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         serflag, err := bh.readFrom(blockchain.NewReader(decoded))
60         if err != nil {
61                 return err
62         }
63
64         if serflag == SerBlockTransactions {
65                 return fmt.Errorf("unsupported serialization flags 0x%02x", serflag)
66         }
67
68         return nil
69 }
70
71 // WriteTo writes the block header to the input io.Writer
72 func (bh *BlockHeader) WriteTo(w io.Writer) (int64, error) {
73         ew := errors.NewWriter(w)
74         if err := bh.writeTo(ew, SerBlockHeader); err != nil {
75                 return 0, err
76         }
77         return ew.Written(), ew.Err()
78 }
79
80 func (bh *BlockHeader) readFrom(r *blockchain.Reader) (serflag uint8, err error) {
81         var serflags [1]byte
82         if _, err := io.ReadFull(r, serflags[:]); err != nil {
83                 return 0, err
84         }
85
86         serflag = serflags[0]
87         switch serflag {
88         case SerBlockHeader, SerBlockFull:
89         case SerBlockTransactions:
90                 return
91         default:
92                 return 0, fmt.Errorf("unsupported serialization flags 0x%x", serflags)
93         }
94
95         if bh.Version, err = blockchain.ReadVarint63(r); err != nil {
96                 return 0, err
97         }
98
99         if bh.Height, err = blockchain.ReadVarint63(r); err != nil {
100                 return 0, err
101         }
102
103         if _, err = bh.PreviousBlockHash.ReadFrom(r); err != nil {
104                 return 0, err
105         }
106
107         if bh.Timestamp, err = blockchain.ReadVarint63(r); err != nil {
108                 return 0, err
109         }
110
111         if _, err = blockchain.ReadExtensibleString(r, bh.BlockCommitment.readFrom); err != nil {
112                 return 0, err
113         }
114
115         if _, err = blockchain.ReadExtensibleString(r, bh.BlockWitness.readFrom); err != nil {
116                 return 0, err
117         }
118
119         if _, err = blockchain.ReadExtensibleString(r, bh.SupLinks.readFrom); err != nil {
120                 return 0, err
121         }
122
123         return
124 }
125
126 func (bh *BlockHeader) writeTo(w io.Writer, serflags uint8) (err error) {
127         w.Write([]byte{serflags})
128         if serflags == SerBlockTransactions {
129                 return nil
130         }
131
132         if _, err = blockchain.WriteVarint63(w, bh.Version); err != nil {
133                 return err
134         }
135
136         if _, err = blockchain.WriteVarint63(w, bh.Height); err != nil {
137                 return err
138         }
139
140         if _, err = bh.PreviousBlockHash.WriteTo(w); err != nil {
141                 return err
142         }
143
144         if _, err = blockchain.WriteVarint63(w, bh.Timestamp); err != nil {
145                 return err
146         }
147
148         if _, err = blockchain.WriteExtensibleString(w, nil, bh.BlockCommitment.writeTo); err != nil {
149                 return err
150         }
151
152         if _, err = blockchain.WriteExtensibleString(w, nil, bh.BlockWitness.writeTo); err != nil {
153                 return err
154         }
155
156         if _, err = blockchain.WriteExtensibleString(w, nil, bh.SupLinks.writeTo); err != nil {
157                 return err
158         }
159
160         return
161 }