github.com/luckypickle/go-ethereum-vet@v1.14.2/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/luckypickle/go-ethereum-vet/common"
    25  	"github.com/luckypickle/go-ethereum-vet/core/rawdb"
    26  	"github.com/luckypickle/go-ethereum-vet/crypto"
    27  	"github.com/luckypickle/go-ethereum-vet/eth"
    28  	"github.com/luckypickle/go-ethereum-vet/ethdb"
    29  	"github.com/luckypickle/go-ethereum-vet/light"
    30  )
    31  
    32  var testBankSecureTrieKey = secAddr(testBankAddress)
    33  
    34  func secAddr(addr common.Address) []byte {
    35  	return crypto.Keccak256(addr[:])
    36  }
    37  
    38  type accessTestFn func(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest
    39  
    40  func TestBlockAccessLes1(t *testing.T) { testAccess(t, 1, tfBlockAccess) }
    41  
    42  func TestBlockAccessLes2(t *testing.T) { testAccess(t, 2, tfBlockAccess) }
    43  
    44  func tfBlockAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    45  	return &light.BlockRequest{Hash: bhash, Number: number}
    46  }
    47  
    48  func TestReceiptsAccessLes1(t *testing.T) { testAccess(t, 1, tfReceiptsAccess) }
    49  
    50  func TestReceiptsAccessLes2(t *testing.T) { testAccess(t, 2, tfReceiptsAccess) }
    51  
    52  func tfReceiptsAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    53  	return &light.ReceiptsRequest{Hash: bhash, Number: number}
    54  }
    55  
    56  func TestTrieEntryAccessLes1(t *testing.T) { testAccess(t, 1, tfTrieEntryAccess) }
    57  
    58  func TestTrieEntryAccessLes2(t *testing.T) { testAccess(t, 2, tfTrieEntryAccess) }
    59  
    60  func tfTrieEntryAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    61  	if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
    62  		return &light.TrieRequest{Id: light.StateTrieID(rawdb.ReadHeader(db, bhash, *number)), Key: testBankSecureTrieKey}
    63  	}
    64  	return nil
    65  }
    66  
    67  func TestCodeAccessLes1(t *testing.T) { testAccess(t, 1, tfCodeAccess) }
    68  
    69  func TestCodeAccessLes2(t *testing.T) { testAccess(t, 2, tfCodeAccess) }
    70  
    71  func tfCodeAccess(db ethdb.Database, bhash common.Hash, num uint64) light.OdrRequest {
    72  	number := rawdb.ReadHeaderNumber(db, bhash)
    73  	if number != nil {
    74  		return nil
    75  	}
    76  	header := rawdb.ReadHeader(db, bhash, *number)
    77  	if header.Number.Uint64() < testContractDeployed {
    78  		return nil
    79  	}
    80  	sti := light.StateTrieID(header)
    81  	ci := light.StorageTrieID(sti, crypto.Keccak256Hash(testContractAddr[:]), common.Hash{})
    82  	return &light.CodeRequest{Id: ci, Hash: crypto.Keccak256Hash(testContractCodeDeployed)}
    83  }
    84  
    85  func testAccess(t *testing.T, protocol int, fn accessTestFn) {
    86  	// Assemble the test environment
    87  	peers := newPeerSet()
    88  	dist := newRequestDistributor(peers, make(chan struct{}))
    89  	rm := newRetrieveManager(peers, dist, nil)
    90  	db := ethdb.NewMemDatabase()
    91  	ldb := ethdb.NewMemDatabase()
    92  	odr := NewLesOdr(ldb, rm)
    93  	odr.SetIndexers(light.NewChtIndexer(db, true, nil), light.NewBloomTrieIndexer(db, true, nil), eth.NewBloomIndexer(db, light.BloomTrieFrequency, light.HelperTrieConfirmations))
    94  
    95  	pm := newTestProtocolManagerMust(t, false, 4, testChainGen, nil, nil, db)
    96  	lpm := newTestProtocolManagerMust(t, true, 0, nil, peers, odr, ldb)
    97  	_, err1, lpeer, err2 := newTestPeerPair("peer", protocol, pm, lpm)
    98  	select {
    99  	case <-time.After(time.Millisecond * 100):
   100  	case err := <-err1:
   101  		t.Fatalf("peer 1 handshake error: %v", err)
   102  	case err := <-err2:
   103  		t.Fatalf("peer 1 handshake error: %v", err)
   104  	}
   105  
   106  	lpm.synchronise(lpeer)
   107  
   108  	test := func(expFail uint64) {
   109  		for i := uint64(0); i <= pm.blockchain.CurrentHeader().Number.Uint64(); i++ {
   110  			bhash := rawdb.ReadCanonicalHash(db, i)
   111  			if req := fn(ldb, bhash, i); req != nil {
   112  				ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
   113  				defer cancel()
   114  
   115  				err := odr.Retrieve(ctx, req)
   116  				got := err == nil
   117  				exp := i < expFail
   118  				if exp && !got {
   119  					t.Errorf("object retrieval failed")
   120  				}
   121  				if !exp && got {
   122  					t.Errorf("unexpected object retrieval success")
   123  				}
   124  			}
   125  		}
   126  	}
   127  
   128  	// temporarily remove peer to test odr fails
   129  	peers.Unregister(lpeer.id)
   130  	time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
   131  	// expect retrievals to fail (except genesis block) without a les peer
   132  	test(0)
   133  
   134  	peers.Register(lpeer)
   135  	time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
   136  	lpeer.lock.Lock()
   137  	lpeer.hasBlock = func(common.Hash, uint64) bool { return true }
   138  	lpeer.lock.Unlock()
   139  	// expect all retrievals to pass
   140  	test(5)
   141  }