Skip to content

Commit

Permalink
ibctesting: make testing.T public (#1020) (#1030)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6d6888b)

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Damian Nolan <damiannolan@gmail.com>
  • Loading branch information
3 people authored Mar 1, 2022
1 parent 3c33f77 commit 171498a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 52 deletions.
41 changes: 19 additions & 22 deletions testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ import (
"github.com/cosmos/ibc-go/v3/testing/simapp"
)

var (
MaxAccounts = 10
)
var MaxAccounts = 10

type SenderAccount struct {
SenderPrivKey cryptotypes.PrivKey
Expand All @@ -51,7 +49,7 @@ type SenderAccount struct {
// is used for delivering transactions through the application state.
// NOTE: the actual application uses an empty chain-id for ease of testing.
type TestChain struct {
t *testing.T
*testing.T

Coordinator *Coordinator
App TestingApp
Expand Down Expand Up @@ -88,7 +86,6 @@ type TestChain struct {
// CONTRACT: Validator and signer array must be provided in the order expected by Tendermint.
// i.e. sorted first by power and then lexicographically by address.
func NewTestChainWithValSet(t *testing.T, coord *Coordinator, chainID string, valSet *tmtypes.ValidatorSet, signers []tmtypes.PrivValidator) *TestChain {

genAccs := []authtypes.GenesisAccount{}
genBals := []banktypes.Balance{}
senderAccs := []SenderAccount{}
Expand Down Expand Up @@ -129,7 +126,7 @@ func NewTestChainWithValSet(t *testing.T, coord *Coordinator, chainID string, va

// create an account to send transactions from
chain := &TestChain{
t: t,
T: t,
Coordinator: coord,
ChainID: chainID,
App: app,
Expand Down Expand Up @@ -191,7 +188,7 @@ func (chain *TestChain) GetContext() sdk.Context {
// their own SimApp.
func (chain *TestChain) GetSimApp() *simapp.SimApp {
app, ok := chain.App.(*simapp.SimApp)
require.True(chain.t, ok)
require.True(chain.T, ok)

return app
}
Expand All @@ -213,10 +210,10 @@ func (chain *TestChain) QueryProofAtHeight(key []byte, height int64) ([]byte, cl
})

merkleProof, err := commitmenttypes.ConvertProofs(res.ProofOps)
require.NoError(chain.t, err)
require.NoError(chain.T, err)

proof, err := chain.App.AppCodec().Marshal(&merkleProof)
require.NoError(chain.t, err)
require.NoError(chain.T, err)

revision := clienttypes.ParseChainID(chain.ChainID)

Expand All @@ -237,10 +234,10 @@ func (chain *TestChain) QueryUpgradeProof(key []byte, height uint64) ([]byte, cl
})

merkleProof, err := commitmenttypes.ConvertProofs(res.ProofOps)
require.NoError(chain.t, err)
require.NoError(chain.T, err)

proof, err := chain.App.AppCodec().Marshal(&merkleProof)
require.NoError(chain.t, err)
require.NoError(chain.T, err)

revision := clienttypes.ParseChainID(chain.ChainID)

Expand Down Expand Up @@ -300,7 +297,7 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) {
chain.Coordinator.UpdateTimeForChain(chain)

_, r, err := simapp.SignAndDeliver(
chain.t,
chain.T,
chain.TxConfig,
chain.App.GetBaseApp(),
chain.GetContext().BlockHeader(),
Expand Down Expand Up @@ -329,7 +326,7 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) {
// expected to exist otherwise testing will fail.
func (chain *TestChain) GetClientState(clientID string) exported.ClientState {
clientState, found := chain.App.GetIBCKeeper().ClientKeeper.GetClientState(chain.GetContext(), clientID)
require.True(chain.t, found)
require.True(chain.T, found)

return clientState
}
Expand Down Expand Up @@ -361,7 +358,7 @@ func (chain *TestChain) GetValsAtHeight(height int64) (*tmtypes.ValidatorSet, bo
// acknowledgement does not exist then testing will fail.
func (chain *TestChain) GetAcknowledgement(packet exported.PacketI) []byte {
ack, found := chain.App.GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(chain.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
require.True(chain.t, found)
require.True(chain.T, found)

return ack
}
Expand Down Expand Up @@ -436,7 +433,7 @@ func (chain *TestChain) CreateTMClientHeader(chainID string, blockHeight int64,
valSet *tmproto.ValidatorSet
trustedVals *tmproto.ValidatorSet
)
require.NotNil(chain.t, tmValSet)
require.NotNil(chain.T, tmValSet)

vsetHash := tmValSet.Hash()

Expand All @@ -461,7 +458,7 @@ func (chain *TestChain) CreateTMClientHeader(chainID string, blockHeight int64,
voteSet := tmtypes.NewVoteSet(chainID, blockHeight, 1, tmproto.PrecommitType, tmValSet)

commit, err := tmtypes.MakeCommit(blockID, blockHeight, 1, voteSet, signers, timestamp)
require.NoError(chain.t, err)
require.NoError(chain.T, err)

signedHeader := &tmproto.SignedHeader{
Header: tmHeader.ToProto(),
Expand Down Expand Up @@ -533,11 +530,11 @@ func (chain *TestChain) CreatePortCapability(scopedKeeper capabilitykeeper.Scope
if !ok {
// create capability using the IBC capability keeper
cap, err := chain.App.GetScopedIBCKeeper().NewCapability(chain.GetContext(), host.PortPath(portID))
require.NoError(chain.t, err)
require.NoError(chain.T, err)

// claim capability using the scopedKeeper
err = scopedKeeper.ClaimCapability(chain.GetContext(), cap, host.PortPath(portID))
require.NoError(chain.t, err)
require.NoError(chain.T, err)
}

chain.App.Commit()
Expand All @@ -549,7 +546,7 @@ func (chain *TestChain) CreatePortCapability(scopedKeeper capabilitykeeper.Scope
// exist, otherwise testing will fail.
func (chain *TestChain) GetPortCapability(portID string) *capabilitytypes.Capability {
cap, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), host.PortPath(portID))
require.True(chain.t, ok)
require.True(chain.T, ok)

return cap
}
Expand All @@ -563,9 +560,9 @@ func (chain *TestChain) CreateChannelCapability(scopedKeeper capabilitykeeper.Sc
_, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), capName)
if !ok {
cap, err := chain.App.GetScopedIBCKeeper().NewCapability(chain.GetContext(), capName)
require.NoError(chain.t, err)
require.NoError(chain.T, err)
err = scopedKeeper.ClaimCapability(chain.GetContext(), cap, capName)
require.NoError(chain.t, err)
require.NoError(chain.T, err)
}

chain.App.Commit()
Expand All @@ -577,7 +574,7 @@ func (chain *TestChain) CreateChannelCapability(scopedKeeper capabilitykeeper.Sc
// The capability must exist, otherwise testing will fail.
func (chain *TestChain) GetChannelCapability(portID, channelID string) *capabilitytypes.Capability {
cap, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), host.ChannelCapabilityPath(portID, channelID))
require.True(chain.t, ok)
require.True(chain.T, ok)

return cap
}
26 changes: 13 additions & 13 deletions testing/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
// Coordinator is a testing struct which contains N TestChain's. It handles keeping all chains
// in sync with regards to time.
type Coordinator struct {
t *testing.T
*testing.T

CurrentTime time.Time
Chains map[string]*TestChain
Expand All @@ -29,7 +29,7 @@ type Coordinator struct {
func NewCoordinator(t *testing.T, n int) *Coordinator {
chains := make(map[string]*TestChain)
coord := &Coordinator{
t: t,
T: t,
CurrentTime: globalStartTime,
}

Expand Down Expand Up @@ -84,10 +84,10 @@ func (coord *Coordinator) Setup(path *Path) {
// caller does not anticipate any errors.
func (coord *Coordinator) SetupClients(path *Path) {
err := path.EndpointA.CreateClient()
require.NoError(coord.t, err)
require.NoError(coord.T, err)

err = path.EndpointB.CreateClient()
require.NoError(coord.t, err)
require.NoError(coord.T, err)
}

// SetupClientConnections is a helper function to create clients and the appropriate
Expand All @@ -105,16 +105,16 @@ func (coord *Coordinator) SetupConnections(path *Path) {
// successfully opened otherwise testing will fail.
func (coord *Coordinator) CreateConnections(path *Path) {
err := path.EndpointA.ConnOpenInit()
require.NoError(coord.t, err)
require.NoError(coord.T, err)

err = path.EndpointB.ConnOpenTry()
require.NoError(coord.t, err)
require.NoError(coord.T, err)

err = path.EndpointA.ConnOpenAck()
require.NoError(coord.t, err)
require.NoError(coord.T, err)

err = path.EndpointB.ConnOpenConfirm()
require.NoError(coord.t, err)
require.NoError(coord.T, err)

// ensure counterparty is up to date
path.EndpointA.UpdateClient()
Expand Down Expand Up @@ -146,16 +146,16 @@ func (coord *Coordinator) CreateTransferChannels(path *Path) {
// opened otherwise testing will fail.
func (coord *Coordinator) CreateChannels(path *Path) {
err := path.EndpointA.ChanOpenInit()
require.NoError(coord.t, err)
require.NoError(coord.T, err)

err = path.EndpointB.ChanOpenTry()
require.NoError(coord.t, err)
require.NoError(coord.T, err)

err = path.EndpointA.ChanOpenAck()
require.NoError(coord.t, err)
require.NoError(coord.T, err)

err = path.EndpointB.ChanOpenConfirm()
require.NoError(coord.t, err)
require.NoError(coord.T, err)

// ensure counterparty is up to date
path.EndpointA.UpdateClient()
Expand All @@ -165,7 +165,7 @@ func (coord *Coordinator) CreateChannels(path *Path) {
// not exist.
func (coord *Coordinator) GetChain(chainID string) *TestChain {
chain, found := coord.Chains[chainID]
require.True(coord.t, found, fmt.Sprintf("%s chain does not exist", chainID))
require.True(coord.T, found, fmt.Sprintf("%s chain does not exist", chainID))
return chain
}

Expand Down
31 changes: 14 additions & 17 deletions testing/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (endpoint *Endpoint) CreateClient() (err error) {
switch endpoint.ClientConfig.GetClientType() {
case exported.Tendermint:
tmConfig, ok := endpoint.ClientConfig.(*TendermintConfig)
require.True(endpoint.Chain.t, ok)
require.True(endpoint.Chain.T, ok)

height := endpoint.Counterparty.Chain.LastHeader.GetHeight().(clienttypes.Height)
clientState = ibctmtypes.NewClientState(
Expand All @@ -98,7 +98,7 @@ func (endpoint *Endpoint) CreateClient() (err error) {
consensusState = endpoint.Counterparty.Chain.LastHeader.ConsensusState()
case exported.Solomachine:
// TODO
// solo := NewSolomachine(chain.t, endpoint.Chain.Codec, clientID, "", 1)
// solo := NewSolomachine(Chain.T, endpoint.Chain.Codec, clientID, "", 1)
// clientState = solo.ClientState()
// consensusState = solo.ConsensusState()

Expand All @@ -113,15 +113,15 @@ func (endpoint *Endpoint) CreateClient() (err error) {
msg, err := clienttypes.NewMsgCreateClient(
clientState, consensusState, endpoint.Chain.SenderAccount.GetAddress().String(),
)
require.NoError(endpoint.Chain.t, err)
require.NoError(endpoint.Chain.T, err)

res, err := endpoint.Chain.SendMsgs(msg)
if err != nil {
return err
}

endpoint.ClientID, err = ParseClientIDFromEvents(res.GetEvents())
require.NoError(endpoint.Chain.t, err)
require.NoError(endpoint.Chain.T, err)

return nil
}
Expand All @@ -131,9 +131,7 @@ func (endpoint *Endpoint) UpdateClient() (err error) {
// ensure counterparty has committed state
endpoint.Chain.Coordinator.CommitBlock(endpoint.Counterparty.Chain)

var (
header exported.Header
)
var header exported.Header

switch endpoint.ClientConfig.GetClientType() {
case exported.Tendermint:
Expand All @@ -151,10 +149,9 @@ func (endpoint *Endpoint) UpdateClient() (err error) {
endpoint.ClientID, header,
endpoint.Chain.SenderAccount.GetAddress().String(),
)
require.NoError(endpoint.Chain.t, err)
require.NoError(endpoint.Chain.T, err)

return endpoint.Chain.sendMsgs(msg)

}

// ConnOpenInit will construct and execute a MsgConnectionOpenInit on the associated endpoint.
Expand All @@ -171,7 +168,7 @@ func (endpoint *Endpoint) ConnOpenInit() error {
}

endpoint.ConnectionID, err = ParseConnectionIDFromEvents(res.GetEvents())
require.NoError(endpoint.Chain.t, err)
require.NoError(endpoint.Chain.T, err)

return nil
}
Expand All @@ -197,7 +194,7 @@ func (endpoint *Endpoint) ConnOpenTry() error {

if endpoint.ConnectionID == "" {
endpoint.ConnectionID, err = ParseConnectionIDFromEvents(res.GetEvents())
require.NoError(endpoint.Chain.t, err)
require.NoError(endpoint.Chain.T, err)
}

return nil
Expand Down Expand Up @@ -277,7 +274,7 @@ func (endpoint *Endpoint) ChanOpenInit() error {
}

endpoint.ChannelID, err = ParseChannelIDFromEvents(res.GetEvents())
require.NoError(endpoint.Chain.t, err)
require.NoError(endpoint.Chain.T, err)

return nil
}
Expand All @@ -303,7 +300,7 @@ func (endpoint *Endpoint) ChanOpenTry() error {

if endpoint.ChannelID == "" {
endpoint.ChannelID, err = ParseChannelIDFromEvents(res.GetEvents())
require.NoError(endpoint.Chain.t, err)
require.NoError(endpoint.Chain.T, err)
}

// update version to selected app version
Expand Down Expand Up @@ -449,7 +446,7 @@ func (endpoint *Endpoint) TimeoutPacket(packet channeltypes.Packet) error {

proof, proofHeight := endpoint.Counterparty.QueryProof(packetKey)
nextSeqRecv, found := endpoint.Counterparty.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceRecv(endpoint.Counterparty.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID)
require.True(endpoint.Chain.t, found)
require.True(endpoint.Chain.T, found)

timeoutMsg := channeltypes.NewMsgTimeout(
packet, nextSeqRecv,
Expand Down Expand Up @@ -486,7 +483,7 @@ func (endpoint *Endpoint) SetClientState(clientState exported.ClientState) {
// The consensus state is expected to exist otherwise testing will fail.
func (endpoint *Endpoint) GetConsensusState(height exported.Height) exported.ConsensusState {
consensusState, found := endpoint.Chain.GetConsensusState(endpoint.ClientID, height)
require.True(endpoint.Chain.t, found)
require.True(endpoint.Chain.T, found)

return consensusState
}
Expand All @@ -500,7 +497,7 @@ func (endpoint *Endpoint) SetConsensusState(consensusState exported.ConsensusSta
// connection is expected to exist otherwise testing will fail.
func (endpoint *Endpoint) GetConnection() connectiontypes.ConnectionEnd {
connection, found := endpoint.Chain.App.GetIBCKeeper().ConnectionKeeper.GetConnection(endpoint.Chain.GetContext(), endpoint.ConnectionID)
require.True(endpoint.Chain.t, found)
require.True(endpoint.Chain.T, found)

return connection
}
Expand All @@ -514,7 +511,7 @@ func (endpoint *Endpoint) SetConnection(connection connectiontypes.ConnectionEnd
// is expected to exist otherwise testing will fail.
func (endpoint *Endpoint) GetChannel() channeltypes.Channel {
channel, found := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.GetChannel(endpoint.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID)
require.True(endpoint.Chain.t, found)
require.True(endpoint.Chain.T, found)

return channel
}
Expand Down

0 comments on commit 171498a

Please sign in to comment.