MCPcopy Create free account
hub / github.com/CPChain/chain / TestGetBlockHeaders

Function TestGetBlockHeaders

protocols/cpc/handler_test.go:64–220  ·  view source on GitHub ↗

Tests that block headers can be retrieved from a remote chain based on user queries.

(t *testing.T)

Source from the content-addressed store, hash-verified

62
63// Tests that block headers can be retrieved from a remote chain based on user queries.
64func 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 },

Callers

nothing calls this directly

Calls 10

newTestPeerFunction · 0.85
NumberU64Method · 0.80
HeaderMethod · 0.80
HashMethod · 0.65
GetBlockByNumberMethod · 0.65
CurrentBlockMethod · 0.65
GetBlockByHashMethod · 0.65
closeMethod · 0.45

Tested by

no test coverage detected