github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/netsync/messages/chain_msg_test.go (about) 1 package messages 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/davecgh/go-spew/spew" 8 9 "github.com/bytom/bytom/consensus" 10 "github.com/bytom/bytom/protocol/bc" 11 "github.com/bytom/bytom/protocol/bc/types" 12 "github.com/bytom/bytom/testcontrol" 13 ) 14 15 var txs = []*types.Tx{ 16 types.NewTx(types.TxData{ 17 SerializedSize: uint64(52), 18 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})}, 19 Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 5000, nil, nil)}, 20 }), 21 types.NewTx(types.TxData{ 22 SerializedSize: uint64(53), 23 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02})}, 24 Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 5000, nil, nil)}, 25 }), 26 types.NewTx(types.TxData{ 27 SerializedSize: uint64(54), 28 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02, 0x03})}, 29 Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 5000, nil, nil)}, 30 }), 31 types.NewTx(types.TxData{ 32 SerializedSize: uint64(54), 33 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02, 0x03})}, 34 Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 2000, nil, nil)}, 35 }), 36 types.NewTx(types.TxData{ 37 SerializedSize: uint64(54), 38 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01, 0x02, 0x03})}, 39 Outputs: []*types.TxOutput{types.NewOriginalTxOutput(*consensus.BTMAssetID, 10000, nil, nil)}, 40 }), 41 } 42 43 func TestTransactionMessage(t *testing.T) { 44 if testcontrol.IgnoreTestTemporary { 45 return 46 } 47 48 for _, tx := range txs { 49 txMsg, err := NewTransactionMessage(tx) 50 if err != nil { 51 t.Fatalf("create tx msg err:%s", err) 52 } 53 54 gotTx, err := txMsg.GetTransaction() 55 if err != nil { 56 t.Fatalf("get txs from txsMsg err:%s", err) 57 } 58 if !reflect.DeepEqual(*tx.Tx, *gotTx.Tx) { 59 t.Errorf("txs msg test err: got %s\nwant %s", spew.Sdump(tx.Tx), spew.Sdump(gotTx.Tx)) 60 } 61 } 62 } 63 64 func TestTransactionsMessage(t *testing.T) { 65 if testcontrol.IgnoreTestTemporary { 66 return 67 } 68 69 txsMsg, err := NewTransactionsMessage(txs) 70 if err != nil { 71 t.Fatalf("create txs msg err:%s", err) 72 } 73 74 gotTxs, err := txsMsg.GetTransactions() 75 if err != nil { 76 t.Fatalf("get txs from txsMsg err:%s", err) 77 } 78 79 if len(gotTxs) != len(txs) { 80 t.Fatal("txs msg test err: number of txs not match ") 81 } 82 83 for i, tx := range txs { 84 if !reflect.DeepEqual(tx.Tx, gotTxs[i].Tx) { 85 t.Errorf("txs msg test err: got %s\nwant %s", spew.Sdump(tx.Tx), spew.Sdump(gotTxs[i].Tx)) 86 } 87 } 88 } 89 90 var testBlock = &types.Block{ 91 BlockHeader: types.BlockHeader{ 92 Version: 1, 93 Height: 0, 94 Timestamp: 1528945000000, 95 BlockCommitment: types.BlockCommitment{ 96 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)}, 97 }, 98 SupLinks: types.SupLinks{}, 99 }, 100 } 101 102 func TestBlockMessage(t *testing.T) { 103 blockMsg, err := NewBlockMessage(testBlock) 104 if err != nil { 105 t.Fatalf("create new block msg err:%s", err) 106 } 107 108 gotBlock, err := blockMsg.GetBlock() 109 if err != nil { 110 t.Fatalf("got block err:%s", err) 111 } 112 113 if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) { 114 t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader)) 115 } 116 117 blockMsg.RawBlock[1] = blockMsg.RawBlock[1] + 0x1 118 _, err = blockMsg.GetBlock() 119 if err == nil { 120 t.Fatalf("get mine block err") 121 } 122 } 123 124 var testHeaders = []*types.BlockHeader{ 125 { 126 Version: 1, 127 Height: 0, 128 Timestamp: 1528945000000, 129 BlockCommitment: types.BlockCommitment{ 130 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)}, 131 }, 132 SupLinks: types.SupLinks{}, 133 }, 134 { 135 Version: 1, 136 Height: 1, 137 Timestamp: 1528945000000, 138 BlockCommitment: types.BlockCommitment{ 139 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)}, 140 }, 141 SupLinks: types.SupLinks{}, 142 }, 143 { 144 Version: 1, 145 Height: 3, 146 Timestamp: 1528945000000, 147 BlockCommitment: types.BlockCommitment{ 148 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)}, 149 }, 150 SupLinks: types.SupLinks{}, 151 }, 152 } 153 154 func TestHeadersMessage(t *testing.T) { 155 headersMsg, err := NewHeadersMessage(testHeaders) 156 if err != nil { 157 t.Fatalf("create headers msg err:%s", err) 158 } 159 160 gotHeaders, err := headersMsg.GetHeaders() 161 if err != nil { 162 t.Fatalf("got headers err:%s", err) 163 } 164 165 if !reflect.DeepEqual(gotHeaders, testHeaders) { 166 t.Errorf("headers msg test err: got %s\nwant %s", spew.Sdump(gotHeaders), spew.Sdump(testHeaders)) 167 } 168 } 169 170 func TestGetBlockMessage(t *testing.T) { 171 getBlockMsg := GetBlockMessage{RawHash: [32]byte{0x01}} 172 gotHash := getBlockMsg.GetHash() 173 174 if !reflect.DeepEqual(gotHash.Byte32(), getBlockMsg.RawHash) { 175 t.Errorf("get block msg test err: got %s\nwant %s", spew.Sdump(gotHash.Byte32()), spew.Sdump(getBlockMsg.RawHash)) 176 } 177 } 178 179 type testGetHeadersMessage struct { 180 blockLocator []*bc.Hash 181 stopHash *bc.Hash 182 skip uint64 183 } 184 185 func TestGetHeadersMessage(t *testing.T) { 186 testMsg := testGetHeadersMessage{ 187 blockLocator: []*bc.Hash{{V0: 0x01}, {V0: 0x02}, {V0: 0x03}}, 188 stopHash: &bc.Hash{V0: 0x01}, 189 skip: 888, 190 } 191 getHeadersMsg := NewGetHeadersMessage(testMsg.blockLocator, testMsg.stopHash, testMsg.skip) 192 gotBlockLocator := getHeadersMsg.GetBlockLocator() 193 gotStopHash := getHeadersMsg.GetStopHash() 194 gotSkip := getHeadersMsg.GetSkip() 195 196 if !reflect.DeepEqual(testMsg.blockLocator, gotBlockLocator) { 197 t.Errorf("get headers msg test err: got %s\nwant %s", spew.Sdump(gotBlockLocator), spew.Sdump(testMsg.blockLocator)) 198 } 199 200 if !reflect.DeepEqual(testMsg.stopHash, gotStopHash) { 201 t.Errorf("get headers msg test err: amount:got %d\nwant %d", gotStopHash, testMsg.stopHash) 202 } 203 204 if !reflect.DeepEqual(testMsg.skip, gotSkip) { 205 t.Errorf("get headers msg test err: skip:got %d\nwant %d", gotSkip, testMsg.skip) 206 } 207 } 208 209 var testBlocks = []*types.Block{ 210 { 211 BlockHeader: types.BlockHeader{ 212 Version: 1, 213 Height: 0, 214 Timestamp: 1528945000000, 215 BlockCommitment: types.BlockCommitment{ 216 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)}, 217 }, 218 }, 219 }, 220 { 221 BlockHeader: types.BlockHeader{ 222 Version: 1, 223 Height: 0, 224 Timestamp: 1528945000000, 225 BlockCommitment: types.BlockCommitment{ 226 TransactionsMerkleRoot: bc.Hash{V0: uint64(0x11)}, 227 }, 228 }, 229 }, 230 } 231 232 func TestBlocksMessage(t *testing.T) { 233 blocksMsg, err := NewBlocksMessage(testBlocks) 234 if err != nil { 235 t.Fatalf("create blocks msg err:%s", err) 236 } 237 gotBlocks, err := blocksMsg.GetBlocks() 238 if err != nil { 239 t.Fatalf("get blocks err:%s", err) 240 } 241 242 for _, gotBlock := range gotBlocks { 243 if !reflect.DeepEqual(gotBlock.BlockHeader, testBlock.BlockHeader) { 244 t.Errorf("block msg test err: got %s\nwant %s", spew.Sdump(gotBlock.BlockHeader), spew.Sdump(testBlock.BlockHeader)) 245 } 246 } 247 } 248 249 func TestStatusMessage(t *testing.T) { 250 statusResponseMsg := NewStatusMessage(&testBlock.BlockHeader, &testBlock.BlockHeader) 251 gotBestHash := statusResponseMsg.GetBestHash() 252 if !reflect.DeepEqual(*gotBestHash, testBlock.Hash()) { 253 t.Errorf("status response msg test err: got %s\nwant %s", spew.Sdump(*gotBestHash), spew.Sdump(testBlock.Hash())) 254 } 255 gotIrreversibleHash := statusResponseMsg.GetIrreversibleHash() 256 if !reflect.DeepEqual(*gotIrreversibleHash, testBlock.Hash()) { 257 t.Errorf("status response msg test err: got %s\nwant %s", spew.Sdump(*gotIrreversibleHash), spew.Sdump(testBlock.Hash())) 258 } 259 }