Skip to content

Commit

Permalink
Moved big tests to separate function and added condition for running
Browse files Browse the repository at this point in the history
  • Loading branch information
sasa-ethernal committed Jun 12, 2024
1 parent 4f5d35d commit 8bb15dd
Showing 1 changed file with 194 additions and 151 deletions.
345 changes: 194 additions & 151 deletions e2e-polybft/e2e/apex_bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,196 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
fmt.Printf("prevAmount: %v. newAmount: %v\n", prevAmount, newAmount)
})

t.Run("Both directions sequential", func(t *testing.T) {
instances := 5
sendAmount := uint64(1_000_000)

for i := 0; i < instances; i++ {
primeTxHash := user.BridgeAmount(t, ctx, txProviderPrime, apex.Bridge.PrimeMultisigAddr,
apex.Bridge.VectorMultisigFeeAddr, sendAmount, true)

fmt.Printf("prime tx %v sent. hash: %s\n", i+1, primeTxHash)

vectorTxHash := user.BridgeAmount(t, ctx, txProviderVector, apex.Bridge.VectorMultisigAddr,
apex.Bridge.PrimeMultisigFeeAddr, sendAmount, false)

fmt.Printf("vector tx %v sent. hash: %s\n", i+1, vectorTxHash)
}

prevAmountOnVector, err := cardanofw.GetTokenAmount(ctx, txProviderVector, user.VectorAddress)
require.NoError(t, err)
prevAmountOnPrime, err := cardanofw.GetTokenAmount(ctx, txProviderPrime, user.PrimeAddress)
require.NoError(t, err)

fmt.Printf("Waiting for %v TXs on vector\n", instances)
expectedAmountOnVector := prevAmountOnVector.Uint64() + uint64(instances)*sendAmount
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)

newAmountOnVector, err := cardanofw.GetTokenAmount(ctx, txProviderVector, user.VectorAddress)
require.NoError(t, err)

fmt.Printf("on vector: prevAmount: %v. newAmount: %v\n", prevAmountOnVector, newAmountOnVector)

fmt.Printf("Waiting for %v TXs on prime\n", instances)
expectedAmountOnPrime := prevAmountOnPrime.Uint64() + uint64(instances)*sendAmount
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)

newAmountOnPrime, err := cardanofw.GetTokenAmount(ctx, txProviderPrime, user.PrimeAddress)
require.NoError(t, err)

fmt.Printf("on prime: prevAmount: %v. newAmount: %v\n", prevAmountOnPrime, newAmountOnPrime)
})

