|
|
@ -103,8 +103,6 @@ const ( |
|
|
|
bodyCacheLimit = 128 |
|
|
|
bodyCacheLimit = 128 |
|
|
|
blockCacheLimit = 128 |
|
|
|
blockCacheLimit = 128 |
|
|
|
receiptsCacheLimit = 32 |
|
|
|
receiptsCacheLimit = 32 |
|
|
|
maxFutureBlocks = 16 |
|
|
|
|
|
|
|
maxTimeFutureBlocks = 30 |
|
|
|
|
|
|
|
badBlockLimit = 10 |
|
|
|
badBlockLimit = 10 |
|
|
|
triesInRedis = 1000 |
|
|
|
triesInRedis = 1000 |
|
|
|
shardCacheLimit = 10 |
|
|
|
shardCacheLimit = 10 |
|
|
@ -198,7 +196,6 @@ type BlockChainImpl struct { |
|
|
|
bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
|
|
|
|
bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
|
|
|
|
receiptsCache *lru.Cache // Cache for the most recent receipts per block
|
|
|
|
receiptsCache *lru.Cache // Cache for the most recent receipts per block
|
|
|
|
blockCache *lru.Cache // Cache for the most recent entire blocks
|
|
|
|
blockCache *lru.Cache // Cache for the most recent entire blocks
|
|
|
|
futureBlocks *lru.Cache // future blocks are blocks added for later processing
|
|
|
|
|
|
|
|
shardStateCache *lru.Cache |
|
|
|
shardStateCache *lru.Cache |
|
|
|
lastCommitsCache *lru.Cache |
|
|
|
lastCommitsCache *lru.Cache |
|
|
|
epochCache *lru.Cache // Cache epoch number → first block number
|
|
|
|
epochCache *lru.Cache // Cache epoch number → first block number
|
|
|
@ -270,7 +267,6 @@ func newBlockChainWithOptions( |
|
|
|
bodyRLPCache, _ := lru.New(bodyCacheLimit) |
|
|
|
bodyRLPCache, _ := lru.New(bodyCacheLimit) |
|
|
|
receiptsCache, _ := lru.New(receiptsCacheLimit) |
|
|
|
receiptsCache, _ := lru.New(receiptsCacheLimit) |
|
|
|
blockCache, _ := lru.New(blockCacheLimit) |
|
|
|
blockCache, _ := lru.New(blockCacheLimit) |
|
|
|
futureBlocks, _ := lru.New(maxFutureBlocks) |
|
|
|
|
|
|
|
badBlocks, _ := lru.New(badBlockLimit) |
|
|
|
badBlocks, _ := lru.New(badBlockLimit) |
|
|
|
shardCache, _ := lru.New(shardCacheLimit) |
|
|
|
shardCache, _ := lru.New(shardCacheLimit) |
|
|
|
commitsCache, _ := lru.New(commitsCacheLimit) |
|
|
|
commitsCache, _ := lru.New(commitsCacheLimit) |
|
|
@ -296,7 +292,6 @@ func newBlockChainWithOptions( |
|
|
|
bodyRLPCache: bodyRLPCache, |
|
|
|
bodyRLPCache: bodyRLPCache, |
|
|
|
receiptsCache: receiptsCache, |
|
|
|
receiptsCache: receiptsCache, |
|
|
|
blockCache: blockCache, |
|
|
|
blockCache: blockCache, |
|
|
|
futureBlocks: futureBlocks, |
|
|
|
|
|
|
|
shardStateCache: shardCache, |
|
|
|
shardStateCache: shardCache, |
|
|
|
lastCommitsCache: commitsCache, |
|
|
|
lastCommitsCache: commitsCache, |
|
|
|
epochCache: epochCache, |
|
|
|
epochCache: epochCache, |
|
|
@ -373,9 +368,6 @@ func newBlockChainWithOptions( |
|
|
|
return nil, errors.WithMessage(err, "failed to write pre-image start end blocks") |
|
|
|
return nil, errors.WithMessage(err, "failed to write pre-image start end blocks") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Take ownership of this particular state
|
|
|
|
|
|
|
|
go bc.update() |
|
|
|
|
|
|
|
return bc, nil |
|
|
|
return bc, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -684,7 +676,6 @@ func (bc *BlockChainImpl) SetHead(head uint64) error { |
|
|
|
bc.bodyRLPCache.Purge() |
|
|
|
bc.bodyRLPCache.Purge() |
|
|
|
bc.receiptsCache.Purge() |
|
|
|
bc.receiptsCache.Purge() |
|
|
|
bc.blockCache.Purge() |
|
|
|
bc.blockCache.Purge() |
|
|
|
bc.futureBlocks.Purge() |
|
|
|
|
|
|
|
bc.shardStateCache.Purge() |
|
|
|
bc.shardStateCache.Purge() |
|
|
|
|
|
|
|
|
|
|
|
// Rewind the block chain, ensuring we don't end up with a stateless head block
|
|
|
|
// Rewind the block chain, ensuring we don't end up with a stateless head block
|
|
|
@ -1216,23 +1207,6 @@ func (bc *BlockChainImpl) Stop() { |
|
|
|
utils.Logger().Info().Msg("Blockchain manager stopped") |
|
|
|
utils.Logger().Info().Msg("Blockchain manager stopped") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (bc *BlockChainImpl) procFutureBlocks() { |
|
|
|
|
|
|
|
blocks := make([]*types.Block, 0, bc.futureBlocks.Len()) |
|
|
|
|
|
|
|
for _, hash := range bc.futureBlocks.Keys() { |
|
|
|
|
|
|
|
if block, exist := bc.futureBlocks.Peek(hash); exist { |
|
|
|
|
|
|
|
blocks = append(blocks, block.(*types.Block)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if len(blocks) > 0 { |
|
|
|
|
|
|
|
types.BlockBy(types.Number).Sort(blocks) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Insert one by one as chain insertion needs contiguous ancestry between blocks
|
|
|
|
|
|
|
|
for i := range blocks { |
|
|
|
|
|
|
|
bc.InsertChain(blocks[i:i+1], true /* verifyHeaders */) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WriteStatus status of write
|
|
|
|
// WriteStatus status of write
|
|
|
|
type WriteStatus byte |
|
|
|
type WriteStatus byte |
|
|
|
|
|
|
|
|
|
|
@ -1624,7 +1598,6 @@ func (bc *BlockChainImpl) WriteBlockWithState( |
|
|
|
return NonStatTy, errors.Wrap(err, "writeHeadBlock") |
|
|
|
return NonStatTy, errors.Wrap(err, "writeHeadBlock") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bc.futureBlocks.Remove(block.Hash()) |
|
|
|
|
|
|
|
return CanonStatTy, nil |
|
|
|
return CanonStatTy, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -1758,20 +1731,10 @@ func (bc *BlockChainImpl) insertChain(chain types.Blocks, verifyHeaders bool) (i |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
case err == consensus_engine.ErrFutureBlock: |
|
|
|
case err == consensus_engine.ErrFutureBlock: |
|
|
|
// Allow up to MaxFuture second in the future blocks. If this limit is exceeded
|
|
|
|
return i, events, coalescedLogs, err |
|
|
|
// the chain is discarded and processed at a later time if given.
|
|
|
|
|
|
|
|
max := big.NewInt(time.Now().Unix() + maxTimeFutureBlocks) |
|
|
|
|
|
|
|
if block.Time().Cmp(max) > 0 { |
|
|
|
|
|
|
|
return i, events, coalescedLogs, fmt.Errorf("future block: %v > %v", block.Time(), max) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
bc.futureBlocks.Add(block.Hash(), block) |
|
|
|
|
|
|
|
stats.queued++ |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case err == consensus_engine.ErrUnknownAncestor && bc.futureBlocks.Contains(block.ParentHash()): |
|
|
|
case err == consensus_engine.ErrUnknownAncestor: |
|
|
|
bc.futureBlocks.Add(block.Hash(), block) |
|
|
|
return i, events, coalescedLogs, err |
|
|
|
stats.queued++ |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case err == consensus_engine.ErrPrunedAncestor: |
|
|
|
case err == consensus_engine.ErrPrunedAncestor: |
|
|
|
// TODO: add fork choice mechanism
|
|
|
|
// TODO: add fork choice mechanism
|
|
|
@ -2020,19 +1983,6 @@ func (bc *BlockChainImpl) PostChainEvents(events []interface{}, logs []*types.Lo |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (bc *BlockChainImpl) update() { |
|
|
|
|
|
|
|
futureTimer := time.NewTicker(5 * time.Second) |
|
|
|
|
|
|
|
defer futureTimer.Stop() |
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
select { |
|
|
|
|
|
|
|
case <-futureTimer.C: |
|
|
|
|
|
|
|
bc.procFutureBlocks() |
|
|
|
|
|
|
|
case <-bc.quit: |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// BadBlock ..
|
|
|
|
// BadBlock ..
|
|
|
|
type BadBlock struct { |
|
|
|
type BadBlock struct { |
|
|
|
Block *types.Block |
|
|
|
Block *types.Block |
|
|
|