Skip to content

Commit

Permalink
apex bridge fw retry (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar authored May 24, 2024
1 parent fe9fd74 commit b3724a5
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 39 deletions.
3 changes: 0 additions & 3 deletions buildb.sh

This file was deleted.

2 changes: 1 addition & 1 deletion command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (

defaultSprintSize = uint64(5) // in blocks
defaultEpochReward = 1 // in blocks
defaultBlockTime = 5 * time.Second
defaultBlockTime = 3 * time.Second
defaultBlockTimeDrift = uint64(10) // in seconds
defaultBlockTrackerPollInterval = time.Second
defaultCheckpointInterval = uint64(900) // in blocks
Expand Down
14 changes: 7 additions & 7 deletions consensus/polybft/contractsapi/apex_sc_data.go

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions delete_folders.sh

This file was deleted.

8 changes: 4 additions & 4 deletions e2e-polybft/cardanofw/test-apex-bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func SetupAndRunApexBridge(

err = wallet.WaitForAmount(context.Background(), txProviderPrime, cb.PrimeMultisigAddr, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(sendAmount)) == 0
}, numOfRetries, waitTime)
}, numOfRetries, waitTime, IsRecoverableError)
require.NoError(t, err)

fmt.Printf("Prime multisig addr funded\n")
Expand All @@ -196,7 +196,7 @@ func SetupAndRunApexBridge(

err = wallet.WaitForAmount(context.Background(), txProviderPrime, cb.PrimeMultisigFeeAddr, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(sendAmount)) == 0
}, numOfRetries, waitTime)
}, numOfRetries, waitTime, IsRecoverableError)
require.NoError(t, err)

fmt.Printf("Prime multisig fee addr funded\n")
Expand All @@ -210,7 +210,7 @@ func SetupAndRunApexBridge(

err = wallet.WaitForAmount(context.Background(), txProviderVector, cb.VectorMultisigAddr, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(sendAmount)) == 0
}, numOfRetries, waitTime)
}, numOfRetries, waitTime, IsRecoverableError)
require.NoError(t, err)

fmt.Printf("Vector multisig addr funded\n")
Expand All @@ -221,7 +221,7 @@ func SetupAndRunApexBridge(

err = wallet.WaitForAmount(context.Background(), txProviderVector, cb.VectorMultisigFeeAddr, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(sendAmount)) == 0
}, numOfRetries, waitTime)
}, numOfRetries, waitTime, IsRecoverableError)
require.NoError(t, err)

fmt.Printf("Vector multisig fee addr funded\n")
Expand Down
7 changes: 4 additions & 3 deletions e2e-polybft/cardanofw/test-apex-user.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (u *TestApexUser) SendToUser(
err = wallet.WaitForAmount(
context.Background(), txProvider, addr, func(val *big.Int) bool {
return val.Cmp(prevAmount) > 0
}, 60, time.Second*2)
}, 60, time.Second*2, IsRecoverableError)
require.NoError(t, err)
}

Expand Down Expand Up @@ -114,7 +114,7 @@ func (u *TestApexUser) SendToAddress(
err = wallet.WaitForAmount(
context.Background(), txProvider, receiver, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(prevAmount.Uint64()+sendAmount)) == 0
}, 60, time.Second*2)
}, 60, time.Second*2, IsRecoverableError)
require.NoError(t, err)
}

Expand Down Expand Up @@ -199,7 +199,8 @@ func BridgeAmountFull(
sendAmount+feeAmount, multisigAddr, int(networkMagic), bridgingRequestMetadata)
require.NoError(t, err)

err = wallet.WaitForTxHashInUtxos(context.Background(), txProvider, multisigAddr, txHash, 60, time.Second*2)
err = wallet.WaitForTxHashInUtxos(
context.Background(), txProvider, multisigAddr, txHash, 60, time.Second*2, IsRecoverableError)
require.NoError(t, err)

return txHash
Expand Down
17 changes: 17 additions & 0 deletions e2e-polybft/cardanofw/test-cardano-tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ func SendTx(ctx context.Context,
receiver string,
testnetMagic int,
metadata []byte,
) (res string, err error) {
err = ExecuteWithRetryIfNeeded(ctx, func() error {
res, err = sendTx(ctx, txProvider, cardanoWallet, amount, receiver, testnetMagic, metadata)

return err
})

return res, err
}