t.Run("Both directions sequential and parallel", func(t *testing.T) {
sequentialInstances := 5
parallelInstances := 6

primeWalletKeys := make([]wallet.IWallet, parallelInstances)

for i := 0; i < parallelInstances; i++ {
primeWalletKeys[i], err = wallet.NewStakeWalletManager().Create(path.Join(apex.PrimeCluster.Config.Dir("keys")), true)
require.NoError(t, err)

walletAddress, _, err := wallet.GetWalletAddressCli(primeWalletKeys[i], uint(apex.PrimeCluster.Config.NetworkMagic))
require.NoError(t, err)

fundSendAmount := uint64(5_000_000)
user.SendToAddress(t, ctx, txProviderPrime, primeGenesisWallet, uint64(sequentialInstances)*fundSendAmount, walletAddress, true)
}

fmt.Printf("Funded %v prime wallets \n", parallelInstances)

vectorWalletKeys := make([]wallet.IWallet, parallelInstances)

for i := 0; i < parallelInstances; i++ {
vectorWalletKeys[i], err = wallet.NewStakeWalletManager().Create(path.Join(apex.VectorCluster.Config.Dir("keys")), true)
require.NoError(t, err)

walletAddress, _, err := wallet.GetWalletAddressCli(vectorWalletKeys[i], uint(apex.VectorCluster.Config.NetworkMagic))
require.NoError(t, err)

fundSendAmount := uint64(5_000_000)
user.SendToAddress(t, ctx, txProviderVector, vectorGenesisWallet, uint64(sequentialInstances)*fundSendAmount, walletAddress, false)
}

fmt.Printf("Funded %v vector wallets \n", parallelInstances)

prevAmountOnVector, err := cardanofw.GetTokenAmount(ctx, txProviderVector, user.VectorAddress)
require.NoError(t, err)
prevAmountOnPrime, err := cardanofw.GetTokenAmount(ctx, txProviderPrime, user.PrimeAddress)
require.NoError(t, err)

sendAmount := uint64(1_000_000)

for j := 0; j < sequentialInstances; j++ {
var wg sync.WaitGroup

for i := 0; i < parallelInstances; i++ {
wg.Add(1)

go func(run, idx int) {
defer wg.Done()

txHash := cardanofw.BridgeAmountFull(t, ctx, txProviderPrime, uint(apex.PrimeCluster.Config.NetworkMagic),
apex.Bridge.PrimeMultisigAddr, apex.Bridge.VectorMultisigFeeAddr, primeWalletKeys[idx], user.VectorAddress, sendAmount,
cardanofw.GetDestinationChainID(true))
fmt.Printf("run: %v. Prime tx %v sent. hash: %s\n", run+1, idx+1, txHash)
}(j, i)
}

wg.Wait()

for i := 0; i < parallelInstances; i++ {
wg.Add(1)

go func(run, idx int) {
defer wg.Done()

txHash := cardanofw.BridgeAmountFull(t, ctx, txProviderVector, uint(apex.VectorCluster.Config.NetworkMagic),
apex.Bridge.VectorMultisigAddr, apex.Bridge.PrimeMultisigFeeAddr, vectorWalletKeys[idx], user.PrimeAddress, sendAmount,
cardanofw.GetDestinationChainID(false))
fmt.Printf("run: %v. Vector tx %v sent. hash: %s\n", run+1, idx+1, txHash)
}(j, i)
}

wg.Wait()
}

fmt.Printf("Waiting for %v TXs on vector:\n", sequentialInstances*parallelInstances)

expectedAmountOnVector := prevAmountOnVector.Uint64() + uint64(sequentialInstances)*uint64(parallelInstances)*sendAmount
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)
fmt.Printf("%v TXs on vector confirmed\n", sequentialInstances*parallelInstances)

newAmountOnVector, err := cardanofw.GetTokenAmount(ctx, txProviderVector, user.VectorAddress)
require.NoError(t, err)

fmt.Printf("on vector: prevAmount: %v. newAmount: %v\n", prevAmountOnVector, newAmountOnVector)

fmt.Printf("Waiting for %v TXs on prime\n", sequentialInstances*parallelInstances)

expectedAmountOnPrime := prevAmountOnPrime.Uint64() + uint64(sequentialInstances)*uint64(parallelInstances)*sendAmount
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)
fmt.Printf("%v TXs on prime confirmed\n", sequentialInstances*parallelInstances)

newAmountOnPrime, err := cardanofw.GetTokenAmount(ctx, txProviderPrime, user.PrimeAddress)
require.NoError(t, err)

fmt.Printf("on prime: prevAmount: %v. newAmount: %v\n", prevAmountOnPrime, newAmountOnPrime)
})
}

