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

Add block head cache #1589

Merged
merged 1 commit into from
Oct 9, 2023
Merged
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
25 changes: 23 additions & 2 deletions go/enclave/components/block_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,30 @@ type l1BlockProcessor struct {
gasOracle gas.Oracle
logger gethlog.Logger
crossChainProcessors *crosschain.Processors

// we store the l1 head to avoid expensive db access
// the host is responsible to always submitting the head l1 block
currentL1Head *common.L1BlockHash
}

func NewBlockProcessor(storage storage.Storage, cc *crosschain.Processors, gasOracle gas.Oracle, logger gethlog.Logger) L1BlockProcessor {
var l1BlockHash *common.L1BlockHash
head, err := storage.FetchHeadBlock()
if err != nil {
if !errors.Is(err, errutil.ErrNotFound) {
logger.Crit("Cannot fetch head block", log.ErrKey, err)
}
} else {
h := head.Hash()
l1BlockHash = &h
}

return &l1BlockProcessor{
storage: storage,
logger: logger,
gasOracle: gasOracle,
crossChainProcessors: cc,
currentL1Head: l1BlockHash,
}
}

Expand Down Expand Up @@ -59,6 +75,8 @@ func (bp *l1BlockProcessor) Process(br *common.BlockAndReceipts) (*BlockIngestio
// todo @siliev - not sure if this is the best way to update the price, will pick up random stale blocks from forks?
bp.gasOracle.ProcessL1Block(br.Block)

h := br.Block.Hash()
bp.currentL1Head = &h
return ingestion, nil
}

Expand Down Expand Up @@ -93,7 +111,7 @@ func (bp *l1BlockProcessor) tryAndInsertBlock(br *common.BlockAndReceipts) (*Blo

func (bp *l1BlockProcessor) ingestBlock(block *common.L1Block) (*BlockIngestionType, error) {
// todo (#1056) - this is minimal L1 tracking/validation, and should be removed when we are using geth's blockchain or lightchain structures for validation
prevL1Head, err := bp.storage.FetchHeadBlock()
prevL1Head, err := bp.GetHead()
if err != nil {
if errors.Is(err, errutil.ErrNotFound) {
// todo (@matt) - we should enforce that this block is a configured hash (e.g. the L1 management contract deployment block)
Expand Down Expand Up @@ -123,7 +141,10 @@ func (bp *l1BlockProcessor) ingestBlock(block *common.L1Block) (*BlockIngestionT
}

func (bp *l1BlockProcessor) GetHead() (*common.L1Block, error) {
return bp.storage.FetchHeadBlock()
if bp.currentL1Head == nil {
return nil, errutil.ErrNotFound
}
return bp.storage.FetchBlock(*bp.currentL1Head)
}

func (bp *l1BlockProcessor) GetCrossChainContractAddress() *gethcommon.Address {
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/nodetype/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (s *sequencer) StoreExecutedBatch(batch *core.Batch, receipts types.Receipt
func (s *sequencer) CreateRollup(lastBatchNo uint64) (*common.ExtRollup, error) {
rollupLimiter := limiters.NewRollupLimiter(s.settings.MaxRollupSize)

currentL1Head, err := s.storage.FetchHeadBlock()
currentL1Head, err := s.blockProcessor.GetHead()
if err != nil {
return nil, err
}
Expand Down
Loading