github.com/letterj/go-ethereum@v1.8.22-0.20190204142846-520024dfd689/les/fetcher_test.go (about)

     1  package les
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"net"
     8  
     9  	"github.com/ethereum/go-ethereum/common"
    10  	"github.com/ethereum/go-ethereum/core/types"
    11  	"github.com/ethereum/go-ethereum/crypto"
    12  	"github.com/ethereum/go-ethereum/p2p"
    13  	"github.com/ethereum/go-ethereum/p2p/enode"
    14  )
    15  
    16  func TestFetcherULCPeerSelector(t *testing.T) {
    17  
    18  	var (
    19  		id1 enode.ID = newNodeID(t).ID()
    20  		id2 enode.ID = newNodeID(t).ID()
    21  		id3 enode.ID = newNodeID(t).ID()
    22  		id4 enode.ID = newNodeID(t).ID()
    23  	)
    24  
    25  	ftn1 := &fetcherTreeNode{
    26  		hash: common.HexToHash("1"),
    27  		td:   big.NewInt(1),
    28  	}
    29  	ftn2 := &fetcherTreeNode{
    30  		hash:   common.HexToHash("2"),
    31  		td:     big.NewInt(2),
    32  		parent: ftn1,
    33  	}
    34  	ftn3 := &fetcherTreeNode{
    35  		hash:   common.HexToHash("3"),
    36  		td:     big.NewInt(3),
    37  		parent: ftn2,
    38  	}
    39  	lf := lightFetcher{
    40  		pm: &ProtocolManager{
    41  			ulc: &ulc{
    42  				trustedKeys: map[string]struct{}{
    43  					id1.String(): {},
    44  					id2.String(): {},
    45  					id3.String(): {},
    46  					id4.String(): {},
    47  				},
    48  				minTrustedFraction: 70,
    49  			},
    50  		},
    51  		maxConfirmedTd: ftn1.td,
    52  
    53  		peers: map[*peer]*fetcherPeerInfo{
    54  			{
    55  				id:        "peer1",
    56  				Peer:      p2p.NewPeer(id1, "peer1", []p2p.Cap{}),
    57  				isTrusted: true,
    58  			}: {
    59  				nodeByHash: map[common.Hash]*fetcherTreeNode{
    60  					ftn1.hash: ftn1,
    61  					ftn2.hash: ftn2,
    62  				},
    63  			},
    64  			{
    65  				Peer:      p2p.NewPeer(id2, "peer2", []p2p.Cap{}),
    66  				id:        "peer2",
    67  				isTrusted: true,
    68  			}: {
    69  				nodeByHash: map[common.Hash]*fetcherTreeNode{
    70  					ftn1.hash: ftn1,
    71  					ftn2.hash: ftn2,
    72  				},
    73  			},
    74  			{
    75  				id:        "peer3",
    76  				Peer:      p2p.NewPeer(id3, "peer3", []p2p.Cap{}),
    77  				isTrusted: true,
    78  			}: {
    79  				nodeByHash: map[common.Hash]*fetcherTreeNode{
    80  					ftn1.hash: ftn1,
    81  					ftn2.hash: ftn2,
    82  					ftn3.hash: ftn3,
    83  				},
    84  			},
    85  			{
    86  				id:        "peer4",
    87  				Peer:      p2p.NewPeer(id4, "peer4", []p2p.Cap{}),
    88  				isTrusted: true,
    89  			}: {
    90  				nodeByHash: map[common.Hash]*fetcherTreeNode{
    91  					ftn1.hash: ftn1,
    92  				},
    93  			},
    94  		},
    95  		chain: &lightChainStub{
    96  			tds: map[common.Hash]*big.Int{},
    97  			headers: map[common.Hash]*types.Header{
    98  				ftn1.hash: {},
    99  				ftn2.hash: {},
   100  				ftn3.hash: {},
   101  			},
   102  		},
   103  	}
   104  	bestHash, bestAmount, bestTD, sync := lf.findBestRequest()
   105  
   106  	if bestTD == nil {
   107  		t.Fatal("Empty result")
   108  	}
   109  
   110  	if bestTD.Cmp(ftn2.td) != 0 {
   111  		t.Fatal("bad td", bestTD)
   112  	}
   113  	if bestHash != ftn2.hash {
   114  		t.Fatal("bad hash", bestTD)
   115  	}
   116  
   117  	_, _ = bestAmount, sync
   118  }
   119  
   120  type lightChainStub struct {
   121  	BlockChain
   122  	tds                         map[common.Hash]*big.Int
   123  	headers                     map[common.Hash]*types.Header
   124  	insertHeaderChainAssertFunc func(chain []*types.Header, checkFreq int) (int, error)
   125  }
   126  
   127  func (l *lightChainStub) GetHeader(hash common.Hash, number uint64) *types.Header {
   128  	if h, ok := l.headers[hash]; ok {
   129  		return h
   130  	}
   131  
   132  	return nil
   133  }
   134  
   135  func (l *lightChainStub) LockChain()   {}
   136  func (l *lightChainStub) UnlockChain() {}
   137  
   138  func (l *lightChainStub) GetTd(hash common.Hash, number uint64) *big.Int {
   139  	if td, ok := l.tds[hash]; ok {
   140  		return td
   141  	}
   142  	return nil
   143  }
   144  
   145  func (l *lightChainStub) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) {
   146  	return l.insertHeaderChainAssertFunc(chain, checkFreq)
   147  }
   148  
   149  func newNodeID(t *testing.T) *enode.Node {
   150  	key, err := crypto.GenerateKey()
   151  	if err != nil {
   152  		t.Fatal("generate key err:", err)
   153  	}
   154  	return enode.NewV4(&key.PublicKey, net.IP{}, 35000, 35000)
   155  }