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

Consolidate predicate prepare storage slots function #908

Merged
merged 2 commits into from
Sep 27, 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
13 changes: 3 additions & 10 deletions core/predicate_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,9 @@ func CheckPredicates(rules params.Rules, predicateContext *precompileconfig.Pred
if len(rules.Predicates) == 0 {
return predicateResults, nil
}
predicateArguments := make(map[common.Address][][]byte)
for _, accessTuple := range tx.AccessList() {
address := accessTuple.Address
_, ok := rules.Predicates[address]
if !ok {
continue
}

predicateArguments[address] = append(predicateArguments[address], predicateutils.HashSliceToBytes(accessTuple.StorageKeys))
}

// Prepare the predicate storage slots from the transaction's access list
predicateArguments := predicateutils.PreparePredicateStorageSlots(rules, tx.AccessList())

for address, predicates := range predicateArguments {
// Since [address] is only added to [predicateArguments] when there's a valid predicate in the ruleset
Expand Down
16 changes: 1 addition & 15 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,26 +1190,12 @@ func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, d
al.AddAddress(coinbase)
}

s.preparePredicateStorageSlots(rules, list)
s.predicateStorageSlots = predicateutils.PreparePredicateStorageSlots(rules, list)
}
// Reset transient storage at the beginning of transaction execution
s.transientStorage = newTransientStorage()
}

// preparePredicateStorageSlots populates the predicateStorageSlots field from the transaction's access list
// Note: if an address is specified multiple times in the access list, each storage slot for that address is
// appended to a slice of byte slices. Each byte slice represents a predicate, making it a slice of predicates
// for each access list address, and every predicate in the slice goes through verification.
func (s *StateDB) preparePredicateStorageSlots(rules params.Rules, list types.AccessList) {
s.predicateStorageSlots = make(map[common.Address][][]byte)
for _, el := range list {
if !rules.PredicateExists(el.Address) {
continue
}
s.predicateStorageSlots[el.Address] = append(s.predicateStorageSlots[el.Address], predicateutils.HashSliceToBytes(el.StorageKeys))
}
}

// AddAddressToAccessList adds the given address to the access list
func (s *StateDB) AddAddressToAccessList(addr common.Address) {
if s.accessList.AddAddress(addr) {
Expand Down
26 changes: 26 additions & 0 deletions utils/predicate/predicate_slots.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package predicate

import (
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/params"
"github.com/ethereum/go-ethereum/common"
)

// PreparePredicateStorageSlots populates the the predicate storage slots of a transaction's access list
// Note: if an address is specified multiple times in the access list, each storage slot for that address is
// appended to a slice of byte slices. Each byte slice represents a predicate, making it a slice of predicates
// for each access list address, and every predicate in the slice goes through verification.
func PreparePredicateStorageSlots(rules params.Rules, list types.AccessList) map[common.Address][][]byte {
predicateStorageSlots := make(map[common.Address][][]byte)
for _, el := range list {
if !rules.PredicateExists(el.Address) {
continue
}
predicateStorageSlots[el.Address] = append(predicateStorageSlots[el.Address], HashSliceToBytes(el.StorageKeys))
}

return predicateStorageSlots
}