Skip to content

Commit

Permalink
remove cache
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene committed Apr 30, 2024
1 parent c592eb1 commit 02ae2a7
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 43 deletions.
2 changes: 1 addition & 1 deletion go/enclave/components/batch_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (executor *batchExecutor) ComputeBatch(ctx context.Context, context *BatchE
// Create a new batch based on the fromBlock of inclusion of the previous, including all new transactions
batch := core.DeterministicEmptyBatch(parent.Header, block, context.AtTime, context.SequencerNo, context.BaseFee, context.Creator)

stateDB, err := executor.batchRegistry.GetBatchState(ctx, &batch.Header.ParentHash, false)
stateDB, err := executor.batchRegistry.GetBatchState(ctx, &batch.Header.ParentHash)
if err != nil {
return nil, fmt.Errorf("could not create stateDB. Cause: %w", err)
}
Expand Down
27 changes: 2 additions & 25 deletions go/enclave/components/batch_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type batchRegistry struct {
storage storage.Storage
logger gethlog.Logger
headBatchSeq *big.Int // keep track of the last executed batch to optimise db access
currentState *state.StateDB

batchesCallback func(*core.Batch, types.Receipts)
callbackMutex sync.RWMutex
Expand All @@ -38,7 +37,6 @@ type batchRegistry struct {

func NewBatchRegistry(storage storage.Storage, logger gethlog.Logger) BatchRegistry {
var headBatchSeq *big.Int
var currentState *state.StateDB
headBatch, err := storage.FetchHeadBatch(context.Background())
if err != nil {
if errors.Is(err, errutil.ErrNotFound) {
Expand All @@ -49,16 +47,11 @@ func NewBatchRegistry(storage storage.Storage, logger gethlog.Logger) BatchRegis
}
} else {
headBatchSeq = headBatch.SeqNo()
currentState, err = getBatchStateAtSeq(context.Background(), storage, headBatchSeq.Uint64())
if err != nil {
logger.Crit("Could not create batch registry", log.ErrKey, err)
}
}

return &batchRegistry{
storage: storage,
headBatchSeq: headBatchSeq,
currentState: currentState,
logger: logger,
healthTimeout: time.Minute,
lastExecutedBatch: async.NewAsyncTimestamp(time.Now().Add(-time.Minute)),
Expand Down Expand Up @@ -89,11 +82,6 @@ func (br *batchRegistry) OnBatchExecuted(batch *core.Batch, receipts types.Recei
defer core.LogMethodDuration(br.logger, measure.NewStopwatch(), "Sending batch and events", log.BatchHashKey, batch.Hash())

br.headBatchSeq = batch.SeqNo()
var err error
br.currentState, err = getBatchStateAtSeq(context.Background(), br.storage, br.headBatchSeq.Uint64())
if err != nil {
br.logger.Error("Could not create the state. This should not happen", log.ErrKey, err)
}
if br.batchesCallback != nil {
br.batchesCallback(batch, receipts)
}
Expand Down Expand Up @@ -169,32 +157,21 @@ func (br *batchRegistry) BatchesAfter(ctx context.Context, batchSeqNo uint64, up
return resultBatches, resultBlocks, nil
}

func (br *batchRegistry) GetBatchState(ctx context.Context, hash *common.L2BatchHash, cache bool) (*state.StateDB, error) {
func (br *batchRegistry) GetBatchState(ctx context.Context, hash *common.L2BatchHash) (*state.StateDB, error) {
batch, err := br.storage.FetchBatch(ctx, *hash)
if err != nil {
return nil, err
}
if cache && br.headBatchSeq == batch.SeqNo() {
return br.currentState, nil
}
return getBatchStateAtSeq(ctx, br.storage, batch.SeqNo().Uint64())
}

func (br *batchRegistry) GetBatchStateAtHeight(ctx context.Context, blockNumber *gethrpc.BlockNumber, cache bool) (*state.StateDB, error) {
if cache && blockNumber.Int64() <= 0 && br.currentState != nil {
return br.currentState, nil
}

func (br *batchRegistry) GetBatchStateAtHeight(ctx context.Context, blockNumber *gethrpc.BlockNumber) (*state.StateDB, error) {
// We retrieve the batch of interest.
batch, err := br.GetBatchAtHeight(ctx, *blockNumber)
if err != nil {
return nil, err
}

if cache && br.headBatchSeq == batch.SeqNo() {
return br.currentState, nil
}

return getBatchStateAtSeq(ctx, br.storage, batch.SeqNo().Uint64())
}

Expand Down
8 changes: 4 additions & 4 deletions go/enclave/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ type BatchRegistry interface {
// BatchesAfter - Given a hash, will return batches following it until the head batch and the l1 blocks referenced by those batches
BatchesAfter(ctx context.Context, batchSeqNo uint64, upToL1Height uint64, rollupLimiter limiters.RollupLimiter) ([]*core.Batch, []*types.Block, error)

// GetBatchStateAtHeight - creates a stateDB that represents the state committed when
// the batch with height matching the blockNumber was created and stored.
GetBatchStateAtHeight(ctx context.Context, blockNumber *gethrpc.BlockNumber, cache bool) (*state.StateDB, error)
// GetBatchStateAtHeight - creates a stateDB for the block number
GetBatchStateAtHeight(ctx context.Context, blockNumber *gethrpc.BlockNumber) (*state.StateDB, error)

GetBatchState(ctx context.Context, hash *common.L2BatchHash, cache bool) (*state.StateDB, error)
// GetBatchState - creates a stateDB for the block hash
GetBatchState(ctx context.Context, hash *common.L2BatchHash) (*state.StateDB, error)

// GetBatchAtHeight - same as `GetBatchStateAtHeight`, but instead returns the full batch
// rather than its stateDB only.
Expand Down
4 changes: 2 additions & 2 deletions go/enclave/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ func (e *enclaveImpl) GetCode(ctx context.Context, address gethcommon.Address, b
return nil, responses.ToInternalError(fmt.Errorf("requested GetCode with the enclave stopping"))
}

stateDB, err := e.registry.GetBatchState(ctx, batchHash, false)
stateDB, err := e.registry.GetBatchState(ctx, batchHash)
if err != nil {
return nil, responses.ToInternalError(fmt.Errorf("could not create stateDB. Cause: %w", err))
}
Expand Down Expand Up @@ -919,7 +919,7 @@ func restoreStateDBCache(ctx context.Context, storage storage.Storage, registry
//
// This method checks if the stateDB data is available for a given batch hash (so it can be restored if not)
func stateDBAvailableForBatch(ctx context.Context, registry components.BatchRegistry, hash common.L2BatchHash) bool {
_, err := registry.GetBatchState(ctx, &hash, true)
_, err := registry.GetBatchState(ctx, &hash)
return err == nil
}

Expand Down
4 changes: 2 additions & 2 deletions go/enclave/events/subscription_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *SubscriptionManager) RemoveSubscription(id gethrpc.ID) {
// FilterLogsForReceipt removes the logs that the sender of a transaction is not allowed to view
func FilterLogsForReceipt(ctx context.Context, receipt *types.Receipt, account *gethcommon.Address, registry components.BatchRegistry) ([]*types.Log, error) {
var filteredLogs []*types.Log
stateDB, err := registry.GetBatchState(ctx, &receipt.BlockHash, false)
stateDB, err := registry.GetBatchState(ctx, &receipt.BlockHash)
if err != nil {
return nil, fmt.Errorf("could not create state DB to filter logs. Cause: %w", err)
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func (s *SubscriptionManager) GetSubscribedLogsForBatch(ctx context.Context, bat

// the stateDb is needed to extract the user addresses from the topics
h := batch.Hash()
stateDB, err := s.registry.GetBatchState(ctx, &h, false)
stateDB, err := s.registry.GetBatchState(ctx, &h)
if err != nil {
return nil, fmt.Errorf("could not create state DB to filter logs. Cause: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions go/enclave/l2chain/l2_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (oc *obscuroChain) AccountOwner(ctx context.Context, address gethcommon.Add
}

func (oc *obscuroChain) GetBalanceAtBlock(ctx context.Context, accountAddr gethcommon.Address, blockNumber *gethrpc.BlockNumber) (*hexutil.Big, error) {
chainState, err := oc.Registry.GetBatchStateAtHeight(ctx, blockNumber, false)
chainState, err := oc.Registry.GetBatchStateAtHeight(ctx, blockNumber)
if err != nil {
return nil, fmt.Errorf("unable to get blockchain state - %w", err)
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func (oc *obscuroChain) ObsCall(ctx context.Context, apiArgs *gethapi.Transactio

func (oc *obscuroChain) ObsCallAtBlock(ctx context.Context, apiArgs *gethapi.TransactionArgs, blockNumber *gethrpc.BlockNumber) (*gethcore.ExecutionResult, error) {
// fetch the chain state at given batch
blockState, err := oc.Registry.GetBatchStateAtHeight(ctx, blockNumber, false)
blockState, err := oc.Registry.GetBatchStateAtHeight(ctx, blockNumber)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func (oc *obscuroChain) GetChainStateAtTransaction(ctx context.Context, batch *c

// Lookup the statedb of parent batch from the live database,
// otherwise regenerate it on the flight.
statedb, err := oc.Registry.GetBatchStateAtHeight(ctx, &parentBlockNumber, false)
statedb, err := oc.Registry.GetBatchStateAtHeight(ctx, &parentBlockNumber)
if err != nil {
return nil, vm.BlockContext{}, nil, err
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func (oc *obscuroChain) GetChainStateAtTransaction(ctx context.Context, batch *c

// Returns whether the account is a contract
func (oc *obscuroChain) isAccountContractAtBlock(ctx context.Context, accountAddr gethcommon.Address, blockNumber *gethrpc.BlockNumber) (bool, error) {
chainState, err := oc.Registry.GetBatchStateAtHeight(ctx, blockNumber, false)
chainState, err := oc.Registry.GetBatchStateAtHeight(ctx, blockNumber)
if err != nil {
return false, fmt.Errorf("unable to get blockchain state - %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/rpc/GetTransactionCount.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetTransactionCountExecute(builder *CallBuilder[uint64, string], rpc *Encry
// 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
h := l2Head.Hash()
s, err := rpc.registry.GetBatchState(builder.ctx, &h, false)
s, err := rpc.registry.GetBatchState(builder.ctx, &h)
if err != nil {
return err
}
Expand Down
6 changes: 2 additions & 4 deletions go/enclave/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ import (
"github.com/ethereum/go-ethereum/rlp"

gethcore "github.com/ethereum/go-ethereum/core"
gethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ten-protocol/go-ten/go/common/syserr"

"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
gethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/ten-protocol/go-ten/go/common"
"github.com/ten-protocol/go-ten/go/common/log"
Expand Down Expand Up @@ -337,7 +335,7 @@ func (s *storageImpl) CreateStateDB(ctx context.Context, batchHash common.L2Batc

statedb, err := state.New(batch.Header.Root, s.stateCache, nil)
if err != nil {
return nil, syserr.NewInternalError(fmt.Errorf("could not create state DB for %s. Cause: %w", batch.Header.Root, err))
return nil, fmt.Errorf("could not create state DB for %s. Cause: %w", batch.Header.Root, err)
}
return statedb, nil
}
Expand Down

0 comments on commit 02ae2a7

Please sign in to comment.