Tests that block headers can be retrieved from a remote chain based on user queries.
(t *testing.T)
| 62 | |
| 63 | // Tests that block headers can be retrieved from a remote chain based on user queries. |
| 64 | func TestGetBlockHeaders(t *testing.T) { |
| 65 | pm, _ := newTestProtocolManagerMust(t, syncer.MaxHashFetch+15, nil, nil) |
| 66 | peer, _ := newTestPeer("peer", 64, pm, true) |
| 67 | defer peer.close() |
| 68 | |
| 69 | // Create a "random" unknown hash for testing |
| 70 | var unknown common.Hash |
| 71 | for i := range unknown { |
| 72 | unknown[i] = byte(i) |
| 73 | } |
| 74 | // Create a batch of tests for various scenarios |
| 75 | limit := uint64(syncer.MaxHeaderFetch) |
| 76 | tests := []struct { |
| 77 | query *getBlockHeadersData // The query to execute for header retrieval |
| 78 | expect []common.Hash // The hashes of the block whose headers are expected |
| 79 | }{ |
| 80 | // A single random block should be retrievable by hash and number too |
| 81 | { |
| 82 | &getBlockHeadersData{Origin: hashOrNumber{Hash: pm.blockchain.GetBlockByNumber(limit / 2).Hash()}, Amount: 1}, |
| 83 | []common.Hash{pm.blockchain.GetBlockByNumber(limit / 2).Hash()}, |
| 84 | }, |
| 85 | { |
| 86 | &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 1}, |
| 87 | []common.Hash{pm.blockchain.GetBlockByNumber(limit / 2).Hash()}, |
| 88 | }, |
| 89 | // Multiple headers should be retrievable in both directions |
| 90 | { |
| 91 | &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3}, |
| 92 | []common.Hash{ |
| 93 | pm.blockchain.GetBlockByNumber(limit / 2).Hash(), |
| 94 | pm.blockchain.GetBlockByNumber(limit/2 + 1).Hash(), |
| 95 | pm.blockchain.GetBlockByNumber(limit/2 + 2).Hash(), |
| 96 | }, |
| 97 | }, |
| 98 | { |
| 99 | &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3, Reverse: true}, |
| 100 | []common.Hash{ |
| 101 | pm.blockchain.GetBlockByNumber(limit / 2).Hash(), |
| 102 | pm.blockchain.GetBlockByNumber(limit/2 - 1).Hash(), |
| 103 | pm.blockchain.GetBlockByNumber(limit/2 - 2).Hash(), |
| 104 | }, |
| 105 | }, |
| 106 | // Multiple headers with skip lists should be retrievable |
| 107 | { |
| 108 | &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3}, |
| 109 | []common.Hash{ |
| 110 | pm.blockchain.GetBlockByNumber(limit / 2).Hash(), |
| 111 | pm.blockchain.GetBlockByNumber(limit/2 + 4).Hash(), |
| 112 | pm.blockchain.GetBlockByNumber(limit/2 + 8).Hash(), |
| 113 | }, |
| 114 | }, |
| 115 | { |
| 116 | &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3, Reverse: true}, |
| 117 | []common.Hash{ |
| 118 | pm.blockchain.GetBlockByNumber(limit / 2).Hash(), |
| 119 | pm.blockchain.GetBlockByNumber(limit/2 - 4).Hash(), |
| 120 | pm.blockchain.GetBlockByNumber(limit/2 - 8).Hash(), |
| 121 | }, |
nothing calls this directly
no test coverage detected