diff --git a/cmd/raw.go b/cmd/raw.go index c7ab81981..e87c54b76 100644 --- a/cmd/raw.go +++ b/cmd/raw.go @@ -125,9 +125,8 @@ $ %s tx raw clnt ibc-1 ibc-0 ibconeclient`, appName, appName)), false, ) - return sendAndPrint([]sdk.Msg{chains[src].PathEnd.CreateClient( - clientState, dstHeader, - chains[src].MustGetAddress())}, + return sendAndPrint([]sdk.Msg{chains[src].CreateClient( + clientState, dstHeader)}, chains[src], cmd) }, } @@ -157,7 +156,7 @@ $ %s tx raw conn-init ibc-0 ibc-1 ibczeroclient ibconeclient ibcconn1 ibcconn2`, return err } - return sendAndPrint([]sdk.Msg{chains[src].PathEnd.ConnInit(chains[dst].PathEnd, chains[src].MustGetAddress())}, + return sendAndPrint([]sdk.Msg{chains[src].ConnInit(chains[dst].PathEnd)}, chains[src], cmd) }, } @@ -301,7 +300,7 @@ $ %s tx raw conn-confirm ibc-0 ibc-1 ibczeroclient ibconeclient ibcconn1 ibcconn return err } txs := []sdk.Msg{ - chains[src].PathEnd.ConnConfirm(dstState, chains[src].MustGetAddress()), + chains[src].ConnConfirm(dstState), chains[src].UpdateClient(updateHeader), } @@ -377,7 +376,7 @@ $ %s tx raw chan-init ibc-0 ibc-1 ibczeroclient ibconeclient ibcconn1 ibcconn2 i return err } - return sendAndPrint([]sdk.Msg{chains[src].PathEnd.ChanInit(chains[dst].PathEnd, chains[src].MustGetAddress())}, + return sendAndPrint([]sdk.Msg{chains[src].ChanInit(chains[dst].PathEnd)}, chains[src], cmd) }, } @@ -525,7 +524,7 @@ $ %s tx raw chan-confirm ibc-0 ibc-1 ibczeroclient ibcchan1 ibcchan2 transfer tr } txs := []sdk.Msg{ chains[src].UpdateClient(updateHeader), - chains[src].PathEnd.ChanConfirm(dstChanState, chains[src].MustGetAddress()), + chains[src].ChanConfirm(dstChanState), } return sendAndPrint(txs, chains[src], cmd) @@ -594,7 +593,7 @@ $ %s tx raw chan-close-init ibc-0 ibcchan1 transfer`, appName, appName)), return err } - return sendAndPrint([]sdk.Msg{src.PathEnd.ChanCloseInit(src.MustGetAddress())}, src, cmd) + return sendAndPrint([]sdk.Msg{src.ChanCloseInit()}, src, cmd) }, } return cmd @@ -639,7 +638,7 @@ $ %s tx raw chan-close-confirm ibc-0 ibc-1 ibczeroclient ibcchan1 ibcchan2 trans } txs := []sdk.Msg{ chains[src].UpdateClient(updateHeader), - chains[src].PathEnd.ChanCloseConfirm(dstChanState, chains[src].MustGetAddress()), + chains[src].ChanCloseConfirm(dstChanState), } return sendAndPrint(txs, chains[src], cmd) diff --git a/relayer/channel-tx.go b/relayer/channel-tx.go index 375feeaad..c6eb166f0 100644 --- a/relayer/channel-tx.go +++ b/relayer/channel-tx.go @@ -196,7 +196,7 @@ func ExecuteChannelStep(src, dst *Chain) (success, last, modified bool, err erro } msgs = []sdk.Msg{ src.UpdateClient(dstUpdateHeader), - src.PathEnd.ChanConfirm(dstChan, src.MustGetAddress()), + src.ChanConfirm(dstChan), } last = true @@ -212,7 +212,7 @@ func ExecuteChannelStep(src, dst *Chain) (success, last, modified bool, err erro } msgs = []sdk.Msg{ dst.UpdateClient(srcUpdateHeader), - dst.PathEnd.ChanConfirm(srcChan, dst.MustGetAddress()), + dst.ChanConfirm(srcChan), } last = true @@ -243,7 +243,7 @@ func InitializeChannel(src, dst *Chain, srcUpdateHeader, dstUpdateHeader *tmclie // cosntruct OpenInit message to be submitted on source chain msgs := []sdk.Msg{ src.UpdateClient(dstUpdateHeader), - src.PathEnd.ChanInit(dst.PathEnd, src.MustGetAddress()), + src.ChanInit(dst.PathEnd), } res, success, err := src.SendMsgs(msgs) @@ -413,7 +413,7 @@ func (c *Chain) CloseChannelStep(dst *Chain) (*RelayMsgs, error) { } out.Src = append(out.Src, c.UpdateClient(dstUpdateHeader), - c.PathEnd.ChanCloseInit(c.MustGetAddress()), + c.ChanCloseInit(), ) } else if dstChan.Channel.State != chantypes.UNINITIALIZED { if dst.debug { @@ -421,7 +421,7 @@ func (c *Chain) CloseChannelStep(dst *Chain) (*RelayMsgs, error) { } out.Dst = append(out.Dst, dst.UpdateClient(srcUpdateHeader), - dst.PathEnd.ChanCloseInit(dst.MustGetAddress()), + dst.ChanCloseInit(), ) } @@ -433,7 +433,7 @@ func (c *Chain) CloseChannelStep(dst *Chain) (*RelayMsgs, error) { } out.Dst = append(out.Dst, dst.UpdateClient(srcUpdateHeader), - dst.PathEnd.ChanCloseConfirm(srcChan, dst.MustGetAddress()), + dst.ChanCloseConfirm(srcChan), ) out.Last = true } @@ -446,7 +446,7 @@ func (c *Chain) CloseChannelStep(dst *Chain) (*RelayMsgs, error) { } out.Src = append(out.Src, c.UpdateClient(dstUpdateHeader), - c.PathEnd.ChanCloseConfirm(dstChan, c.MustGetAddress()), + c.ChanCloseConfirm(dstChan), ) out.Last = true } diff --git a/relayer/client-tx.go b/relayer/client-tx.go index 2c1dd11d0..d27cd1e15 100644 --- a/relayer/client-tx.go +++ b/relayer/client-tx.go @@ -59,10 +59,9 @@ func (c *Chain) CreateClients(dst *Chain) (modified bool, err error) { clientID, found := FindMatchingClient(c, dst, clientState) if !found { msgs := []sdk.Msg{ - c.PathEnd.CreateClient( + c.CreateClient( clientState, dstH, - c.MustGetAddress(), ), } @@ -123,10 +122,9 @@ func (c *Chain) CreateClients(dst *Chain) (modified bool, err error) { clientID, found := FindMatchingClient(dst, c, clientState) if !found { msgs := []sdk.Msg{ - dst.PathEnd.CreateClient( + dst.CreateClient( clientState, srcH, - dst.MustGetAddress(), ), } diff --git a/relayer/connection-tx.go b/relayer/connection-tx.go index 0ed8428b7..cef0176da 100644 --- a/relayer/connection-tx.go +++ b/relayer/connection-tx.go @@ -194,7 +194,7 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e } msgs = []sdk.Msg{ src.UpdateClient(dstUpdateHeader), - src.PathEnd.ConnConfirm(dstConn, src.MustGetAddress()), + src.ConnConfirm(dstConn), } _, success, err = src.SendMsgs(msgs) if !success { @@ -210,7 +210,7 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e } msgs = []sdk.Msg{ dst.UpdateClient(srcUpdateHeader), - dst.PathEnd.ConnConfirm(srcConn, dst.MustGetAddress()), + dst.ConnConfirm(srcConn), } last = true _, success, err = dst.SendMsgs(msgs) @@ -240,7 +240,7 @@ func InitializeConnection(src, dst *Chain, srcUpdateHeader, dstUpdateHeader *tmc // cosntruct OpenInit message to be submitted on source chain msgs := []sdk.Msg{ src.UpdateClient(dstUpdateHeader), - src.PathEnd.ConnInit(dst.PathEnd, src.MustGetAddress()), + src.ConnInit(dst.PathEnd), } res, success, err := src.SendMsgs(msgs) diff --git a/relayer/msgs.go b/relayer/msgs.go index a8008499c..3ffb909be 100644 --- a/relayer/msgs.go +++ b/relayer/msgs.go @@ -7,6 +7,7 @@ import ( conntypes "github.com/cosmos/cosmos-sdk/x/ibc/core/03-connection/types" chantypes "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types" ibcexported "github.com/cosmos/cosmos-sdk/x/ibc/core/exported" + tmclient "github.com/cosmos/cosmos-sdk/x/ibc/light-clients/07-tendermint/types" ) // NOTE: we explicitly call 'MustGetAddress' before 'NewMsg...' @@ -19,6 +20,30 @@ import ( // file handling has been refactored. // https://github.com/cosmos/cosmos-sdk/issues/8332 +// CreateClient creates an sdk.Msg to update the client on src with consensus state from dst +func (c *Chain) CreateClient( + clientState *tmclient.ClientState, + dstHeader *tmclient.Header) sdk.Msg { + + if err := dstHeader.ValidateBasic(); err != nil { + panic(err) + } + + msg, err := clienttypes.NewMsgCreateClient( + clientState, + dstHeader.ConsensusState(), + c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...' + ) + + if err != nil { + panic(err) + } + if err = msg.ValidateBasic(); err != nil { + panic(err) + } + return msg +} + // UpdateClient creates an sdk.Msg to update the client on src with data pulled from dst func (c *Chain) UpdateClient(dstHeader ibcexported.Header) sdk.Msg { if err := dstHeader.ValidateBasic(); err != nil { @@ -35,6 +60,19 @@ func (c *Chain) UpdateClient(dstHeader ibcexported.Header) sdk.Msg { return msg } +// ConnInit creates a MsgConnectionOpenInit +func (c *Chain) ConnInit(counterparty *PathEnd) sdk.Msg { + var version *conntypes.Version + return conntypes.NewMsgConnectionOpenInit( + c.PathEnd.ClientID, + counterparty.ClientID, + defaultChainPrefix, + version, + defaultDelayPeriod, + c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...' + ) +} + // ConnTry creates a MsgConnectionOpenTry func (c *Chain) ConnTry( counterparty *Chain, @@ -92,6 +130,28 @@ func (c *Chain) ConnAck( ), nil } +// ConnConfirm creates a MsgConnectionOpenConfirm +func (c *Chain) ConnConfirm(counterpartyConnState *conntypes.QueryConnectionResponse) sdk.Msg { + return conntypes.NewMsgConnectionOpenConfirm( + c.PathEnd.ConnectionID, + counterpartyConnState.Proof, + counterpartyConnState.ProofHeight, + c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...' + ) +} + +// ChanInit creates a MsgChannelOpenInit +func (c *Chain) ChanInit(counterparty *PathEnd) sdk.Msg { + return chantypes.NewMsgChannelOpenInit( + c.PathEnd.PortID, + c.PathEnd.Version, + c.PathEnd.GetOrder(), + []string{c.PathEnd.ConnectionID}, + counterparty.PortID, + c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...' + ) +} + // ChanTry creates a MsgChannelOpenTry func (c *Chain) ChanTry( counterparty *Chain, @@ -141,6 +201,37 @@ func (c *Chain) ChanAck( ), nil } +// ChanConfirm creates a MsgChannelOpenConfirm +func (c *Chain) ChanConfirm(dstChanState *chantypes.QueryChannelResponse) sdk.Msg { + return chantypes.NewMsgChannelOpenConfirm( + c.PathEnd.PortID, + c.PathEnd.ChannelID, + dstChanState.Proof, + dstChanState.ProofHeight, + c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...' + ) +} + +// ChanCloseInit creates a MsgChannelCloseInit +func (c *Chain) ChanCloseInit() sdk.Msg { + return chantypes.NewMsgChannelCloseInit( + c.PathEnd.PortID, + c.PathEnd.ChannelID, + c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...' + ) +} + +// ChanCloseConfirm creates a MsgChannelCloseConfirm +func (c *Chain) ChanCloseConfirm(dstChanState *chantypes.QueryChannelResponse) sdk.Msg { + return chantypes.NewMsgChannelCloseConfirm( + c.PathEnd.PortID, + c.PathEnd.ChannelID, + dstChanState.Proof, + dstChanState.ProofHeight, + c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...' + ) +} + // MsgTransfer creates a new transfer message func (c *Chain) MsgTransfer(dst *PathEnd, amount sdk.Coin, dstAddr string, timeoutHeight, timeoutTimestamp uint64) sdk.Msg { diff --git a/relayer/pathEnd.go b/relayer/pathEnd.go index 54a0aa454..2ea771e8f 100644 --- a/relayer/pathEnd.go +++ b/relayer/pathEnd.go @@ -6,16 +6,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" transfertypes "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/types" clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types" - conntypes "github.com/cosmos/cosmos-sdk/x/ibc/core/03-connection/types" chantypes "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types" commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/23-commitment/types" - tmclient "github.com/cosmos/cosmos-sdk/x/ibc/light-clients/07-tendermint/types" ) -// TODO: migrate all message construction methods to msgs.go and use the chain -// to construct them. -// https://github.com/cosmos/relayer/issues/368 - var ( defaultChainPrefix = commitmenttypes.NewMerklePrefix([]byte("ibc")) defaultDelayPeriod = uint64(0) @@ -67,97 +61,6 @@ func UnmarshalChain(pe PathEnd) *Chain { return nil } -// CreateClient creates an sdk.Msg to update the client on src with consensus state from dst -func (pe *PathEnd) CreateClient( - clientState *tmclient.ClientState, - dstHeader *tmclient.Header, - signer sdk.AccAddress) sdk.Msg { - - if err := dstHeader.ValidateBasic(); err != nil { - panic(err) - } - - msg, err := clienttypes.NewMsgCreateClient( - clientState, - dstHeader.ConsensusState(), - signer, - ) - - if err != nil { - panic(err) - } - if err = msg.ValidateBasic(); err != nil { - panic(err) - } - return msg -} - -// ConnInit creates a MsgConnectionOpenInit -func (pe *PathEnd) ConnInit(counterparty *PathEnd, signer sdk.AccAddress) sdk.Msg { - var version *conntypes.Version - return conntypes.NewMsgConnectionOpenInit( - pe.ClientID, - counterparty.ClientID, - defaultChainPrefix, - version, - defaultDelayPeriod, - signer, - ) -} - -// ConnConfirm creates a MsgConnectionOpenConfirm -func (pe *PathEnd) ConnConfirm(counterpartyConnState *conntypes.QueryConnectionResponse, signer sdk.AccAddress) sdk.Msg { - return conntypes.NewMsgConnectionOpenConfirm( - pe.ConnectionID, - counterpartyConnState.Proof, - counterpartyConnState.ProofHeight, - signer, - ) -} - -// ChanInit creates a MsgChannelOpenInit -func (pe *PathEnd) ChanInit(counterparty *PathEnd, signer sdk.AccAddress) sdk.Msg { - return chantypes.NewMsgChannelOpenInit( - pe.PortID, - pe.Version, - pe.GetOrder(), - []string{pe.ConnectionID}, - counterparty.PortID, - signer, - ) -} - -// ChanConfirm creates a MsgChannelOpenConfirm -func (pe *PathEnd) ChanConfirm(dstChanState *chantypes.QueryChannelResponse, signer sdk.AccAddress) sdk.Msg { - return chantypes.NewMsgChannelOpenConfirm( - pe.PortID, - pe.ChannelID, - dstChanState.Proof, - dstChanState.ProofHeight, - signer, - ) -} - -// ChanCloseInit creates a MsgChannelCloseInit -func (pe *PathEnd) ChanCloseInit(signer sdk.AccAddress) sdk.Msg { - return chantypes.NewMsgChannelCloseInit( - pe.PortID, - pe.ChannelID, - signer, - ) -} - -// ChanCloseConfirm creates a MsgChannelCloseConfirm -func (pe *PathEnd) ChanCloseConfirm(dstChanState *chantypes.QueryChannelResponse, signer sdk.AccAddress) sdk.Msg { - return chantypes.NewMsgChannelCloseConfirm( - pe.PortID, - pe.ChannelID, - dstChanState.Proof, - dstChanState.ProofHeight, - signer, - ) -} - // NewPacket returns a new packet from src to dist w func (pe *PathEnd) NewPacket(dst *PathEnd, sequence uint64, packetData []byte, timeoutHeight, timeoutStamp uint64) chantypes.Packet {