Skip to content
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
- [\#609](https://github.com/cosmos/evm/pull/609) Make `erc20Keeper` optional in the EVM keeper
- [\#624](https://github.com/cosmos/evm/pull/624) Cleanup unnecessary `fix-revert-gas-refund-height`.
- [\#635](https://github.com/cosmos/evm/pull/635) Move DefaultStaticPrecompiles to /evm and allow projects to set it by default alongside the keeper.

- [\#577](https://github.com/cosmos/evm/pull/577) Cleanup precompiles boilerplate code.

### FEATURES

Expand All @@ -48,6 +48,7 @@

- [\#477](https://github.com/cosmos/evm/pull/477) Refactor precompile constructors to accept keeper interfaces instead of concrete implementations, breaking the existing `NewPrecompile` function signatures.
- [\#594](https://github.com/cosmos/evm/pull/594) Remove all usage of x/params
- [\#577](https://github.com/cosmos/evm/pull/577) Changed the way to create a stateful precompile based on the cmn.Precompile, change `NewPrecompile` to not return error.

## v0.4.1

Expand Down
4 changes: 2 additions & 2 deletions evmd/tests/ibc/ics20_precompile_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func (suite *ICS20TransferTestSuite) SetupTest() {
suite.chainB = suite.coordinator.GetChain(evmibctesting.GetEvmChainID(2))

evmAppA := suite.chainA.App.(*evmd.EVMD)
suite.chainAPrecompile, _ = ics20.NewPrecompile(
suite.chainAPrecompile = ics20.NewPrecompile(
evmAppA.BankKeeper,
*evmAppA.StakingKeeper,
evmAppA.TransferKeeper,
evmAppA.IBCKeeper.ChannelKeeper,
)
evmAppB := suite.chainB.App.(*evmd.EVMD)
suite.chainBPrecompile, _ = ics20.NewPrecompile(
suite.chainBPrecompile = ics20.NewPrecompile(
evmAppB.BankKeeper,
*evmAppB.StakingKeeper,
evmAppB.TransferKeeper,
Expand Down
4 changes: 2 additions & 2 deletions evmd/tests/ibc/v2_ics20_precompile_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func (suite *ICS20TransferV2TestSuite) SetupTest() {
suite.chainB = suite.coordinator.GetChain(evmibctesting.GetEvmChainID(2))

evmAppA := suite.chainA.App.(*evmd.EVMD)
suite.chainAPrecompile, _ = ics20.NewPrecompile(
suite.chainAPrecompile = ics20.NewPrecompile(
evmAppA.BankKeeper,
*evmAppA.StakingKeeper,
evmAppA.TransferKeeper,
evmAppA.IBCKeeper.ChannelKeeper,
)
evmAppB := suite.chainB.App.(*evmd.EVMD)
suite.chainBPrecompile, _ = ics20.NewPrecompile(
suite.chainBPrecompile = ics20.NewPrecompile(
evmAppB.BankKeeper,
*evmAppB.StakingKeeper,
evmAppB.TransferKeeper,
Expand Down
76 changes: 37 additions & 39 deletions precompiles/bank/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/vm"

cmn "github.com/cosmos/evm/precompiles/common"
evmtypes "github.com/cosmos/evm/x/vm/types"

storetypes "cosmossdk.io/store/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
Expand All @@ -33,14 +34,27 @@ const (

var _ vm.PrecompiledContract = &Precompile{}

// Embed abi json file to the executable binary. Needed when importing as dependency.
//
//go:embed abi.json
var f embed.FS
var (
// Embed abi json file to the executable binary. Needed when importing as dependency.
//
//go:embed abi.json
f embed.FS
ABI abi.ABI
)

func init() {
var err error
ABI, err = cmn.LoadABI(f, "abi.json")
if err != nil {
panic(err)
}
}

// Precompile defines the bank precompile
type Precompile struct {
cmn.Precompile

abi.ABI
bankKeeper cmn.BankKeeper
erc20Keeper cmn.ERC20Keeper
}
Expand All @@ -50,28 +64,19 @@ type Precompile struct {
func NewPrecompile(
bankKeeper cmn.BankKeeper,
erc20Keeper cmn.ERC20Keeper,
) (*Precompile, error) {
newABI, err := cmn.LoadABI(f, "abi.json")
if err != nil {
return nil, err
}

) *Precompile {
// NOTE: we set an empty gas configuration to avoid extra gas costs
// during the run execution
p := &Precompile{
return &Precompile{
Precompile: cmn.Precompile{
ABI: newABI,
KvGasConfig: storetypes.GasConfig{},
TransientKVGasConfig: storetypes.GasConfig{},
ContractAddress: common.HexToAddress(evmtypes.BankPrecompileAddress),
},
ABI: ABI,
bankKeeper: bankKeeper,
erc20Keeper: erc20Keeper,
}

// SetAddress defines the address of the bank compile contract.
p.SetAddress(common.HexToAddress(evmtypes.BankPrecompileAddress))

return p, nil
}

// RequiredGas calculates the precompiled contract's base gas rate.
Expand Down Expand Up @@ -101,40 +106,33 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
return 0
}

// Run executes the precompiled contract bank query methods defined in the ABI.
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz []byte, err error) {
ctx, _, method, initialGas, args, err := p.RunSetup(evm, contract, readOnly, p.IsTransaction)
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
return p.RunNativeAction(evm, contract, func(ctx sdk.Context) ([]byte, error) {
return p.Execute(ctx, contract, readonly)
})
}

// Execute executes the precompiled contract bank query methods defined in the ABI.
func (p Precompile) Execute(ctx sdk.Context, contract *vm.Contract, readOnly bool) ([]byte, error) {
method, args, err := cmn.SetupABI(p.ABI, contract, readOnly, p.IsTransaction)
if err != nil {
return nil, err
}

// This handles any out of gas errors that may occur during the execution of a precompile query.
// It avoids panics and returns the out of gas error so the EVM can continue gracefully.
defer cmn.HandleGasError(ctx, contract, initialGas, &err)()

var bz []byte
switch method.Name {
// Bank queries
case BalancesMethod:
bz, err = p.Balances(ctx, contract, method, args)
bz, err = p.Balances(ctx, method, args)
case TotalSupplyMethod:
bz, err = p.TotalSupply(ctx, contract, method, args)
bz, err = p.TotalSupply(ctx, method, args)
case SupplyOfMethod:
bz, err = p.SupplyOf(ctx, contract, method, args)
bz, err = p.SupplyOf(ctx, method, args)
default:
return nil, fmt.Errorf(cmn.ErrUnknownMethod, method.Name)
}

if err != nil {
return nil, err
}

cost := ctx.GasMeter().GasConsumed() - initialGas

if !contract.UseGas(cost, nil, tracing.GasChangeCallPrecompiledContract) {
return nil, vm.ErrOutOfGas
}

return bz, nil
return bz, err
}

// IsTransaction checks if the given method name corresponds to a transaction or query.
Expand Down
4 changes: 0 additions & 4 deletions precompiles/bank/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/vm"

sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -29,7 +28,6 @@ const (
// balanceOf call for each token returned.
func (p Precompile) Balances(
ctx sdk.Context,
_ *vm.Contract,
method *abi.Method,
args []interface{},
) ([]byte, error) {
Expand Down Expand Up @@ -73,7 +71,6 @@ func (p Precompile) Balances(
// call for each token returned.
func (p Precompile) TotalSupply(
ctx sdk.Context,
_ *vm.Contract,
method *abi.Method,
_ []interface{},
) ([]byte, error) {
Expand Down Expand Up @@ -112,7 +109,6 @@ func (p Precompile) TotalSupply(
// stored in the x/bank.
func (p Precompile) SupplyOf(
ctx sdk.Context,
_ *vm.Contract,
method *abi.Method,
args []interface{},
) ([]byte, error) {
Expand Down
26 changes: 16 additions & 10 deletions precompiles/bech32/bech32.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ import (

var _ vm.PrecompiledContract = &Precompile{}

// Embed abi json file to the executable binary. Needed when importing as dependency.
//
//go:embed abi.json
var f embed.FS
var (
// Embed abi json file to the executable binary. Needed when importing as dependency.
//
//go:embed abi.json
f embed.FS
ABI abi.ABI
)

func init() {
var err error
ABI, err = cmn.LoadABI(f, "abi.json")
if err != nil {
panic(err)
}
}

// Precompile defines the precompiled contract for Bech32 encoding.
type Precompile struct {
Expand All @@ -28,17 +39,12 @@ type Precompile struct {
// NewPrecompile creates a new bech32 Precompile instance as a
// PrecompiledContract interface.
func NewPrecompile(baseGas uint64) (*Precompile, error) {
newABI, err := cmn.LoadABI(f, "abi.json")
if err != nil {
return nil, err
}

if baseGas == 0 {
return nil, fmt.Errorf("baseGas cannot be zero")
}

return &Precompile{
ABI: newABI,
ABI: ABI,
baseGas: baseGas,
}, nil
}
Expand Down
Loading
Loading