Skip to content

Commit

Permalink
Cleanup EVM CLI (sei-protocol#1578)
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored Apr 22, 2024
1 parent 8977848 commit a5ee880
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 374 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ func New(

if enableCustomEVMPrecompiles {
if err := precompiles.InitializePrecompiles(
false,
&app.EvmKeeper,
app.BankKeeper,
wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper),
Expand Down
4 changes: 4 additions & 0 deletions precompiles/addr/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "addr"
}

func (p Precompile) Run(evm *vm.EVM, _ common.Address, _ common.Address, input []byte, value *big.Int, _ bool) (bz []byte, err error) {
ctx, method, args, err := p.Prepare(evm, input)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions precompiles/bank/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "bank"
}

func (p Precompile) Run(evm *vm.EVM, caller common.Address, callingContract common.Address, input []byte, value *big.Int, readOnly bool) (bz []byte, err error) {
ctx, method, args, err := p.Prepare(evm, input)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions precompiles/common/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (p Precompile) Prepare(evm *vm.EVM, input []byte) (sdk.Context, *abi.Method
return ctxer.Ctx(), method, args, nil
}

func (p Precompile) GetABI() abi.ABI {
return p.ABI
}

func ValidateArgsLength(args []interface{}, length int) error {
if len(args) != length {
return fmt.Errorf("expected %d arguments but got %d", length, len(args))
Expand Down
4 changes: 4 additions & 0 deletions precompiles/distribution/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "distribution"
}

func (p Precompile) Run(evm *vm.EVM, caller common.Address, callingContract common.Address, input []byte, value *big.Int, readOnly bool) (bz []byte, err error) {
if readOnly {
return nil, errors.New("cannot call distr precompile from staticcall")
Expand Down
4 changes: 4 additions & 0 deletions precompiles/gov/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "gov"
}

func (p Precompile) Run(evm *vm.EVM, caller common.Address, callingContract common.Address, input []byte, value *big.Int, readOnly bool) (bz []byte, err error) {
if readOnly {
return nil, errors.New("cannot call gov precompile from staticcall")
Expand Down
4 changes: 4 additions & 0 deletions precompiles/ibc/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "ibc"
}

func (p Precompile) accAddressFromArg(ctx sdk.Context, arg interface{}) (sdk.AccAddress, error) {
addr := arg.(common.Address)
if addr == (common.Address{}) {
Expand Down
4 changes: 4 additions & 0 deletions precompiles/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "json"
}

func (p Precompile) Run(evm *vm.EVM, _ common.Address, _ common.Address, input []byte, value *big.Int, _ bool) (bz []byte, err error) {
ctx, method, args, err := p.Prepare(evm, input)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions precompiles/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "oracle"
}

func (p Precompile) Run(evm *vm.EVM, _ common.Address, _ common.Address, input []byte, value *big.Int, _ bool) (bz []byte, err error) {
ctx, method, args, err := p.Prepare(evm, input)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions precompiles/pointer/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "pointer"
}

func (p Precompile) RunAndCalculateGas(evm *vm.EVM, caller common.Address, callingContract common.Address, input []byte, suppliedGas uint64, value *big.Int, _ *tracing.Hooks, readOnly bool) (ret []byte, remainingGas uint64, err error) {
if readOnly {
return nil, 0, errors.New("cannot call pointer precompile from staticcall")
Expand Down
4 changes: 4 additions & 0 deletions precompiles/pointerview/pointerview.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "pointerview"
}

func (p Precompile) Run(evm *vm.EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool) (ret []byte, err error) {
ctx, method, args, err := p.Prepare(evm, input)
if err != nil {
Expand Down
90 changes: 66 additions & 24 deletions precompiles/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package precompiles
import (
"sync"

"github.com/ethereum/go-ethereum/accounts/abi"
ecommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/sei-protocol/sei-chain/precompiles/addr"
Expand All @@ -22,7 +23,23 @@ import (
var SetupMtx = &sync.Mutex{}
var Initialized = false

type PrecompileInfo struct {
ABI abi.ABI
Address ecommon.Address
}

// PrecompileNamesToInfo is Populated by InitializePrecompiles
var PrecompileNamesToInfo = map[string]PrecompileInfo{}

type IPrecompile interface {
vm.PrecompiledContract
GetABI() abi.ABI
GetName() string
Address() ecommon.Address
}

func InitializePrecompiles(
dryRun bool,
evmKeeper common.EVMKeeper,
bankKeeper common.BankKeeper,
wasmdKeeper common.WasmdKeeper,
Expand All @@ -42,73 +59,98 @@ func InitializePrecompiles(
if err != nil {
return err
}
addPrecompileToVM(bankp, bankp.Address())
wasmdp, err := wasmd.NewPrecompile(evmKeeper, wasmdKeeper, wasmdViewKeeper, bankKeeper)
if err != nil {
return err
}
addPrecompileToVM(wasmdp, wasmdp.Address())
jsonp, err := json.NewPrecompile()
if err != nil {
return err
}
addPrecompileToVM(jsonp, jsonp.Address())
addrp, err := addr.NewPrecompile(evmKeeper)
if err != nil {
return err
}
addPrecompileToVM(addrp, addrp.Address())
stakingp, err := staking.NewPrecompile(stakingKeeper, evmKeeper, bankKeeper)
if err != nil {
return err
}
addPrecompileToVM(stakingp, stakingp.Address())
govp, err := gov.NewPrecompile(govKeeper, evmKeeper, bankKeeper)
if err != nil {
return err
}
addPrecompileToVM(govp, govp.Address())
distrp, err := distribution.NewPrecompile(distrKeeper, evmKeeper)
if err != nil {
return err
}
addPrecompileToVM(distrp, distrp.Address())
oraclep, err := oracle.NewPrecompile(oracleKeeper, evmKeeper)
if err != nil {
return err
}
addPrecompileToVM(oraclep, oraclep.Address())
ibcp, err := ibc.NewPrecompile(transferKeeper, evmKeeper)
if err != nil {
return err
}
addPrecompileToVM(ibcp, ibcp.Address())
pointerp, err := pointer.NewPrecompile(evmKeeper, bankKeeper, wasmdViewKeeper)
if err != nil {
return err
}
addPrecompileToVM(pointerp, pointerp.Address())
pointerviewp, err := pointerview.NewPrecompile(evmKeeper)
if err != nil {
return err
}
addPrecompileToVM(pointerviewp, pointerviewp.Address())
Initialized = true
PrecompileNamesToInfo[bankp.GetName()] = PrecompileInfo{ABI: bankp.GetABI(), Address: bankp.Address()}
PrecompileNamesToInfo[wasmdp.GetName()] = PrecompileInfo{ABI: wasmdp.GetABI(), Address: wasmdp.Address()}
PrecompileNamesToInfo[jsonp.GetName()] = PrecompileInfo{ABI: jsonp.GetABI(), Address: jsonp.Address()}
PrecompileNamesToInfo[addrp.GetName()] = PrecompileInfo{ABI: addrp.GetABI(), Address: addrp.Address()}
PrecompileNamesToInfo[stakingp.GetName()] = PrecompileInfo{ABI: stakingp.GetABI(), Address: stakingp.Address()}
PrecompileNamesToInfo[govp.GetName()] = PrecompileInfo{ABI: govp.GetABI(), Address: govp.Address()}
PrecompileNamesToInfo[distrp.GetName()] = PrecompileInfo{ABI: distrp.GetABI(), Address: distrp.Address()}
PrecompileNamesToInfo[oraclep.GetName()] = PrecompileInfo{ABI: oraclep.GetABI(), Address: oraclep.Address()}
PrecompileNamesToInfo[ibcp.GetName()] = PrecompileInfo{ABI: ibcp.GetABI(), Address: ibcp.Address()}
PrecompileNamesToInfo[pointerp.GetName()] = PrecompileInfo{ABI: pointerp.GetABI(), Address: pointerp.Address()}
PrecompileNamesToInfo[pointerviewp.GetName()] = PrecompileInfo{ABI: pointerviewp.GetABI(), Address: pointerviewp.Address()}
if !dryRun {
addPrecompileToVM(bankp)
addPrecompileToVM(wasmdp)
addPrecompileToVM(jsonp)
addPrecompileToVM(addrp)
addPrecompileToVM(stakingp)
addPrecompileToVM(govp)
addPrecompileToVM(distrp)
addPrecompileToVM(oraclep)
addPrecompileToVM(ibcp)
addPrecompileToVM(pointerp)
addPrecompileToVM(pointerviewp)
Initialized = true
}
return nil
}

func GetPrecompileInfo(name string) PrecompileInfo {
if !Initialized {
// Precompile Info does not require any keeper state
_ = InitializePrecompiles(true, nil, nil, nil, nil, nil, nil, nil, nil, nil)
}
i, ok := PrecompileNamesToInfo[name]
if !ok {
panic(name + "doesn't exist as a precompile")
}
return i
}

// This function modifies global variable in `vm` module. It should only be called once
// per precompile during initialization
func addPrecompileToVM(p vm.PrecompiledContract, addr ecommon.Address) {
vm.PrecompiledContractsHomestead[addr] = p
vm.PrecompiledContractsByzantium[addr] = p
vm.PrecompiledContractsIstanbul[addr] = p
vm.PrecompiledContractsBerlin[addr] = p
vm.PrecompiledContractsCancun[addr] = p
vm.PrecompiledContractsBLS[addr] = p
vm.PrecompiledAddressesHomestead = append(vm.PrecompiledAddressesHomestead, addr)
vm.PrecompiledAddressesByzantium = append(vm.PrecompiledAddressesByzantium, addr)
vm.PrecompiledAddressesIstanbul = append(vm.PrecompiledAddressesIstanbul, addr)
vm.PrecompiledAddressesBerlin = append(vm.PrecompiledAddressesBerlin, addr)
vm.PrecompiledAddressesCancun = append(vm.PrecompiledAddressesCancun, addr)
func addPrecompileToVM(p IPrecompile) {
vm.PrecompiledContractsHomestead[p.Address()] = p
vm.PrecompiledContractsByzantium[p.Address()] = p
vm.PrecompiledContractsIstanbul[p.Address()] = p
vm.PrecompiledContractsBerlin[p.Address()] = p
vm.PrecompiledContractsCancun[p.Address()] = p
vm.PrecompiledContractsBLS[p.Address()] = p
vm.PrecompiledAddressesHomestead = append(vm.PrecompiledAddressesHomestead, p.Address())
vm.PrecompiledAddressesByzantium = append(vm.PrecompiledAddressesByzantium, p.Address())
vm.PrecompiledAddressesIstanbul = append(vm.PrecompiledAddressesIstanbul, p.Address())
vm.PrecompiledAddressesBerlin = append(vm.PrecompiledAddressesBerlin, p.Address())
vm.PrecompiledAddressesCancun = append(vm.PrecompiledAddressesCancun, p.Address())
}
4 changes: 4 additions & 0 deletions precompiles/staking/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "staking"
}

func (p Precompile) Run(evm *vm.EVM, caller common.Address, callingContract common.Address, input []byte, value *big.Int, readOnly bool) (bz []byte, err error) {
if readOnly {
return nil, errors.New("cannot call staking precompile from staticcall")
Expand Down
4 changes: 4 additions & 0 deletions precompiles/wasmd/wasmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (p Precompile) Address() common.Address {
return p.address
}

func (p Precompile) GetName() string {
return "wasmd"
}

func (p Precompile) RunAndCalculateGas(evm *vm.EVM, caller common.Address, callingContract common.Address, input []byte, suppliedGas uint64, value *big.Int, _ *tracing.Hooks, readOnly bool) (ret []byte, remainingGas uint64, err error) {
ctx, method, args, err := p.Prepare(evm, input)
if err != nil {
Expand Down
Loading

0 comments on commit a5ee880

Please sign in to comment.