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

Function TestHeaderVerification

core/block_validator_test.go:32–82  ·  view source on GitHub ↗

Tests that simple header verification works, for both good and bad blocks.

(t *testing.T)

Source from the content-addressed store, hash-verified

30
31// Tests that simple header verification works, for both good and bad blocks.
32func TestHeaderVerification(t *testing.T) {
33 testdb := database.NewMemDatabase()
34 genesis := DefaultGenesisBlock()
35 genesisBlock := genesis.MustCommit(testdb)
36
37 remoteDB := database.NewIpfsDbWithAdapter(database.NewFakeIpfsAdapter())
38
39 config := configs.ChainConfigInfo().Dpor
40 d := dpor.NewFaker(config, testdb)
41
42 blocks, _ := GenerateChain(genesis.Config, genesisBlock, d, testdb, remoteDB, 8, nil)
43
44 headers := make([]*types.Header, len(blocks))
45 for i, block := range blocks {
46 headers[i] = block.Header()
47 }
48 // Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
49 chain, _ := NewBlockChain(testdb, nil, genesis.Config, d, vm.Config{}, remoteDB, nil)
50 defer chain.Stop()
51
52 for i := 0; i < len(blocks); i++ {
53 for j, valid := range []bool{true, false} {
54 var results <-chan error
55
56 if valid {
57 engine := dpor.NewFaker(config, testdb)
58 _, results = engine.VerifyHeaders(chain, []*types.Header{headers[i]}, []bool{true}, []*types.Header{headers[i]})
59 } else {
60 // engine := dpor.New(config, testdb)
61 engine := dpor.NewFakeFailer(config, testdb, headers[i].Number.Uint64())
62 _, results = engine.VerifyHeaders(chain, []*types.Header{headers[i]}, []bool{true}, []*types.Header{headers[i]})
63 }
64 // Wait for the verification result
65 select {
66 case result := <-results:
67 if (result == nil) != valid {
68 t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, result, valid)
69 }
70 case <-time.After(time.Second):
71 t.Fatalf("test %d.%d: verification timeout", i, j)
72 }
73 // Make sure no more data is returned
74 select {
75 case result := <-results:
76 t.Fatalf("test %d.%d: unexpected result returned: %v", i, j, result)
77 case <-time.After(25 * time.Millisecond):
78 }
79 }
80 chain.InsertChain(blocks[i : i+1])
81 }
82}
83
84// Tests that concurrent header verification works, for both good and bad blocks.
85func TestHeaderConcurrentVerification2(t *testing.T) {

Callers

nothing calls this directly

Calls 10

StopMethod · 0.95
InsertChainMethod · 0.95
DefaultGenesisBlockFunction · 0.85
GenerateChainFunction · 0.85
NewBlockChainFunction · 0.85
MustCommitMethod · 0.80
ChainConfigInfoMethod · 0.80
HeaderMethod · 0.80
Uint64Method · 0.80
VerifyHeadersMethod · 0.65

Tested by

no test coverage detected