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