Skip to content

Commit

Permalink
wip adding ante and adjusting example chain
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Aug 7, 2024
1 parent 2ba0608 commit 1648ef5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
9 changes: 6 additions & 3 deletions example_chain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import (
"github.com/evmos/evmos/v19/x/feemarket"
feemarketkeeper "github.com/evmos/evmos/v19/x/feemarket/keeper"
feemarkettypes "github.com/evmos/evmos/v19/x/feemarket/types"
"github.com/evmos/os/ethereum/eip712"
srvflags "github.com/evmos/os/server/flags"
evmosutils "github.com/evmos/os/utils"
)
Expand Down Expand Up @@ -162,6 +163,7 @@ var (
// capabilities aren't needed for testing.
type ExampleChain struct {
*baseapp.BaseApp

legacyAmino *codec.LegacyAmino
appCodec codec.Codec
txConfig client.TxConfig
Expand Down Expand Up @@ -208,11 +210,11 @@ func init() {
panic(err)
}

DefaultNodeHome = filepath.Join(userHomeDir, ".simapp")
DefaultNodeHome = filepath.Join(userHomeDir, ".osd")
}

// NewSimApp returns a reference to an initialized ExampleChain.
func NewSimApp(
// NewExampleApp returns a reference to an initialized ExampleChain.
func NewExampleApp(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
Expand All @@ -221,6 +223,7 @@ func NewSimApp(
baseAppOptions ...func(*baseapp.BaseApp),
) *ExampleChain {
encodingConfig := makeEncodingConfig()
eip712.SetEncodingConfig(encodingConfig)

appCodec := encodingConfig.Codec
legacyAmino := encodingConfig.Amino
Expand Down
8 changes: 8 additions & 0 deletions example_chain/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import (

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

"cosmossdk.io/simapp"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/evmos/os/encoding"
)

// NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState() simapp.GenesisState {
encCfg := encoding.MakeConfig(ModuleBasics)
return ModuleBasics.DefaultGenesis(encCfg.Codec)
}

// ExportAppStateAndValidators exports the state of the application for a genesis
// file.
func (app *ExampleChain) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAddrs []string, modulesToExport []string) (servertypes.ExportedApp, error) {
Expand Down
8 changes: 4 additions & 4 deletions example_chain/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func setup(withGenesis bool, invCheckPeriod uint) (*ExampleChain, GenesisState)
appOptions[flags.FlagHome] = DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = invCheckPeriod

app := NewSimApp(log.NewNopLogger(), db, nil, true, appOptions)
app := NewExampleApp(log.NewNopLogger(), db, nil, true, appOptions)
if withGenesis {
return app, app.DefaultGenesis()
}
Expand All @@ -72,7 +72,7 @@ func NewSimappWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptio
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
}

app := NewSimApp(options.Logger, options.DB, nil, true, options.AppOpts)
app := NewExampleApp(options.Logger, options.DB, nil, true, options.AppOpts)
genesisState := app.DefaultGenesis()
genesisState, err = simtestutil.GenesisStateWithValSet(app.AppCodec(), genesisState, valSet, []authtypes.GenesisAccount{acc}, balance)
require.NoError(t, err)
Expand Down Expand Up @@ -223,10 +223,10 @@ func NewTestNetworkFixture() network.TestFixture {
}
defer os.RemoveAll(dir)

app := NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(dir))
app := NewExampleApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(dir))

