Skip to content

Commit 2641fa1

Browse files
authored
imp: support custom keys for testing (#893) (#902)
1 parent 45b8e92 commit 2641fa1

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
6161

6262
### Improvements
6363

64+
* (testing) [\#893](https://github.com/cosmos/ibc-go/pull/893) Support custom private keys for testing.
6465
* (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.
6566
* (connection) [\#721](https://github.com/cosmos/ibc-go/pull/721) Simplify connection handshake error messages when unpacking client state.
6667
* (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.

testing/app.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,17 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
126126

127127
// commit genesis changes
128128
app.Commit()
129-
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{
130-
ChainID: chainID,
131-
Height: app.LastBlockHeight() + 1,
132-
AppHash: app.LastCommitID().Hash,
133-
ValidatorsHash: valSet.Hash(),
134-
NextValidatorsHash: valSet.Hash(),
135-
}})
129+
app.BeginBlock(
130+
abci.RequestBeginBlock{
131+
Header: tmproto.Header{
132+
ChainID: chainID,
133+
Height: app.LastBlockHeight() + 1,
134+
AppHash: app.LastCommitID().Hash,
135+
ValidatorsHash: valSet.Hash(),
136+
NextValidatorsHash: valSet.Hash(),
137+
},
138+
},
139+
)
136140

137141
return app
138142
}

testing/chain.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,23 @@ type TestChain struct {
5656
Vals *tmtypes.ValidatorSet
5757
Signers []tmtypes.PrivValidator
5858

59-
senderPrivKey cryptotypes.PrivKey
59+
// autogenerated sender private key
60+
SenderPrivKey cryptotypes.PrivKey
6061
SenderAccount authtypes.AccountI
6162
}
6263

6364
// NewTestChain initializes a new TestChain instance with a single validator set using a
64-
// generated private key. It also creates a sender account to be used for delivering transactions.
65+
// generated secp256k1 Tendermint private key. It also creates a sender BaseAccount to be used for
66+
// delivering transactions.
6567
//
6668
// The first block height is committed to state in order to allow for client creations on
6769
// counterparty chains. The TestChain will return with a block height starting at 2.
6870
//
6971
// Time management is handled by the Coordinator in order to ensure synchrony between chains.
7072
// Each update of any chain increments the block header time for all chains by 5 seconds.
73+
//
74+
// NOTE: to use a custom sender privkey and account for testing purposes, replace and modify this
75+
// constructor function.
7176
func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
7277
// generate validator private/public key
7378
privVal := mock.NewPV()
@@ -113,7 +118,7 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
113118
Codec: app.AppCodec(),
114119
Vals: valSet,
115120
Signers: signers,
116-
senderPrivKey: senderPrivKey,
121+
SenderPrivKey: senderPrivKey,
117122
SenderAccount: acc,
118123
}
119124

@@ -237,7 +242,6 @@ func (chain *TestChain) sendMsgs(msgs ...sdk.Msg) error {
237242
// number and updates the TestChain's headers. It returns the result and error if one
238243
// occurred.
239244
func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) {
240-
241245
// ensure the chain has the latest time
242246
chain.Coordinator.UpdateTimeForChain(chain)
243247

@@ -250,7 +254,7 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) {
250254
chain.ChainID,
251255
[]uint64{chain.SenderAccount.GetAccountNumber()},
252256
[]uint64{chain.SenderAccount.GetSequence()},
253-
true, true, chain.senderPrivKey,
257+
true, true, chain.SenderPrivKey,
254258
)
255259
if err != nil {
256260
return nil, err
@@ -357,7 +361,6 @@ func (chain *TestChain) ConstructUpdateTMClientHeaderWithTrustedHeight(counterpa
357361
header.TrustedValidators = trustedVals
358362

359363
return header, nil
360-
361364
}
362365

363366
// 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,
468471

469472
// CreatePortCapability binds and claims a capability for the given portID if it does not
470473
// already exist. This function will fail testing on any resulting error.
471-
// NOTE: only creation of a capbility for a transfer or mock port is supported
474+
// NOTE: only creation of a capability for a transfer or mock port is supported
472475
// Other applications must bind to the port in InitGenesis or modify this code.
473476
func (chain *TestChain) CreatePortCapability(scopedKeeper capabilitykeeper.ScopedKeeper, portID string) {
474477
// check if the portId is already binded, if not bind it

testing/coordinator.go

-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func (coord *Coordinator) IncrementTime() {
5555
func (coord *Coordinator) IncrementTimeBy(increment time.Duration) {
5656
coord.CurrentTime = coord.CurrentTime.Add(increment).UTC()
5757
coord.UpdateTime()
58-
5958
}
6059

6160
// UpdateTime updates all clocks for the TestChains to the current global time.
@@ -105,7 +104,6 @@ func (coord *Coordinator) SetupConnections(path *Path) {
105104
// are returned within a TestConnection struct. The function expects the connections to be
106105
// successfully opened otherwise testing will fail.
107106
func (coord *Coordinator) CreateConnections(path *Path) {
108-
109107
err := path.EndpointA.ConnOpenInit()
110108
require.NoError(coord.t, err)
111109

0 commit comments

Comments
 (0)