github.com/phillinzzz/newBsc@v1.1.6/eth/protocols/diff/protocol_test.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package diff 18 19 import ( 20 "bytes" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 25 "github.com/phillinzzz/newBsc/common" 26 "github.com/phillinzzz/newBsc/core/types" 27 "github.com/phillinzzz/newBsc/rlp" 28 ) 29 30 // Tests that the custom union field encoder and decoder works correctly. 31 func TestDiffLayersPacketDataEncodeDecode(t *testing.T) { 32 // Create a "random" hash for testing 33 var hash common.Hash 34 for i := range hash { 35 hash[i] = byte(i) 36 } 37 38 testDiffLayers := []*types.DiffLayer{ 39 { 40 BlockHash: common.HexToHash("0x1e9624dcd0874958723aa3dae1fe299861e93ef32b980143d798c428bdd7a20a"), 41 Number: 10479133, 42 Receipts: []*types.Receipt{{ 43 GasUsed: 100, 44 TransactionIndex: 1, 45 }}, 46 Codes: []types.DiffCode{{ 47 Hash: common.HexToHash("0xaece2dbf80a726206cf4df299afa09f9d8f3dcd85ff39bb4b3f0402a3a6af2f5"), 48 Code: []byte{1, 2, 3, 4}, 49 }}, 50 Destructs: []common.Address{ 51 common.HexToAddress("0x0205bb28ece9289d3fb8eb0c9e999bbd5be2b931"), 52 }, 53 Accounts: []types.DiffAccount{{ 54 Account: common.HexToAddress("0x18b2a687610328590bc8f2e5fedde3b582a49cda"), 55 Blob: []byte{2, 3, 4, 5}, 56 }}, 57 Storages: []types.DiffStorage{{ 58 Account: common.HexToAddress("0x18b2a687610328590bc8f2e5fedde3b582a49cda"), 59 Keys: []string{"abc"}, 60 Vals: [][]byte{{1, 2, 3}}, 61 }}, 62 }, 63 } 64 // Assemble some table driven tests 65 tests := []struct { 66 diffLayers []*types.DiffLayer 67 fail bool 68 }{ 69 {fail: false, diffLayers: testDiffLayers}, 70 } 71 // Iterate over each of the tests and try to encode and then decode 72 for i, tt := range tests { 73 originPacket := make([]rlp.RawValue, 0) 74 for _, diff := range tt.diffLayers { 75 bz, err := rlp.EncodeToBytes(diff) 76 assert.NoError(t, err) 77 originPacket = append(originPacket, bz) 78 } 79 80 bz, err := rlp.EncodeToBytes(DiffLayersPacket(originPacket)) 81 if err != nil && !tt.fail { 82 t.Fatalf("test %d: failed to encode packet: %v", i, err) 83 } else if err == nil && tt.fail { 84 t.Fatalf("test %d: encode should have failed", i) 85 } 86 if !tt.fail { 87 packet := new(DiffLayersPacket) 88 if err := rlp.DecodeBytes(bz, packet); err != nil { 89 t.Fatalf("test %d: failed to decode packet: %v", i, err) 90 } 91 diffLayers, err := packet.Unpack() 92 assert.NoError(t, err) 93 94 if len(diffLayers) != len(tt.diffLayers) { 95 t.Fatalf("test %d: encode length mismatch: have %+v, want %+v", i, len(diffLayers), len(tt.diffLayers)) 96 } 97 expectedPacket := make([]rlp.RawValue, 0) 98 for _, diff := range diffLayers { 99 bz, err := rlp.EncodeToBytes(diff) 100 assert.NoError(t, err) 101 expectedPacket = append(expectedPacket, bz) 102 } 103 for i := 0; i < len(expectedPacket); i++ { 104 if !bytes.Equal(expectedPacket[i], originPacket[i]) { 105 t.Fatalf("test %d: data change during encode and decode", i) 106 } 107 } 108 } 109 } 110 } 111 112 func TestDiffMessages(t *testing.T) { 113 114 for i, tc := range []struct { 115 message interface{} 116 want []byte 117 }{ 118 { 119 DiffCapPacket{true, defaultExtra}, 120 common.FromHex("c20100"), 121 }, 122 { 123 GetDiffLayersPacket{1111, []common.Hash{common.HexToHash("0xaece2dbf80a726206cf4df299afa09f9d8f3dcd85ff39bb4b3f0402a3a6af2f5")}}, 124 common.FromHex("e5820457e1a0aece2dbf80a726206cf4df299afa09f9d8f3dcd85ff39bb4b3f0402a3a6af2f5"), 125 }, 126 } { 127 if have, _ := rlp.EncodeToBytes(tc.message); !bytes.Equal(have, tc.want) { 128 t.Errorf("test %d, type %T, have\n\t%x\nwant\n\t%x", i, tc.message, have, tc.want) 129 } 130 } 131 }