From cf85105e9132ffc9623d8f4b2ff4c72afc96ee89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Kunze=20K=C3=BCllmer?= <31522760+fedekunze@users.noreply.github.com> Date: Thu, 10 Feb 2022 15:10:24 +0100 Subject: [PATCH] imp: support custom keys for testing (#893) (cherry picked from commit fb2f0416254b997f41ac69a079136b1092f5fa6f) --- CHANGELOG.md | 1 + testing/app.go | 18 +++++++++++------- testing/chain.go | 17 ++++++++++------- testing/coordinator.go | 2 -- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c67ce543ca..0b690fe916f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (testing) [\#893](https://github.com/cosmos/ibc-go/pull/893) Support custom private keys for testing. * (testing) [\#810](https://github.com/cosmos/ibc-go/pull/810) Additional testing function added to `Endpoint` type called `RecvPacketWithResult`. Performs the same functionality as the existing `RecvPacket` function but also returns the message result. `path.RelayPacket` no longer uses the provided acknowledgement argument and instead obtains the acknowledgement via MsgRecvPacket events. * (connection) [\#721](https://github.com/cosmos/ibc-go/pull/721) Simplify connection handshake error messages when unpacking client state. * (channel) [\#692](https://github.com/cosmos/ibc-go/pull/692) Minimize channel logging by only emitting the packet sequence, source port/channel, destination port/channel upon packet receives, acknowledgements and timeouts. diff --git a/testing/app.go b/testing/app.go index 1c5bbe7c88a..487e0569ba2 100644 --- a/testing/app.go +++ b/testing/app.go @@ -126,13 +126,17 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs // commit genesis changes app.Commit() - app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{ - ChainID: chainID, - Height: app.LastBlockHeight() + 1, - AppHash: app.LastCommitID().Hash, - ValidatorsHash: valSet.Hash(), - NextValidatorsHash: valSet.Hash(), - }}) + app.BeginBlock( + abci.RequestBeginBlock{ + Header: tmproto.Header{ + ChainID: chainID, + Height: app.LastBlockHeight() + 1, + AppHash: app.LastCommitID().Hash, + ValidatorsHash: valSet.Hash(), + NextValidatorsHash: valSet.Hash(), + }, + }, + ) return app } diff --git a/testing/chain.go b/testing/chain.go index 281782f7837..8dfe4b8fb27 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -56,18 +56,23 @@ type TestChain struct { Vals *tmtypes.ValidatorSet Signers []tmtypes.PrivValidator - senderPrivKey cryptotypes.PrivKey + // autogenerated sender private key + SenderPrivKey cryptotypes.PrivKey SenderAccount authtypes.AccountI } // NewTestChain initializes a new TestChain instance with a single validator set using a -// generated private key. It also creates a sender account to be used for delivering transactions. +// generated secp256k1 Tendermint private key. It also creates a sender BaseAccount to be used for +// delivering transactions. // // The first block height is committed to state in order to allow for client creations on // counterparty chains. The TestChain will return with a block height starting at 2. // // Time management is handled by the Coordinator in order to ensure synchrony between chains. // Each update of any chain increments the block header time for all chains by 5 seconds. +// +// NOTE: to use a custom sender privkey and account for testing purposes, replace and modify this +// constructor function. func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain { // generate validator private/public key privVal := mock.NewPV() @@ -113,7 +118,7 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain { Codec: app.AppCodec(), Vals: valSet, Signers: signers, - senderPrivKey: senderPrivKey, + SenderPrivKey: senderPrivKey, SenderAccount: acc, } @@ -237,7 +242,6 @@ func (chain *TestChain) sendMsgs(msgs ...sdk.Msg) error { // number and updates the TestChain's headers. It returns the result and error if one // occurred. func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) { - // ensure the chain has the latest time chain.Coordinator.UpdateTimeForChain(chain) @@ -250,7 +254,7 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) { chain.ChainID, []uint64{chain.SenderAccount.GetAccountNumber()}, []uint64{chain.SenderAccount.GetSequence()}, - true, true, chain.senderPrivKey, + true, true, chain.SenderPrivKey, ) if err != nil { return nil, err @@ -357,7 +361,6 @@ func (chain *TestChain) ConstructUpdateTMClientHeaderWithTrustedHeight(counterpa header.TrustedValidators = trustedVals return header, nil - } // ExpireClient fast forwards the chain's block time by the provided amount of time which will @@ -468,7 +471,7 @@ func CreateSortedSignerArray(altPrivVal, suitePrivVal tmtypes.PrivValidator, // CreatePortCapability binds and claims a capability for the given portID if it does not // already exist. This function will fail testing on any resulting error. -// NOTE: only creation of a capbility for a transfer or mock port is supported +// NOTE: only creation of a capability for a transfer or mock port is supported // Other applications must bind to the port in InitGenesis or modify this code. func (chain *TestChain) CreatePortCapability(scopedKeeper capabilitykeeper.ScopedKeeper, portID string) { // check if the portId is already binded, if not bind it diff --git a/testing/coordinator.go b/testing/coordinator.go index 615bd830ced..be308c790d5 100644 --- a/testing/coordinator.go +++ b/testing/coordinator.go @@ -55,7 +55,6 @@ func (coord *Coordinator) IncrementTime() { func (coord *Coordinator) IncrementTimeBy(increment time.Duration) { coord.CurrentTime = coord.CurrentTime.Add(increment).UTC() coord.UpdateTime() - } // UpdateTime updates all clocks for the TestChains to the current global time. @@ -105,7 +104,6 @@ func (coord *Coordinator) SetupConnections(path *Path) { // are returned within a TestConnection struct. The function expects the connections to be // successfully opened otherwise testing will fail. func (coord *Coordinator) CreateConnections(path *Path) { - err := path.EndpointA.ConnOpenInit() require.NoError(coord.t, err)