Skip to content

Commit

Permalink
move point cache to cachingDB and remove more witness stuff
Browse files Browse the repository at this point in the history
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
  • Loading branch information
gballet and rjl493456442 committed May 3, 2024
1 parent 3777222 commit 72efd71
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 32 deletions.
16 changes: 15 additions & 1 deletion core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const (

// Cache size granted for caching clean code.
codeCacheSize = 64 * 1024 * 1024

// Number of address->curve point associations to keep.
pointCacheSize = 4096
)

// Database wraps access to tries and contract code.
Expand All @@ -60,6 +63,9 @@ type Database interface {
// DiskDB returns the underlying key-value disk database.
DiskDB() ethdb.KeyValueStore

// PointCache returns the cache holding points used in verkle tree key computation
PointCache() *utils.PointCache

// TrieDB returns the underlying trie database for managing trie nodes.
TrieDB() *triedb.Database
}
Expand Down Expand Up @@ -153,6 +159,7 @@ func NewDatabaseWithConfig(db ethdb.Database, config *triedb.Config) Database {
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
triedb: triedb.NewDatabase(db, config),
pointCache: utils.NewPointCache(pointCacheSize),
}
}

Expand All @@ -163,6 +170,7 @@ func NewDatabaseWithNodeDB(db ethdb.Database, triedb *triedb.Database) Database
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
triedb: triedb,
pointCache: utils.NewPointCache(pointCacheSize),
}
}

Expand All @@ -171,12 +179,13 @@ type cachingDB struct {
codeSizeCache *lru.Cache[common.Hash, int]
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
triedb *triedb.Database
pointCache *utils.PointCache
}

// OpenTrie opens the main account trie at a specific root hash.
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
if db.triedb.IsVerkle() {
return trie.NewVerkleTrie(root, db.triedb, utils.NewPointCache(100))
return trie.NewVerkleTrie(root, db.triedb, db.pointCache)
}
tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb)
if err != nil {
Expand Down Expand Up @@ -262,3 +271,8 @@ func (db *cachingDB) DiskDB() ethdb.KeyValueStore {
func (db *cachingDB) TrieDB() *triedb.Database {
return db.triedb
}

// PointCache returns the cache of evaluated curve points.
func (db *cachingDB) PointCache() *utils.PointCache {
return db.pointCache
}
25 changes: 4 additions & 21 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ type StateDB struct {
// Transient storage
transientStorage transientStorage

// State access events, used for Verkle tries/EIP4762
accessEvents *AccessEvents

// Journal of state modifications. This is the backbone of
// Snapshot and RevertToSnapshot.
journal *journal
Expand Down Expand Up @@ -197,30 +194,12 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
transientStorage: newTransientStorage(),
hasher: crypto.NewKeccakState(),
}
if tr.IsVerkle() {
sdb.accessEvents = sdb.NewAccessEvents()
}
if sdb.snaps != nil {
sdb.snap = sdb.snaps.Snapshot(root)
}
return sdb, nil
}

func (s *StateDB) NewAccessEvents() *AccessEvents {
return NewAccessEvents(utils.NewPointCache(100))
}

func (s *StateDB) AccessEvents() *AccessEvents {
if s.accessEvents == nil {
s.accessEvents = s.NewAccessEvents()
}
return s.accessEvents
}

func (s *StateDB) SetAccessEvents(ae *AccessEvents) {
s.accessEvents = ae
}

// SetLogger sets the logger for account update hooks.
func (s *StateDB) SetLogger(l *tracing.Hooks) {
s.logger = l
Expand Down Expand Up @@ -1412,3 +1391,7 @@ func (s *StateDB) markUpdate(addr common.Address) {
s.mutations[addr].applied = false
s.mutations[addr].typ = update
}

func (s *StateDB) PointCache() *utils.PointCache {
return s.db.PointCache()
}
5 changes: 0 additions & 5 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPo
}
// Create a new context to be used in the EVM environment.
txContext := NewEVMTxContext(msg)
txContext.AccessEvents = statedb.NewAccessEvents()
evm.Reset(txContext, statedb)

// Apply the transaction to the current state (included in the env).
Expand Down Expand Up @@ -159,10 +158,6 @@ func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPo
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
}

if statedb.AccessEvents() != nil {
statedb.AccessEvents().Merge(txContext.AccessEvents)
}

// Set the receipt logs and create the bloom filter.
receipt.Logs = statedb.GetLogs(tx.Hash(), blockNumber.Uint64(), blockHash)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
Expand Down
7 changes: 2 additions & 5 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,15 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig
chainConfig: chainConfig,
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time),
}
if txCtx.AccessEvents == nil && chainConfig.IsPrague(blockCtx.BlockNumber, blockCtx.Time) {
evm.AccessEvents = evm.StateDB.(*state.StateDB).NewAccessEvents()
}
evm.interpreter = NewEVMInterpreter(evm)
return evm
}

// Reset resets the EVM with a new transaction context.Reset
// This is not threadsafe and should only be done very cautiously.
func (evm *EVM) Reset(txCtx TxContext, statedb StateDB) {
if txCtx.AccessEvents == nil && evm.chainRules.IsPrague {
txCtx.AccessEvents = evm.StateDB.(*state.StateDB).NewAccessEvents()
if evm.chainRules.IsEIP4762 {
txCtx.AccessEvents = state.NewAccessEvents(statedb.PointCache())
}
evm.TxContext = txCtx
evm.StateDB = statedb
Expand Down
5 changes: 5 additions & 0 deletions core/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie/utils"
"github.com/holiman/uint256"
)

Expand Down Expand Up @@ -75,6 +76,10 @@ type StateDB interface {
// AddSlotToAccessList adds the given (address,slot) to the access list. This operation is safe to perform
// even if the feature/fork is not active yet
AddSlotToAccessList(addr common.Address, slot common.Hash)

// PointCache returns the point cache used in computations
PointCache() *utils.PointCache

Prepare(rules params.Rules, sender, coinbase common.Address, dest *common.Address, precompiles []common.Address, txAccesses types.AccessList)

RevertToSnapshot(int)
Expand Down

0 comments on commit 72efd71

Please sign in to comment.