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

Function NewBlockChain

core/blockchain.go:213–285  ·  view source on GitHub ↗

NewBlockChain returns a fully initialised block chain using information available in the database. It initialises the default Ethereum Validator and Processor.

(db database.Database, cacheConfig *CacheConfig, chainConfig *configs.ChainConfig, engine consensus.Engine,
	vmConfig vm.Config, remoteDB database.RemoteDatabase, accm *accounts.Manager)

Source from the content-addressed store, hash-verified

211// available in the database. It initialises the default Ethereum Validator and
212// Processor.
213func NewBlockChain(db database.Database, cacheConfig *CacheConfig, chainConfig *configs.ChainConfig, engine consensus.Engine,
214 vmConfig vm.Config, remoteDB database.RemoteDatabase, accm *accounts.Manager) (*BlockChain, error) {
215 if cacheConfig == nil {
216 cacheConfig = &CacheConfig{
217 TrieNodeLimit: 256 * 1024 * 1024,
218 TrieTimeLimit: 5 * time.Minute,
219 }
220 }
221 bodyCache, _ := lru.New(bodyCacheLimit)
222 bodyRLPCache, _ := lru.New(bodyCacheLimit)
223 blockCache, _ := lru.New(blockCacheLimit)
224 futureBlocks, _ := lru.New(maxFutureBlocks)
225 unknownAncestors, _ := lru.New(maxFutureBlocks)
226 badBlocks, _ := lru.New(badBlockLimit)
227
228 bc := &BlockChain{
229 chainConfig: chainConfig,
230 cacheConfig: cacheConfig,
231 db: db,
232 triegc: prque.New(),
233 stateCache: state.NewDatabase(db),
234 Quit: make(chan struct{}),
235 bodyCache: bodyCache,
236 bodyRLPCache: bodyRLPCache,
237 blockCache: blockCache,
238 futureBlocks: futureBlocks,
239 unknownAncestors: unknownAncestors,
240 engine: engine,
241 vmConfig: vmConfig,
242 badBlocks: badBlocks,
243 privateStateCache: state.NewDatabase(db),
244 remoteDB: remoteDB,
245 ErrChan: make(chan error),
246 syncMode: syncer.FullSync,
247 srCache: newStatesAndReceiptsCache(bodyCacheLimit),
248 }
249 bc.SetValidator(NewBlockValidator(chainConfig, bc, engine))
250 bc.SetProcessor(NewStateProcessor(chainConfig, bc, engine, accm))
251
252 var err error
253 bc.hc, err = NewHeaderChain(db, chainConfig, engine, bc.getProcInterrupt)
254 if err != nil {
255 return nil, err
256 }
257 bc.genesisBlock = bc.GetBlockByNumber(0)
258 if bc.genesisBlock == nil {
259 return nil, ErrNoGenesis
260 }
261 if err := bc.loadLastState(); err != nil {
262 return nil, err
263 }
264 // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain
265 for hash := range BadHashes {
266 if header := bc.GetHeaderByHash(hash); header != nil {
267 // get the canonical block corresponding to the offending header's number
268 headerByNumber := bc.GetHeaderByNumber(header.Number.Uint64())
269 // make sure the headerByNumber (if present) is in our current canonical chain
270 if headerByNumber != nil && headerByNumber.Hash() == header.Hash() {

Callers 15

GenerateChainFunction · 0.85
TestHeaderVerificationFunction · 0.85
ExampleGenerateChainFunction · 0.85
benchInsertChainFunction · 0.85
benchReadChainFunction · 0.85
newCanonicalFunction · 0.85
TestChainTxReorgsFunction · 0.85
TestLogReorgsFunction · 0.85
TestReorgSideEventFunction · 0.85

Calls 15

SetValidatorMethod · 0.95
SetProcessorMethod · 0.95
GetBlockByNumberMethod · 0.95
loadLastStateMethod · 0.95
GetHeaderByHashMethod · 0.95
GetHeaderByNumberMethod · 0.95
SetHeadMethod · 0.95
updateMethod · 0.95
CurrentBlockMethod · 0.95
SetKnownHeadMethod · 0.95
NewBlockValidatorFunction · 0.85

Tested by 14

TestHeaderVerificationFunction · 0.68
ExampleGenerateChainFunction · 0.68
benchInsertChainFunction · 0.68
benchReadChainFunction · 0.68
newCanonicalFunction · 0.68
TestChainTxReorgsFunction · 0.68
TestLogReorgsFunction · 0.68
TestReorgSideEventFunction · 0.68
TestTrieForkGCFunction · 0.68