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

remove slow query #1586

Merged
merged 2 commits into from
Oct 6, 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
7 changes: 5 additions & 2 deletions go/enclave/components/batch_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (br *batchRegistry) HasGenesisBatch() (bool, error) {

func (br *batchRegistry) BatchesAfter(batchSeqNo uint64, upToL1Height uint64, rollupLimiter limiters.RollupLimiter) ([]*core.Batch, []*types.Block, error) {
// sanity check
headBatch, err := br.storage.FetchHeadBatch()
headBatch, err := br.storage.FetchBatchBySeqNo(br.headBatchSeq.Uint64())
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -166,6 +166,9 @@ func (br *batchRegistry) GetBatchStateAtHeight(blockNumber *gethrpc.BlockNumber)
}

func (br *batchRegistry) GetBatchAtHeight(height gethrpc.BlockNumber) (*core.Batch, error) {
if br.headBatchSeq == nil {
return nil, fmt.Errorf("chain not initialised")
}
var batch *core.Batch
switch height {
case gethrpc.EarliestBlockNumber:
Expand All @@ -178,7 +181,7 @@ func (br *batchRegistry) GetBatchAtHeight(height gethrpc.BlockNumber) (*core.Bat
// todo - depends on the current pending rollup; leaving it for a different iteration as it will need more thought
return nil, fmt.Errorf("requested balance for pending block. This is not handled currently")
case gethrpc.SafeBlockNumber, gethrpc.FinalizedBlockNumber, gethrpc.LatestBlockNumber:
headBatch, err := br.storage.FetchHeadBatch()
headBatch, err := br.storage.FetchBatchBySeqNo(br.headBatchSeq.Uint64())
if err != nil {
return nil, fmt.Errorf("batch with requested height %d was not found. Cause: %w", height, err)
}
Expand Down
20 changes: 12 additions & 8 deletions go/enclave/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func NewEnclave(
)

// ensure cached chain state data is up-to-date using the persisted batch data
err = restoreStateDBCache(storage, batchExecutor, genesis, logger)
err = restoreStateDBCache(storage, registry, batchExecutor, genesis, logger)
if err != nil {
logger.Crit("failed to resync L2 chain state DB after restart", log.ErrKey, err)
}
Expand Down Expand Up @@ -712,7 +712,7 @@ func (e *enclaveImpl) GetTransactionCount(encryptedParams common.EncryptedParams
}

var nonce uint64
l2Head, err := e.storage.FetchHeadBatch()
l2Head, err := e.storage.FetchBatchBySeqNo(e.registry.HeadBatchSeq().Uint64())
if err == nil {
// todo - we should return an error when head state is not available, but for current test situations with race
// conditions we allow it to return zero while head state is uninitialized
Expand Down Expand Up @@ -1101,7 +1101,7 @@ func (e *enclaveImpl) GetLogs(encryptedParams common.EncryptedParamsGetLogs) (*r

from := filter.FromBlock
if from != nil && from.Int64() < 0 {
batch, err := e.storage.FetchHeadBatch()
batch, err := e.storage.FetchBatchBySeqNo(e.registry.HeadBatchSeq().Uint64())
if err != nil {
return responses.AsPlaintextError(fmt.Errorf("could not retrieve head batch. Cause: %w", err)), nil
}
Expand Down Expand Up @@ -1526,8 +1526,12 @@ func serializeEVMError(err error) ([]byte, error) {

// this function looks at the batch chain and makes sure the resulting stateDB snapshots are available, replaying them if needed
// (if there had been a clean shutdown and all stateDB data was persisted this should do nothing)
func restoreStateDBCache(storage storage.Storage, producer components.BatchExecutor, gen *genesis.Genesis, logger gethlog.Logger) error {
batch, err := storage.FetchHeadBatch()
func restoreStateDBCache(storage storage.Storage, registry components.BatchRegistry, producer components.BatchExecutor, gen *genesis.Genesis, logger gethlog.Logger) error {
if registry.HeadBatchSeq() == nil {
// not initialised yet
return nil
}
batch, err := storage.FetchBatchBySeqNo(registry.HeadBatchSeq().Uint64())
if err != nil {
if errors.Is(err, errutil.ErrNotFound) {
// there is no head batch, this is probably a new node - there is no state to rebuild
Expand All @@ -1538,7 +1542,7 @@ func restoreStateDBCache(storage storage.Storage, producer components.BatchExecu
}
if !stateDBAvailableForBatch(storage, batch.Hash()) {
logger.Info("state not available for latest batch after restart - rebuilding stateDB cache from batches")
err = replayBatchesToValidState(storage, producer, gen, logger)
err = replayBatchesToValidState(storage, registry, producer, gen, logger)
if err != nil {
return fmt.Errorf("unable to replay batches to restore valid state - %w", err)
}
Expand All @@ -1559,13 +1563,13 @@ func stateDBAvailableForBatch(storage storage.Storage, hash common.L2BatchHash)
// 1. step backwards from head batch until we find a batch that is already in stateDB cache, builds list of batches to replay
// 2. iterate that list of batches from the earliest, process the transactions to calculate and cache the stateDB
// todo (#1416) - get unit test coverage around this (and L2 Chain code more widely, see ticket #1416 )
func replayBatchesToValidState(storage storage.Storage, batchExecutor components.BatchExecutor, gen *genesis.Genesis, logger gethlog.Logger) error {
func replayBatchesToValidState(storage storage.Storage, registry components.BatchRegistry, batchExecutor components.BatchExecutor, gen *genesis.Genesis, logger gethlog.Logger) error {
// this slice will be a stack of batches to replay as we walk backwards in search of latest valid state
// todo - consider capping the size of this batch list using FIFO to avoid memory issues, and then repeating as necessary
var batchesToReplay []*core.Batch
// `batchToReplayFrom` variable will eventually be the latest batch for which we are able to produce a StateDB
// - we will then set that as the head of the L2 so that this node can rebuild its missing state
batchToReplayFrom, err := storage.FetchHeadBatch()
batchToReplayFrom, err := storage.FetchBatchBySeqNo(registry.HeadBatchSeq().Uint64())
if err != nil {
return fmt.Errorf("no head batch found in DB but expected to replay batches - %w", err)
}
Expand Down
File renamed without changes.
Loading