Skip to content

Commit

Permalink
make InsertChain return an error when stopped due to l2 migration block
Browse files Browse the repository at this point in the history
  • Loading branch information
alecps committed Aug 14, 2024
1 parent 02d8916 commit 6113d1f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
// If the chain is terminating, stop processing blocks
if bc.insertStopped() {
log.Debug("Abort during block processing")
if bc.Config().IsL2Migration(block.Number()) {
err = errInsertionInterrupted
}
break
}
// If the header is a banned one, straight out abort
Expand Down
20 changes: 20 additions & 0 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/celo-org/celo-blockchain/ethdb"
"github.com/celo-org/celo-blockchain/params"
"github.com/celo-org/celo-blockchain/trie"
"github.com/stretchr/testify/require"
)

// So we can deterministically seed different blockchains
Expand Down Expand Up @@ -205,6 +206,25 @@ func TestLastBlock(t *testing.T) {
}
}

func TestNoInsertPastL2MigrationBlock(t *testing.T) {
_, blockchain, err := newCanonical(mockEngine.NewFaker(), 0, true)
if err != nil {
t.Fatalf("failed to create pristine chain: %v", err)
}
defer blockchain.Stop()

migrationBlock := 2
blockchain.chainConfig.L2MigrationBlock = big.NewInt(int64(migrationBlock))

blocks := makeBlockChain(blockchain.CurrentBlock(), 100, mockEngine.NewFullFaker(), blockchain.db, 0)
failedBlock, err := blockchain.InsertChain(blocks)
require.EqualError(t, err, errInsertionInterrupted.Error())
require.EqualValues(t, migrationBlock-1, failedBlock)
if blocks[migrationBlock-2].Hash() != rawdb.ReadHeadBlockHash(blockchain.db) {
t.Fatalf("Write/Get HeadBlockHash failed")
}
}

// Tests that given a starting canonical chain of a given size, it can be extended
// with various length chains.
func TestExtendCanonicalHeaders(t *testing.T) { testExtendCanonical(t, false) }
Expand Down

0 comments on commit 6113d1f

Please sign in to comment.