func TestE2E_ApexBridge_ValidScenarios_BigTests(t *testing.T) {
if shouldRun := os.Getenv("RUN_E2E_BIG_TESTS"); shouldRun != "true" {
t.Skip()
}

const (
apiKey = "test_api_key"
)

ctx, cncl := context.WithCancel(context.Background())
defer cncl()

apex := cardanofw.RunApexBridge(
t, ctx,
cardanofw.WithAPIKey(apiKey),
)
user := apex.CreateAndFundUser(t, ctx, uint64(20_000_000_000))

txProviderPrime := apex.GetPrimeTxProvider()
txProviderVector := apex.GetVectorTxProvider()

primeGenesisWallet := apex.GetPrimeGenesisWallet(t)
vectorGenesisWallet := apex.GetVectorGenesisWallet(t)

primeGenesisAddress, _, err := wallet.GetWalletAddressCli(primeGenesisWallet, uint(apex.PrimeCluster.Config.NetworkMagic))
require.NoError(t, err)

vectorGenesisAddress, _, err := wallet.GetWalletAddressCli(vectorGenesisWallet, uint(apex.VectorCluster.Config.NetworkMagic))
require.NoError(t, err)

fmt.Println("prime genesis addr: ", primeGenesisAddress)
fmt.Println("vector genesis addr: ", vectorGenesisAddress)
fmt.Println("prime user addr: ", user.PrimeAddress)
fmt.Println("vector user addr: ", user.VectorAddress)
fmt.Println("prime multisig addr: ", apex.Bridge.PrimeMultisigAddr)
fmt.Println("prime fee addr: ", apex.Bridge.PrimeMultisigFeeAddr)
fmt.Println("vector multisig addr: ", apex.Bridge.VectorMultisigAddr)
fmt.Println("vector fee addr: ", apex.Bridge.VectorMultisigFeeAddr)

//nolint:dupl
t.Run("From prime to vector 200x 5min 90%", func(t *testing.T) {
instances := 200
Expand Down Expand Up @@ -788,6 +978,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
}

fmt.Printf("Funding Complete\n")
fmt.Printf("Sending %v transactions in %v seconds\n", instances, maxWaitTime)

var wg sync.WaitGroup
for i := 0; i < instances; i++ {
Expand Down Expand Up @@ -875,6 +1066,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
}

fmt.Printf("Funding Complete\n")
fmt.Printf("Sending %v transactions in %v seconds\n", instances, maxWaitTime)

var wg sync.WaitGroup
for i := 0; i < instances; i++ {
Expand Down Expand Up @@ -948,7 +1140,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
prevAmountVector, err := cardanofw.GetTokenAmount(ctx, txProviderPrime, user.PrimeAddress)
require.NoError(t, err)

fmt.Printf("Funding %v Wallets\n", instances)
fmt.Printf("Funding %v Wallets\n", instances*2)

for i := 0; i < instances; i++ {
if (i+1)%100 == 0 {
Expand Down Expand Up @@ -979,6 +1171,7 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
}

fmt.Printf("Funding Complete\n")
fmt.Printf("Sending %v transactions in %v seconds\n", instances*2, maxWaitTime)

var wg sync.WaitGroup
for i := 0; i < instances; i++ {
Expand Down Expand Up @@ -1076,154 +1269,4 @@ func TestE2E_ApexBridge_ValidScenarios(t *testing.T) {
require.NoError(t, err)
fmt.Printf("Success count: %v. prevAmount: %v. newAmount: %v. expectedAmount: %v\n", succeededCountVector, prevAmountVector, newAmountVector, expectedAmountVector)
})

t.Run("Both directions sequential", func(t *testing.T) {
instances := 5
sendAmount := uint64(1_000_000)

for i := 0; i < instances; i++ {
primeTxHash := user.BridgeAmount(t, ctx, txProviderPrime, apex.Bridge.PrimeMultisigAddr,
apex.Bridge.VectorMultisigFeeAddr, sendAmount, true)

fmt.Printf("prime tx %v sent. hash: %s\n", i+1, primeTxHash)

vectorTxHash := user.BridgeAmount(t, ctx, txProviderVector, apex.Bridge.VectorMultisigAddr,
apex.Bridge.PrimeMultisigFeeAddr, sendAmount, false)

fmt.Printf("vector tx %v sent. hash: %s\n", i+1, vectorTxHash)
}

prevAmountOnVector, err := cardanofw.GetTokenAmount(ctx, txProviderVector, user.VectorAddress)
require.NoError(t, err)
prevAmountOnPrime, err := cardanofw.GetTokenAmount(ctx, txProviderPrime, user.PrimeAddress)
require.NoError(t, err)

fmt.Printf("Waiting for %v TXs on vector\n", instances)
expectedAmountOnVector := prevAmountOnVector.Uint64() + uint64(instances)*sendAmount
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)

newAmountOnVector, err := cardanofw.GetTokenAmount(ctx, txProviderVector, user.VectorAddress)
require.NoError(t, err)

fmt.Printf("on vector: prevAmount: %v. newAmount: %v\n", prevAmountOnVector, newAmountOnVector)

fmt.Printf("Waiting for %v TXs on prime\n", instances)
expectedAmountOnPrime := prevAmountOnPrime.Uint64() + uint64(instances)*sendAmount
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)

newAmountOnPrime, err := cardanofw.GetTokenAmount(ctx, txProviderPrime, user.PrimeAddress)
require.NoError(t, err)

fmt.Printf("on prime: prevAmount: %v. newAmount: %v\n", prevAmountOnPrime, newAmountOnPrime)
})

t.Run("Both directions sequential and parallel", func(t *testing.T) {
sequentialInstances := 5
parallelInstances := 6

primeWalletKeys := make([]wallet.IWallet, parallelInstances)

for i := 0; i < parallelInstances; i++ {
primeWalletKeys[i], err = wallet.NewStakeWalletManager().Create(path.Join(apex.PrimeCluster.Config.Dir("keys")), true)
require.NoError(t, err)

walletAddress, _, err := wallet.GetWalletAddressCli(primeWalletKeys[i], uint(apex.PrimeCluster.Config.NetworkMagic))
require.NoError(t, err)

fundSendAmount := uint64(5_000_000)
user.SendToAddress(t, ctx, txProviderPrime, primeGenesisWallet, uint64(sequentialInstances)*fundSendAmount, walletAddress, true)
}

fmt.Printf("Funded %v prime wallets \n", parallelInstances)

vectorWalletKeys := make([]wallet.IWallet, parallelInstances)

for i := 0; i < parallelInstances; i++ {
vectorWalletKeys[i], err = wallet.NewStakeWalletManager().Create(path.Join(apex.VectorCluster.Config.Dir("keys")), true)
require.NoError(t, err)

walletAddress, _, err := wallet.GetWalletAddressCli(vectorWalletKeys[i], uint(apex.VectorCluster.Config.NetworkMagic))
require.NoError(t, err)

fundSendAmount := uint64(5_000_000)
user.SendToAddress(t, ctx, txProviderVector, vectorGenesisWallet, uint64(sequentialInstances)*fundSendAmount, walletAddress, false)
}

fmt.Printf("Funded %v vector wallets \n", parallelInstances)

prevAmountOnVector, err := cardanofw.GetTokenAmount(ctx, txProviderVector, user.VectorAddress)
require.NoError(t, err)
prevAmountOnPrime, err := cardanofw.GetTokenAmount(ctx, txProviderPrime, user.PrimeAddress)
require.NoError(t, err)

sendAmount := uint64(1_000_000)

for j := 0; j < sequentialInstances; j++ {
var wg sync.WaitGroup

for i := 0; i < parallelInstances; i++ {
wg.Add(1)

go func(run, idx int) {
defer wg.Done()

txHash := cardanofw.BridgeAmountFull(t, ctx, txProviderPrime, uint(apex.PrimeCluster.Config.NetworkMagic),
apex.Bridge.PrimeMultisigAddr, apex.Bridge.VectorMultisigFeeAddr, primeWalletKeys[idx], user.VectorAddress, sendAmount,
cardanofw.GetDestinationChainID(true))
fmt.Printf("run: %v. Prime tx %v sent. hash: %s\n", run+1, idx+1, txHash)
}(j, i)
}

wg.Wait()

for i := 0; i < parallelInstances; i++ {
wg.Add(1)

go func(run, idx int) {
defer wg.Done()

txHash := cardanofw.BridgeAmountFull(t, ctx, txProviderVector, uint(apex.VectorCluster.Config.NetworkMagic),
apex.Bridge.VectorMultisigAddr, apex.Bridge.PrimeMultisigFeeAddr, vectorWalletKeys[idx], user.PrimeAddress, sendAmount,
cardanofw.GetDestinationChainID(false))
fmt.Printf("run: %v. Vector tx %v sent. hash: %s\n", run+1, idx+1, txHash)
}(j, i)
}

wg.Wait()
}

fmt.Printf("Waiting for %v TXs on vector:\n", sequentialInstances*parallelInstances)

expectedAmountOnVector := prevAmountOnVector.Uint64() + uint64(sequentialInstances)*uint64(parallelInstances)*sendAmount
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)
fmt.Printf("%v TXs on vector confirmed\n", sequentialInstances*parallelInstances)

newAmountOnVector, err := cardanofw.GetTokenAmount(ctx, txProviderVector, user.VectorAddress)
require.NoError(t, err)

fmt.Printf("on vector: prevAmount: %v. newAmount: %v\n", prevAmountOnVector, newAmountOnVector)

fmt.Printf("Waiting for %v TXs on prime\n", sequentialInstances*parallelInstances)

expectedAmountOnPrime := prevAmountOnPrime.Uint64() + uint64(sequentialInstances)*uint64(parallelInstances)*sendAmount
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)
fmt.Printf("%v TXs on prime confirmed\n", sequentialInstances*parallelInstances)

newAmountOnPrime, err := cardanofw.GetTokenAmount(ctx, txProviderPrime, user.PrimeAddress)
require.NoError(t, err)

fmt.Printf("on prime: prevAmount: %v. newAmount: %v\n", prevAmountOnPrime, newAmountOnPrime)
})
}

0 comments on commit 8bb15dd

Please sign in to comment.