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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- [\#664](https://github.com/cosmos/evm/pull/664) Add EIP-7702 integration test
- [\#684](https://github.com/cosmos/evm/pull/684) Add unit test cases for EIP-7702
- [\#685](https://github.com/cosmos/evm/pull/685) Add EIP-7702 e2e test
- [\#680](https://github.com/cosmos/evm/pull/680) Introduce a `StaticPrecompiles` builder

### FEATURES

Expand Down
93 changes: 12 additions & 81 deletions precompiles/types/defaults.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
package types

import (
"fmt"
"maps"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"

evmaddress "github.com/cosmos/evm/encoding/address"
bankprecompile "github.com/cosmos/evm/precompiles/bank"
"github.com/cosmos/evm/precompiles/bech32"
cmn "github.com/cosmos/evm/precompiles/common"
distprecompile "github.com/cosmos/evm/precompiles/distribution"
govprecompile "github.com/cosmos/evm/precompiles/gov"
ics20precompile "github.com/cosmos/evm/precompiles/ics20"
"github.com/cosmos/evm/precompiles/p256"
slashingprecompile "github.com/cosmos/evm/precompiles/slashing"
stakingprecompile "github.com/cosmos/evm/precompiles/staking"
erc20Keeper "github.com/cosmos/evm/x/erc20/keeper"
transferkeeper "github.com/cosmos/evm/x/ibc/transfer/keeper"
channelkeeper "github.com/cosmos/ibc-go/v10/modules/core/04-channel/keeper"
Expand Down Expand Up @@ -85,74 +74,16 @@ func DefaultStaticPrecompiles(
codec codec.Codec,
opts ...Option,
) map[common.Address]vm.PrecompiledContract {
options := defaultOptionals()
for _, opt := range opts {
opt(&options)
}
// Clone the mapping from the latest EVM fork.
precompiles := maps.Clone(vm.PrecompiledContractsPrague)

// secp256r1 precompile as per EIP-7212
p256Precompile := &p256.Precompile{}

bech32Precompile, err := bech32.NewPrecompile(bech32PrecompileBaseGas)
if err != nil {
panic(fmt.Errorf("failed to instantiate bech32 precompile: %w", err))
}

stakingPrecompile := stakingprecompile.NewPrecompile(
stakingKeeper,
stakingkeeper.NewMsgServerImpl(&stakingKeeper),
stakingkeeper.NewQuerier(&stakingKeeper),
bankKeeper,
options.AddressCodec,
)

distributionPrecompile := distprecompile.NewPrecompile(
distributionKeeper,
distributionkeeper.NewMsgServerImpl(distributionKeeper),
distributionkeeper.NewQuerier(distributionKeeper),
stakingKeeper,
bankKeeper,
options.AddressCodec,
)

ibcTransferPrecompile := ics20precompile.NewPrecompile(
bankKeeper,
stakingKeeper,
transferKeeper,
channelKeeper,
)

bankPrecompile := bankprecompile.NewPrecompile(bankKeeper, erc20Keeper)

govPrecompile := govprecompile.NewPrecompile(
govkeeper.NewMsgServerImpl(&govKeeper),
govkeeper.NewQueryServer(&govKeeper),
bankKeeper,
codec,
options.AddressCodec,
)

slashingPrecompile := slashingprecompile.NewPrecompile(
slashingKeeper,
slashingkeeper.NewMsgServerImpl(slashingKeeper),
bankKeeper,
options.ValidatorAddrCodec,
options.ConsensusAddrCodec,
)

// Stateless precompiles
precompiles[bech32Precompile.Address()] = bech32Precompile
precompiles[p256Precompile.Address()] = p256Precompile

// Stateful precompiles
precompiles[stakingPrecompile.Address()] = stakingPrecompile
precompiles[distributionPrecompile.Address()] = distributionPrecompile
precompiles[ibcTransferPrecompile.Address()] = ibcTransferPrecompile
precompiles[bankPrecompile.Address()] = bankPrecompile
precompiles[govPrecompile.Address()] = govPrecompile
precompiles[slashingPrecompile.Address()] = slashingPrecompile

return precompiles
precompiles := NewStaticPrecompiles().
WithPraguePrecompiles().
WithP256Precompile().
WithBech32Precompile().
WithStakingPrecompile(stakingKeeper, bankKeeper, opts...).
WithDistributionPrecompile(distributionKeeper, stakingKeeper, bankKeeper, opts...).
WithICS20Precompile(bankKeeper, stakingKeeper, transferKeeper, channelKeeper).
WithBankPrecompile(bankKeeper, erc20Keeper).
WithGovPrecompile(govKeeper, bankKeeper, codec, opts...).
WithSlashingPrecompile(slashingKeeper, bankKeeper, opts...)

return map[common.Address]vm.PrecompiledContract(precompiles)
}
171 changes: 171 additions & 0 deletions precompiles/types/static_precompiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package types

import (
"fmt"
"maps"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"

bankprecompile "github.com/cosmos/evm/precompiles/bank"
"github.com/cosmos/evm/precompiles/bech32"
cmn "github.com/cosmos/evm/precompiles/common"
distprecompile "github.com/cosmos/evm/precompiles/distribution"
govprecompile "github.com/cosmos/evm/precompiles/gov"
ics20precompile "github.com/cosmos/evm/precompiles/ics20"
"github.com/cosmos/evm/precompiles/p256"
slashingprecompile "github.com/cosmos/evm/precompiles/slashing"
stakingprecompile "github.com/cosmos/evm/precompiles/staking"
erc20Keeper "github.com/cosmos/evm/x/erc20/keeper"
transferkeeper "github.com/cosmos/evm/x/ibc/transfer/keeper"
channelkeeper "github.com/cosmos/ibc-go/v10/modules/core/04-channel/keeper"

"github.com/cosmos/cosmos-sdk/codec"
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
)

type StaticPrecompiles map[common.Address]vm.PrecompiledContract

func NewStaticPrecompiles() StaticPrecompiles {
return make(StaticPrecompiles)
}

func (s StaticPrecompiles) WithPraguePrecompiles() StaticPrecompiles {
maps.Copy(s, vm.PrecompiledContractsPrague)
return s
}

func (s StaticPrecompiles) WithP256Precompile() StaticPrecompiles {
p256Precompile := &p256.Precompile{}
s[p256Precompile.Address()] = p256Precompile
return s
}

func (s StaticPrecompiles) WithBech32Precompile() StaticPrecompiles {
bech32Precompile, err := bech32.NewPrecompile(bech32PrecompileBaseGas)
if err != nil {
panic(fmt.Errorf("failed to instantiate bech32 precompile: %w", err))
}
s[bech32Precompile.Address()] = bech32Precompile
return s
}

func (s StaticPrecompiles) WithStakingPrecompile(
stakingKeeper stakingkeeper.Keeper,
bankKeeper cmn.BankKeeper,
opts ...Option,
) StaticPrecompiles {
options := defaultOptionals()
for _, opt := range opts {
opt(&options)
}

stakingPrecompile := stakingprecompile.NewPrecompile(
stakingKeeper,
stakingkeeper.NewMsgServerImpl(&stakingKeeper),
stakingkeeper.NewQuerier(&stakingKeeper),
bankKeeper,
options.AddressCodec,
)

s[stakingPrecompile.Address()] = stakingPrecompile
return s
}

func (s StaticPrecompiles) WithDistributionPrecompile(
distributionKeeper distributionkeeper.Keeper,
stakingKeeper stakingkeeper.Keeper,
bankKeeper cmn.BankKeeper,
opts ...Option,
) StaticPrecompiles {
options := defaultOptionals()
for _, opt := range opts {
opt(&options)
}

distributionPrecompile := distprecompile.NewPrecompile(
distributionKeeper,
distributionkeeper.NewMsgServerImpl(distributionKeeper),
distributionkeeper.NewQuerier(distributionKeeper),
stakingKeeper,
bankKeeper,
options.AddressCodec,
)

s[distributionPrecompile.Address()] = distributionPrecompile
return s
}

func (s StaticPrecompiles) WithICS20Precompile(
bankKeeper cmn.BankKeeper,
stakingKeeper stakingkeeper.Keeper,
transferKeeper *transferkeeper.Keeper,
channelKeeper *channelkeeper.Keeper,
) StaticPrecompiles {
ibcTransferPrecompile := ics20precompile.NewPrecompile(
bankKeeper,
stakingKeeper,
transferKeeper,
channelKeeper,
)

s[ibcTransferPrecompile.Address()] = ibcTransferPrecompile
return s
}

func (s StaticPrecompiles) WithBankPrecompile(
bankKeeper cmn.BankKeeper,
erc20Keeper *erc20Keeper.Keeper,
) StaticPrecompiles {
bankPrecompile := bankprecompile.NewPrecompile(bankKeeper, erc20Keeper)
s[bankPrecompile.Address()] = bankPrecompile
return s
}

func (s StaticPrecompiles) WithGovPrecompile(
govKeeper govkeeper.Keeper,
bankKeeper cmn.BankKeeper,
codec codec.Codec,
opts ...Option,
) StaticPrecompiles {
options := defaultOptionals()
for _, opt := range opts {
opt(&options)
}

govPrecompile := govprecompile.NewPrecompile(
govkeeper.NewMsgServerImpl(&govKeeper),
govkeeper.NewQueryServer(&govKeeper),
bankKeeper,
codec,
options.AddressCodec,
)

s[govPrecompile.Address()] = govPrecompile
return s
}

func (s StaticPrecompiles) WithSlashingPrecompile(
slashingKeeper slashingkeeper.Keeper,
bankKeeper cmn.BankKeeper,
opts ...Option,
) StaticPrecompiles {
options := defaultOptionals()
for _, opt := range opts {
opt(&options)
}

slashingPrecompile := slashingprecompile.NewPrecompile(
slashingKeeper,
slashingkeeper.NewMsgServerImpl(slashingKeeper),
bankKeeper,
options.ValidatorAddrCodec,
options.ConsensusAddrCodec,
)

s[slashingPrecompile.Address()] = slashingPrecompile
return s
}
Loading