appCtr := func(val network.ValidatorI) servertypes.Application {
return NewSimApp(
return NewExampleApp(
val.GetCtx().Logger, dbm.NewMemDB(), nil, true,
simtestutil.NewAppOptionsWithFlagHome(val.GetCtx().Config.RootDir),
bam.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
Expand Down
14 changes: 7 additions & 7 deletions testutil/tx/cosmos.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// Copyright Tharsis Labs Ltd.(ExampleChain)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)

package tx
Expand All @@ -11,7 +11,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/evmos/evmos/v19/app"
app "github.com/evmos/os/example_chain"
"github.com/evmos/os/testutil"
)

Expand Down Expand Up @@ -41,7 +41,7 @@ type CosmosTxArgs struct {
// It returns the signed transaction and an error
func PrepareCosmosTx(
ctx sdk.Context,
appEvmos *app.Evmos,
app *app.ExampleChain,
args CosmosTxArgs,
) (authsigning.Tx, error) {
txBuilder := args.TxCfg.NewTxBuilder()
Expand All @@ -64,7 +64,7 @@ func PrepareCosmosTx(

return signCosmosTx(
ctx,
appEvmos,
app,
args,
txBuilder,
)
Expand All @@ -74,12 +74,12 @@ func PrepareCosmosTx(
// the provided private key
func signCosmosTx(
ctx sdk.Context,
appEvmos *app.Evmos,
app *app.ExampleChain,
args CosmosTxArgs,
txBuilder client.TxBuilder,
) (authsigning.Tx, error) {
addr := sdk.AccAddress(args.Priv.PubKey().Address().Bytes())
seq, err := appEvmos.AccountKeeper.GetSequence(ctx, addr)
seq, err := app.AccountKeeper.GetSequence(ctx, addr)
if err != nil {
return nil, err
}
Expand All @@ -102,7 +102,7 @@ func signCosmosTx(
}

// Second round: all signer infos are set, so each signer can sign.
accNumber := appEvmos.AccountKeeper.GetAccount(ctx, addr).GetAccountNumber()
accNumber := app.AccountKeeper.GetAccount(ctx, addr).GetAccountNumber()
signerData := authsigning.SignerData{
ChainID: args.ChainID,
AccountNumber: accNumber,
Expand Down
18 changes: 9 additions & 9 deletions testutil/tx/eip712.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/evmos/evmos/v19/app"
cryptocodec "github.com/evmos/os/crypto/codec"
"github.com/evmos/os/ethereum/eip712"
app "github.com/evmos/os/example_chain"
"github.com/evmos/os/types"
)

Expand All @@ -44,12 +44,12 @@ type signatureV2Args struct {
// It returns the signed transaction and an error
func CreateEIP712CosmosTx(
ctx sdk.Context,
appEvmos *app.Evmos,
app *app.ExampleChain,
args EIP712TxArgs,
) (sdk.Tx, error) {
builder, err := PrepareEIP712CosmosTx(
ctx,
appEvmos,
app,
args,
)
return builder.GetTx(), err
Expand All @@ -60,7 +60,7 @@ func CreateEIP712CosmosTx(
// It returns the tx builder with the signed transaction and an error
func PrepareEIP712CosmosTx(
ctx sdk.Context,
appEvmos *app.Evmos,
exampleApp *app.ExampleChain,
args EIP712TxArgs,
) (client.TxBuilder, error) {
txArgs := args.CosmosTxArgs
Expand All @@ -72,9 +72,9 @@ func PrepareEIP712CosmosTx(
chainIDNum := pc.Uint64()

from := sdk.AccAddress(txArgs.Priv.PubKey().Address().Bytes())
accNumber := appEvmos.AccountKeeper.GetAccount(ctx, from).GetAccountNumber()
accNumber := exampleApp.AccountKeeper.GetAccount(ctx, from).GetAccountNumber()

nonce, err := appEvmos.AccountKeeper.GetSequence(ctx, from)
nonce, err := exampleApp.AccountKeeper.GetSequence(ctx, from)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func PrepareEIP712CosmosTx(

return signCosmosEIP712Tx(
ctx,
appEvmos,
exampleApp,
args,
builder,
typedData,
Expand All @@ -123,15 +123,15 @@ func PrepareEIP712CosmosTx(
// the provided private key and the typed data
func signCosmosEIP712Tx(
ctx sdk.Context,
appEvmos *app.Evmos,
exampleApp *app.ExampleChain,
args EIP712TxArgs,
builder authtx.ExtensionOptionsTxBuilder,
data apitypes.TypedData,
) (client.TxBuilder, error) {
priv := args.CosmosTxArgs.Priv

from := sdk.AccAddress(priv.PubKey().Address().Bytes())
nonce, err := appEvmos.AccountKeeper.GetSequence(ctx, from)
nonce, err := exampleApp.AccountKeeper.GetSequence(ctx, from)
if err != nil {
return nil, err
}
Expand Down
16 changes: 8 additions & 8 deletions testutil/tx/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/evmos/evmos/v19/app"
evmtypes "github.com/evmos/evmos/v19/x/evm/types"
app "github.com/evmos/os/example_chain"
"github.com/evmos/os/server/config"
"github.com/evmos/os/testutil"
)
Expand All @@ -28,13 +28,13 @@ import (
// It returns the signed transaction and an error
func PrepareEthTx(
txCfg client.TxConfig,
appEvmos *app.Evmos,
app *app.ExampleChain,
priv cryptotypes.PrivKey,
msgs ...sdk.Msg,
) (authsigning.Tx, error) {
txBuilder := txCfg.NewTxBuilder()

signer := ethtypes.LatestSignerForChainID(appEvmos.EvmKeeper.ChainID())
signer := ethtypes.LatestSignerForChainID(app.EVMKeeper.ChainID())
txFee := sdk.Coins{}
txGasLimit := uint64(0)

Expand Down Expand Up @@ -91,7 +91,7 @@ func PrepareEthTx(
// Should this not be the case, just pass in zero.
func CreateEthTx(
ctx sdk.Context,
appEvmos *app.Evmos,
app *app.ExampleChain,
privKey cryptotypes.PrivKey,
from sdk.AccAddress,
dest sdk.AccAddress,
Expand All @@ -100,17 +100,17 @@ func CreateEthTx(
) (*evmtypes.MsgEthereumTx, error) {
toAddr := common.BytesToAddress(dest.Bytes())
fromAddr := common.BytesToAddress(from.Bytes())
chainID := appEvmos.EvmKeeper.ChainID()
chainID := app.EVMKeeper.ChainID()

// When we send multiple Ethereum Tx's in one Cosmos Tx, we need to increment the nonce for each one.
nonce := appEvmos.EvmKeeper.GetNonce(ctx, fromAddr) + uint64(nonceIncrement)
nonce := app.EVMKeeper.GetNonce(ctx, fromAddr) + uint64(nonceIncrement)
evmTxParams := &evmtypes.EvmTxArgs{
ChainID: chainID,
Nonce: nonce,
To: &toAddr,
Amount: amount,
GasLimit: 100000,
GasFeeCap: appEvmos.FeeMarketKeeper.GetBaseFee(ctx),
GasFeeCap: app.FeeMarketKeeper.GetBaseFee(ctx),
GasTipCap: big.NewInt(1),
Accesses: &ethtypes.AccessList{},
}
Expand All @@ -119,7 +119,7 @@ func CreateEthTx(

// If we are creating multiple eth Tx's with different senders, we need to sign here rather than later.
if privKey != nil {
signer := ethtypes.LatestSignerForChainID(appEvmos.EvmKeeper.ChainID())
signer := ethtypes.LatestSignerForChainID(app.EVMKeeper.ChainID())
err := msgEthereumTx.Sign(signer, NewSigner(privKey))
if err != nil {
return nil, err
Expand Down

0 comments on commit 1648ef5

Please sign in to comment.