10 "github.com/davecgh/go-spew/spew"
12 "github.com/vapor/encoding/blockchain"
13 "github.com/vapor/errors"
14 "github.com/vapor/testutil"
17 func TestBlockHeader(t *testing.T) {
18 blockHeader := &BlockHeader{
21 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
22 Timestamp: 1522908275,
23 BlockCommitment: BlockCommitment{
24 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
25 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
29 wantHex := strings.Join([]string{
30 "01", // serialization flags
32 "eab01a", // block height
33 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
34 "f3f896d605", // timestamp
35 "40", // commitment extensible field length
36 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
37 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
40 gotHex := testutil.Serialize(t, blockHeader)
41 want, err := hex.DecodeString(wantHex)
46 if !bytes.Equal(gotHex, want) {
47 t.Errorf("empty block header bytes = %x want %x", gotHex, want)
50 gotBlockHeader := BlockHeader{}
51 if _, err := gotBlockHeader.readFrom(blockchain.NewReader(want)); err != nil {
55 if !testutil.DeepEqual(gotBlockHeader, *blockHeader) {
56 t.Errorf("got:\n%s\nwant:\n%s", spew.Sdump(gotBlockHeader), spew.Sdump(*blockHeader))
60 func TestMarshalBlockHeader(t *testing.T) {
62 blockHeader *BlockHeader
67 blockHeader: &BlockHeader{
70 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
71 Timestamp: 1528945000,
72 BlockCommitment: BlockCommitment{
73 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
74 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
77 wantHex: strings.Join([]string{
78 "01", // serialization flags
80 "904e", // block height
81 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
82 "e8b287d905", // timestamp
83 "40", // commitment extensible field length
84 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
85 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
89 blockHeader: &BlockHeader{
91 Height: 9223372036854775808, // Height > MaxInt64(9223372036854775807)
92 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
93 Timestamp: 1528945000,
94 BlockCommitment: BlockCommitment{
95 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
96 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
99 wantError: blockchain.ErrRange,
102 blockHeader: &BlockHeader{
105 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
106 Timestamp: 9223372036854775808, // Timestamp > MaxInt64(9223372036854775807)
107 BlockCommitment: BlockCommitment{
108 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
109 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
112 wantError: blockchain.ErrRange,
115 blockHeader: &BlockHeader{
117 Height: 9223372036854775807, // MaxInt64(9223372036854775807)
118 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
119 Timestamp: 1528945000,
121 wantHex: strings.Join([]string{
122 "01", // serialization flags
124 "ffffffffffffffff7f", // block height
125 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
126 "e8b287d905", // timestamp
127 "40", // commitment extensible field length
128 "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
129 "0000000000000000000000000000000000000000000000000000000000000000", // tx status hash
134 for i, test := range cases {
135 got, err := test.blockHeader.MarshalText()
136 if err != nil && err != test.wantError {
137 t.Errorf("test %d: got error = %x, want = %x", i, err, test.wantError)
138 } else if err != nil && err == test.wantError {
142 if string(got) != test.wantHex {
143 t.Errorf("test %d: got strbytes = %s, want %s", i, string(got), test.wantHex)
146 resultBlockHeader := &BlockHeader{}
147 if err := resultBlockHeader.UnmarshalText(got); err != nil {
151 if !testutil.DeepEqual(*resultBlockHeader, *test.blockHeader) {
152 t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(*resultBlockHeader), spew.Sdump(*test.blockHeader))
157 func TestUnmarshalBlockHeader(t *testing.T) {
159 hexBlockHeader string
160 wantBlockHeader *BlockHeader
164 hexBlockHeader: strings.Join([]string{
165 "01", // serialization flags (SerBlockHeader = 01)
167 "904e", // block height
168 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
169 "e8b287d905", // timestamp
170 "40", // commitment extensible field length
171 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
172 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
174 wantBlockHeader: &BlockHeader{
177 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
178 Timestamp: 1528945000,
179 BlockCommitment: BlockCommitment{
180 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
181 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
186 hexBlockHeader: strings.Join([]string{
187 "03", // serialization flags (SerBlockFull = 03)
189 "904e", // block height
190 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
191 "e8b287d905", // timestamp
192 "40", // commitment extensible field length
193 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
194 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
196 wantBlockHeader: &BlockHeader{
199 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
200 Timestamp: 1528945000,
201 BlockCommitment: BlockCommitment{
202 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
203 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
208 hexBlockHeader: strings.Join([]string{
209 "02", // serialization flags (SerBlockTransactions = 02)
211 "904e", // block height
212 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
213 "e8b287d905", // timestamp
214 "40", // commitment extensible field length
215 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
216 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
218 wantError: fmt.Errorf("unsupported serialization flags 0x02"),
221 hexBlockHeader: strings.Join([]string{
222 "01", // serialization flags
224 "908", // block height (error with odd length)
225 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
226 "e8b287d905", // timestamp
227 "40", // commitment extensible field length
228 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
229 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
231 wantError: hex.ErrLength,
234 hexBlockHeader: strings.Join([]string{
235 "01", // serialization flags
237 "ffffffffffffffffff", // block height
238 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
239 "e8b287d905", // timestamp
240 "40", // commitment extensible field length
241 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
242 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
244 wantError: errors.New("binary: varint overflows a 64-bit integer"),
247 hexBlockHeader: strings.Join([]string{
248 "01", // serialization flags
250 "ffffffffffffffff7f", // block height
251 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
252 "e8b287d905", // timestamp
253 "40", // commitment extensible field length
254 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
255 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a14", // tx status hash
257 wantError: fmt.Errorf("unexpected EOF"),
260 hexBlockHeader: strings.Join([]string{
261 "01", // serialization flags
263 "ffffffffffffffff7f", // block height
264 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
265 "e8b287d905", // timestamp
266 "40", // commitment extensible field length
267 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
268 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
270 wantBlockHeader: &BlockHeader{
272 Height: 9223372036854775807, // MaxInt64(9223372036854775807)
273 PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
274 Timestamp: 1528945000,
275 BlockCommitment: BlockCommitment{
276 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
277 TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
283 for i, test := range cases {
284 resultBlockHeader := &BlockHeader{}
285 err := resultBlockHeader.UnmarshalText([]byte(test.hexBlockHeader))
286 if err != nil && err.Error() != test.wantError.Error() {
287 t.Errorf("test %d: got error = %s, want = %s", i, err.Error(), test.wantError.Error())
288 } else if err != nil && err.Error() == test.wantError.Error() {
291 if !testutil.DeepEqual(*resultBlockHeader, *test.wantBlockHeader) {
292 t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(*resultBlockHeader), spew.Sdump(*test.wantBlockHeader))