Skip to content

Commit

Permalink
imp(tests): Add Solidity and Ledger tests (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Aug 26, 2024
1 parent c7da69b commit b35efda
Show file tree
Hide file tree
Showing 46 changed files with 14,334 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This changelog was created using the `clu` binary

### Improvements

- (tests) [#41](https://github.com/evmos/os/pull/41) Add Solidity and Ledger tests.
- (all) [#37](https://github.com/evmos/os/pull/37) Add EVM, feemarket and precompiles from evmOS.
- (all) [#38](https://github.com/evmos/os/pull/38) Add readme.
- (all) [#30](https://github.com/evmos/os/pull/30) Add example chain implementation and ante handlers.
Expand Down
2 changes: 1 addition & 1 deletion example_chain/local_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ if [[ $overwrite == "y" || $overwrite == "Y" ]]; then
fi

# Start the node
osd start \
osd start "$TRACE" \
--log_level $LOGLEVEL \
--minimum-gas-prices=0.0001aevmos \
--home "$HOMEDIR" \
Expand Down
25 changes: 25 additions & 0 deletions scripts/run-solidity-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
export GOPATH="$HOME"/go
export PATH="$PATH":"$GOPATH"/bin

# remove existing data
rm -rf "$HOME"/.tmp-osd-solidity-tests

# used to exit on first error (any non-zero exit code)
set -e

# build example chain binary
cd example_chain && make install

cd ../tests/solidity || exit

if command -v yarn &>/dev/null; then
yarn install
else
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn
yarn install
fi

yarn test --network evmos "$@"
182 changes: 182 additions & 0 deletions tests/integration/ledger/evmosd_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package ledger_test

import (
"bufio"
"context"
"fmt"
"io"
"testing"
"time"

"cosmossdk.io/simapp/params"
"github.com/cometbft/cometbft/crypto/tmhash"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmversion "github.com/cometbft/cometbft/proto/tendermint/version"
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
"github.com/cometbft/cometbft/version"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cosmosledger "github.com/cosmos/cosmos-sdk/crypto/ledger"
"github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
clientkeys "github.com/evmos/os/client/keys"
"github.com/evmos/os/crypto/hd"
evmoskeyring "github.com/evmos/os/crypto/keyring"
example_app "github.com/evmos/os/example_chain"
"github.com/evmos/os/tests/integration/ledger/mocks"
"github.com/evmos/os/testutil"
utiltx "github.com/evmos/os/testutil/tx"
"github.com/spf13/cobra"
"github.com/stretchr/testify/suite"

//nolint:revive // dot imports are fine for Ginkgo
. "github.com/onsi/ginkgo/v2"
//nolint:revive // dot imports are fine for Ginkgo
. "github.com/onsi/gomega"
)

var s *LedgerTestSuite

type LedgerTestSuite struct {
suite.Suite

app *example_app.ExampleChain
ctx sdk.Context

ledger *mocks.SECP256K1
accRetriever *mocks.AccountRetriever

accAddr sdk.AccAddress

privKey types.PrivKey
pubKey types.PubKey
}

func TestLedger(t *testing.T) {
s = new(LedgerTestSuite)
suite.Run(t, s)

RegisterFailHandler(Fail)
RunSpecs(t, "Evmosd Suite")
}

func (suite *LedgerTestSuite) SetupTest() {
var (
err error
ethAddr common.Address
)

suite.ledger = mocks.NewSECP256K1(s.T())

ethAddr, s.privKey = utiltx.NewAddrKey()

s.Require().NoError(err)
suite.pubKey = s.privKey.PubKey()

suite.accAddr = sdk.AccAddress(ethAddr.Bytes())
}

func (suite *LedgerTestSuite) SetupEvmosApp() {
consAddress := sdk.ConsAddress(utiltx.GenerateAddress().Bytes())

// init app
chainID := testutil.ExampleChainID
suite.app = example_app.Setup(suite.T(), false, chainID)
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{
Height: 1,
ChainID: chainID,
Time: time.Now().UTC(),
ProposerAddress: consAddress.Bytes(),

Version: tmversion.Consensus{
Block: version.BlockProtocol,
},
LastBlockId: tmproto.BlockID{
Hash: tmhash.Sum([]byte("block_id")),
PartSetHeader: tmproto.PartSetHeader{
Total: 11,
Hash: tmhash.Sum([]byte("partset_header")),
},
},
AppHash: tmhash.Sum([]byte("app")),
DataHash: tmhash.Sum([]byte("data")),
EvidenceHash: tmhash.Sum([]byte("evidence")),
ValidatorsHash: tmhash.Sum([]byte("validators")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators")),
ConsensusHash: tmhash.Sum([]byte("consensus")),
LastResultsHash: tmhash.Sum([]byte("last_result")),
})
}

func (suite *LedgerTestSuite) NewKeyringAndCtxs(krHome string, input io.Reader, encCfg params.EncodingConfig) (keyring.Keyring, client.Context, context.Context) {
kr, err := keyring.New(
sdk.KeyringServiceName(),
keyring.BackendTest,
krHome,
input,
encCfg.Codec,
s.MockKeyringOption(),
)
s.Require().NoError(err)
s.accRetriever = mocks.NewAccountRetriever(s.T())

initClientCtx := client.Context{}.
WithCodec(encCfg.Codec).
// NOTE: cmd.Execute() panics without account retriever
WithAccountRetriever(s.accRetriever).
WithTxConfig(encCfg.TxConfig).
WithLedgerHasProtobuf(true).
WithUseLedger(true).
WithKeyring(kr).
WithClient(mocks.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithChainID(testutil.ExampleChainIDPrefix + "-13")

srvCtx := server.NewDefaultContext()
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &initClientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, srvCtx)

return kr, initClientCtx, ctx
}

func (suite *LedgerTestSuite) evmosAddKeyCmd() *cobra.Command {
cmd := keys.AddKeyCommand()

algoFlag := cmd.Flag(flags.FlagKeyType)
algoFlag.DefValue = string(hd.EthSecp256k1Type)

err := algoFlag.Value.Set(string(hd.EthSecp256k1Type))
suite.Require().NoError(err)

cmd.Flags().AddFlagSet(keys.Commands("home").PersistentFlags())

cmd.RunE = func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd).WithKeyringOptions(hd.EthSecp256k1Option())
clientCtx, err := client.ReadPersistentCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
buf := bufio.NewReader(clientCtx.Input)
return clientkeys.RunAddCmd(clientCtx, cmd, args, buf)
}
return cmd
}

func (suite *LedgerTestSuite) MockKeyringOption() keyring.Option {
return func(options *keyring.Options) {
options.SupportedAlgos = evmoskeyring.SupportedAlgorithms
options.SupportedAlgosLedger = evmoskeyring.SupportedAlgorithmsLedger
options.LedgerDerivation = func() (cosmosledger.SECP256K1, error) { return suite.ledger, nil }
options.LedgerCreateKey = evmoskeyring.CreatePubkey
options.LedgerAppName = evmoskeyring.AppName
options.LedgerSigSkipDERConv = evmoskeyring.SkipDERConversion
}
}

func (suite *LedgerTestSuite) FormatFlag(flag string) string {
return fmt.Sprintf("--%s", flag)
}
Loading

0 comments on commit b35efda

Please sign in to comment.