Skip to content

Commit

Permalink
Tudor/fix canonical update (#2140)
Browse files Browse the repository at this point in the history
* fix canonical block for a weird corner case

* fix canonical block for a weird corner case
  • Loading branch information
tudor-malene authored Nov 13, 2024
1 parent 2302c3a commit d83dd77
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 31 deletions.
9 changes: 8 additions & 1 deletion go/enclave/components/block_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (bp *l1BlockProcessor) tryAndInsertBlock(ctx context.Context, br *common.Bl
}

bp.logger.Trace("BlockHeader inserted successfully",
log.BlockHeightKey, block.Number, log.BlockHashKey, block.Hash(), "ingestionType", ingestionType)
log.BlockHeightKey, block.Number, log.BlockHashKey, block.Hash(), "parentHash", block.ParentHash, "ingestionType", ingestionType)

return ingestionType, nil
}
Expand All @@ -136,6 +136,11 @@ func (bp *l1BlockProcessor) ingestBlock(ctx context.Context, block *types.Header
}
return nil, fmt.Errorf("could not retrieve head block. Cause: %w", err)
}

if prevL1Head.Hash() == block.Hash() {
return &BlockIngestionType{OldCanonicalBlock: true}, nil
}

// we do a basic sanity check, comparing the received block to the head block on the chain
if block.ParentHash != prevL1Head.Hash() {
isCanon, err := bp.storage.IsBlockCanonical(ctx, block.Hash())
Expand All @@ -158,6 +163,8 @@ func (bp *l1BlockProcessor) ingestBlock(ctx context.Context, block *types.Header

if chainFork.IsFork() {
bp.logger.Info("Fork detected in the l1 chain", "can", chainFork.CommonAncestor.Hash(), "noncan", prevL1Head.Hash())
} else {
bp.logger.Error("Should not happen. Weird Fork detected in the l1 chain", "fork", chainFork)
}
return &BlockIngestionType{ChainFork: chainFork, PreGenesis: false}, nil
}
Expand Down
2 changes: 0 additions & 2 deletions go/enclave/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ func NewEnclave(config *enclaveconfig.EnclaveConfig, genesis *genesis.Genesis, m
batchExecutor,
registry,
rProducer,
rConsumer,
rollupCompression,
gethEncodingService,
logger,
Expand All @@ -217,7 +216,6 @@ func NewEnclave(config *enclaveconfig.EnclaveConfig, genesis *genesis.Genesis, m
blockProcessor,
batchExecutor,
registry,
rConsumer,
chainConfig,
storage,
sigVerifier,
Expand Down
3 changes: 0 additions & 3 deletions go/enclave/nodetype/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type sequencer struct {
batchProducer components.BatchExecutor
batchRegistry components.BatchRegistry
rollupProducer components.RollupProducer
rollupConsumer components.RollupConsumer
rollupCompression *components.RollupCompression
gethEncoding gethencoding.EncodingService

Expand All @@ -68,7 +67,6 @@ func NewSequencer(
batchExecutor components.BatchExecutor,
registry components.BatchRegistry,
rollupProducer components.RollupProducer,
rollupConsumer components.RollupConsumer,
rollupCompression *components.RollupCompression,
gethEncodingService gethencoding.EncodingService,
logger gethlog.Logger,
Expand All @@ -86,7 +84,6 @@ func NewSequencer(
batchProducer: batchExecutor,
batchRegistry: registry,
rollupProducer: rollupProducer,
rollupConsumer: rollupConsumer,
rollupCompression: rollupCompression,
gethEncoding: gethEncodingService,
logger: logger,
Expand Down
3 changes: 0 additions & 3 deletions go/enclave/nodetype/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type obsValidator struct {
blockProcessor components.L1BlockProcessor
batchExecutor components.BatchExecutor
batchRegistry components.BatchRegistry
rollupConsumer components.RollupConsumer

chainConfig *params.ChainConfig

Expand All @@ -44,7 +43,6 @@ func NewValidator(
consumer components.L1BlockProcessor,
batchExecutor components.BatchExecutor,
registry components.BatchRegistry,
rollupConsumer components.RollupConsumer,
chainConfig *params.ChainConfig,
storage storage.Storage,
sigValidator *components.SignatureValidator,
Expand All @@ -58,7 +56,6 @@ func NewValidator(
blockProcessor: consumer,
batchExecutor: batchExecutor,
batchRegistry: registry,
rollupConsumer: rollupConsumer,
chainConfig: chainConfig,
storage: storage,
sigValidator: sigValidator,
Expand Down
3 changes: 3 additions & 0 deletions go/enclave/storage/enclavedb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func WriteBatchHeader(ctx context.Context, dbtx *sql.Tx, batch *core.Batch, conv
}

func UpdateCanonicalBatch(ctx context.Context, dbtx *sql.Tx, isCanonical bool, blocks []common.L1BlockHash) error {
if len(blocks) == 0 {
return nil
}
args := make([]any, 0)
args = append(args, isCanonical)
for _, blockHash := range blocks {
Expand Down
3 changes: 3 additions & 0 deletions go/enclave/storage/enclavedb/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func WriteBlock(ctx context.Context, dbtx *sql.Tx, b *types.Header) error {
}

func UpdateCanonicalBlock(ctx context.Context, dbtx *sql.Tx, isCanonical bool, blocks []common.L1BlockHash) error {
if len(blocks) == 0 {
return nil
}
args := make([]any, 0)
args = append(args, isCanonical)
for _, blockHash := range blocks {
Expand Down
51 changes: 29 additions & 22 deletions go/enclave/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,31 +248,38 @@ func (s *storageImpl) StoreBlock(ctx context.Context, block *types.Header, chain
return err
}

var nonCanonical, canonical []common.L1BlockHash
if chainFork != nil && chainFork.IsFork() {
s.logger.Info(fmt.Sprintf("Update Fork. %s", chainFork))
err := enclavedb.UpdateCanonicalBlock(ctx, dbTx, false, chainFork.NonCanonicalPath)
if err != nil {
return err
}
err = enclavedb.UpdateCanonicalBlock(ctx, dbTx, true, chainFork.CanonicalPath)
if err != nil {
return err
}
err = enclavedb.UpdateCanonicalBatch(ctx, dbTx, false, chainFork.NonCanonicalPath)
if err != nil {
return err
}
err = enclavedb.UpdateCanonicalBatch(ctx, dbTx, true, chainFork.CanonicalPath)
if err != nil {
return err
}
nonCanonical = chainFork.NonCanonicalPath
canonical = chainFork.CanonicalPath
} else {
// handle the case when this block was canonical at some point, then reverted
canonical = []common.L1BlockHash{block.Hash()}
}

// sanity check that there is always a single canonical batch or block per layer
// called after forks, for the latest 50 blocks
err = enclavedb.CheckCanonicalValidity(ctx, dbTx, blockId-50)
if err != nil {
s.logger.Crit("Should not happen.", log.ErrKey, err)
}
err = enclavedb.UpdateCanonicalBlock(ctx, dbTx, false, nonCanonical)
if err != nil {
return err
}
err = enclavedb.UpdateCanonicalBlock(ctx, dbTx, true, canonical)
if err != nil {
return err
}
err = enclavedb.UpdateCanonicalBatch(ctx, dbTx, false, nonCanonical)
if err != nil {
return err
}
err = enclavedb.UpdateCanonicalBatch(ctx, dbTx, true, canonical)
if err != nil {
return err
}

// sanity check that there is always a single canonical batch or block per layer
// called after forks, for the latest 50 blocks
err = enclavedb.CheckCanonicalValidity(ctx, dbTx, blockId-50)
if err != nil {
s.logger.Crit("Should not happen.", log.ErrKey, err)
}

if err := dbTx.Commit(); err != nil {
Expand Down

0 comments on commit d83dd77

Please sign in to comment.