forked from zeta-chain/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_stress_btc_withdraw.go
73 lines (57 loc) · 2.16 KB
/
test_stress_btc_withdraw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package e2etests
import (
"fmt"
"math/big"
"time"
"github.com/btcsuite/btcutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/e2e/utils"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
)
// TestStressBTCWithdraw tests the stressing withdraw of btc
func TestStressBTCWithdraw(r *runner.E2ERunner, args []string) {
require.Len(r, args, 2)
withdrawalAmount := parseFloat(r, args[0])
numWithdraws := parseInt(r, args[1])
r.SetBtcAddress(r.Name, false)
r.Logger.Print("starting stress test of %d withdraws", numWithdraws)
// create a wait group to wait for all the withdraws to complete
var eg errgroup.Group
satAmount, err := btcutil.NewAmount(withdrawalAmount)
require.NoError(r, err)
// send the withdraws
for i := 0; i < numWithdraws; i++ {
i := i
tx, err := r.BTCZRC20.Withdraw(
r.ZEVMAuth,
[]byte(r.BTCDeployerAddress.EncodeAddress()),
big.NewInt(int64(satAmount)),
)
require.NoError(r, err)
receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)
r.Logger.Print("index %d: starting withdraw, tx hash: %s", i, tx.Hash().Hex())
eg.Go(func() error { return monitorBTCWithdraw(r, tx, i, time.Now()) })
}
require.NoError(r, eg.Wait())
r.Logger.Print("all withdraws completed")
}
// monitorBTCWithdraw monitors the withdraw of BTC, returns once the withdraw is complete
func monitorBTCWithdraw(r *runner.E2ERunner, tx *ethtypes.Transaction, index int, startTime time.Time) error {
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, tx.Hash().Hex(), r.CctxClient, r.Logger, r.ReceiptTimeout)
if cctx.CctxStatus.Status != crosschaintypes.CctxStatus_OutboundMined {
return fmt.Errorf(
"index %d: withdraw cctx failed with status %s, message %s, cctx index %s",
index,
cctx.CctxStatus.Status,
cctx.CctxStatus.StatusMessage,
cctx.Index,
)
}
timeToComplete := time.Now().Sub(startTime)
r.Logger.Print("index %d: withdraw cctx success in %s", index, timeToComplete.String())
return nil
}