Skip to content

Commit

Permalink
Properly Unlock SDKConfig (cosmos#309)
Browse files Browse the repository at this point in the history
* properly unlock everywhere

* add sleep before retrying
  • Loading branch information
AdityaSripal authored Oct 21, 2020
1 parent 6dd5cd4 commit 08f544c
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/xfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
5 changes: 2 additions & 3 deletions relayer/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand Down
20 changes: 6 additions & 14 deletions relayer/channel-tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -105,90 +107,80 @@ 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
case srcChan.Channel.State == chantypes.OPEN && dstChan.Channel.State == chantypes.TRYOPEN:
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
}

Expand Down
19 changes: 5 additions & 14 deletions relayer/connection-tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -144,91 +146,80 @@ 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:
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.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:
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.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:
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.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
case srcConn.Connection.State == conntypes.OPEN && dstConn.Connection.State == conntypes.TRYOPEN:
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
}

Expand Down
2 changes: 1 addition & 1 deletion relayer/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand Down
3 changes: 2 additions & 1 deletion relayer/naive-strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion relayer/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -354,7 +355,6 @@ func (c *Chain) QueryValsetAtHeight(height clienttypes.Height) (*tmproto.Validat
Validators: tmVals,
}
tmValSet.GetProposer()
unlock()

return tmValSet.ToProto()
}
Expand Down
6 changes: 3 additions & 3 deletions relayer/relayPackets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -88,7 +89,6 @@ func (rp *relayMsgTimeout) Msg(src, dst *Chain) sdk.Msg {
rp.dstRecvRes.ProofHeight,
src.MustGetAddress(),
)
unlock()
return msg
}

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

Expand All @@ -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(
Expand All @@ -213,7 +214,6 @@ func (rp *relayMsgPacketAck) Msg(src, dst *Chain) sdk.Msg {
rp.dstComRes.ProofHeight,
src.MustGetAddress(),
)
unlock()
return msg
}

Expand Down

0 comments on commit 08f544c

Please sign in to comment.