Skip to content

Commit

Permalink
Replace old packers (ava-labs#857)
Browse files Browse the repository at this point in the history
* add equality tests

* regenerate abis

* add extra padding tests

* override network upgrades (ava-labs#839)

* override network upgrades

* genesis network upgrades defaults

* fix conflicts

* Update plugin/evm/vm.go

* update native minter

* nativeminter nit test changes

* replace fee manager packers

* remove generated abis

* Update precompile/contracts/feemanager/contract.go

Co-authored-by: Darioush Jalali <darioush.jalali@avalabs.org>
Signed-off-by: Ceyhun Onur <ceyhunonur54@gmail.com>

* fix comments

* readd pack/unpack tests

* add signature tests

* remove skip len check vars

* add native minter equality tests

* add get fee config output fuzz tests

* add fee manager fuzz tests

* move tests to another test file

* move signatures to unpack test

* nits

---------

Signed-off-by: Ceyhun Onur <ceyhunonur54@gmail.com>
Co-authored-by: Darioush Jalali <darioush.jalali@avalabs.org>
  • Loading branch information
ceyonur and darioush authored Dec 15, 2023
1 parent ba46d7b commit ccda76d
Show file tree
Hide file tree
Showing 23 changed files with 1,610 additions and 324 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
var _ contract.Configurator = &configurator{}
// ConfigKey is the key used in json config files to specify this precompile precompileconfig.
// ConfigKey is the key used in json config files to specify this precompile config.
// must be unique across all precompiles.
const ConfigKey = "{{decapitalise .Contract.Type}}Config"
Expand Down Expand Up @@ -50,7 +50,7 @@ func init() {
}
// MakeConfig returns a new precompile config instance.
// This is required for Marshal/Unmarshal the precompile config.
// This is required to Marshal/Unmarshal the precompile config.
func (*configurator) MakeConfig() precompileconfig.Config {
return new(Config)
}
Expand Down
6 changes: 1 addition & 5 deletions precompile/allowlist/allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,10 @@ func CreateAllowListPrecompile(precompileAddr common.Address) contract.StatefulP

func CreateAllowListFunctions(precompileAddr common.Address) []*contract.StatefulPrecompileFunction {
setAdmin := contract.NewStatefulPrecompileFunction(setAdminSignature, createAllowListRoleSetter(precompileAddr, AdminRole))
setManager := contract.NewStatefulPrecompileFunctionWithActivator(setManagerSignature, createAllowListRoleSetter(precompileAddr, ManagerRole), isManagerRoleActivated)
setManager := contract.NewStatefulPrecompileFunctionWithActivator(setManagerSignature, createAllowListRoleSetter(precompileAddr, ManagerRole), contract.IsDUpgradeActivated)
setEnabled := contract.NewStatefulPrecompileFunction(setEnabledSignature, createAllowListRoleSetter(precompileAddr, EnabledRole))
setNone := contract.NewStatefulPrecompileFunction(setNoneSignature, createAllowListRoleSetter(precompileAddr, NoRole))
read := contract.NewStatefulPrecompileFunction(readAllowListSignature, createReadAllowList(precompileAddr))

return []*contract.StatefulPrecompileFunction{setAdmin, setManager, setEnabled, setNone, read}
}

func isManagerRoleActivated(evm contract.AccessibleState) bool {
return evm.GetChainConfig().IsDUpgrade(evm.GetBlockContext().Timestamp())
}
1 change: 0 additions & 1 deletion precompile/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type ActivationFunc func(AccessibleState) bool
// StatefulPrecompileFunction defines a function implemented by a stateful precompile
type StatefulPrecompileFunction struct {
// selector is the 4 byte function selector for this function
// This should be calculated from the function signature using CalculateFunctionSelector
selector []byte
// execute is performed when this function is selected
execute RunStatefulPrecompileFunc
Expand Down
49 changes: 49 additions & 0 deletions precompile/contract/test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package contract

import (
"fmt"

"github.com/ethereum/go-ethereum/common"
)

// PackOrderedHashesWithSelector packs the function selector and ordered list of hashes into [dst]
// byte slice.
// assumes that [dst] has sufficient room for [functionSelector] and [hashes].
// Kept for testing backwards compatibility.
func PackOrderedHashesWithSelector(dst []byte, functionSelector []byte, hashes []common.Hash) error {
copy(dst[:len(functionSelector)], functionSelector)
return PackOrderedHashes(dst[len(functionSelector):], hashes)
}

// PackOrderedHashes packs the ordered list of [hashes] into the [dst] byte buffer.
// assumes that [dst] has sufficient space to pack [hashes] or else this function will panic.
// Kept for testing backwards compatibility.
func PackOrderedHashes(dst []byte, hashes []common.Hash) error {
if len(dst) != len(hashes)*common.HashLength {
return fmt.Errorf("destination byte buffer has insufficient length (%d) for %d hashes", len(dst), len(hashes))
}

var (
start = 0
end = common.HashLength
)
for _, hash := range hashes {
copy(dst[start:end], hash.Bytes())
start += common.HashLength
end += common.HashLength
}
return nil
}

// PackedHash returns packed the byte slice with common.HashLength from [packed]
// at the given [index].
// Assumes that [packed] is composed entirely of packed 32 byte segments.
// Kept for testing backwards compatibility.
func PackedHash(packed []byte, index int) []byte {
start := common.HashLength * index
end := start + common.HashLength
return packed[start:end]
}
41 changes: 4 additions & 37 deletions precompile/contract/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/ava-labs/subnet-evm/accounts/abi"
"github.com/ava-labs/subnet-evm/vmerrs"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

Expand Down Expand Up @@ -42,42 +41,6 @@ func DeductGas(suppliedGas uint64, requiredGas uint64) (uint64, error) {
return suppliedGas - requiredGas, nil
}

// PackOrderedHashesWithSelector packs the function selector and ordered list of hashes into [dst]
// byte slice.
// assumes that [dst] has sufficient room for [functionSelector] and [hashes].
func PackOrderedHashesWithSelector(dst []byte, functionSelector []byte, hashes []common.Hash) error {
copy(dst[:len(functionSelector)], functionSelector)
return PackOrderedHashes(dst[len(functionSelector):], hashes)
}

// PackOrderedHashes packs the ordered list of [hashes] into the [dst] byte buffer.
// assumes that [dst] has sufficient space to pack [hashes] or else this function will panic.
func PackOrderedHashes(dst []byte, hashes []common.Hash) error {
if len(dst) != len(hashes)*common.HashLength {
return fmt.Errorf("destination byte buffer has insufficient length (%d) for %d hashes", len(dst), len(hashes))
}

var (
start = 0
end = common.HashLength
)
for _, hash := range hashes {
copy(dst[start:end], hash.Bytes())
start += common.HashLength
end += common.HashLength
}
return nil
}

// PackedHash returns packed the byte slice with common.HashLength from [packed]
// at the given [index].
// Assumes that [packed] is composed entirely of packed 32 byte segments.
func PackedHash(packed []byte, index int) []byte {
start := common.HashLength * index
end := start + common.HashLength
return packed[start:end]
}

// ParseABI parses the given ABI string and returns the parsed ABI.
// If the ABI is invalid, it panics.
func ParseABI(rawABI string) abi.ABI {
Expand All @@ -88,3 +51,7 @@ func ParseABI(rawABI string) abi.ABI {

return parsed
}

func IsDUpgradeActivated(evm AccessibleState) bool {
return evm.GetChainConfig().IsDUpgrade(evm.GetBlockContext().Timestamp())
}
5 changes: 4 additions & 1 deletion precompile/contracts/deployerallowlist/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ func init() {
}
}

// MakeConfig returns a new precompile config instance.
// This is required to Marshal/Unmarshal the precompile config.
func (*configurator) MakeConfig() precompileconfig.Config {
return new(Config)
}

// Configure configures [state] with the given [cfg] config.
// Configure configures [state] with the given [cfg] precompileconfig.
// This function is called by the EVM once per precompile contract activation.
func (c *configurator) Configure(chainConfig precompileconfig.ChainConfig, cfg precompileconfig.Config, state contract.StateDB, blockContext contract.ConfigurationBlockContext) error {
config, ok := cfg.(*Config)
if !ok {
Expand Down
3 changes: 3 additions & 0 deletions precompile/contracts/feemanager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func NewDisableConfig(blockTimestamp *uint64) *Config {
}
}

// Key returns the key for the FeeManager precompileconfig.
// This should be the same key as used in the precompile module.
func (*Config) Key() string { return ConfigKey }

// Equal returns true if [cfg] is a [*FeeManagerConfig] and it has been configured identical to [c].
Expand All @@ -67,6 +69,7 @@ func (c *Config) Equal(cfg precompileconfig.Config) bool {
return c.InitialFeeConfig.Equal(other.InitialFeeConfig)
}

// Verify tries to verify Config and returns an error accordingly.
func (c *Config) Verify(chainConfig precompileconfig.ChainConfig) error {
if err := c.AllowListConfig.Verify(chainConfig, c.Upgrade); err != nil {
return err
Expand Down
169 changes: 169 additions & 0 deletions precompile/contracts/feemanager/contract.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
[
{
"inputs": [],
"name": "getFeeConfig",
"outputs": [
{
"internalType": "uint256",
"name": "gasLimit",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "targetBlockRate",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "minBaseFee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "targetGas",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "baseFeeChangeDenominator",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "minBlockGasCost",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "maxBlockGasCost",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "blockGasCostStep",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getFeeConfigLastChangedAt",
"outputs": [
{
"internalType": "uint256",
"name": "blockNumber",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
}
],
"name": "readAllowList",
"outputs": [
{
"internalType": "uint256",
"name": "role",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
}
],
"name": "setAdmin",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
}
],
"name": "setEnabled",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "gasLimit",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "targetBlockRate",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "minBaseFee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "targetGas",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "baseFeeChangeDenominator",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "minBlockGasCost",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "maxBlockGasCost",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "blockGasCostStep",
"type": "uint256"
}
],
"name": "setFeeConfig",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
}
],
"name": "setNone",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
Loading

0 comments on commit ccda76d

Please sign in to comment.