Skip to content

Commit

Permalink
test(ledger): integration cli/keyring tests (#1163)
Browse files Browse the repository at this point in the history
* feat: initial

* test(cli): initial

* chore(cli_test): remove useless code

* chore(ledger_tests): separated e2e suite from integration

* feat(tests): cli tesutil && go mod tidy

* feat(test): sk&pk keys lib changed, added mock signing emulation

* feat(tests): add cli execution context block for bank txs

* feat(test): replaced cli test util call with cmd executions

* fix(tests): cli execution approach

* fix(test: fixed false positive signing

* chore(testutil): remove sdk cli testutil

* feat(tests): cli tx send execution

* feat(tests): added tendermint rpc client && account retriever mocks

* fix: fixed bank tx execution

* fix: fixed send tx cli execution for ledger key

* chore(tests): refactoring before each handlers

* feat: add cli device signing error case

* chore: removed useless  code

* chore(tests): apply suggestions from code review

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* chore: apply review suggestions

* chore: removed blank lines

* chore: grouped imports

* feat(mocks): mocked acc retriever with mockery and added methods to registry

* feat(mocks): mocked acc retriever with mockery and added methods to registry

* chore: go mod tidy

* Update tests/integration/ledger/ledger_test.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* Update tests/integration/ledger/ledger_test.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* Update tests/integration/ledger/ledger_test.go

Co-authored-by: Daniel Burckhardt <daniel.m.burckhardt@gmail.com>

* chore: apply suggestions from code review

Co-authored-by: Daniel Burckhardt <daniel.m.burckhardt@gmail.com>

* chore: apply review suggestions

* chore: formated cases structure

* chore: APPLY REVIEW SUGGESTIONS

* fix: add ethermint add keys cmd

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Daniel Burckhardt <daniel.m.burckhardt@gmail.com>
  • Loading branch information
3 people committed Jan 4, 2023
1 parent e2f8066 commit 0e0a566
Show file tree
Hide file tree
Showing 8 changed files with 748 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -30,7 +30,7 @@ require (
github.com/tendermint/tendermint v0.34.24
github.com/tendermint/tm-db v0.6.7
go.opencensus.io v0.24.0
google.golang.org/genproto v0.0.0-20221116193143-41c2ba794472
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6
google.golang.org/grpc v1.51.0
google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -1506,8 +1506,8 @@ google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6D
google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210126160654-44e461bb6506/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20221116193143-41c2ba794472 h1:kIfItBRE5gkUKpH4H5lNGciZbka1JrmRli3ArqrKFkA=
google.golang.org/genproto v0.0.0-20221116193143-41c2ba794472/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg=
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 h1:a2S6M0+660BgMNl++4JPlcAO/CjkqYItDEZwkoDQK7c=
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.19.1/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
Expand Down
182 changes: 182 additions & 0 deletions tests/integration/ledger/evmosd_suite_test.go
@@ -0,0 +1,182 @@
package ledger_test

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

"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"
"github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"

"github.com/evmos/ethermint/crypto/hd"
"github.com/evmos/ethermint/tests"
"github.com/evmos/evmos/v10/app"
"github.com/evmos/evmos/v10/tests/integration/ledger/mocks"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/version"

cosmosledger "github.com/cosmos/cosmos-sdk/crypto/ledger"
sdk "github.com/cosmos/cosmos-sdk/types"
clientkeys "github.com/evmos/ethermint/client/keys"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
evmoskeyring "github.com/evmos/evmos/v10/crypto/keyring"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
rpcclientmock "github.com/tendermint/tendermint/rpc/client/mock"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var s *LedgerTestSuite

type LedgerTestSuite struct {
suite.Suite

app *app.Evmos
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 = tests.NewAddrKey()

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

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

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

// init app
suite.app = app.Setup(false, feemarkettypes.DefaultGenesisState())
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{
Height: 1,
ChainID: "evmos_9001-1",
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("evmos_9000-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) ethermintAddKeyCmd() *cobra.Command {
cmd := keys.AddKeyCommand()

algoFlag := cmd.Flag(flags.FlagKeyAlgorithm)
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)
}

0 comments on commit 0e0a566

Please sign in to comment.