Skip to content

Commit

Permalink
Add precompile and update evm param
Browse files Browse the repository at this point in the history
  • Loading branch information
trinitys7 committed Nov 4, 2024
1 parent 6b89d71 commit ff11ac9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
42 changes: 41 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"io"
"io/fs"
"maps"
"net/http"
"os"
"path/filepath"

"github.com/ethereum/go-ethereum/common"
"github.com/realiotech/realio-network/app/ante"
"github.com/realiotech/realio-network/client/docs"
"github.com/realiotech/realio-network/crypto/ethsecp256k1"
Expand Down Expand Up @@ -40,7 +42,11 @@ import (
srvflags "github.com/evmos/os/server/flags"
ethermint "github.com/evmos/os/types"

"github.com/evmos/os/precompiles/bech32"
"github.com/evmos/os/precompiles/p256"
stakingprecompile "github.com/evmos/os/precompiles/staking"
"github.com/evmos/os/x/evm"
"github.com/evmos/os/x/evm/core/vm"
evmkeeper "github.com/evmos/os/x/evm/keeper"
evmtypes "github.com/evmos/os/x/evm/types"
"github.com/evmos/os/x/feemarket"
Expand Down Expand Up @@ -159,7 +165,8 @@ import (
)

const (
Name = "realio-network"
Name = "realio-network"
bech32PrecompileBaseGas = 6_000
)

var (
Expand Down Expand Up @@ -553,6 +560,9 @@ func New(
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack)
app.IBCKeeper.SetRouter(ibcRouter)

app.EvmKeeper.WithStaticPrecompiles(
RealioPrecompile(*app.StakingKeeper, app.AuthzKeeper),
)
/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down Expand Up @@ -1089,6 +1099,36 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
return paramsKeeper
}

func RealioPrecompile(
stakingKeeper stakingkeeper.Keeper,
authzKeeper authzkeeper.Keeper,
) map[common.Address]vm.PrecompiledContract {
// Clone the mapping from the latest EVM fork.
precompiles := maps.Clone(vm.PrecompiledContractsBerlin)

// 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, err := stakingprecompile.NewPrecompile(stakingKeeper, authzKeeper)
if err != nil {
panic(fmt.Errorf("failed to instantiate staking precompile: %w", err))
}

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

// Stateful precompiles
precompiles[stakingPrecompile.Address()] = stakingPrecompile

return precompiles
}

func RealioSigVerificationGasConsumer(
meter storetypes.GasMeter, sig signing.SignatureV2, params authtypes.Params,
) error {
Expand Down
8 changes: 8 additions & 0 deletions app/upgrades/v2/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ func CreateUpgradeHandler(
return nil, err
}

evmParams := evmtypes.DefaultParams()
evmParams.ActiveStaticPrecompiles = []string{
evmtypes.StakingPrecompileAddress,
}
err = evmKeeper.SetParams(sdkCtx, evmParams)
if err != nil {
return nil, err
}
return newVM, nil
}
}
Expand Down

0 comments on commit ff11ac9

Please sign in to comment.