OSDN Git Service

update master (#487)
[bytom/bytom.git] / protocol / bc / types / block_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "encoding/json"
7         "testing"
8         "time"
9
10         "github.com/davecgh/go-spew/spew"
11
12         "github.com/bytom/protocol/bc"
13         "github.com/bytom/testutil"
14 )
15
16 func TestMarshalBlock(t *testing.T) {
17         b := &Block{
18                 BlockHeader: BlockHeader{
19                         Version: 1,
20                         Height:  1,
21                 },
22
23                 Transactions: []*Tx{
24                         NewTx(TxData{
25                                 Version:        1,
26                                 SerializedSize: uint64(44),
27                                 Outputs: []*TxOutput{
28                                         NewTxOutput(bc.AssetID{}, 1, nil),
29                                 },
30                         }),
31                 }}
32
33         got, err := json.Marshal(b)
34         if err != nil {
35                 t.Errorf("unexpected error %s", err)
36         }
37
38         // Include start and end quote marks because json.Marshal adds them
39         // to the result of Block.MarshalText.
40         wantHex := ("\"03" + // serialization flags
41                 "01" + // version
42                 "01" + // block height
43                 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
44                 "00" + // timestamp
45                 "40" + // commitment extensible field length
46                 "0000000000000000000000000000000000000000000000000000000000000000" + // tx merkle root
47                 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status root
48                 "00" + // nonce
49                 "00" + // bits
50
51                 "01" + // num transactions
52                 "07" + // tx 0, serialization flags
53                 "01" + // tx 0, tx version
54                 "00" + // tx maxtime
55                 "00" + // tx 0, common witness extensible string length
56                 "00" + // tx 0, inputs count
57                 "01" + // tx 0, outputs count
58                 "01" + // tx 0 output 0, asset version
59                 "23" + // tx 0, output 0, output commitment length
60                 "0000000000000000000000000000000000000000000000000000000000000000" + // tx 0, output 0 commitment, asset id
61                 "01" + // tx 0, output 0 commitment, amount
62                 "01" + // tx 0, output 0 commitment vm version
63                 "00" + // tx 0, output 0 control program
64                 "00\"") // tx 0, output 0 output witness
65
66         if !bytes.Equal(got, []byte(wantHex)) {
67                 t.Errorf("marshaled block bytes = %s want %s", got, []byte(wantHex))
68         }
69
70         var c Block
71         err = json.Unmarshal(got, &c)
72         if err != nil {
73                 t.Errorf("unexpected error %s", err)
74         }
75
76         if !testutil.DeepEqual(*b, c) {
77                 t.Errorf("expected marshaled/unmarshaled block to be:\n%sgot:\n%s", spew.Sdump(*b), spew.Sdump(c))
78         }
79
80         got[7] = 'q'
81         err = json.Unmarshal(got, &c)
82         if err == nil {
83                 t.Error("unmarshaled corrupted JSON ok, wanted error")
84         }
85 }
86
87 func TestEmptyBlock(t *testing.T) {
88         block := Block{
89                 BlockHeader: BlockHeader{
90                         Version: 1,
91                         Height:  1,
92                 },
93         }
94
95         got := serialize(t, &block)
96         wantHex := ("03" + // serialization flags
97                 "01" + // version
98                 "01" + // block height
99                 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
100                 "00" + // timestamp
101                 "40" + // commitment extensible field length
102                 "0000000000000000000000000000000000000000000000000000000000000000" + // transactions merkle root
103                 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status hash
104                 "00" + // nonce
105                 "00" + // bits
106                 "00") // num transactions
107         want, _ := hex.DecodeString(wantHex)
108         if !bytes.Equal(got, want) {
109                 t.Errorf("empty block bytes = %x want %x", got, want)
110         }
111
112         got = serialize(t, &block.BlockHeader)
113         wantHex = ("01" + // serialization flags
114                 "01" + // version
115                 "01" + // block height
116                 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
117                 "00" + // timestamp
118                 "40" + // commitment extensible field length
119                 "0000000000000000000000000000000000000000000000000000000000000000" + // transactions merkle root
120                 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status hash
121                 "00" + // nonce
122                 "00") // bits
123         want, _ = hex.DecodeString(wantHex)
124         if !bytes.Equal(got, want) {
125                 t.Errorf("empty block header bytes = %x want %x", got, want)
126         }
127
128         wantHash := mustDecodeHash("9609d2e45760f34cbc6c6d948c3fb9b6d7b61552d9d17fdd5b7d0cb5d2e67244")
129         if h := block.Hash(); h != wantHash {
130                 t.Errorf("got block hash %x, want %x", h.Bytes(), wantHash.Bytes())
131         }
132
133         wTime := time.Unix(0, 0).UTC()
134         if got := block.Time(); got != wTime {
135                 t.Errorf("empty block time = %v want %v", got, wTime)
136         }
137 }
138
139 func TestSmallBlock(t *testing.T) {
140         block := Block{
141                 BlockHeader: BlockHeader{
142                         Version: 1,
143                         Height:  1,
144                 },
145                 Transactions: []*Tx{NewTx(TxData{Version: CurrentTransactionVersion})},
146         }
147
148         got := serialize(t, &block)
149         wantHex := ("03" + // serialization flags
150                 "01" + // version
151                 "01" + // block height
152                 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
153                 "00" + // timestamp
154                 "40" + // commitment extensible field length
155                 "0000000000000000000000000000000000000000000000000000000000000000" + // transactions merkle root
156                 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status hash
157                 "00" + // nonce
158                 "00" + // bits
159                 "01" + // num transactions
160                 "070100000000") // transaction
161         want, _ := hex.DecodeString(wantHex)
162         if !bytes.Equal(got, want) {
163                 t.Errorf("small block bytes = %x want %x", got, want)
164         }
165 }