github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/les/odr_test.go (about) 1 // Copyright 2016 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 les 18 19 import ( 20 "bytes" 21 "context" 22 "math/big" 23 "testing" 24 "time" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/common/math" 28 "github.com/ethereum/go-ethereum/core" 29 "github.com/ethereum/go-ethereum/core/state" 30 "github.com/ethereum/go-ethereum/core/types" 31 "github.com/ethereum/go-ethereum/core/vm" 32 "github.com/ethereum/go-ethereum/ethdb" 33 "github.com/ethereum/go-ethereum/light" 34 "github.com/ethereum/go-ethereum/params" 35 "github.com/ethereum/go-ethereum/rlp" 36 ) 37 38 type odrTestFn func(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte 39 40 func TestOdrGetBlockLes1(t *testing.T) { testOdr(t, 1, 1, odrGetBlock) } 41 42 func odrGetBlock(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte { 43 var block *types.Block 44 if bc != nil { 45 block = bc.GetBlockByHash(bhash) 46 } else { 47 block, _ = lc.GetBlockByHash(ctx, bhash) 48 } 49 if block == nil { 50 return nil 51 } 52 rlp, _ := rlp.EncodeToBytes(block) 53 return rlp 54 } 55 56 func TestOdrGetReceiptsLes1(t *testing.T) { testOdr(t, 1, 1, odrGetReceipts) } 57 58 func odrGetReceipts(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte { 59 var receipts types.Receipts 60 if bc != nil { 61 receipts = core.GetBlockReceipts(db, bhash, core.GetBlockNumber(db, bhash)) 62 } else { 63 receipts, _ = light.GetBlockReceipts(ctx, lc.Odr(), bhash, core.GetBlockNumber(db, bhash)) 64 } 65 if receipts == nil { 66 return nil 67 } 68 rlp, _ := rlp.EncodeToBytes(receipts) 69 return rlp 70 } 71 72 func TestOdrAccountsLes1(t *testing.T) { testOdr(t, 1, 1, odrAccounts) } 73 74 func odrAccounts(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte { 75 dummyAddr := common.HexToAddress("1234567812345678123456781234567812345678") 76 acc := []common.Address{testBankAddress, acc1Addr, acc2Addr, dummyAddr} 77 78 var res []byte 79 for _, addr := range acc { 80 if bc != nil { 81 header := bc.GetHeaderByHash(bhash) 82 st, err := state.New(header.Root, db) 83 if err == nil { 84 bal := st.GetBalance(addr) 85 rlp, _ := rlp.EncodeToBytes(bal) 86 res = append(res, rlp...) 87 } 88 } else { 89 header := lc.GetHeaderByHash(bhash) 90 st := light.NewLightState(light.StateTrieID(header), lc.Odr()) 91 bal, err := st.GetBalance(ctx, addr) 92 if err == nil { 93 rlp, _ := rlp.EncodeToBytes(bal) 94 res = append(res, rlp...) 95 } 96 } 97 } 98 99 return res 100 } 101 102 func TestOdrContractCallLes1(t *testing.T) { testOdr(t, 1, 2, odrContractCall) } 103 104 type callmsg struct { 105 types.Message 106 } 107 108 func (callmsg) CheckNonce() bool { return false } 109 110 func odrContractCall(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte { 111 data := common.Hex2Bytes("60CD26850000000000000000000000000000000000000000000000000000000000000000") 112 113 var res []byte 114 for i := 0; i < 3; i++ { 115 data[35] = byte(i) 116 if bc != nil { 117 header := bc.GetHeaderByHash(bhash) 118 statedb, err := state.New(header.Root, db) 119 120 if err == nil { 121 from := statedb.GetOrNewStateObject(testBankAddress) 122 from.SetBalance(math.MaxBig256) 123 124 msg := callmsg{types.NewMessage(from.Address(), &testContractAddr, 0, new(big.Int), big.NewInt(100000), new(big.Int), data, false)} 125 126 context := core.NewEVMContext(msg, header, bc, nil) 127 vmenv := vm.NewEVM(context, statedb, config, vm.Config{}) 128 129 //vmenv := core.NewEnv(statedb, config, bc, msg, header, vm.Config{}) 130 gp := new(core.GasPool).AddGas(math.MaxBig256) 131 ret, _, _ := core.ApplyMessage(vmenv, msg, gp) 132 res = append(res, ret...) 133 } 134 } else { 135 header := lc.GetHeaderByHash(bhash) 136 state := light.NewLightState(light.StateTrieID(header), lc.Odr()) 137 vmstate := light.NewVMState(ctx, state) 138 from, err := state.GetOrNewStateObject(ctx, testBankAddress) 139 if err == nil { 140 from.SetBalance(math.MaxBig256) 141 142 msg := callmsg{types.NewMessage(from.Address(), &testContractAddr, 0, new(big.Int), big.NewInt(100000), new(big.Int), data, false)} 143 144 context := core.NewEVMContext(msg, header, lc, nil) 145 vmenv := vm.NewEVM(context, vmstate, config, vm.Config{}) 146 147 //vmenv := light.NewEnv(ctx, state, config, lc, msg, header, vm.Config{}) 148 gp := new(core.GasPool).AddGas(math.MaxBig256) 149 ret, _, _ := core.ApplyMessage(vmenv, msg, gp) 150 if vmstate.Error() == nil { 151 res = append(res, ret...) 152 } 153 } 154 } 155 } 156 return res 157 } 158 159 func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { 160 // Assemble the test environment 161 pm, db, odr := newTestProtocolManagerMust(t, false, 4, testChainGen) 162 lpm, ldb, odr := newTestProtocolManagerMust(t, true, 0, nil) 163 _, err1, lpeer, err2 := newTestPeerPair("peer", protocol, pm, lpm) 164 pool := &testServerPool{} 165 lpm.reqDist = newRequestDistributor(pool.getAllPeers, lpm.quitSync) 166 odr.reqDist = lpm.reqDist 167 pool.setPeer(lpeer) 168 odr.serverPool = pool 169 lpeer.hasBlock = func(common.Hash, uint64) bool { return true } 170 select { 171 case <-time.After(time.Millisecond * 100): 172 case err := <-err1: 173 t.Fatalf("peer 1 handshake error: %v", err) 174 case err := <-err2: 175 t.Fatalf("peer 1 handshake error: %v", err) 176 } 177 178 lpm.synchronise(lpeer) 179 180 test := func(expFail uint64) { 181 for i := uint64(0); i <= pm.blockchain.CurrentHeader().Number.Uint64(); i++ { 182 bhash := core.GetCanonicalHash(db, i) 183 b1 := fn(light.NoOdr, db, pm.chainConfig, pm.blockchain.(*core.BlockChain), nil, bhash) 184 185 ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) 186 defer cancel() 187 b2 := fn(ctx, ldb, lpm.chainConfig, nil, lpm.blockchain.(*light.LightChain), bhash) 188 189 eq := bytes.Equal(b1, b2) 190 exp := i < expFail 191 if exp && !eq { 192 t.Errorf("odr mismatch") 193 } 194 if !exp && eq { 195 t.Errorf("unexpected odr match") 196 } 197 } 198 } 199 200 // temporarily remove peer to test odr fails 201 pool.setPeer(nil) 202 // expect retrievals to fail (except genesis block) without a les peer 203 test(expFail) 204 pool.setPeer(lpeer) 205 // expect all retrievals to pass 206 test(5) 207 pool.setPeer(nil) 208 // still expect all retrievals to pass, now data should be cached locally 209 test(5) 210 }