github.com/hardtosaygoodbye/go-ethereum@v1.10.16-0.20220122011429-97003b9e6c15/les/request_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 "context" 21 "testing" 22 "time" 23 24 "github.com/hardtosaygoodbye/go-ethereum/common" 25 "github.com/hardtosaygoodbye/go-ethereum/core/rawdb" 26 "github.com/hardtosaygoodbye/go-ethereum/crypto" 27 "github.com/hardtosaygoodbye/go-ethereum/ethdb" 28 "github.com/hardtosaygoodbye/go-ethereum/light" 29 ) 30 31 var testBankSecureTrieKey = secAddr(bankAddr) 32 33 func secAddr(addr common.Address) []byte { 34 return crypto.Keccak256(addr[:]) 35 } 36 37 type accessTestFn func(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest 38 39 func TestBlockAccessLes2(t *testing.T) { testAccess(t, 2, tfBlockAccess) } 40 func TestBlockAccessLes3(t *testing.T) { testAccess(t, 3, tfBlockAccess) } 41 func TestBlockAccessLes4(t *testing.T) { testAccess(t, 4, tfBlockAccess) } 42 43 func tfBlockAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest { 44 return &light.BlockRequest{Hash: bhash, Number: number} 45 } 46 47 func TestReceiptsAccessLes2(t *testing.T) { testAccess(t, 2, tfReceiptsAccess) } 48 func TestReceiptsAccessLes3(t *testing.T) { testAccess(t, 3, tfReceiptsAccess) } 49 func TestReceiptsAccessLes4(t *testing.T) { testAccess(t, 4, tfReceiptsAccess) } 50 51 func tfReceiptsAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest { 52 return &light.ReceiptsRequest{Hash: bhash, Number: number} 53 } 54 55 func TestTrieEntryAccessLes2(t *testing.T) { testAccess(t, 2, tfTrieEntryAccess) } 56 func TestTrieEntryAccessLes3(t *testing.T) { testAccess(t, 3, tfTrieEntryAccess) } 57 func TestTrieEntryAccessLes4(t *testing.T) { testAccess(t, 4, tfTrieEntryAccess) } 58 59 func tfTrieEntryAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest { 60 if number := rawdb.ReadHeaderNumber(db, bhash); number != nil { 61 return &light.TrieRequest{Id: light.StateTrieID(rawdb.ReadHeader(db, bhash, *number)), Key: testBankSecureTrieKey} 62 } 63 return nil 64 } 65 66 func TestCodeAccessLes2(t *testing.T) { testAccess(t, 2, tfCodeAccess) } 67 func TestCodeAccessLes3(t *testing.T) { testAccess(t, 3, tfCodeAccess) } 68 func TestCodeAccessLes4(t *testing.T) { testAccess(t, 4, tfCodeAccess) } 69 70 func tfCodeAccess(db ethdb.Database, bhash common.Hash, num uint64) light.OdrRequest { 71 number := rawdb.ReadHeaderNumber(db, bhash) 72 if number != nil { 73 return nil 74 } 75 header := rawdb.ReadHeader(db, bhash, *number) 76 if header.Number.Uint64() < testContractDeployed { 77 return nil 78 } 79 sti := light.StateTrieID(header) 80 ci := light.StorageTrieID(sti, crypto.Keccak256Hash(testContractAddr[:]), common.Hash{}) 81 return &light.CodeRequest{Id: ci, Hash: crypto.Keccak256Hash(testContractCodeDeployed)} 82 } 83 84 func testAccess(t *testing.T, protocol int, fn accessTestFn) { 85 // Assemble the test environment 86 netconfig := testnetConfig{ 87 blocks: 4, 88 protocol: protocol, 89 indexFn: nil, 90 connect: true, 91 nopruning: true, 92 } 93 server, client, tearDown := newClientServerEnv(t, netconfig) 94 defer tearDown() 95 96 // Ensure the client has synced all necessary data. 97 clientHead := client.handler.backend.blockchain.CurrentHeader() 98 if clientHead.Number.Uint64() != 4 { 99 t.Fatalf("Failed to sync the chain with server, head: %v", clientHead.Number.Uint64()) 100 } 101 102 test := func(expFail uint64) { 103 for i := uint64(0); i <= server.handler.blockchain.CurrentHeader().Number.Uint64(); i++ { 104 bhash := rawdb.ReadCanonicalHash(server.db, i) 105 if req := fn(client.db, bhash, i); req != nil { 106 ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) 107 err := client.handler.backend.odr.Retrieve(ctx, req) 108 cancel() 109 110 got := err == nil 111 exp := i < expFail 112 if exp && !got { 113 t.Errorf("object retrieval failed") 114 } 115 if !exp && got { 116 t.Errorf("unexpected object retrieval success") 117 } 118 } 119 } 120 } 121 test(5) 122 }