Skip to content

Commit

Permalink
DRY handshake code (#424)
Browse files Browse the repository at this point in the history
* DRY handshake code

* fix bug

* update first
  • Loading branch information
colin-axner committed Feb 12, 2021
1 parent 81dd43c commit 067f477
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 116 deletions.
69 changes: 17 additions & 52 deletions relayer/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,20 @@ func ExecuteChannelStep(src, dst *Chain) (success, last, modified bool, err erro
msgs []sdk.Msg
)

if _, _, err := UpdateLightClients(src, dst); err != nil {
srcUpdateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, false, err
}

dstUpdateMsg, err := dst.UpdateClient(src)
if err != nil {
return false, false, false, err
}

// if either identifier is missing, an existing channel that matches the required fields
// is chosen or a new channel is created.
if src.PathEnd.ChannelID == "" || dst.PathEnd.ChannelID == "" {
success, modified, err := InitializeChannel(src, dst)
success, modified, err := InitializeChannel(src, dst, srcUpdateMsg, dstUpdateMsg)
if err != nil {
return false, false, false, err
}
Expand Down Expand Up @@ -122,13 +128,8 @@ func ExecuteChannelStep(src, dst *Chain) (success, last, modified bool, err erro
return false, false, false, err
}

updateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, false, err
}

msgs = []sdk.Msg{
updateMsg,
srcUpdateMsg,
openTry,
}

Expand All @@ -151,13 +152,8 @@ func ExecuteChannelStep(src, dst *Chain) (success, last, modified bool, err erro
return false, false, false, err
}

updateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, false, err
}

msgs = []sdk.Msg{
updateMsg,
srcUpdateMsg,
openAck,
}

Expand All @@ -179,13 +175,8 @@ func ExecuteChannelStep(src, dst *Chain) (success, last, modified bool, err erro
return false, false, false, err
}

updateMsg, err := dst.UpdateClient(src)
if err != nil {
return false, false, false, err
}

msgs = []sdk.Msg{
updateMsg,
dstUpdateMsg,
openAck,
}

Expand All @@ -200,13 +191,8 @@ func ExecuteChannelStep(src, dst *Chain) (success, last, modified bool, err erro
logChannelStates(src, dst, srcChan, dstChan)
}

updateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, false, err
}

msgs = []sdk.Msg{
updateMsg,
srcUpdateMsg,
src.ChanConfirm(dstChan),
}
last = true
Expand All @@ -222,13 +208,8 @@ func ExecuteChannelStep(src, dst *Chain) (success, last, modified bool, err erro
logChannelStates(dst, src, dstChan, srcChan)
}

updateMsg, err := dst.UpdateClient(src)
if err != nil {
return false, false, false, err
}

msgs = []sdk.Msg{
updateMsg,
dstUpdateMsg,
dst.ChanConfirm(srcChan),
}
last = true
Expand All @@ -251,7 +232,7 @@ func ExecuteChannelStep(src, dst *Chain) (success, last, modified bool, err erro
// initialized. The PathEnds are updated upon a successful transaction.
// NOTE: This function may need to be called twice if neither channel exists.
func InitializeChannel(
src, dst *Chain,
src, dst *Chain, srcUpdateMsg, dstUpdateMsg sdk.Msg,
) (success, modified bool, err error) {
switch {

Expand All @@ -266,14 +247,8 @@ func InitializeChannel(
channelID, found := FindMatchingChannel(src, dst)
if !found {
// construct OpenInit message to be submitted on source chain

updateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, err
}

msgs := []sdk.Msg{
updateMsg,
srcUpdateMsg,
src.ChanInit(dst.PathEnd),
}

Expand Down Expand Up @@ -309,13 +284,8 @@ func InitializeChannel(
return false, false, err
}

updateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, err
}

msgs := []sdk.Msg{
updateMsg,
srcUpdateMsg,
openTry,
}
res, success, err := src.SendMsgs(msgs)
Expand Down Expand Up @@ -350,13 +320,8 @@ func InitializeChannel(
return false, false, err
}

updateMsg, err := dst.UpdateClient(src)
if err != nil {
return false, false, err
}

msgs := []sdk.Msg{
updateMsg,
dstUpdateMsg,
openTry,
}
res, success, err := dst.SendMsgs(msgs)
Expand Down
74 changes: 23 additions & 51 deletions relayer/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,19 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e
msgs []sdk.Msg
)

if _, _, err := UpdateLightClients(src, dst); err != nil {
// NOTE: proof construction for handshake messages
// relies on delivery of the associated update message.
// Updating the light client again could result in
// failed handshakes since the proof height would
// rely on a consensus state that has not been committed
// to the chain.
srcUpdateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, false, err
}

dstUpdateMsg, err := dst.UpdateClient(src)
if err != nil {
return false, false, false, err
}

Expand All @@ -92,7 +104,7 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e
// is chosen or a new connection is created.
// This will perform either an OpenInit or OpenTry step and return
if src.PathEnd.ConnectionID == "" || dst.PathEnd.ConnectionID == "" {
success, modified, err := InitializeConnection(src, dst)
success, modified, err := InitializeConnection(src, dst, srcUpdateMsg, dstUpdateMsg)
if err != nil {
return false, false, false, err
}
Expand Down Expand Up @@ -122,13 +134,8 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e
return false, false, false, err
}

updateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, false, err
}

msgs = []sdk.Msg{
updateMsg,
srcUpdateMsg,
openTry,
}
_, success, err = src.SendMsgs(msgs)
Expand All @@ -150,13 +157,8 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e
return false, false, false, err
}

updateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, false, err
}

msgs = []sdk.Msg{
updateMsg,
srcUpdateMsg,
openAck,
}
_, success, err = src.SendMsgs(msgs)
Expand All @@ -177,13 +179,8 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e
return false, false, false, err
}

updateMsg, err := dst.UpdateClient(src)
if err != nil {
return false, false, false, err
}

msgs = []sdk.Msg{
updateMsg,
dstUpdateMsg,
openAck,
}
_, success, err = dst.SendMsgs(msgs)
Expand All @@ -197,13 +194,8 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e
logConnectionStates(src, dst, srcConn, dstConn)
}

updateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, false, err
}

msgs = []sdk.Msg{
updateMsg,
srcUpdateMsg,
src.ConnConfirm(dstConn),
}
_, success, err = src.SendMsgs(msgs)
Expand All @@ -219,13 +211,8 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e
logConnectionStates(dst, src, dstConn, srcConn)
}

updateMsg, err := dst.UpdateClient(src)
if err != nil {
return false, false, false, err
}

msgs = []sdk.Msg{
updateMsg,
dstUpdateMsg,
dst.ConnConfirm(srcConn),
}
last = true
Expand All @@ -247,7 +234,7 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e
// initialized. The PathEnds are updated upon a successful transaction.
// NOTE: This function may need to be called twice if neither connection exists.
func InitializeConnection(
src, dst *Chain,
src, dst *Chain, srcUpdateMsg, dstUpdateMsg sdk.Msg,
) (success, modified bool, err error) {
switch {

Expand All @@ -263,13 +250,8 @@ func InitializeConnection(
if !found {
// construct OpenInit message to be submitted on source chain

updateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, err
}

msgs := []sdk.Msg{
updateMsg,
srcUpdateMsg,
src.ConnInit(dst.PathEnd),
}

Expand Down Expand Up @@ -304,13 +286,8 @@ func InitializeConnection(
return false, false, err
}

updateMsg, err := src.UpdateClient(dst)
if err != nil {
return false, false, err
}

msgs := []sdk.Msg{
updateMsg,
srcUpdateMsg,
openTry,
}
res, success, err := src.SendMsgs(msgs)
Expand Down Expand Up @@ -344,13 +321,8 @@ func InitializeConnection(
return false, false, err
}

updateMsg, err := dst.UpdateClient(src)
if err != nil {
return false, false, err
}

msgs := []sdk.Msg{
updateMsg,
dstUpdateMsg,
openTry,
}
res, success, err := dst.SendMsgs(msgs)
Expand Down
25 changes: 12 additions & 13 deletions relayer/naive-strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,15 @@ func (nrs *NaiveStrategy) RelayAcknowledgements(src, dst *Chain, sp *RelaySequen
MaxTxSize: nrs.MaxTxSize,
MaxMsgLength: nrs.MaxMsgLength,
}
srcUpdateMsg, err := src.UpdateClient(dst)
if err != nil {
return err
}

dstUpdateMsg, err := dst.UpdateClient(src)
if err != nil {
return err
}

// add messages for sequences on src
for _, seq := range sp.Src {
Expand Down Expand Up @@ -446,21 +455,11 @@ func (nrs *NaiveStrategy) RelayAcknowledgements(src, dst *Chain, sp *RelaySequen

// Prepend non-empty msg lists with UpdateClient
if len(msgs.Dst) != 0 {
updateMsg, err := dst.UpdateClient(src)
if err != nil {
return err
}

msgs.Dst = append([]sdk.Msg{updateMsg}, msgs.Dst...)
msgs.Dst = append([]sdk.Msg{dstUpdateMsg}, msgs.Dst...)
}

if len(msgs.Src) != 0 {
updateMsg, err := src.UpdateClient(dst)
if err != nil {
return err
}

msgs.Src = append([]sdk.Msg{updateMsg}, msgs.Src...)
msgs.Src = append([]sdk.Msg{srcUpdateMsg}, msgs.Src...)
}

// send messages to their respective chains
Expand Down Expand Up @@ -563,7 +562,7 @@ func (nrs *NaiveStrategy) RelayPackets(src, dst *Chain, sp *RelaySequences) erro
}

if len(msgs.Src) != 0 {
updateMsg, err := dst.UpdateClient(src)
updateMsg, err := src.UpdateClient(dst)
if err != nil {
return err
}
Expand Down

0 comments on commit 067f477

Please sign in to comment.