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

Function testBlockChainImport

core/blockchain_test.go:100–137  ·  view source on GitHub ↗

testBlockChainImport tries to process a chain of blocks, writing them into the database if successful.

(chain types.Blocks, blockchain *BlockChain)

Source from the content-addressed store, hash-verified

98// testBlockChainImport tries to process a chain of blocks, writing them into
99// the database if successful.
100func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
101 for _, block := range chain {
102 // Try and process the block
103 err := blockchain.engine.VerifyHeader(blockchain, block.Header(), true, nil)
104 if err == nil {
105 err = blockchain.validator.ValidateBody(block)
106 }
107 if err != nil {
108 if err == ErrKnownBlock {
109 continue
110 }
111 return err
112 }
113 statedb, err := state.New(blockchain.GetBlockByHash(block.ParentHash()).StateRoot(), blockchain.stateCache)
114 if err != nil {
115 return err
116 }
117 // TODO: check if below statement is correct.
118 privStateDB, _ := state.New(GetPrivateStateRoot(blockchain.db,
119 blockchain.GetBlockByHash(block.ParentHash()).StateRoot()), blockchain.privateStateCache)
120 // TODO: remoteDB is not used as there are no private tx here, add a concrete remoteDB if test private tx in future.
121 receipts, _, _, usedGas, err := blockchain.Processor().Process(block, statedb, privStateDB, nil, vm.Config{})
122 if err != nil {
123 blockchain.reportBlock(block, receipts, err)
124 return err
125 }
126 err = blockchain.validator.ValidateState(block, blockchain.GetBlockByHash(block.ParentHash()), statedb, receipts, usedGas)
127 if err != nil {
128 blockchain.reportBlock(block, receipts, err)
129 return err
130 }
131 blockchain.mu.Lock()
132 rawdb.WriteBlock(blockchain.db, block)
133 _, _ = statedb.Commit(true)
134 blockchain.mu.Unlock()
135 }
136 return nil
137}
138
139// testHeaderChainImport tries to process a chain of header, writing them into
140// the database if successful.

Callers 1

TestBrokenBlockChainFunction · 0.85

Calls 14

GetPrivateStateRootFunction · 0.85
HeaderMethod · 0.80
StateRootMethod · 0.80
ParentHashMethod · 0.80
ProcessorMethod · 0.80
reportBlockMethod · 0.80
LockMethod · 0.80
UnlockMethod · 0.80
VerifyHeaderMethod · 0.65
ValidateBodyMethod · 0.65
GetBlockByHashMethod · 0.65
ProcessMethod · 0.65

Tested by

no test coverage detected