Skip to content

Commit

Permalink
Fix BroadcastSync for cosmos broadcaster (#360)
Browse files Browse the repository at this point in the history
* Fix BroadcastSync for cosmos broadcaster

* Fixing docstring
  • Loading branch information
chatton authored Jan 11, 2023
1 parent 65302bc commit 5bdad74
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
# regardless of where it was built.
ibctest.test

/bin
/bin
.idea
vendor
32 changes: 31 additions & 1 deletion chain/cosmos/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import (
"fmt"
"path"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authTx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/strangelove-ventures/ibctest/v6/internal/dockerutil"
"github.com/strangelove-ventures/ibctest/v6/testutil"
)

type ClientContextOpt func(clientContext client.Context) client.Context
Expand Down Expand Up @@ -201,5 +204,32 @@ func BroadcastTx(ctx context.Context, broadcaster *Broadcaster, broadcastingUser
return sdk.TxResponse{}, err
}

return broadcaster.UnmarshalTxResponseBytes(ctx, txBytes)
err = testutil.WaitForCondition(time.Second*30, time.Second*5, func() (bool, error) {
_, err := broadcaster.GetTxResponseBytes(ctx, broadcastingUser)
if err != nil {
return false, nil
}
return true, nil
})

if err != nil {
return sdk.TxResponse{}, err
}

txBytes, err = broadcaster.GetTxResponseBytes(ctx, broadcastingUser)
if err != nil {
return sdk.TxResponse{}, err
}

respWithTxHash, err := broadcaster.UnmarshalTxResponseBytes(ctx, txBytes)
if err != nil {
return sdk.TxResponse{}, err
}

resp, err := authTx.QueryTx(cc, respWithTxHash.TxHash)
if err != nil {
return sdk.TxResponse{}, err
}

return *resp, nil
}
25 changes: 25 additions & 0 deletions testutil/wait_for_blocks.go → testutil/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutil
import (
"context"
"fmt"
"time"

"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -112,3 +113,27 @@ func (h *height) update(height uint64) {
}
h.current = height
}

// WaitForCondition periodically executes the given function fn based on the provided pollingInterval.
// The function fn should return true of the desired condition is met. If the function never returns true within the timeoutAfter
// period, or fn returns an error, the condition will not have been met.
func WaitForCondition(timeoutAfter, pollingInterval time.Duration, fn func() (bool, error)) error {
ctx, cancel := context.WithTimeout(context.Background(), timeoutAfter)
defer cancel()

for {
select {
case <-ctx.Done():
return fmt.Errorf("failed waiting for condition after %f seconds", timeoutAfter.Seconds())
case <-time.After(pollingInterval):
reachedCondition, err := fn()
if err != nil {
return fmt.Errorf("error occurred while waiting for condition: %s", err)
}

if reachedCondition {
return nil
}
}
}
}
File renamed without changes.

0 comments on commit 5bdad74

Please sign in to comment.