func sendTx(ctx context.Context,
txProvider wallet.ITxProvider,
cardanoWallet wallet.IWallet,
amount uint64,
receiver string,
testnetMagic int,
metadata []byte,
) (string, error) {
cardanoWalletAddr, _, err := wallet.GetWalletAddress(cardanoWallet, uint(testnetMagic))
if err != nil {
Expand Down
39 changes: 38 additions & 1 deletion e2e-polybft/cardanofw/test-cardano-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (

const (
InvalidState = "InvalidRequest"

retryWait = time.Millisecond * 1000
retriesMaxCount = 10
)

func ResolveCardanoCliBinary() string {
Expand Down Expand Up @@ -256,10 +259,44 @@ type BridgingRequestStateResponse struct {

// GetTokenAmount returns token amount for address
func GetTokenAmount(ctx context.Context, txProvider wallet.ITxProvider, addr string) (*big.Int, error) {
utxos, err := txProvider.GetUtxos(ctx, addr)
var utxos []wallet.Utxo

err := ExecuteWithRetryIfNeeded(ctx, func() (err error) {
utxos, err = txProvider.GetUtxos(ctx, addr)

return err
})
if err != nil {
return nil, err
}

return wallet.GetUtxosSum(utxos), nil
}

// WaitForAmount waits for address to have amount specified by cmpHandler
func WaitForAmount(ctx context.Context, txRetriever wallet.IUTxORetriever,
addr string, cmpHandler func(*big.Int) bool, numRetries int, waitTime time.Duration,
) error {
return wallet.WaitForAmount(ctx, txRetriever, addr, cmpHandler, numRetries, waitTime, IsRecoverableError)
}

func ExecuteWithRetryIfNeeded(ctx context.Context, handler func() error) error {
for i := 1; ; i++ {
err := handler()
if err == nil || !IsRecoverableError(err) {
return err
} else if i == retriesMaxCount {
return fmt.Errorf("execution failed after %d retries: %w", retriesMaxCount, err)
}

select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(retryWait):
}
}
}

func IsRecoverableError(err error) bool {
return strings.Contains(err.Error(), "status code 500")
}
25 changes: 13 additions & 12 deletions e2e-polybft/e2e/apex_bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestE2E_ApexBridge(t *testing.T) {
apex.Bridge.VectorMultisigFeeAddr, sendAmount, true)

time.Sleep(time.Second * 60)
err = wallet.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmount)) == 0
}, 200, time.Second*20)
require.NoError(t, err)
Expand Down Expand Up @@ -233,7 +233,8 @@ func TestE2E_ApexBridge_InvalidScenarios(t *testing.T) {
txHash, err := cardanofw.SendTx(ctx, txProviderPrime, primeGenesisWallet,
fundSendAmount, walletAddress, apex.PrimeCluster.Config.NetworkMagic, []byte{})
require.NoError(t, err)
err = wallet.WaitForTxHashInUtxos(ctx, txProviderPrime, walletAddress, txHash, 60, time.Second*2)
err = wallet.WaitForTxHashInUtxos(ctx, txProviderPrime, walletAddress, txHash,
60, time.Second*2, cardanofw.IsRecoverableError)
require.NoError(t, err)
}

Expand Down Expand Up @@ -497,7 +498,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
expectedAmount := prevAmount.Uint64() + sendAmount
fmt.Printf("%v - expectedAmount %v\n", i+1, expectedAmount)

err = wallet.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmount)) == 0
}, 20, time.Second*10)
require.NoError(t, err)
Expand All @@ -518,7 +519,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
}

expectedAmount := prevAmount.Uint64() + uint64(instances)*sendAmount
err = wallet.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmount)) == 0
}, 20, time.Second*10)
require.NoError(t, err)
Expand Down Expand Up @@ -561,7 +562,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
wg.Wait()

expectedAmount := prevAmount.Uint64() + uint64(instances)*sendAmount
err = wallet.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmount)) == 0
}, 100, time.Second*10)
require.NoError(t, err)
Expand All @@ -587,7 +588,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
}

expectedAmount := prevAmount.Uint64() + uint64(instances)*sendAmount
err = wallet.WaitForAmount(context.Background(), txProviderPrime, user.PrimeAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderPrime, user.PrimeAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmount)) == 0
}, 20, time.Second*10)
require.NoError(t, err)
Expand Down Expand Up @@ -630,7 +631,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
wg.Wait()

expectedAmount := prevAmount.Uint64() + uint64(instances)*sendAmount
err = wallet.WaitForAmount(context.Background(), txProviderPrime, user.PrimeAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderPrime, user.PrimeAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmount)) == 0
}, 100, time.Second*10)
require.NoError(t, err)
Expand Down Expand Up @@ -687,7 +688,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
fmt.Printf("Waiting for %v TXs\n", sequentialInstances*parallelInstances)

expectedAmount := prevAmount.Uint64() + uint64(sequentialInstances)*uint64(parallelInstances)*sendAmount
err = wallet.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmount)) == 0
}, 100, time.Second*10)
require.NoError(t, err)
Expand Down Expand Up @@ -722,7 +723,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {

fmt.Printf("Waiting for %v TXs on vector\n", instances)
expectedAmountOnVector := prevAmountOnVector.Uint64() + uint64(instances)*sendAmount
err = wallet.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmountOnVector)) == 0
}, 100, time.Second*10)
require.NoError(t, err)
Expand All @@ -734,7 +735,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {

fmt.Printf("Waiting for %v TXs on prime\n", instances)
expectedAmountOnPrime := prevAmountOnPrime.Uint64() + uint64(instances)*sendAmount
err = wallet.WaitForAmount(context.Background(), txProviderPrime, user.PrimeAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderPrime, user.PrimeAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmountOnPrime)) == 0
}, 100, time.Second*10)
require.NoError(t, err)
Expand Down Expand Up @@ -823,7 +824,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
fmt.Printf("Waiting for %v TXs on vector:\n", sequentialInstances*parallelInstances)

