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