10 "github.com/davecgh/go-spew/spew"
12 "github.com/bytom/protocol/bc"
13 "github.com/bytom/testutil"
16 func TestMarshalBlock(t *testing.T) {
18 BlockHeader: BlockHeader{
26 SerializedSize: uint64(44),
28 NewTxOutput(bc.AssetID{}, 1, nil),
33 got, err := json.Marshal(b)
35 t.Errorf("unexpected error %s", err)
38 // Include start and end quote marks because json.Marshal adds them
39 // to the result of Block.MarshalText.
40 wantHex := ("\"03" + // serialization flags
42 "01" + // block height
43 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
45 "40" + // commitment extensible field length
46 "0000000000000000000000000000000000000000000000000000000000000000" + // tx merkle root
47 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status root
51 "01" + // num transactions
52 "07" + // tx 0, serialization flags
53 "01" + // tx 0, tx version
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
66 if !bytes.Equal(got, []byte(wantHex)) {
67 t.Errorf("marshaled block bytes = %s want %s", got, []byte(wantHex))
71 err = json.Unmarshal(got, &c)
73 t.Errorf("unexpected error %s", err)
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))
81 err = json.Unmarshal(got, &c)
83 t.Error("unmarshaled corrupted JSON ok, wanted error")
87 func TestEmptyBlock(t *testing.T) {
89 BlockHeader: BlockHeader{
95 got := serialize(t, &block)
96 wantHex := ("03" + // serialization flags
98 "01" + // block height
99 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
101 "40" + // commitment extensible field length
102 "0000000000000000000000000000000000000000000000000000000000000000" + // transactions merkle root
103 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status hash
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)
112 got = serialize(t, &block.BlockHeader)
113 wantHex = ("01" + // serialization flags
115 "01" + // block height
116 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
118 "40" + // commitment extensible field length
119 "0000000000000000000000000000000000000000000000000000000000000000" + // transactions merkle root
120 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status hash
123 want, _ = hex.DecodeString(wantHex)
124 if !bytes.Equal(got, want) {
125 t.Errorf("empty block header bytes = %x want %x", got, want)
128 wantHash := mustDecodeHash("9609d2e45760f34cbc6c6d948c3fb9b6d7b61552d9d17fdd5b7d0cb5d2e67244")
129 if h := block.Hash(); h != wantHash {
130 t.Errorf("got block hash %x, want %x", h.Bytes(), wantHash.Bytes())
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)
139 func TestSmallBlock(t *testing.T) {
141 BlockHeader: BlockHeader{
145 Transactions: []*Tx{NewTx(TxData{Version: CurrentTransactionVersion})},
148 got := serialize(t, &block)
149 wantHex := ("03" + // serialization flags
151 "01" + // block height
152 "0000000000000000000000000000000000000000000000000000000000000000" + // prev block hash
154 "40" + // commitment extensible field length
155 "0000000000000000000000000000000000000000000000000000000000000000" + // transactions merkle root
156 "0000000000000000000000000000000000000000000000000000000000000000" + // tx status hash
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)