expectedAmountOnVector := prevAmountOnVector.Uint64() + uint64(sequentialInstances)*uint64(parallelInstances)*sendAmount
err = wallet.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderVector, user.VectorAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmountOnVector)) == 0
}, 100, time.Second*10)
require.NoError(t, err)
Expand All @@ -837,7 +838,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
fmt.Printf("Waiting for %v TXs on prime\n", sequentialInstances*parallelInstances)

expectedAmountOnPrime := prevAmountOnPrime.Uint64() + uint64(sequentialInstances)*uint64(parallelInstances)*sendAmount
err = wallet.WaitForAmount(context.Background(), txProviderPrime, user.PrimeAddress, func(val *big.Int) bool {
err = cardanofw.WaitForAmount(context.Background(), txProviderPrime, user.PrimeAddress, func(val *big.Int) bool {
return val.Cmp(new(big.Int).SetUint64(expectedAmountOnPrime)) == 0
}, 100, time.Second*10)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
cloud.google.com/go/secretmanager v1.12.0
github.com/0xPolygon/go-ibft v0.4.1-0.20240429085428-e2032115c799
github.com/Ethernal-Tech/blockchain-event-tracker v0.0.0-20231202204931-b886edca635a
github.com/Ethernal-Tech/cardano-infrastructure v0.0.0-20240517134201-b5de104c54db
github.com/Ethernal-Tech/cardano-infrastructure v0.0.0-20240523142455-6d19ab77797c
github.com/Ethernal-Tech/merkle-tree v0.0.0-20231213143318-4db9da419e04
github.com/armon/go-metrics v0.4.1
github.com/aws/aws-sdk-go v1.51.18
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ github.com/DataDog/sketches-go v1.4.2 h1:gppNudE9d19cQ98RYABOetxIhpTCl4m7CnbRZjv
github.com/DataDog/sketches-go v1.4.2/go.mod h1:xJIXldczJyyjnbDop7ZZcLxJdV3+7Kra7H1KMgpgkLk=
github.com/Ethernal-Tech/blockchain-event-tracker v0.0.0-20231202204931-b886edca635a h1:IujnjiVu6UcVYhUIAaCN1ZI122VZzTO80epIbF6pDbk=
github.com/Ethernal-Tech/blockchain-event-tracker v0.0.0-20231202204931-b886edca635a/go.mod h1:IgWSrKhiPnqV6M68wQt8MyWNeWwA+3sYZQP6Y6STR2M=
github.com/Ethernal-Tech/cardano-infrastructure v0.0.0-20240517134201-b5de104c54db h1:dl5lyyFkDklD+U3eyONrAboBW/HzGfzkzla//URJPuQ=
github.com/Ethernal-Tech/cardano-infrastructure v0.0.0-20240517134201-b5de104c54db/go.mod h1:fu6FAOJ2BTQSmsU33kZTMDvtNc/PjBtPCEcV7gcdGxs=
github.com/Ethernal-Tech/cardano-infrastructure v0.0.0-20240523142455-6d19ab77797c h1:sJNj6chCY0wofT1S03f0aTE6TNWxrN3s8857PXUHjXo=
github.com/Ethernal-Tech/cardano-infrastructure v0.0.0-20240523142455-6d19ab77797c/go.mod h1:fu6FAOJ2BTQSmsU33kZTMDvtNc/PjBtPCEcV7gcdGxs=
github.com/Ethernal-Tech/merkle-tree v0.0.0-20231213143318-4db9da419e04 h1:DGIOHe3qAeU+mrRLNJ83mJWyH1t5SqN8XvTrwBUAXK4=
github.com/Ethernal-Tech/merkle-tree v0.0.0-20231213143318-4db9da419e04/go.mod h1:vwaNJ2xOVILZoOcD6CxswTg8XOCDvIhcRIp8xanmS3s=
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
Expand Down
4 changes: 2 additions & 2 deletions build-sc.sh → scripts/build-sc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ git checkout main
git fetch origin
git pull origin
if [ "$BRANCH" != "main" ]; then
PRINT "SWITCHING TO ${BRANCH}"
echo "SWITCHING TO ${BRANCH}"
git branch -D ${BRANCH}
git switch ${BRANCH}
git pull origin # this is not important but lets have it here
Expand All @@ -16,4 +16,4 @@ npm i && npx hardhat compile
cd ..
go run consensus/polybft/contractsapi/apex-artifacts-gen/main.go
go run consensus/polybft/contractsapi/bindings-gen/main.go
./buildb.sh
./scripts/buildb.sh
5 changes: 5 additions & 0 deletions scripts/buildb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

make build
mv ./blade ~/go/bin
./scripts/delete_folders.sh
4 changes: 4 additions & 0 deletions scripts/delete_folders.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

rm -r -f e2e-logs-*
rm -r -f e2e-bridge-data-tmp-*

0 comments on commit b3724a5

Please sign in to comment.