OSDN Git Service

Btm2.0 remove pow (#1859)
[bytom/bytom.git] / protocol / bc / types / block_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "encoding/json"
7         "strings"
8         "testing"
9
10         "github.com/bytom/bytom/consensus"
11         "github.com/bytom/bytom/encoding/blockchain"
12         "github.com/bytom/bytom/protocol/bc"
13         "github.com/bytom/bytom/testutil"
14         "github.com/davecgh/go-spew/spew"
15 )
16
17 func TestBlock(t *testing.T) {
18         cases := []struct {
19                 block *Block
20                 hex   string
21                 hash  bc.Hash
22         }{
23                 {
24                         block: &Block{
25                                 BlockHeader: BlockHeader{
26                                         Version: 1,
27                                         Height:  1,
28                                 },
29                                 Transactions: []*Tx{},
30                         },
31                         hex: strings.Join([]string{
32                                 "03", // serialization flags
33                                 "01", // version
34                                 "01", // block height
35                                 "0000000000000000000000000000000000000000000000000000000000000000", // prev block hash
36                                 "00", // timestamp
37                                 "40", // commitment extensible field length
38                                 "0000000000000000000000000000000000000000000000000000000000000000", // transactions merkle root
39                                 "0000000000000000000000000000000000000000000000000000000000000000", // tx status hash
40                                 "00", // num transactions
41                         }, ""),
42                         hash: testutil.MustDecodeHash("ba63a0f8e9a7e5f93e3ec7ce49f07276c065bdf75e270984d9390bee0b3de027"),
43                 },
44                 {
45                         block: &Block{
46                                 BlockHeader: BlockHeader{
47                                         Version:           1,
48                                         Height:            432234,
49                                         PreviousBlockHash: testutil.MustDecodeHash("c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0"),
50                                         Timestamp:         1522908275,
51                                         BlockCommitment: BlockCommitment{
52                                                 TransactionStatusHash:  testutil.MustDecodeHash("b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470"),
53                                                 TransactionsMerkleRoot: testutil.MustDecodeHash("ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03"),
54                                         },
55                                 },
56                                 Transactions: []*Tx{
57                                         NewTx(TxData{
58                                                 Version:        1,
59                                                 SerializedSize: uint64(261),
60                                                 TimeRange:      654,
61                                                 Inputs: []*TxInput{
62                                                         NewIssuanceInput([]byte("nonce"), 254354, []byte("issuanceProgram"), [][]byte{[]byte("arguments1"), []byte("arguments2")}, []byte("assetDefinition")),
63                                                         NewSpendInput([][]byte{[]byte("arguments3"), []byte("arguments4")}, testutil.MustDecodeHash("fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409"), *consensus.BTMAssetID, 254354, 3, []byte("spendProgram")),
64                                                 },
65                                                 Outputs: []*TxOutput{
66                                                         NewTxOutput(testutil.MustDecodeAsset("a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf"), 254354, []byte("true")),
67                                                 },
68                                         }),
69                                         NewTx(TxData{
70                                                 Version:        1,
71                                                 SerializedSize: uint64(108),
72                                                 Inputs: []*TxInput{
73                                                         NewCoinbaseInput([]byte("arbitrary")),
74                                                 },
75                                                 Outputs: []*TxOutput{
76                                                         NewTxOutput(*consensus.BTMAssetID, 254354, []byte("true")),
77                                                         NewTxOutput(*consensus.BTMAssetID, 254354, []byte("false")),
78                                                 },
79                                         }),
80                                 },
81                         },
82                         hex: strings.Join([]string{
83                                 "03",     // serialization flags
84                                 "01",     // version
85                                 "eab01a", // block height
86                                 "c34048bd60c4c13144fd34f408627d1be68f6cb4fdd34e879d6d791060ea73a0", // prev block hash
87                                 "f3f896d605", // timestamp
88                                 "40",         // commitment extensible field length
89                                 "ad9ac003d08ff305181a345d64fe0b02311cc1a6ec04ab73f3318d90139bfe03", // transactions merkle root
90                                 "b94301ea4e316bee00109f68d25beaca90aeff08e9bf439a37d91d7a3b5a1470", // tx status hash
91                                 "02", // num transactions
92                                 "07018e0502012a00056e6f6e6365a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf92c30f380f6173736574446566696e6974696f6e010f69737375616e636550726f6772616d020a617267756d656e7473310a617267756d656e74733201540152fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f03010c7370656e6450726f6772616d17020a617267756d656e7473330a617267756d656e747334010129a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf92c30f01047472756500",
93                                 "07010001010b020961726269747261727900020129ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f01047472756500012affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff92c30f010566616c736500",
94                         }, ""),
95                         hash: testutil.MustDecodeHash("fca0cfd96604b0b28e4a16286974db155d0bbd42dd729d2e2ba058d09a963016"),
96                 },
97         }
98
99         for i, test := range cases {
100                 got := testutil.Serialize(t, test.block)
101                 want, err := hex.DecodeString(test.hex)
102                 if err != nil {
103                         t.Fatal(err)
104                 }
105
106                 if !bytes.Equal(got, want) {
107                         t.Errorf("test %d: bytes = %x want %x", i, got, want)
108                 }
109
110                 blockHash := test.block.Hash()
111                 if blockHash != test.hash {
112                         t.Errorf("test %d: hash = %s want %s", i, blockHash.String(), test.hash.String())
113                 }
114
115                 blockJSON, err := json.Marshal(test.block)
116                 if err != nil {
117                         t.Errorf("test %d: error marshaling block to json: %s", i, err)
118                 }
119
120                 blockFromJSON := Block{}
121                 if err := json.Unmarshal(blockJSON, &blockFromJSON); err != nil {
122                         t.Errorf("test %d: error unmarshaling block from json: %s", i, err)
123                 }
124                 if !testutil.DeepEqual(*test.block, blockFromJSON) {
125                         t.Errorf("test %d: got:\n%s\nwant:\n%s", i, spew.Sdump(blockFromJSON), spew.Sdump(*test.block))
126                 }
127         }
128 }
129
130 func TestReadFrom(t *testing.T) {
131         btmAssetID := testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
132
133         cases := []struct {
134                 rawBlock  string
135                 wantBlock Block
136         }{
137                 {
138                         rawBlock: "03018b5f3077f24528e94ecfc4491bb2e9ed6264a632a9a4b86b00c88093ca545d14a137d4f5e1e4054035a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad26978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a0207010001010802060031323137310001013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff809df3b49a010116001437e1aec83a4e6587ca9609e4e5aa728db700744900070100020160015e4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adeaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c480c1240201160014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e6302405760b15cc09e543437c4e3aad05bf073e82ebdb214beccb5f4473653dfc0a9d5ae59fb149de19eb71c1c1399594757aeea4dd6327ca2790ef919bd20caa86104201381d35e235813ad1e62f9a602c82abee90565639cc4573568206b55bcd2aed90130000840142084606f20ca7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f402b001467b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d0125ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad0140621c2c3554da50d2a492d9d78be7c6159359d8f5f0b93a054ce0133617a61d85c532aff449b97a3ec2804ca5fe12b4d54aa6e8c3215c33d04abee9c9abdfdb0302013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c0d1e123011600144b61da45324299e40dacc255e2ea07dfce3a56d200013e7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad1480d0dbc3f4020116001437e1aec83a4e6587ca9609e4e5aa728db700744900",
139                         wantBlock: Block{
140                                 BlockHeader: BlockHeader{
141                                         Version:           1,
142                                         Height:            12171,
143                                         PreviousBlockHash: testutil.MustDecodeHash("3077f24528e94ecfc4491bb2e9ed6264a632a9a4b86b00c88093ca545d14a137"),
144                                         Timestamp:         1553496788,
145                                         BlockCommitment: BlockCommitment{
146                                                 TransactionsMerkleRoot: testutil.MustDecodeHash("35a2d11158f47a5c5267630b2b6cf9e9a5f79a598085a2572a68defeb8013ad2"),
147                                                 TransactionStatusHash:  testutil.MustDecodeHash("6978a65b4ee5b6f4914fe5c05000459a803ecf59132604e5d334d64249c5e50a"),
148                                         },
149                                 },
150                                 Transactions: []*Tx{
151                                         {
152                                                 TxData: TxData{
153                                                         Version:        1,
154                                                         SerializedSize: 81,
155                                                         TimeRange:      0,
156                                                         Inputs: []*TxInput{
157                                                                 NewCoinbaseInput(testutil.MustDecodeHexString("003132313731")),
158                                                         },
159                                                         Outputs: []*TxOutput{
160                                                                 NewTxOutput(btmAssetID, 41450000000, testutil.MustDecodeHexString("001437e1aec83a4e6587ca9609e4e5aa728db7007449")),
161                                                         },
162                                                 },
163                                         },
164                                         {
165                                                 TxData: TxData{
166                                                         Version:        1,
167                                                         SerializedSize: 560,
168                                                         TimeRange:      0,
169                                                         Inputs: []*TxInput{
170                                                                 NewSpendInput(
171                                                                         [][]byte{
172                                                                                 testutil.MustDecodeHexString("5760b15cc09e543437c4e3aad05bf073e82ebdb214beccb5f4473653dfc0a9d5ae59fb149de19eb71c1c1399594757aeea4dd6327ca2790ef919bd20caa86104"),
173                                                                                 testutil.MustDecodeHexString("1381d35e235813ad1e62f9a602c82abee90565639cc4573568206b55bcd2aed9"),
174                                                                         },
175                                                                         testutil.MustDecodeHash("4b5cb973f5bef4eadde4c89b92ee73312b940e84164da0594149554cc8a2adea"),
176                                                                         btmAssetID,
177                                                                         9800000000,
178                                                                         2,
179                                                                         testutil.MustDecodeHexString("0014cb9f2391bafe2bc1159b2c4c8a0f17ba1b4dd94e"),
180                                                                 ),
181                                                                 NewIssuanceInput(
182                                                                         testutil.MustDecodeHexString("40142084606f20ca"),
183                                                                         100000000000,
184                                                                         testutil.MustDecodeHexString("ae2054a71277cc162eb3eb21b5bd9fe54402829a53b294deaed91692a2cd8a081f9c5151ad"),
185                                                                         [][]byte{testutil.MustDecodeHexString("621c2c3554da50d2a492d9d78be7c6159359d8f5f0b93a054ce0133617a61d85c532aff449b97a3ec2804ca5fe12b4d54aa6e8c3215c33d04abee9c9abdfdb03")},
186                                                                         testutil.MustDecodeHexString("7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d"),
187                                                                 ),
188                                                         },
189                                                         Outputs: []*TxOutput{
190                                                                 NewTxOutput(btmAssetID, 9600000000, testutil.MustDecodeHexString("00144b61da45324299e40dacc255e2ea07dfce3a56d2")),
191                                                                 NewTxOutput(testutil.MustDecodeAsset("7b38dc897329a288ea31031724f5c55bcafec80468a546955023380af2faad14"), 100000000000, testutil.MustDecodeHexString("001437e1aec83a4e6587ca9609e4e5aa728db7007449")),
192                                                         },
193                                                 },
194                                         },
195                                 },
196                         },
197                 },
198         }
199
200         for _, c := range cases {
201                 blockBytes, err := hex.DecodeString(c.rawBlock)
202                 if err != nil {
203                         t.Fatal(err)
204                 }
205
206                 block := &Block{}
207                 if err := block.readFrom(blockchain.NewReader(blockBytes)); err != nil {
208                         t.Fatal(err)
209                 }
210
211                 for _, tx := range c.wantBlock.Transactions {
212                         tx.Tx = MapTx(&tx.TxData)
213                 }
214
215                 if !testutil.DeepEqual(*block, c.wantBlock) {
216                         t.Errorf("test block read from fail, got:%v, want:%v", *block, c.wantBlock)
217                 }
218         }
219 }