Skip to content

Commit

Permalink
add retryable
Browse files Browse the repository at this point in the history
  • Loading branch information
tuantran1702 committed Jul 7, 2024
1 parent a40e23b commit 47e5de2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
3 changes: 2 additions & 1 deletion interchaintest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
cosmossdk.io/math v1.3.0
github.com/CosmWasm/wasmd v0.45.0
github.com/CosmosContracts/juno/v23 v23.0.0-00010101000000-000000000000
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/cosmos/cosmos-sdk v0.47.12
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.6.0
Expand Down Expand Up @@ -78,7 +79,7 @@ require (
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/bufbuild/protocompile v0.5.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cockroachdb/errors v1.11.1 // indirect
Expand Down
21 changes: 18 additions & 3 deletions interchaintest/helpers/cosmwasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"testing"
"time"

"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
Expand All @@ -12,9 +13,17 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cenkalti/backoff/v4"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)

var backoffPolicy = &backoff.ExponentialBackOff{
MaxElapsedTime: time.Minute,
InitialInterval: 500 * time.Millisecond,
Multiplier: 1.5,
MaxInterval: 10 * time.Second,
}

func SmartQueryString(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, contractAddr, queryMsg string, res interface{}) error {
var jsonMap map[string]interface{}
if err := json.Unmarshal([]byte(queryMsg), &jsonMap); err != nil {
Expand All @@ -24,10 +33,16 @@ func SmartQueryString(t *testing.T, ctx context.Context, chain *cosmos.CosmosCha
return err
}

func StoreContract(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, keyname string, fileLoc string) (codeId string) {
codeId, err := chain.StoreContract(ctx, keyname, fileLoc)
func StoreContract(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, keyname, fileLoc string) string {
var codeId string
err := backoff.Retry(func() error {
var err error
codeId, err = chain.StoreContract(ctx, keyname, fileLoc)
return err
}, backoffPolicy)

if err != nil {
t.Fatal(err)
t.Fatalf("Failed to store contract: %v", err)
}
return codeId
}
Expand Down
45 changes: 29 additions & 16 deletions interchaintest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"fmt"
"testing"
"time"

sdkmath "cosmossdk.io/math"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cenkalti/backoff"
"github.com/docker/docker/client"
interchaintest "github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
Expand All @@ -26,6 +28,13 @@ import (
tokenfactorytypes "github.com/CosmosContracts/juno/v23/x/tokenfactory/types"
)

var backoffPolicy = &backoff.ExponentialBackOff{
MaxElapsedTime: 5 * time.Minute,
InitialInterval: 1 * time.Second,
Multiplier: 2,
MaxInterval: 30 * time.Second,
}

var (
VotingPeriod = "15s"
MaxDepositPeriod = "10s"
Expand Down Expand Up @@ -123,22 +132,26 @@ func CreateThisBranchChain(t *testing.T, numVals, numFull int) []ibc.Chain {
}

func CreateChainWithCustomConfig(t *testing.T, numVals, numFull int, config ibc.ChainConfig) []ibc.Chain {
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
Name: "juno",
ChainName: "juno",
Version: config.Images[0].Version,
ChainConfig: config,
NumValidators: &numVals,
NumFullNodes: &numFull,
},
})

// Get chains from the chain factory
chains, err := cf.Chains(t.Name())
require.NoError(t, err)

// chain := chains[0].(*cosmos.CosmosChain)
var chains []ibc.Chain

err := backoff.Retry(func() error {
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
Name: "juno",
ChainName: "juno",
Version: config.Images[0].Version,
ChainConfig: config,
NumValidators: &numVals,
NumFullNodes: &numFull,
},
})

var err error
chains, err = cf.Chains(t.Name())
return err
}, backoffPolicy)

require.NoError(t, err, "Failed to create chain after multiple attempts")
return chains
}

Expand Down

0 comments on commit 47e5de2

Please sign in to comment.