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)
| 211 | // available in the database. It initialises the default Ethereum Validator and |
| 212 | // Processor. |
| 213 | func 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() { |