Skip to content

Commit

Permalink
predicate results: alternate approach (similar to master) (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
darioush authored Nov 6, 2024
1 parent e6ec110 commit f575788
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 96 deletions.
3 changes: 2 additions & 1 deletion core/extstate/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type StateDB struct {
}

func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) {
s.predicateStorageSlots = predicate.PreparePredicateStorageSlots(rules, list)
rulesExtra := params.GetRulesExtra(rules)
s.predicateStorageSlots = predicate.PreparePredicateStorageSlots(rulesExtra, list)
s.VmStateDB.Prepare(rules, sender, coinbase, dst, precompiles, list)
}

Expand Down
5 changes: 3 additions & 2 deletions core/predicate_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ func CheckPredicates(rules params.Rules, predicateContext *precompileconfig.Pred
return nil, fmt.Errorf("%w for predicate verification (%d) < intrinsic gas (%d)", ErrIntrinsicGas, tx.Gas(), intrinsicGas)
}

rulesExtra := params.GetRulesExtra(rules)
predicateResults := make(map[common.Address][]byte)
// Short circuit early if there are no precompile predicates to verify
if !params.GetRulesExtra(rules).PredicatersExist() {
if !rulesExtra.PredicatersExist() {
return predicateResults, nil
}

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

// If there are no predicates to verify, return early and skip requiring the proposervm block
// context to be populated.
Expand Down
5 changes: 1 addition & 4 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/internal/ethapi"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/predicate"
"github.com/ava-labs/coreth/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -967,9 +966,7 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
config.BlockOverrides.Apply(&vmctx)
// Apply all relevant upgrades from [originalTime] to the block time set in the override.
// Should be applied before the state overrides.
predicateBytes := predicate.GetPredicateResultBytes(vmctx.Header.Extra)
blockContext := params.NewBlockContext(vmctx.BlockNumber, vmctx.Time, predicateBytes)
err = core.ApplyUpgrades(api.backend.ChainConfig(), &originalTime, blockContext, statedb)
err = core.ApplyUpgrades(api.backend.ChainConfig(), &originalTime, block, statedb)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion params/avalanche_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"

"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/coreth/predicate"
)

// Minimum Gas Price
Expand All @@ -32,7 +33,9 @@ const (
ApricotPhase5BaseFeeChangeDenominator uint64 = 36
EtnaMinBaseFee int64 = GWei

DynamicFeeExtraDataSize = 80
// DynamicFeeExtraDataSize is defined in the predicate package to avoid a circular dependency.
// After Durango, the extra data past the dynamic fee rollup window represents predicate results.
DynamicFeeExtraDataSize = predicate.DynamicFeeExtraDataSize
RollupWindow uint64 = 10

// The base cost to charge per atomic transaction. Added in Apricot Phase 5.
Expand Down
51 changes: 25 additions & 26 deletions params/hooks_libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ava-labs/coreth/precompile/contract"
"github.com/ava-labs/coreth/precompile/modules"
"github.com/ava-labs/coreth/precompile/precompileconfig"
"github.com/ava-labs/coreth/predicate"
"github.com/ava-labs/coreth/vmerrs"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -106,16 +107,19 @@ func makePrecompile(contract contract.StatefulPrecompiledContract) libevm.Precom
if err != nil {
panic(err) // Should never happen
}
var predicateResultsBytes []byte
if len(header.Extra) >= DynamicFeeExtraDataSize {
predicateResultsBytes = header.Extra[DynamicFeeExtraDataSize:]
var predicateResults *predicate.Results
if predicateResultsBytes := predicate.GetPredicateResultBytes(header.Extra); len(predicateResultsBytes) > 0 {
predicateResults, err = predicate.ParseResults(predicateResultsBytes)
if err != nil {
panic(err) // Should never happen, as results are already validated in block validation
}
}
accessableState := accessableState{
env: env,
blockContext: &BlockContext{
number: env.BlockNumber(),
time: env.BlockTime(),
predicateResultsBytes: predicateResultsBytes,
blockContext: &precompileBlockContext{
number: env.BlockNumber(),
time: env.BlockTime(),
predicateResults: predicateResults,
},
}
return contract.Run(accessableState, env.Addresses().Caller, env.Addresses().Self, input, suppliedGas, env.ReadOnly())
Expand All @@ -140,7 +144,7 @@ func (r RulesExtra) PrecompileOverride(addr common.Address) (libevm.PrecompiledC

type accessableState struct {
env vm.PrecompileEnvironment
blockContext *BlockContext
blockContext *precompileBlockContext
}

func (a accessableState) GetStateDB() contract.StateDB {
Expand Down Expand Up @@ -170,28 +174,23 @@ func (a accessableState) Call(addr common.Address, input []byte, gas uint64, val
return a.env.Call(addr, input, gas, value)
}

type BlockContext struct {
number *big.Int
time uint64
predicateResultsBytes []byte
type precompileBlockContext struct {
number *big.Int
time uint64
predicateResults *predicate.Results
}

func NewBlockContext(number *big.Int, time uint64, predicateResultsBytes []byte) *BlockContext {
return &BlockContext{
number: number,
time: time,
predicateResultsBytes: predicateResultsBytes,
}
}

func (b *BlockContext) Number() *big.Int {
return b.number
func (p *precompileBlockContext) Number() *big.Int {
return p.number
}

func (b *BlockContext) Timestamp() uint64 {
return b.time
func (p *precompileBlockContext) Timestamp() uint64 {
return p.time
}

func (b *BlockContext) GetPredicateResultsBytes() []byte {
return b.predicateResultsBytes
func (p *precompileBlockContext) GetPredicateResults(txHash common.Hash, precompileAddress common.Address) []byte {
if p.predicateResults == nil {
return nil
}
return p.predicateResults.GetPredicateResults(txHash, precompileAddress)
}
6 changes: 3 additions & 3 deletions precompile/contract/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ type ConfigurationBlockContext interface {

type BlockContext interface {
ConfigurationBlockContext
// GetPredicateResults returns an byte array result of verifying the predicates
// of the given block.
GetPredicateResultsBytes() []byte
// GetPredicateResults returns a the result of verifying the predicates of the
// given transaction, precompile address pair as a byte array.
GetPredicateResults(txHash common.Hash, precompileAddress common.Address) []byte
}

type Configurator interface {
Expand Down
12 changes: 6 additions & 6 deletions precompile/contract/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f575788

Please sign in to comment.