Official Go implementation of the Bytom protocol
Revision | defcab2c2be439f61fcb78d1e29dd649b2285b10 (tree) |
---|---|
Time | 2019-03-26 12:13:44 |
Author | Chengcheng Zhang <943420582@qq.c...> |
Commiter | Chengcheng Zhang |
Merge branch 'dev' into dev-entryid
@@ -0,0 +1,38 @@ | ||
1 | +package bc | |
2 | + | |
3 | +import ( | |
4 | + "testing" | |
5 | +) | |
6 | + | |
7 | +func TestSigHash(t *testing.T) { | |
8 | + cases := []struct { | |
9 | + tx *Tx | |
10 | + wantHash string | |
11 | + }{ | |
12 | + { | |
13 | + tx: &Tx{ | |
14 | + ID: Hash{V0: 13464118406972499748, V1: 5083224803004805715, V2: 16263625389659454272, V3: 9428032044180324575}, | |
15 | + InputIDs: []Hash{ | |
16 | + {V0: 14760873410800997144, V1: 1698395500822741684, V2: 5965908492734661392, V3: 9445539829830863994}, | |
17 | + }, | |
18 | + }, | |
19 | + wantHash: "17dfad182df66212f6f694d774285e5989c5d9d1add6d5ce51a5930dbef360d8", | |
20 | + }, | |
21 | + { | |
22 | + tx: &Tx{ | |
23 | + ID: Hash{V0: 17091584763764411831, V1: 2315724244669489432, V2: 4322938623810388342, V3: 11167378497724951792}, | |
24 | + InputIDs: []Hash{ | |
25 | + {V0: 6970879411704044573, V1: 10086395903308657573, V2: 10107608596190358115, V3: 8645856247221333302}, | |
26 | + }, | |
27 | + }, | |
28 | + wantHash: "f650ba3a58f90d3a2215f6c50a692a86c621b7968bb2a059a4c8e0c819770430", | |
29 | + }, | |
30 | + } | |
31 | + | |
32 | + for _, c := range cases { | |
33 | + gotHash := c.tx.SigHash(0) | |
34 | + if gotHash.String() != c.wantHash { | |
35 | + t.Errorf("got hash:%s, want hash:%s", gotHash.String(), c.wantHash) | |
36 | + } | |
37 | + } | |
38 | +} |
@@ -0,0 +1,54 @@ | ||
1 | +package types | |
2 | + | |
3 | +import ( | |
4 | + "bytes" | |
5 | + "encoding/hex" | |
6 | + "testing" | |
7 | + | |
8 | + "github.com/bytom/encoding/blockchain" | |
9 | + "github.com/bytom/testutil" | |
10 | +) | |
11 | + | |
12 | +func TestReadWriteBlockCommitment(t *testing.T) { | |
13 | + cases := []struct { | |
14 | + bc BlockCommitment | |
15 | + hexString string | |
16 | + }{ | |
17 | + { | |
18 | + bc: BlockCommitment{ | |
19 | + TransactionsMerkleRoot: testutil.MustDecodeHash("35a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad2"), | |
20 | + TransactionStatusHash: testutil.MustDecodeHash("6978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a"), | |
21 | + }, | |
22 | + hexString: "35a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad26978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a", | |
23 | + }, | |
24 | + { | |
25 | + bc: BlockCommitment{ | |
26 | + TransactionsMerkleRoot: testutil.MustDecodeHash("8ec3ee7589f95eee9b534f71fcd37142bcc839a0dbfe78124df9663827b90c35"), | |
27 | + TransactionStatusHash: testutil.MustDecodeHash("011bd3380852b2946df507e0c6234222c559eec8f545e4bc58a89e960892259b"), | |
28 | + }, | |
29 | + hexString: "8ec3ee7589f95eee9b534f71fcd37142bcc839a0dbfe78124df9663827b90c35011bd3380852b2946df507e0c6234222c559eec8f545e4bc58a89e960892259b", | |
30 | + }, | |
31 | + } | |
32 | + | |
33 | + for _, c := range cases { | |
34 | + buff := []byte{} | |
35 | + buffer := bytes.NewBuffer(buff) | |
36 | + if err := c.bc.writeTo(buffer); err != nil { | |
37 | + t.Fatal(err) | |
38 | + } | |
39 | + | |
40 | + hexString := hex.EncodeToString(buffer.Bytes()) | |
41 | + if hexString != c.hexString { | |
42 | + t.Errorf("test write block commitment fail, got:%s, want:%s", hexString, c.hexString) | |
43 | + } | |
44 | + | |
45 | + bc := &BlockCommitment{} | |
46 | + if err := bc.readFrom(blockchain.NewReader(buffer.Bytes())); err != nil { | |
47 | + t.Fatal(err) | |
48 | + } | |
49 | + | |
50 | + if !testutil.DeepEqual(*bc, c.bc) { | |
51 | + t.Errorf("test read block commitment fail, got:%v, want:%v", *bc, c.bc) | |
52 | + } | |
53 | + } | |
54 | +} |
@@ -3,12 +3,14 @@ package types | ||
3 | 3 | import ( |
4 | 4 | "bytes" |
5 | 5 | "encoding/hex" |
6 | + "fmt" | |
6 | 7 | "strings" |
7 | 8 | "testing" |
8 | 9 | |
9 | 10 | "github.com/davecgh/go-spew/spew" |
10 | 11 | |
11 | 12 | "github.com/bytom/encoding/blockchain" |
13 | + "github.com/bytom/errors" | |
12 | 14 | "github.com/bytom/testutil" |
13 | 15 | ) |
14 | 16 |
@@ -58,3 +60,318 @@ func TestBlockHeader(t *testing.T) { | ||
58 | 60 | t.Errorf("got:\n%s\nwant:\n%s", spew.Sdump(gotBlockHeader), spew.Sdump(*blockHeader)) |
59 | 61 | } |
60 | 62 | } |
63 | + | |
64 | +func TestMarshalBlockHeader(t *testing.T) { | |
65 | + cases := []struct { | |
66 | + blockHeader *BlockHeader | |
67 | + wantHex string | |
68 | + wantError error | |
69 | + }{ | |
70 | + { | |
71 | + blockHeader: &BlockHeader{ | |
72 | + Version: 1, | |
73 | + Height: 10000, | |
74 | + PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"), | |
75 | + Timestamp: 1528945000, | |
76 | + Nonce: 9253507043297, | |
77 | + Bits: 2305843009214532812, | |
78 | + BlockCommitment: BlockCommitment{ | |
79 | + TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"), | |
80 | + TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"), | |
81 | + }, | |
82 | + }, | |
83 | + wantHex: strings.Join([]string{ | |
84 | + "01", // serialization flags | |
85 | + "01", // version | |
86 | + "904e", // block height | |
87 | + "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash | |
88 | + "e8b287d905", // timestamp | |
89 | + "40", // commitment extensible field length | |
90 | + "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root | |
91 | + "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash | |
92 | + "e19f8080a88d02", // nonce | |
93 | + "cc99b3808080808020", // bits | |
94 | + }, ""), | |
95 | + }, | |
96 | + { | |
97 | + blockHeader: &BlockHeader{ | |
98 | + Version: 1, | |
99 | + Height: 9223372036854775808, // Height > MaxInt64(9223372036854775807) | |
100 | + PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"), | |
101 | + Timestamp: 1528945000, | |
102 | + Nonce: 9253507043297, | |
103 | + Bits: 2305843009222082559, | |
104 | + BlockCommitment: BlockCommitment{ | |
105 | + TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"), | |
106 | + TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"), | |
107 | + }, | |
108 | + }, | |
109 | + wantError: blockchain.ErrRange, | |
110 | + }, | |
111 | + { | |
112 | + blockHeader: &BlockHeader{ | |
113 | + Version: 1, | |
114 | + Height: 10000, | |
115 | + PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"), | |
116 | + Timestamp: 9223372036854775808, // Timestamp > MaxInt64(9223372036854775807) | |
117 | + Nonce: 9253507043297, | |
118 | + Bits: 2305843009222082559, | |
119 | + BlockCommitment: BlockCommitment{ | |
120 | + TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"), | |
121 | + TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"), | |
122 | + }, | |
123 | + }, | |
124 | + wantError: blockchain.ErrRange, | |
125 | + }, | |
126 | + { | |
127 | + blockHeader: &BlockHeader{ | |
128 | + Version: 1, | |
129 | + Height: 20000, | |
130 | + PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"), | |
131 | + Timestamp: 1528945000, | |
132 | + Nonce: 9223372036854775808, // Nonce > MaxInt64(9223372036854775807) | |
133 | + Bits: 2305843009222082559, | |
134 | + BlockCommitment: BlockCommitment{ | |
135 | + TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"), | |
136 | + TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"), | |
137 | + }, | |
138 | + }, | |
139 | + wantError: blockchain.ErrRange, | |
140 | + }, | |
141 | + { | |
142 | + blockHeader: &BlockHeader{ | |
143 | + Version: 1, | |
144 | + Height: 20000, | |
145 | + PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"), | |
146 | + Timestamp: 1528945000, | |
147 | + Nonce: 9253507043297, | |
148 | + Bits: 9223372036854775808, // Bits > MaxInt64(9223372036854775807) | |
149 | + BlockCommitment: BlockCommitment{ | |
150 | + TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"), | |
151 | + TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"), | |
152 | + }, | |
153 | + }, | |
154 | + wantError: blockchain.ErrRange, | |
155 | + }, | |
156 | + { | |
157 | + blockHeader: &BlockHeader{ | |
158 | + Version: 1, | |
159 | + Height: 9223372036854775807, // MaxInt64(9223372036854775807) | |
160 | + PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"), | |
161 | + Timestamp: 1528945000, | |
162 | + Nonce: 9253507043297, | |
163 | + Bits: 2305843009214532812, | |
164 | + }, | |
165 | + wantHex: strings.Join([]string{ | |
166 | + "01", // serialization flags | |
167 | + "01", // version | |
168 | + "ffffffffffffffff7f", // block height | |
169 | + "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash | |
170 | + "e8b287d905", // timestamp | |
171 | + "40", // commitment extensible field length | |
172 | + "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root | |
173 | + "0000000000000000000000000000000000000000000000000000000000000000", // tx status hash | |
174 | + "e19f8080a88d02", // nonce | |
175 | + "cc99b3808080808020", // bits | |
176 | + }, ""), | |
177 | + }, | |
178 | + } | |
179 | + | |
180 | + for i, test := range cases { | |
181 | + got, err := test.blockHeader.MarshalText() | |
182 | + if err != nil && err != test.wantError { | |
183 | + t.Errorf("test %d: got error = %x, want = %x", i, err, test.wantError) | |
184 | + } else if err != nil && err == test.wantError { | |
185 | + continue | |
186 | + } | |
187 | + | |
188 | + if string(got) != test.wantHex { | |
189 | + t.Errorf("test %d: got strbytes = %s, want %s", i, string(got), test.wantHex) | |
190 | + } | |
191 | + | |
192 | + resultBlockHeader := &BlockHeader{} | |
193 | + if err := resultBlockHeader.UnmarshalText(got); err != nil { | |
194 | + t.Fatal(err) | |
195 | + } | |
196 | + | |
197 | + if !testutil.DeepEqual(*resultBlockHeader, *test.blockHeader) { | |
198 | + t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(*resultBlockHeader), spew.Sdump(*test.blockHeader)) | |
199 | + } | |
200 | + } | |
201 | +} | |
202 | + | |
203 | +func TestUnmarshalBlockHeader(t *testing.T) { | |
204 | + cases := []struct { | |
205 | + hexBlockHeader string | |
206 | + wantBlockHeader *BlockHeader | |
207 | + wantError error | |
208 | + }{ | |
209 | + { | |
210 | + hexBlockHeader: strings.Join([]string{ | |
211 | + "01", // serialization flags (SerBlockHeader = 01) | |
212 | + "01", // version | |
213 | + "904e", // block height | |
214 | + "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash | |
215 | + "e8b287d905", // timestamp | |
216 | + "40", // commitment extensible field length | |
217 | + "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root | |
218 | + "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash | |
219 | + "e19f8080a88d02", // nonce | |
220 | + "cc99b3808080808020", // bits | |
221 | + }, ""), | |
222 | + wantBlockHeader: &BlockHeader{ | |
223 | + Version: 1, | |
224 | + Height: 10000, | |
225 | + PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"), | |
226 | + Timestamp: 1528945000, | |
227 | + Nonce: 9253507043297, | |
228 | + Bits: 2305843009214532812, | |
229 | + BlockCommitment: BlockCommitment{ | |
230 | + TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"), | |
231 | + TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"), | |
232 | + }, | |
233 | + }, | |
234 | + }, | |
235 | + { | |
236 | + hexBlockHeader: strings.Join([]string{ | |
237 | + "03", // serialization flags (SerBlockFull = 03) | |
238 | + "01", // version | |
239 | + "904e", // block height | |
240 | + "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash | |
241 | + "e8b287d905", // timestamp | |
242 | + "40", // commitment extensible field length | |
243 | + "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root | |
244 | + "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash | |
245 | + "e19f8080a88d02", // nonce | |
246 | + "cc99b3808080808020", // bits | |
247 | + }, ""), | |
248 | + wantBlockHeader: &BlockHeader{ | |
249 | + Version: 1, | |
250 | + Height: 10000, | |
251 | + PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"), | |
252 | + Timestamp: 1528945000, | |
253 | + Nonce: 9253507043297, | |
254 | + Bits: 2305843009214532812, | |
255 | + BlockCommitment: BlockCommitment{ | |
256 | + TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"), | |
257 | + TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"), | |
258 | + }, | |
259 | + }, | |
260 | + }, | |
261 | + { | |
262 | + hexBlockHeader: strings.Join([]string{ | |
263 | + "02", // serialization flags (SerBlockTransactions = 02) | |
264 | + "01", // version | |
265 | + "904e", // block height | |
266 | + "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash | |
267 | + "e8b287d905", // timestamp | |
268 | + "40", // commitment extensible field length | |
269 | + "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root | |
270 | + "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash | |
271 | + "e19f8080a88d02", // nonce | |
272 | + "cc99b3808080808020", // bits | |
273 | + }, ""), | |
274 | + wantError: fmt.Errorf("unsupported serialization flags 0x02"), | |
275 | + }, | |
276 | + { | |
277 | + hexBlockHeader: strings.Join([]string{ | |
278 | + "01", // serialization flags | |
279 | + "01", // version | |
280 | + "908", // block height (error with odd length) | |
281 | + "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash | |
282 | + "e8b287d905", // timestamp | |
283 | + "40", // commitment extensible field length | |
284 | + "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root | |
285 | + "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash | |
286 | + "e19f8080a88d02", // nonce | |
287 | + "cc99b3808080808020", // bits | |
288 | + }, ""), | |
289 | + wantError: hex.ErrLength, | |
290 | + }, | |
291 | + { | |
292 | + hexBlockHeader: strings.Join([]string{ | |
293 | + "01", // serialization flags | |
294 | + "01", // version | |
295 | + "ffffffffffffffff7f", // block height | |
296 | + "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash | |
297 | + "e8b287d905", // timestamp | |
298 | + "40", // commitment extensible field length | |
299 | + "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root | |
300 | + "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash | |
301 | + "e19f8080a88d02", // nonce | |
302 | + "ffffffffffffffffff20", // bits | |
303 | + }, ""), | |
304 | + wantError: errors.New("binary: varint overflows a 64-bit integer"), | |
305 | + }, | |
306 | + { | |
307 | + hexBlockHeader: strings.Join([]string{ | |
308 | + "01", // serialization flags | |
309 | + "01", // version | |
310 | + "ffffffffffffffff7f", // block height | |
311 | + "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash | |
312 | + "e8b287d905", // timestamp | |
313 | + "40", // commitment extensible field length | |
314 | + "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root | |
315 | + "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash | |
316 | + "e19f8080a88d02", // nonce | |
317 | + "ffffffffffffffff80", // bits | |
318 | + }, ""), | |
319 | + wantError: fmt.Errorf("EOF"), | |
320 | + }, | |
321 | + { | |
322 | + hexBlockHeader: strings.Join([]string{ | |
323 | + "01", // serialization flags | |
324 | + "01", // version | |
325 | + "ffffffffffffffff7f", // block height | |
326 | + "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash | |
327 | + "e8b287d905", // timestamp | |
328 | + "40", // commitment extensible field length | |
329 | + "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root | |
330 | + "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash | |
331 | + "e19f8080a88d02", // nonce | |
332 | + "ffffffffffffffffff01", // bits | |
333 | + }, ""), | |
334 | + wantError: blockchain.ErrRange, | |
335 | + }, | |
336 | + { | |
337 | + hexBlockHeader: strings.Join([]string{ | |
338 | + "01", // serialization flags | |
339 | + "01", // version | |
340 | + "ffffffffffffffff7f", // block height | |
341 | + "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash | |
342 | + "e8b287d905", // timestamp | |
343 | + "40", // commitment extensible field length | |
344 | + "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root | |
345 | + "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash | |
346 | + "e19f8080a88d02", // nonce | |
347 | + "cc99b3808080808020", // bits | |
348 | + }, ""), | |
349 | + wantBlockHeader: &BlockHeader{ | |
350 | + Version: 1, | |
351 | + Height: 9223372036854775807, // MaxInt64(9223372036854775807) | |
352 | + PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"), | |
353 | + Timestamp: 1528945000, | |
354 | + Nonce: 9253507043297, | |
355 | + Bits: 2305843009214532812, | |
356 | + BlockCommitment: BlockCommitment{ | |
357 | + TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"), | |
358 | + TransactionStatusHash: testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"), | |
359 | + }, | |
360 | + }, | |
361 | + }, | |
362 | + } | |
363 | + | |
364 | + for i, test := range cases { | |
365 | + resultBlockHeader := &BlockHeader{} | |
366 | + err := resultBlockHeader.UnmarshalText([]byte(test.hexBlockHeader)) | |
367 | + if err != nil && err.Error() != test.wantError.Error() { | |
368 | + t.Errorf("test %d: got error = %s, want = %s", i, err.Error(), test.wantError.Error()) | |
369 | + } else if err != nil && err.Error() == test.wantError.Error() { | |
370 | + continue | |
371 | + } | |
372 | + | |
373 | + if !testutil.DeepEqual(*resultBlockHeader, *test.wantBlockHeader) { | |
374 | + t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(*resultBlockHeader), spew.Sdump(*test.wantBlockHeader)) | |
375 | + } | |
376 | + } | |
377 | +} |
@@ -10,6 +10,7 @@ import ( | ||
10 | 10 | "github.com/davecgh/go-spew/spew" |
11 | 11 | |
12 | 12 | "github.com/bytom/consensus" |
13 | + "github.com/bytom/encoding/blockchain" | |
13 | 14 | "github.com/bytom/protocol/bc" |
14 | 15 | "github.com/bytom/testutil" |
15 | 16 | ) |
@@ -132,3 +133,96 @@ func TestBlock(t *testing.T) { | ||
132 | 133 | } |
133 | 134 | } |
134 | 135 | } |
136 | + | |
137 | +func TestReadFrom(t *testing.T) { | |
138 | + btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") | |
139 | + | |
140 | + cases := []struct { | |
141 | + rawBlock string | |
142 | + wantBlock Block | |
143 | + }{ | |
144 | + { | |
145 | + rawBlock: "03018b5f3077f24528e94ecfc4491bb2e9ed6264a632a9a4b86b00c88093ca545d14a137d4f5e1e4054035a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad26978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a17ebee908080808080200207010001010802060031323137310001013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff809df3b49a010116001437e1aec83a4e6587ca9609e4e5aa728db700744900070100020160015e4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adeaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c480c1240201160014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e6302405760b15cc09e543437c4e3aad05bf073e82ebdb214beccb5f4473653dfc0a9d5ae59fb149de19eb71c1c1399594757aeea4dd6327ca2790ef919bd20caa86104201381d35e235813ad1e62f9a602c82abee90565639cc4573568206b55bcd2aed90130000840142084606f20ca7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f402b001467b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d0125ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad0140621c2c3554da50d2a492d9d78be7c6159359d8f5f0b93a054ce0133617a61d85c532aff449b97a3ec2804ca5fe12b4d54aa6e8c3215c33d04abee9c9abdfdb0302013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c0d1e123011600144b61da45324299e40dacc255e2ea07dfce3a56d200013e7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f4020116001437e1aec83a4e6587ca9609e4e5aa728db700744900", | |
146 | + wantBlock: Block{ | |
147 | + BlockHeader: BlockHeader{ | |
148 | + Version: 1, | |
149 | + Height: 12171, | |
150 | + PreviousBlockHash: testutil.MustDecodeHash("3077f24528e94ecfc4491bb2e9ed6264a632a9a4b86b00c88093ca545d14a137"), | |
151 | + Timestamp: 1553496788, | |
152 | + Nonce: 23, | |
153 | + Bits: 2305843009213970283, | |
154 | + BlockCommitment: BlockCommitment{ | |
155 | + TransactionsMerkleRoot: testutil.MustDecodeHash("35a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad2"), | |
156 | + TransactionStatusHash: testutil.MustDecodeHash("6978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a"), | |
157 | + }, | |
158 | + }, | |
159 | + Transactions: []*Tx{ | |
160 | + { | |
161 | + TxData: TxData{ | |
162 | + Version: 1, | |
163 | + SerializedSize: 81, | |
164 | + TimeRange: 0, | |
165 | + Inputs: []*TxInput{ | |
166 | + NewCoinbaseInput(testutil.MustDecodeHexString("003132313731")), | |
167 | + }, | |
168 | + Outputs: []*TxOutput{ | |
169 | + NewTxOutput(btmAssetID, 41450000000, testutil.MustDecodeHexString("001437e1aec83a4e6587ca9609e4e5aa728db7007449")), | |
170 | + }, | |
171 | + }, | |
172 | + }, | |
173 | + { | |
174 | + TxData: TxData{ | |
175 | + Version: 1, | |
176 | + SerializedSize: 560, | |
177 | + TimeRange: 0, | |
178 | + Inputs: []*TxInput{ | |
179 | + NewSpendInput( | |
180 | + [][]byte{ | |
181 | + testutil.MustDecodeHexString("5760b15cc09e543437c4e3aad05bf073e82ebdb214beccb5f4473653dfc0a9d5ae59fb149de19eb71c1c1399594757aeea4dd6327ca2790ef919bd20caa86104"), | |
182 | + testutil.MustDecodeHexString("1381d35e235813ad1e62f9a602c82abee90565639cc4573568206b55bcd2aed9"), | |
183 | + }, | |
184 | + testutil.MustDecodeHash("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"), | |
185 | + btmAssetID, | |
186 | + 9800000000, | |
187 | + 2, | |
188 | + testutil.MustDecodeHexString("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"), | |
189 | + ), | |
190 | + NewIssuanceInput( | |
191 | + testutil.MustDecodeHexString("40142084606f20ca"), | |
192 | + 100000000000, | |
193 | + testutil.MustDecodeHexString("ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad"), | |
194 | + [][]byte{testutil.MustDecodeHexString("621c2c3554da50d2a492d9d78be7c6159359d8f5f0b93a054ce0133617a61d85c532aff449b97a3ec2804ca5fe12b4d54aa6e8c3215c33d04abee9c9abdfdb03")}, | |
195 | + testutil.MustDecodeHexString("7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d"), | |
196 | + ), | |
197 | + }, | |
198 | + Outputs: []*TxOutput{ | |
199 | + NewTxOutput(btmAssetID, 9600000000, testutil.MustDecodeHexString("00144b61da45324299e40dacc255e2ea07dfce3a56d2")), | |
200 | + NewTxOutput(testutil.MustDecodeAsset("7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad14"), 100000000000, testutil.MustDecodeHexString("001437e1aec83a4e6587ca9609e4e5aa728db7007449")), | |
201 | + }, | |
202 | + }, | |
203 | + }, | |
204 | + }, | |
205 | + }, | |
206 | + }, | |
207 | + } | |
208 | + | |
209 | + for _, c := range cases { | |
210 | + blockBytes, err := hex.DecodeString(c.rawBlock) | |
211 | + if err != nil { | |
212 | + t.Fatal(err) | |
213 | + } | |
214 | + | |
215 | + block := &Block{} | |
216 | + if err := block.readFrom(blockchain.NewReader(blockBytes)); err != nil { | |
217 | + t.Fatal(err) | |
218 | + } | |
219 | + | |
220 | + for _, tx := range c.wantBlock.Transactions { | |
221 | + tx.Tx = MapTx(&tx.TxData) | |
222 | + } | |
223 | + | |
224 | + if !testutil.DeepEqual(*block, c.wantBlock) { | |
225 | + t.Errorf("test block read from fail, got:%v, want:%v", *block, c.wantBlock) | |
226 | + } | |
227 | + } | |
228 | +} |
@@ -0,0 +1,59 @@ | ||
1 | +package types | |
2 | + | |
3 | +import ( | |
4 | + "bytes" | |
5 | + "encoding/hex" | |
6 | + "testing" | |
7 | + | |
8 | + "github.com/bytom/encoding/blockchain" | |
9 | + "github.com/bytom/protocol/bc" | |
10 | + "github.com/bytom/testutil" | |
11 | +) | |
12 | + | |
13 | +func TestReadWriteOutputCommitment(t *testing.T) { | |
14 | + btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") | |
15 | + cases := []struct { | |
16 | + oc *OutputCommitment | |
17 | + encodeString string | |
18 | + }{ | |
19 | + { | |
20 | + oc: &OutputCommitment{ | |
21 | + AssetAmount: bc.AssetAmount{AssetId: &btmAssetID, Amount: 100}, | |
22 | + VMVersion: 1, | |
23 | + ControlProgram: testutil.MustDecodeHexString("00140876db6ca8f4542a836f0edd42b87d095d081182"), | |
24 | + }, | |
25 | + encodeString: "39ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff64011600140876db6ca8f4542a836f0edd42b87d095d081182", | |
26 | + }, | |
27 | + { | |
28 | + oc: &OutputCommitment{ | |
29 | + AssetAmount: bc.AssetAmount{AssetId: &btmAssetID, Amount: 50}, | |
30 | + VMVersion: 1, | |
31 | + ControlProgram: testutil.MustDecodeHexString("00148bf7800b2333afd8414d6e903d58c4908b9bbcc7"), | |
32 | + }, | |
33 | + encodeString: "39ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff32011600148bf7800b2333afd8414d6e903d58c4908b9bbcc7", | |
34 | + }, | |
35 | + } | |
36 | + | |
37 | + for _, c := range cases { | |
38 | + buff := []byte{} | |
39 | + buffer := bytes.NewBuffer(buff) | |
40 | + if err := c.oc.writeExtensibleString(buffer, nil, 1); err != nil { | |
41 | + t.Fatal(err) | |
42 | + } | |
43 | + | |
44 | + got := hex.EncodeToString(buffer.Bytes()) | |
45 | + if got != c.encodeString { | |
46 | + t.Errorf("got:%s, want:%s", got, c.encodeString) | |
47 | + } | |
48 | + | |
49 | + oc := &OutputCommitment{} | |
50 | + _, err := oc.readFrom(blockchain.NewReader(buffer.Bytes()), 1) | |
51 | + if err != nil { | |
52 | + t.Fatal(err) | |
53 | + } | |
54 | + | |
55 | + if !testutil.DeepEqual(*oc, *c.oc) { | |
56 | + t.Errorf("got:%v, want:%v", *oc, *c.oc) | |
57 | + } | |
58 | + } | |
59 | +} |
@@ -0,0 +1,69 @@ | ||
1 | +package types | |
2 | + | |
3 | +import ( | |
4 | + "bytes" | |
5 | + "encoding/hex" | |
6 | + "testing" | |
7 | + | |
8 | + "github.com/bytom/encoding/blockchain" | |
9 | + "github.com/bytom/protocol/bc" | |
10 | + "github.com/bytom/testutil" | |
11 | +) | |
12 | + | |
13 | +func TestReadWriteSpendCommitment(t *testing.T) { | |
14 | + btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") | |
15 | + cases := []struct { | |
16 | + sc *SpendCommitment | |
17 | + encodeString string | |
18 | + }{ | |
19 | + { | |
20 | + sc: &SpendCommitment{ | |
21 | + AssetAmount: bc.AssetAmount{ | |
22 | + AssetId: &btmAssetID, | |
23 | + Amount: 100, | |
24 | + }, | |
25 | + SourceID: testutil.MustDecodeHash("3160fb24f97e06ad5a9717cd47fe2b65c7409903216b39120b10550282b20e99"), | |
26 | + SourcePosition: 0, | |
27 | + VMVersion: 1, | |
28 | + ControlProgram: testutil.MustDecodeHexString("0014d927424f4e8c242460b538f04c2676b97842e9a7"), | |
29 | + }, | |
30 | + encodeString: "5a3160fb24f97e06ad5a9717cd47fe2b65c7409903216b39120b10550282b20e99ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff640001160014d927424f4e8c242460b538f04c2676b97842e9a7", | |
31 | + }, | |
32 | + { | |
33 | + sc: &SpendCommitment{ | |
34 | + AssetAmount: bc.AssetAmount{ | |
35 | + AssetId: &btmAssetID, | |
36 | + Amount: 999, | |
37 | + }, | |
38 | + SourceID: testutil.MustDecodeHash("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"), | |
39 | + SourcePosition: 2, | |
40 | + VMVersion: 1, | |
41 | + ControlProgram: testutil.MustDecodeHexString("001418549d84daf53344d32563830c7cf979dc19d5c0"), | |
42 | + }, | |
43 | + encodeString: "5b4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adeaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe707020116001418549d84daf53344d32563830c7cf979dc19d5c0", | |
44 | + }, | |
45 | + } | |
46 | + | |
47 | + for _, c := range cases { | |
48 | + buff := []byte{} | |
49 | + buffer := bytes.NewBuffer(buff) | |
50 | + if err := c.sc.writeExtensibleString(buffer, nil, 1); err != nil { | |
51 | + t.Fatal(err) | |
52 | + } | |
53 | + | |
54 | + got := hex.EncodeToString(buffer.Bytes()) | |
55 | + if got != c.encodeString { | |
56 | + t.Errorf("test write spend commitment fail, got:%s, want:%s", got, c.encodeString) | |
57 | + } | |
58 | + | |
59 | + sc := &SpendCommitment{} | |
60 | + _, err := sc.readFrom(blockchain.NewReader(buffer.Bytes()), 1) | |
61 | + if err != nil { | |
62 | + t.Fatal(err) | |
63 | + } | |
64 | + | |
65 | + if !testutil.DeepEqual(*sc, *c.sc) { | |
66 | + t.Errorf("test read spend commitment fail, got:%v, want:%v", *sc, *c.sc) | |
67 | + } | |
68 | + } | |
69 | +} |
@@ -9,6 +9,7 @@ import ( | ||
9 | 9 | "github.com/davecgh/go-spew/spew" |
10 | 10 | |
11 | 11 | "github.com/bytom/encoding/blockchain" |
12 | + "github.com/bytom/protocol/bc" | |
12 | 13 | "github.com/bytom/testutil" |
13 | 14 | ) |
14 | 15 |
@@ -53,3 +54,43 @@ func TestSerializationTxOutput(t *testing.T) { | ||
53 | 54 | t.Errorf("expected marshaled/unmarshaled txoutput to be:\n%sgot:\n%s", spew.Sdump(*txOutput), spew.Sdump(gotTxOutput)) |
54 | 55 | } |
55 | 56 | } |
57 | + | |
58 | +func TestComputeOutputID(t *testing.T) { | |
59 | + btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") | |
60 | + cases := []struct { | |
61 | + sc *SpendCommitment | |
62 | + wantOutputID string | |
63 | + }{ | |
64 | + { | |
65 | + sc: &SpendCommitment{ | |
66 | + AssetAmount: bc.AssetAmount{AssetId: &btmAssetID, Amount: 1000}, | |
67 | + SourceID: testutil.MustDecodeHash("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"), | |
68 | + SourcePosition: 2, | |
69 | + VMVersion: 1, | |
70 | + ControlProgram: testutil.MustDecodeHexString("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"), | |
71 | + }, | |
72 | + wantOutputID: "c9902bad769008917d14710d60391a43fe6cbd255c839045425c65f749c39d81", | |
73 | + }, | |
74 | + { | |
75 | + sc: &SpendCommitment{ | |
76 | + AssetAmount: bc.AssetAmount{AssetId: &btmAssetID, Amount: 999}, | |
77 | + SourceID: testutil.MustDecodeHash("9e74e35362ffc73c8967aa0008da8fcbc62a21d35673fb970445b5c2972f8603"), | |
78 | + SourcePosition: 2, | |
79 | + VMVersion: 1, | |
80 | + ControlProgram: testutil.MustDecodeHexString("001418549d84daf53344d32563830c7cf979dc19d5c0"), | |
81 | + }, | |
82 | + wantOutputID: "4d038eed93338f4dfc8603101bc70f4b8e662e69828c6dadf4207b5dfaf66275", | |
83 | + }, | |
84 | + } | |
85 | + | |
86 | + for _, c := range cases { | |
87 | + outputID, err := ComputeOutputID(c.sc) | |
88 | + if err != nil { | |
89 | + t.Fatal(err) | |
90 | + } | |
91 | + | |
92 | + if c.wantOutputID != outputID.String() { | |
93 | + t.Errorf("test compute output id fail, got:%s, want:%s", outputID.String(), c.wantOutputID) | |
94 | + } | |
95 | + } | |
96 | +} |
@@ -2,6 +2,7 @@ package testutil | ||
2 | 2 | |
3 | 3 | import ( |
4 | 4 | "bytes" |
5 | + "encoding/hex" | |
5 | 6 | "io" |
6 | 7 | "testing" |
7 | 8 |
@@ -15,6 +16,14 @@ func MustDecodeHash(s string) (h bc.Hash) { | ||
15 | 16 | return h |
16 | 17 | } |
17 | 18 | |
19 | +func MustDecodeHexString(s string) []byte { | |
20 | + bytes, err := hex.DecodeString(s) | |
21 | + if err != nil { | |
22 | + panic(err) | |
23 | + } | |
24 | + return bytes | |
25 | +} | |
26 | + | |
18 | 27 | func MustDecodeAsset(s string) (h bc.AssetID) { |
19 | 28 | if err := h.UnmarshalText([]byte(s)); err != nil { |
20 | 29 | panic(err) |