OSDN Git Service

Remove transaction reference data (#416)
[bytom/bytom.git] / protocol / bc / legacy / block_test.go
1 package legacy
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(45),
27                                 Outputs: []*TxOutput{
28                                         NewTxOutput(bc.AssetID{}, 1, nil, 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 reference data
65                 "00\"") // tx 0, output 0 output witness
66
67         if !bytes.Equal(got, []byte(wantHex)) {
68                 t.Errorf("marshaled block bytes = %s want %s", got, []byte(wantHex))
69         }
70
71         var c Block
72         err = json.Unmarshal(got, &c)
73         if err != nil {
74                 t.Errorf("unexpected error %s", err)
75         }
76
77         if !testutil.DeepEqual(*b, c) {
78                 t.Errorf("expected marshaled/unmarshaled block to be:\n%sgot:\n%s", spew.Sdump(*b), spew.Sdump(c))
79         }
80
81         got[7] = 'q'
82         err = json.Unmarshal(got, &c)
83         if err == nil {
84                 t.Error("unmarshaled corrupted JSON ok, wanted error")
85         }
86 }
87
88 func TestEmptyBlock(t *testing.T) {
89         block := Block{
90                 BlockHeader: BlockHeader{
91                         Version: 1,
92                         Height:  1,
93                 },
94         }
95
96         got := serialize(t, &block)
97         wantHex := ("03" + // serialization flags
98                 "01" + // version
99                 "01" + // block height
100                 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
101                 "00" + // timestamp
102                 "40" + // commitment extensible field length
103                 "0000000000000000000000000000000000000000000000000000000000000000" + // transactions merkle root
104                 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status hash
105                 "00" + // nonce
106                 "00" + // bits
107                 "00") // num transactions
108         want, _ := hex.DecodeString(wantHex)
109         if !bytes.Equal(got, want) {
110                 t.Errorf("empty block bytes = %x want %x", got, want)
111         }
112
113         got = serialize(t, &block.BlockHeader)
114         wantHex = ("01" + // serialization flags
115                 "01" + // version
116                 "01" + // block height
117                 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
118                 "00" + // timestamp
119                 "40" + // commitment extensible field length
120                 "0000000000000000000000000000000000000000000000000000000000000000" + // transactions merkle root
121                 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status hash
122                 "00" + // nonce
123                 "00") // bits
124         want, _ = hex.DecodeString(wantHex)
125         if !bytes.Equal(got, want) {
126                 t.Errorf("empty block header bytes = %x want %x", got, want)
127         }
128
129         wantHash := mustDecodeHash("a950a33eb49913c06c0c5c0aa643bb55c5cd898f3b54e91971f07ca2123d7204")
130         if h := block.Hash(); h != wantHash {
131                 t.Errorf("got block hash %x, want %x", h.Bytes(), wantHash.Bytes())
132         }
133
134         wTime := time.Unix(0, 0).UTC()
135         if got := block.Time(); got != wTime {
136                 t.Errorf("empty block time = %v want %v", got, wTime)
137         }
138 }
139
140 func TestSmallBlock(t *testing.T) {
141         block := Block{
142                 BlockHeader: BlockHeader{
143                         Version: 1,
144                         Height:  1,
145                 },
146                 Transactions: []*Tx{NewTx(TxData{Version: CurrentTransactionVersion})},
147         }
148
149         got := serialize(t, &block)
150         wantHex := ("03" + // serialization flags
151                 "01" + // version
152                 "01" + // block height
153                 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
154                 "00" + // timestamp
155                 "40" + // commitment extensible field length
156                 "0000000000000000000000000000000000000000000000000000000000000000" + // transactions merkle root
157                 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status hash
158                 "00" + // nonce
159                 "00" + // bits
160                 "01" + // num transactions
161                 "070100000000") // transaction
162         want, _ := hex.DecodeString(wantHex)
163         if !bytes.Equal(got, want) {
164                 t.Errorf("small block bytes = %x want %x", got, want)
165         }
166 }