-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imp(tests): Add Solidity and Ledger tests (#41)
- Loading branch information
1 parent
c7da69b
commit b35efda
Showing
46 changed files
with
14,334 additions
and
1 deletion.
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,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 "$@" |
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" | ||
|
||
"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) | ||
} |
Oops, something went wrong.