Skip to content

Commit

Permalink
host should return only confirmed batches (#2361)
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene authored Mar 3, 2025
1 parent 95d5ac4 commit f4c3e96
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions go/common/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Host interface {
// NewHeadsChan returns live batch headers
// Note - do not use directly. This is meant only for the NewHeadsManager, which multiplexes the headers
NewHeadsChan() chan *common.BatchHeader

// ConfirmedHeadBatch retrieves the most recent batch processed by the enclave
ConfirmedHeadBatch() (*common.BatchHeader, error)
}

type BatchMsg struct {
Expand Down
1 change: 1 addition & 0 deletions go/common/host/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type L2BatchRepository interface {
FetchBatchBySeqNo(background context.Context, seqNo *big.Int) (*common.ExtBatch, error)

FetchLatestBatchSeqNo() *big.Int
FetchLatestValidatedBatchSeqNo() *big.Int

// AddBatch is used to notify the repository of a new batch, e.g. from the enclave when seq produces one or a rollup is consumed
// Note: it is fine to add batches that the repo already has, it will just ignore them
Expand Down
12 changes: 12 additions & 0 deletions go/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,18 @@ func (h *host) NewHeadsChan() chan *common.BatchHeader {
return h.newHeads
}

func (h *host) ConfirmedHeadBatch() (*common.BatchHeader, error) {
seqNo := h.services.L2Repo().FetchLatestValidatedBatchSeqNo()
if seqNo == nil {
return nil, responses.ToInternalError(fmt.Errorf("no confirmed head batch found"))
}
b, err := h.storage.FetchBatchBySeqNo(seqNo.Uint64())
if err != nil {
return nil, err
}
return b.Header, nil
}

// Checks the host config is valid.
func (h *host) validateConfig() {
if h.config.IsGenesis && h.config.NodeType != common.Sequencer {
Expand Down
4 changes: 4 additions & 0 deletions go/host/l2/batchrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ func (r *Repository) FetchLatestBatchSeqNo() *big.Int {
return r.latestBatchSeqNo
}

func (r *Repository) FetchLatestValidatedBatchSeqNo() *big.Int {
return r.latestValidatedSeqNo
}

// AddBatch allows the host to add a batch to the repository, this is used:
// - when the node is a sequencer to store newly produced batches (the only way the sequencer host receives batches)
// - when the node is a validator to store batches read from roll-ups
Expand Down
5 changes: 2 additions & 3 deletions go/host/rpc/clientapi/client_api_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ func (api *ChainAPI) ChainId() (*hexutil.Big, error) { //nolint:stylecheck,reviv

// BatchNumber returns the height of the current head batch.
func (api *ChainAPI) BatchNumber() hexutil.Uint64 {
header, err := api.host.Storage().FetchHeadBatchHeader()
enclaveHeadBatch, err := api.host.ConfirmedHeadBatch()
if err != nil {
// This error may be nefarious, but unfortunately the Eth API doesn't allow us to return an error.
api.logger.Error("could not retrieve head batch header", log.ErrKey, err)
return 0
}
return hexutil.Uint64(header.Number.Uint64())
return hexutil.Uint64(enclaveHeadBatch.Number.Uint64())
}

// GetBatchByNumber returns the header of the batch with the given height.
Expand Down

0 comments on commit f4c3e96

Please sign in to comment.