Skip to content
Open
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
4 changes: 2 additions & 2 deletions core/predicate_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestCheckBlockPredicates(t *testing.T) {

rules := params.TestChainConfig.Rules(common.Big0, params.IsMergeTODO, 0)
predicater := precompileconfig.NewMockPredicater(gomock.NewController(t))
predicater.EXPECT().PredicateGas(gomock.Any()).Return(uint64(0), nil).AnyTimes()
predicater.EXPECT().PredicateGas(gomock.Any(), gomock.Any()).Return(uint64(0), nil).AnyTimes()
predicater.EXPECT().VerifyPredicate(gomock.Any(), gomock.Any()).DoAndReturn(verifyMockPredicate).AnyTimes()

rulesExtra := params.GetRulesExtra(rules)
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestCheckTxPredicates(t *testing.T) {

rules := params.TestChainConfig.Rules(common.Big0, params.IsMergeTODO, 0)
predicater := precompileconfig.NewMockPredicater(gomock.NewController(t))
predicater.EXPECT().PredicateGas(gomock.Any()).Return(test.predicateGas, nil).AnyTimes()
predicater.EXPECT().PredicateGas(gomock.Any(), gomock.Any()).Return(test.predicateGas, nil).AnyTimes()
predicater.EXPECT().VerifyPredicate(gomock.Any(), gomock.Any()).DoAndReturn(verifyMockPredicate).AnyTimes()

rulesExtra := params.GetRulesExtra(rules)
Expand Down
2 changes: 1 addition & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func accessListGas(rules params.Rules, accessList types.AccessList) (uint64, err
}
gas = totalGas
} else {
predicateGas, err := predicaterContract.PredicateGas(predicate.Predicate(accessTuple.StorageKeys))
predicateGas, err := predicaterContract.PredicateGas(predicate.Predicate(accessTuple.StorageKeys), rulesExtra)
if err != nil {
return 0, err
}
Expand Down
4 changes: 4 additions & 0 deletions params/extras/network_upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ type AvalancheRules struct {
IsGranite bool
}

func (a AvalancheRules) IsGraniteActivated() bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment why this is required (to make it clear this is called from precompile environment etc)

return a.IsGranite
}

func (n *NetworkUpgrades) GetAvalancheRules(timestamp uint64) AvalancheRules {
return AvalancheRules{
IsApricotPhase1: n.IsApricotPhase1(timestamp),
Expand Down
5 changes: 5 additions & 0 deletions params/hooks_libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ func (a accessibleState) GetChainConfig() precompileconfig.ChainConfig {
return GetExtra(a.env.ChainConfig())
}

func (a accessibleState) GetRules() precompileconfig.Rules {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for refactoring this.

extra := GetExtra(a.GetPrecompileEnv().ChainConfig())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we rename this to chainConfigExtra as there are actual extras package which can be confusing

return extra.GetAvalancheRules(a.GetBlockContext().Timestamp())
}

func (a accessibleState) GetSnowContext() *snow.Context {
return GetExtra(a.env.ChainConfig()).SnowCtx
}
Expand Down
1 change: 1 addition & 0 deletions precompile/contract/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type AccessibleState interface {
GetSnowContext() *snow.Context
GetChainConfig() precompileconfig.ChainConfig
GetPrecompileEnv() vm.PrecompileEnvironment
GetRules() precompileconfig.Rules
}

// ConfigurationBlockContext defines the interface required to configure a precompile.
Expand Down
14 changes: 14 additions & 0 deletions precompile/contract/mocks.go

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

10 changes: 6 additions & 4 deletions precompile/contracts/warp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ func (*Config) Accept(acceptCtx *precompileconfig.AcceptContext, blockHash commo
// 4. TODO: Lookup of the validator set
//
// If the payload of the warp message fails parsing, return a non-nil error invalidating the transaction.
func (*Config) PredicateGas(pred predicate.Predicate) (uint64, error) {
totalGas := GasCostPerSignatureVerification
bytesGasCost, overflow := math.SafeMul(GasCostPerWarpMessageChunk, uint64(len(pred)))
func (*Config) PredicateGas(pred predicate.Predicate, rules precompileconfig.Rules) (uint64, error) {
gasConfig := CurrentGasConfig(rules)

totalGas := gasConfig.PerSignatureVerification
bytesGasCost, overflow := math.SafeMul(gasConfig.PerWarpMessageChunk, uint64(len(pred)))
if overflow {
return 0, fmt.Errorf("overflow calculating gas cost for %d warp message chunks", len(pred))
}
Expand All @@ -172,7 +174,7 @@ func (*Config) PredicateGas(pred predicate.Predicate) (uint64, error) {
if err != nil {
return 0, fmt.Errorf("%w: %w", errCannotGetNumSigners, err)
}
signerGas, overflow := math.SafeMul(uint64(numSigners), GasCostPerWarpSigner)
signerGas, overflow := math.SafeMul(uint64(numSigners), gasConfig.PerWarpSigner)
if overflow {
return 0, errOverflowSignersGasCost
}
Expand Down
29 changes: 25 additions & 4 deletions precompile/contracts/warp/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/ava-labs/coreth/accounts/abi"
"github.com/ava-labs/coreth/precompile/contract"
"github.com/ava-labs/coreth/precompile/precompileconfig"
)

const (
Expand All @@ -31,12 +32,17 @@ const (
SendWarpMessageGasCost uint64 = contract.LogGas + 3*contract.LogTopicGas + AddWarpMessageGasCost + contract.WriteGasCostPerSlot
// SendWarpMessageGasCostPerByte cost accounts for producing a signed message of a given size
SendWarpMessageGasCostPerByte uint64 = contract.LogDataGas

GasCostPerWarpSigner uint64 = 500
GasCostPerWarpMessageChunk uint64 = 3_200
GasCostPerSignatureVerification uint64 = 200_000
)

type GasConfig struct {
// Gas cost per warp signer in the validator set
PerWarpSigner uint64
// Gas cost per chunk of the warp message (each chunk is 128 bytes)
PerWarpMessageChunk uint64
// Gas cost to verify a BLS signature
PerSignatureVerification uint64
}

var (
errInvalidSendInput = errors.New("invalid sendWarpMessage input")
errInvalidIndexInput = errors.New("invalid index to specify warp message")
Expand Down Expand Up @@ -343,3 +349,18 @@ func createWarpPrecompile() contract.StatefulPrecompiledContract {
}
return statefulContract
}

func CurrentGasConfig(rules precompileconfig.Rules) GasConfig {
if rules.IsGraniteActivated() {
return GasConfig{
PerWarpSigner: 250,
PerWarpMessageChunk: 3_200,
PerSignatureVerification: 100_000,
}
Comment on lines +355 to +359
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can just define them as constants just for a better visibility

}
return GasConfig{
PerWarpSigner: 500,
PerWarpMessageChunk: 3_200,
PerSignatureVerification: 200_000,
}
}
Loading
Loading