fetcher_test.go 3.2 KB

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