diff --git a/cmd/chains.go b/cmd/chains.go index 6dbdcae166e..2783b97c803 100644 --- a/cmd/chains.go +++ b/cmd/chains.go @@ -53,13 +53,13 @@ func chainsAddrCmd() *cobra.Command { } unlock := relayer.SDKConfig.SetLock(chain) + defer unlock() addr, err := chain.GetAddress() if err != nil { return err } fmt.Println(addr.String()) - unlock() return nil }, } diff --git a/cmd/xfer.go b/cmd/xfer.go index 3c19a4c2ffb..4f3fd67e888 100644 --- a/cmd/xfer.go +++ b/cmd/xfer.go @@ -61,11 +61,11 @@ func xfersend() *cobra.Command { // --timeout-height-offset=1000 // --timeout-time-offset=2h unlock := relayer.SDKConfig.SetLock(c[dst]) + defer unlock() dstAddr, err := sdk.AccAddressFromBech32(args[3]) if err != nil { return err } - unlock() return c[src].SendTransferMsg(c[dst], amount, dstAddr) }, diff --git a/relayer/chain.go b/relayer/chain.go index f21e338394e..a86e9e54dc0 100644 --- a/relayer/chain.go +++ b/relayer/chain.go @@ -219,6 +219,7 @@ func (c *Chain) SendMsg(datagram sdk.Msg) (*sdk.TxResponse, error) { // SendMsgs wraps the msgs in a stdtx, signs and sends it func (c *Chain) SendMsgs(msgs []sdk.Msg) (res *sdk.TxResponse, err error) { unlock := SDKConfig.SetLock(c) + defer unlock() // Instantiate the client context ctx := c.CLIContext(0) @@ -257,8 +258,6 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) (res *sdk.TxResponse, err error) { return nil, err } - unlock() - // Broadcast those bytes return ctx.BroadcastTx(txBytes) } @@ -370,8 +369,8 @@ func (c *Chain) UseSDKContext() { func (c *Chain) String() string { unlock := SDKConfig.SetLock(c) + defer unlock() out, _ := json.Marshal(c) - unlock() return string(out) } diff --git a/relayer/channel-tx.go b/relayer/channel-tx.go index decf5aebb70..89c1b0a0bd4 100644 --- a/relayer/channel-tx.go +++ b/relayer/channel-tx.go @@ -59,6 +59,8 @@ func (c *Chain) CreateChannel(dst *Chain, ordered bool, to time.Duration) error // In the case of failure, increment the failures counter and exit if this is the 3rd failure case !chanSteps.success: failures++ + c.Log(fmt.Sprintf("retrying transaction...")) + time.Sleep(5 * time.Second) if failures > 2 { return fmt.Errorf("! Channel failed: [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}", c.ChainID, c.PathEnd.ChannelID, c.PathEnd.PortID, @@ -105,77 +107,69 @@ func (c *Chain) CreateChannelStep(dst *Chain, ordering chantypes.Order) (*RelayM return nil, err } + unlock := SDKConfig.SetLock(c) + defer unlock() + switch { // Handshake hasn't been started on src or dst, relay `chanOpenInit` to src case srcChan.Channel.State == chantypes.UNINITIALIZED && dstChan.Channel.State == chantypes.UNINITIALIZED: if c.debug { logChannelStates(c, dst, srcChan, dstChan) } - unlock := SDKConfig.SetLock(c) + out.Src = append(out.Src, c.PathEnd.ChanInit(dst.PathEnd, c.MustGetAddress()), ) - unlock() // Handshake has started on dst (1 step done), relay `chanOpenTry` and `updateClient` to src case srcChan.Channel.State == chantypes.UNINITIALIZED && dstChan.Channel.State == chantypes.INIT: if c.debug { logChannelStates(c, dst, srcChan, dstChan) } - unlock := SDKConfig.SetLock(c) out.Src = append(out.Src, c.PathEnd.UpdateClient(dstUpdateHeader, c.MustGetAddress()), c.PathEnd.ChanTry(dst.PathEnd, dstChan, c.MustGetAddress()), ) - unlock() // Handshake has started on src (1 step done), relay `chanOpenTry` and `updateClient` to dst case srcChan.Channel.State == chantypes.INIT && dstChan.Channel.State == chantypes.UNINITIALIZED: if dst.debug { logChannelStates(dst, c, dstChan, srcChan) } - unlock := SDKConfig.SetLock(dst) out.Dst = append(out.Dst, dst.PathEnd.UpdateClient(srcUpdateHeader, dst.MustGetAddress()), dst.PathEnd.ChanTry(c.PathEnd, srcChan, dst.MustGetAddress()), ) - unlock() // Handshake has started on src (2 steps done), relay `chanOpenAck` and `updateClient` to dst case srcChan.Channel.State == chantypes.TRYOPEN && dstChan.Channel.State == chantypes.INIT: if dst.debug { logChannelStates(dst, c, dstChan, srcChan) } - unlock := SDKConfig.SetLock(dst) out.Dst = append(out.Dst, dst.PathEnd.UpdateClient(srcUpdateHeader, dst.MustGetAddress()), dst.PathEnd.ChanAck(c.PathEnd, srcChan, dst.MustGetAddress()), ) - unlock() // Handshake has started on dst (2 steps done), relay `chanOpenAck` and `updateClient` to src case srcChan.Channel.State == chantypes.INIT && dstChan.Channel.State == chantypes.TRYOPEN: if c.debug { logChannelStates(c, dst, srcChan, dstChan) } - unlock := SDKConfig.SetLock(c) out.Src = append(out.Src, c.PathEnd.UpdateClient(dstUpdateHeader, c.MustGetAddress()), c.PathEnd.ChanAck(dst.PathEnd, dstChan, c.MustGetAddress()), ) - unlock() // Handshake has confirmed on dst (3 steps done), relay `chanOpenConfirm` and `updateClient` to src case srcChan.Channel.State == chantypes.TRYOPEN && dstChan.Channel.State == chantypes.OPEN: if c.debug { logChannelStates(c, dst, srcChan, dstChan) } - unlock := SDKConfig.SetLock(c) out.Src = append(out.Src, c.PathEnd.UpdateClient(dstUpdateHeader, c.MustGetAddress()), c.PathEnd.ChanConfirm(dstChan, c.MustGetAddress()), ) - unlock() out.last = true // Handshake has confirmed on src (3 steps done), relay `chanOpenConfirm` and `updateClient` to dst @@ -183,12 +177,10 @@ func (c *Chain) CreateChannelStep(dst *Chain, ordering chantypes.Order) (*RelayM if dst.debug { logChannelStates(dst, c, dstChan, srcChan) } - unlock := SDKConfig.SetLock(dst) out.Dst = append(out.Dst, dst.PathEnd.UpdateClient(srcUpdateHeader, dst.MustGetAddress()), dst.PathEnd.ChanConfirm(srcChan, dst.MustGetAddress()), ) - unlock() out.last = true } diff --git a/relayer/connection-tx.go b/relayer/connection-tx.go index 7345b84784d..81e166d3cea 100644 --- a/relayer/connection-tx.go +++ b/relayer/connection-tx.go @@ -56,6 +56,8 @@ func (c *Chain) CreateConnection(dst *Chain, to time.Duration) error { // In the case of failure, increment the failures counter and exit if this is the 3rd failure case !connSteps.success: failed++ + c.Log(fmt.Sprintf("retrying transaction...")) + time.Sleep(5 * time.Second) if failed > 2 { return fmt.Errorf("! Connection failed: [%s]client{%s}conn{%s} -> [%s]client{%s}conn{%s}", c.ChainID, c.PathEnd.ClientID, c.PathEnd.ConnectionID, @@ -144,15 +146,16 @@ func (c *Chain) CreateConnectionStep(dst *Chain) (*RelayMsgs, error) { } } + unlock := SDKConfig.SetLock(c) + defer unlock() + switch { // Handshake hasn't been started on src or dst, relay `connOpenInit` to src case srcConn.Connection.State == conntypes.UNINITIALIZED && dstConn.Connection.State == conntypes.UNINITIALIZED: if c.debug { logConnectionStates(c, dst, srcConn, dstConn) } - unlock := SDKConfig.SetLock(c) out.Src = append(out.Src, c.PathEnd.ConnInit(dst.PathEnd, c.MustGetAddress())) - unlock() // Handshake has started on dst (1 stepdone), relay `connOpenTry` and `updateClient` on src case srcConn.Connection.State == conntypes.UNINITIALIZED && dstConn.Connection.State == conntypes.INIT: @@ -160,12 +163,10 @@ func (c *Chain) CreateConnectionStep(dst *Chain) (*RelayMsgs, error) { logConnectionStates(c, dst, srcConn, dstConn) } - unlock := SDKConfig.SetLock(c) out.Src = append(out.Src, c.PathEnd.UpdateClient(dstUpdateHeader, c.MustGetAddress()), c.PathEnd.ConnTry(dst.PathEnd, dstCsRes, dstConn, dstCons, c.MustGetAddress()), ) - unlock() // Handshake has started on src (1 step done), relay `connOpenTry` and `updateClient` on dst case srcConn.Connection.State == conntypes.INIT && dstConn.Connection.State == conntypes.UNINITIALIZED: @@ -173,12 +174,10 @@ func (c *Chain) CreateConnectionStep(dst *Chain) (*RelayMsgs, error) { logConnectionStates(dst, c, dstConn, srcConn) } - unlock := SDKConfig.SetLock(dst) out.Dst = append(out.Dst, dst.PathEnd.UpdateClient(srcUpdateHeader, dst.MustGetAddress()), dst.PathEnd.ConnTry(c.PathEnd, srcCsRes, srcConn, srcCons, dst.MustGetAddress()), ) - unlock() // Handshake has started on src end (2 steps done), relay `connOpenAck` and `updateClient` to dst end case srcConn.Connection.State == conntypes.TRYOPEN && dstConn.Connection.State == conntypes.INIT: @@ -186,36 +185,30 @@ func (c *Chain) CreateConnectionStep(dst *Chain) (*RelayMsgs, error) { logConnectionStates(dst, c, dstConn, srcConn) } - unlock := SDKConfig.SetLock(dst) out.Dst = append(out.Dst, dst.PathEnd.UpdateClient(srcUpdateHeader, dst.MustGetAddress()), dst.PathEnd.ConnAck(c.PathEnd, srcCsRes, srcConn, srcCons, dst.MustGetAddress()), ) - unlock() // Handshake has started on dst end (2 steps done), relay `connOpenAck` and `updateClient` to src end case srcConn.Connection.State == conntypes.INIT && dstConn.Connection.State == conntypes.TRYOPEN: if c.debug { logConnectionStates(c, dst, srcConn, dstConn) } - unlock := SDKConfig.SetLock(c) out.Src = append(out.Src, c.PathEnd.UpdateClient(dstUpdateHeader, c.MustGetAddress()), c.PathEnd.ConnAck(dst.PathEnd, dstCsRes, dstConn, dstCons, c.MustGetAddress()), ) - unlock() // Handshake has confirmed on dst (3 steps done), relay `connOpenConfirm` and `updateClient` to src end case srcConn.Connection.State == conntypes.TRYOPEN && dstConn.Connection.State == conntypes.OPEN: if c.debug { logConnectionStates(c, dst, srcConn, dstConn) } - unlock := SDKConfig.SetLock(c) out.Src = append(out.Src, c.PathEnd.UpdateClient(dstUpdateHeader, c.MustGetAddress()), c.PathEnd.ConnConfirm(dstConn, c.MustGetAddress()), ) - unlock() out.last = true // Handshake has confirmed on src (3 steps done), relay `connOpenConfirm` and `updateClient` to dst end @@ -223,12 +216,10 @@ func (c *Chain) CreateConnectionStep(dst *Chain) (*RelayMsgs, error) { if dst.debug { logConnectionStates(dst, c, dstConn, srcConn) } - unlock := SDKConfig.SetLock(dst) out.Dst = append(out.Dst, dst.PathEnd.UpdateClient(srcUpdateHeader, dst.MustGetAddress()), dst.PathEnd.ConnConfirm(srcConn, dst.MustGetAddress()), ) - unlock() out.last = true } diff --git a/relayer/faucet.go b/relayer/faucet.go index 24d7cc18a2e..661ce21392a 100644 --- a/relayer/faucet.go +++ b/relayer/faucet.go @@ -55,12 +55,12 @@ func (c *Chain) FaucetHandler(fromKey sdk.AccAddress, amounts sdk.Coins) func(w } unlock := SDKConfig.SetLock(c) + defer unlock() if err := c.faucetSend(fromKey, fr.addr(), amounts); err != nil { c.Error(err) respondWithError(w, http.StatusInternalServerError, err.Error()) return } - unlock() c.Log(fmt.Sprintf("%s was sent %s successfully", fr.Address, amounts.String())) respondWithJSON(w, http.StatusCreated, success{Address: fr.Address, Amount: amounts.String()}) diff --git a/relayer/naive-strategy.go b/relayer/naive-strategy.go index f5cd26e911c..043ab7887b1 100644 --- a/relayer/naive-strategy.go +++ b/relayer/naive-strategy.go @@ -237,6 +237,8 @@ func (nrs *NaiveStrategy) sendTxFromEventPackets(src, dst *Chain, rlyPackets []r } // instantiate the RelayMsgs with the appropriate update client unlock := SDKConfig.SetLock(src) + defer unlock() + txs := &RelayMsgs{ Src: []sdk.Msg{ src.PathEnd.UpdateClient(updateHeader, src.MustGetAddress()), @@ -245,7 +247,6 @@ func (nrs *NaiveStrategy) sendTxFromEventPackets(src, dst *Chain, rlyPackets []r MaxTxSize: nrs.MaxTxSize, MaxMsgLength: nrs.MaxMsgLength, } - unlock() // add the packet msgs to RelayPackets for _, rp := range rlyPackets { diff --git a/relayer/query.go b/relayer/query.go index e2ba68d0648..a9691c1b140 100644 --- a/relayer/query.go +++ b/relayer/query.go @@ -341,6 +341,7 @@ func (c *Chain) QueryHistoricalInfo(height clienttypes.Height) (*stakingtypes.Qu // QueryValsetAtHeight returns the validator set at a given height func (c *Chain) QueryValsetAtHeight(height clienttypes.Height) (*tmproto.ValidatorSet, error) { unlock := SDKConfig.SetLock(c) + defer unlock() res, err := c.QueryHistoricalInfo(height) if err != nil { return nil, err @@ -354,7 +355,6 @@ func (c *Chain) QueryValsetAtHeight(height clienttypes.Height) (*tmproto.Validat Validators: tmVals, } tmValSet.GetProposer() - unlock() return tmValSet.ToProto() } diff --git a/relayer/relayPackets.go b/relayer/relayPackets.go index 95dbdb88247..b5068e8f466 100644 --- a/relayer/relayPackets.go +++ b/relayer/relayPackets.go @@ -71,6 +71,7 @@ func (rp *relayMsgTimeout) Msg(src, dst *Chain) sdk.Msg { return nil } unlock := SDKConfig.SetLock(src) + defer unlock() version := clienttypes.ParseChainID(src.PathEnd.ChainID) msg := chantypes.NewMsgTimeout( chantypes.NewPacket( @@ -88,7 +89,6 @@ func (rp *relayMsgTimeout) Msg(src, dst *Chain) sdk.Msg { rp.dstRecvRes.ProofHeight, src.MustGetAddress(), ) - unlock() return msg } @@ -165,13 +165,13 @@ func (rp *relayMsgRecvPacket) Msg(src, dst *Chain) sdk.Msg { rp.timeoutStamp, ) unlock := SDKConfig.SetLock(src) + defer unlock() msg := chantypes.NewMsgRecvPacket( packet, rp.dstComRes.Proof, rp.dstComRes.ProofHeight, src.MustGetAddress(), ) - unlock() return msg } @@ -196,6 +196,7 @@ func (rp *relayMsgPacketAck) Timeout() uint64 { func (rp *relayMsgPacketAck) Msg(src, dst *Chain) sdk.Msg { unlock := SDKConfig.SetLock(src) + defer unlock() version := clienttypes.ParseChainID(dst.PathEnd.ChainID) msg := chantypes.NewMsgAcknowledgement( chantypes.NewPacket( @@ -213,7 +214,6 @@ func (rp *relayMsgPacketAck) Msg(src, dst *Chain) sdk.Msg { rp.dstComRes.ProofHeight, src.MustGetAddress(), ) - unlock() return msg }