Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(ledger): integration cli/keyring tests (#1163)
* 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
1 parent
e2f8066
commit 0e0a566
Showing
8 changed files
with
748 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
Oops, something went wrong.