Skip to content

Commit

Permalink
pass bytes of predicate results to avoid parsing in params
Browse files Browse the repository at this point in the history
  • Loading branch information
darioush committed Oct 12, 2024
1 parent 9a158f0 commit aa77826
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 87 deletions.
11 changes: 2 additions & 9 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,21 +961,14 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc

vmctx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)

var predicateResults params.PredicateResults
predicateBytes, ok := predicate.GetPredicateResultBytes(vmctx.Header.Extra)
if ok {
predicateResults, err = predicate.ParseResults(predicateBytes)
if err != nil {
return nil, err
}
}
predicateBytes, _ := predicate.GetPredicateResultBytes(vmctx.Header.Extra)
// Apply the customization rules if required.
if config != nil {
originalTime := block.Time()
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.
blockContext := params.NewBlockContext(vmctx.BlockNumber, vmctx.Time, predicateResults)
blockContext := params.NewBlockContext(vmctx.BlockNumber, vmctx.Time, predicateBytes)
err = core.ApplyUpgrades(api.backend.ChainConfig(), &originalTime, blockContext, statedb)
if err != nil {
return nil, err
Expand Down
41 changes: 15 additions & 26 deletions params/hooks_libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import (
"golang.org/x/exp/maps"
)

var PredicateParser = func(extra []byte) (PredicateResults, error) {
return nil, nil
}

func (r RulesExtra) CanCreateContract(ac *libevm.AddressContext, gas uint64, state libevm.StateReader) (uint64, error) {
// IsProhibited
if ac.Self == constants.BlackholeAddr || modules.ReservedAddress(ac.Self) {
Expand Down Expand Up @@ -111,17 +107,17 @@ func makePrecompile(contract contract.StatefulPrecompiledContract) libevm.Precom
if err != nil {
panic(err) // Should never happen
}
predicateResults, err := PredicateParser(header.Extra)
if err != nil {
panic(err) // Should never happen, because predicates are parsed in NewEVMBlockContext.
var predicateResultsBytes []byte
if len(header.Extra) >= DynamicFeeExtraDataSize {
predicateResultsBytes = header.Extra[DynamicFeeExtraDataSize:]
}
accessableState := accessableState{
env: env,
chainConfig: GetRulesExtra(env.Rules()).chainConfig,
blockContext: &BlockContext{
number: env.BlockNumber(),
time: env.BlockTime(),
predicateResults: predicateResults,
number: env.BlockNumber(),
time: env.BlockTime(),
predicateResultsBytes: predicateResultsBytes,
},
}
return contract.Run(accessableState, env.Addresses().Caller, env.Addresses().Self, input, suppliedGas, env.ReadOnly())
Expand Down Expand Up @@ -227,21 +223,17 @@ func (a accessableState) NativeAssetCall(caller common.Address, input []byte, su
return ret, remainingGas, err
}

type PredicateResults interface {
GetPredicateResults(txHash common.Hash, address common.Address) []byte
}

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

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

Expand All @@ -253,9 +245,6 @@ func (b *BlockContext) Timestamp() uint64 {
return b.time
}

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

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

type Configurator interface {
Expand Down
36 changes: 30 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 aa77826

Please sign in to comment.