Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollback removes physically block data from db. #4568

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/service/legacysync/syncing.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ func (ss *StateSync) UpdateBlockAndStatus(block *types.Block, bc core.BlockChain
Uint64("blockEpoch", block.Epoch().Uint64()).
Str("blockHex", block.Hash().Hex()).
Uint32("ShardID", block.ShardID()).
Err(err).
Msg("[SYNC] UpdateBlockAndStatus: Block exists")
return nil
case err != nil:
Expand Down
2 changes: 1 addition & 1 deletion core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewBlockValidator(blockchain BlockChain) *BlockValidator {
func (v *BlockValidator) ValidateBody(block *types.Block) error {
// Check whether the block's known, and if not, that it's linkable
if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) {
return ErrKnownBlock
return errors.WithMessage(ErrKnownBlock, "has block and state")
}
if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) {
if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) {
Expand Down
8 changes: 0 additions & 8 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,6 @@ type BlockChain interface {
// but does not write any state. This is used to construct competing side forks
// up to the point where they exceed the canonical total difficulty.
WriteBlockWithoutState(block *types.Block, td *big.Int) (err error)
// WriteBlockWithState writes the block and all associated state to the database.
WriteBlockWithState(
block *types.Block, receipts []*types.Receipt,
cxReceipts []*types.CXReceipt,
stakeMsgs []types2.StakeMsg,
paid reward.Reader,
state *state.DB,
) (status WriteStatus, err error)
// GetMaxGarbageCollectedBlockNumber ..
GetMaxGarbageCollectedBlockNumber() int64
// InsertChain attempts to insert the given batch of blocks in to the canonical
Expand Down
32 changes: 20 additions & 12 deletions core/blockchain_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,10 @@ func (bc *BlockChainImpl) GetBlock(hash common.Hash, number uint64) *types.Block
return block
}

func (bc *BlockChainImpl) deleteBlock(hash common.Hash, number uint64) error {
return rawdb.DeleteBlock(bc.db, hash, number)
}

func (bc *BlockChainImpl) GetBlockByHash(hash common.Hash) *types.Block {
number := bc.hc.GetBlockNumber(hash)
if number == nil {
Expand Down Expand Up @@ -1244,8 +1248,8 @@ const (
)

func (bc *BlockChainImpl) Rollback(chain []common.Hash) error {
bc.mu.Lock()
defer bc.mu.Unlock()
bc.chainmu.Lock()
defer bc.chainmu.Unlock()

valsToRemove := map[common.Address]struct{}{}
for i := len(chain) - 1; i >= 0; i-- {
Expand Down Expand Up @@ -1287,6 +1291,15 @@ func (bc *BlockChainImpl) Rollback(chain []common.Hash) error {
}
}
}
num := bc.CurrentBlock().NumberU64()
for i := uint64(1); i < 100; i++ {
blk := bc.GetBlockByNumber(num + i)
if blk != nil {
if err := bc.deleteBlock(blk.Hash(), blk.NumberU64()); err != nil {
return err
}
}
}
return bc.removeInValidatorList(valsToRemove)
}

Expand Down Expand Up @@ -1749,15 +1762,10 @@ func (bc *BlockChainImpl) insertChain(chain types.Blocks, verifyHeaders bool) (i
err = NewBlockValidator(bc).ValidateBody(block)
}
switch {
case err == ErrKnownBlock:
// Block and state both already known. However if the current block is below
// this number we did a rollback and we should reimport it nonetheless.
if bc.CurrentBlock().NumberU64() >= block.NumberU64() {
stats.ignored++
continue
}
case errors.Is(err, ErrKnownBlock):
return i, events, coalescedLogs, err

case err == consensus_engine.ErrFutureBlock:
case errors.Is(err, consensus_engine.ErrFutureBlock):
// Allow up to MaxFuture second in the future blocks. If this limit is exceeded
// the chain is discarded and processed at a later time if given.
max := big.NewInt(time.Now().Unix() + maxTimeFutureBlocks)
Expand All @@ -1768,12 +1776,12 @@ func (bc *BlockChainImpl) insertChain(chain types.Blocks, verifyHeaders bool) (i
stats.queued++
continue

case err == consensus_engine.ErrUnknownAncestor && bc.futureBlocks.Contains(block.ParentHash()):
case errors.Is(err, consensus_engine.ErrUnknownAncestor) && bc.futureBlocks.Contains(block.ParentHash()):
bc.futureBlocks.Add(block.Hash(), block)
stats.queued++
continue

case err == consensus_engine.ErrPrunedAncestor:
case errors.Is(err, consensus_engine.ErrPrunedAncestor):
// TODO: add fork choice mechanism
// Block competing with the canonical chain, store in the db, but don't process
// until the competitor TD goes above the canonical TD
Expand Down