Skip to content

Commit

Permalink
imp: support custom keys for testing (#893) (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Feb 10, 2022
1 parent 45b8e92 commit 2641fa1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 11 additions & 7 deletions testing/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
17 changes: 10 additions & 7 deletions testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions testing/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 2641fa1

Please sign in to comment.