Skip to content

Commit

Permalink
add balance check after Bitcoin e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Nov 6, 2024
1 parent 6473216 commit c051a04
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 40 deletions.
32 changes: 14 additions & 18 deletions cmd/zetae2e/local/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func startBitcoinTestRoutines(
// send BTC to TSS for gas fees and to tester ZEVM address
if initNetwork {
// mine 101 blocks to ensure the BTC rewards are spendable
// Note: the rewards can be sent to any address in here
// Note: the block rewards can be sent to any address in here
_, err := runnerDeposit.GenerateToAddressIfLocalBitcoin(101, runnerDeposit.BTCDeployerAddress)
require.NoError(runnerDeposit, err)

Expand All @@ -61,7 +61,7 @@ func initRunnerDeposit(
var (
name = "btc_deposit"
account = conf.AdditionalAccounts.UserBitcoin1
createWallet = true // deposit tests need Bitcoin node wallet
createWallet = true // deposit tests need Bitcoin node wallet to handle UTXOs
)

return initRunner(name, account, conf, deployerRunner, verbose, initNetwork, createWallet)
Expand All @@ -76,7 +76,7 @@ func initRunnerWithdraw(
var (
name = "btc_withdraw"
account = conf.AdditionalAccounts.UserBitcoin2
createWallet = false // withdraw tests do not use Bitcoin node wallet
createWallet = false // withdraw tests DON'T use Bitcoin node wallet
)

return initRunner(name, account, conf, deployerRunner, verbose, initNetwork, createWallet)
Expand All @@ -90,8 +90,8 @@ func initRunner(
deployerRunner *runner.E2ERunner,
verbose, initNetwork, createWallet bool,
) *runner.E2ERunner {
// initialize r for bitcoin test
r, err := initTestRunner(
// initialize runner for bitcoin test
runner, err := initTestRunner(
name,
conf,
deployerRunner,
Expand All @@ -101,27 +101,27 @@ func initRunner(
testutil.NoError(err)

// setup TSS address and setup deployer wallet
r.SetupBitcoinAccounts(createWallet)
runner.SetupBitcoinAccounts(createWallet)

// initialize funds
if initNetwork {
// send some BTC block rewards to the deployer address
_, err = r.GenerateToAddressIfLocalBitcoin(4, r.BTCDeployerAddress)
require.NoError(r, err)
_, err = runner.GenerateToAddressIfLocalBitcoin(4, runner.BTCDeployerAddress)
require.NoError(runner, err)

// send ERC20 token on EVM
txERC20Send := deployerRunner.SendERC20OnEvm(account.EVMAddress(), 1000)
r.WaitForTxReceiptOnEvm(txERC20Send)
runner.WaitForTxReceiptOnEvm(txERC20Send)

// deposit ETH and ERC20 tokens on ZetaChain
txEtherDeposit := r.DepositEther()
txERC20Deposit := r.DepositERC20()
txEtherDeposit := runner.DepositEther()
txERC20Deposit := runner.DepositERC20()

r.WaitForMinedCCTX(txEtherDeposit)
r.WaitForMinedCCTX(txERC20Deposit)
runner.WaitForMinedCCTX(txEtherDeposit)
runner.WaitForMinedCCTX(txERC20Deposit)
}

return r
return runner
}

// createTestRoutine creates a test routine for given test names
Expand All @@ -145,10 +145,6 @@ func createTestRoutine(r *runner.E2ERunner, testNames []string) func() error {
return fmt.Errorf("bitcoin tests failed: %v", err)
}

// if err := bitcoinRunner.CheckBtcTSSBalance(); err != nil {
// return err
// }

r.Logger.Print("🍾 bitcoin tests completed in %s", time.Since(startTime).String())

return err
Expand Down
12 changes: 9 additions & 3 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
e2etests.TestMessagePassingEVMtoZEVMRevertFailName,
}

// btc withdraw tests are those that need a Bitcoin node wallet to send UTXOs
bitcoinDepositTests := []string{
e2etests.TestBitcoinDonationName,
e2etests.TestBitcoinDepositName,
Expand All @@ -318,7 +319,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
e2etests.TestBitcoinWithdrawInvalidAddressName,
e2etests.TestZetaWithdrawBTCRevertName,
}
bitcoinAdvancedTests := []string{
bitcoinWithdrawTestsAdvanced := []string{
e2etests.TestBitcoinWithdrawTaprootName,
e2etests.TestBitcoinWithdrawLegacyName,
e2etests.TestBitcoinWithdrawP2SHName,
Expand Down Expand Up @@ -357,7 +358,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
erc20Tests = append(erc20Tests, erc20AdvancedTests...)
zetaTests = append(zetaTests, zetaAdvancedTests...)
zevmMPTests = append(zevmMPTests, zevmMPAdvancedTests...)
bitcoinWithdrawTests = append(bitcoinWithdrawTests, bitcoinAdvancedTests...)
bitcoinWithdrawTests = append(bitcoinWithdrawTests, bitcoinWithdrawTestsAdvanced...)
ethereumTests = append(ethereumTests, ethereumAdvancedTests...)
}

Expand Down Expand Up @@ -466,7 +467,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
}

// if all tests pass, cancel txs priority monitoring and check if tx priority is not correct in some blocks
logger.Print("⏳ e2e tests passed,checking tx priority")
logger.Print("⏳ e2e tests passed, checking tx priority")
monitorPriorityCancel()
if err := <-txPriorityErrCh; err != nil && errors.Is(err, errWrongTxPriority) {
logger.Print("❌ %v", err)
Expand All @@ -479,10 +480,15 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
if testTSSMigration {
TSSMigration(deployerRunner, logger, verbose, conf)
}

// Verify that there are no trackers left over after tests complete
if !skipTrackerCheck {
deployerRunner.EnsureNoTrackers()
}

// Verify that the balance of restricted address is zero
deployerRunner.EnsureZeroBalanceOnRestrictedAddressZEVM()

// print and validate report
networkReport, err := deployerRunner.GenerateNetworkReport()
if err != nil {
Expand Down
23 changes: 14 additions & 9 deletions e2e/runner/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ type Response struct {
Amount Amount `json:"amount"`
}

func (r *E2ERunner) CheckZRC20ReserveAndSupply() error {
r.Logger.Info("Checking ZRC20 Reserve and Supply")
if err := r.checkEthTSSBalance(); err != nil {
return err
}
if err := r.checkERC20TSSBalance(); err != nil {
return err
}
return r.checkZetaTSSBalance()
func (r *E2ERunner) CheckZRC20BalanceAndSupply() {
r.Logger.Info("Checking ZRC20 Balance vs. Supply")

err := r.checkEthTSSBalance()
require.NoError(r, err, "ETH balance check failed")

err = r.checkERC20TSSBalance()
require.NoError(r, err, "ERC20 balance check failed")

err = r.checkZetaTSSBalance()
require.NoError(r, err, "ZETA balance check failed")

err = r.CheckBtcTSSBalance()
require.NoError(r, err, "BTC balance check failed")
}

func (r *E2ERunner) checkEthTSSBalance() error {
Expand Down
3 changes: 2 additions & 1 deletion e2e/runner/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (r *E2ERunner) DepositBTC(receiver common.Address) {
r.Logger.Info("ListUnspent:")
r.Logger.Info(" spendableAmount: %f", spendableAmount)
r.Logger.Info(" spendableUTXOs: %d", spendableUTXOs)
r.Logger.Info("Now sending BTC to tester ZEVM address...")
r.Logger.Info("Now sending two txs to TSS address and tester ZEVM address...")

// send initial BTC to the tester ZEVM address
amount := 1.15 + zetabitcoin.DefaultDepositorFee
Expand All @@ -154,6 +154,7 @@ func (r *E2ERunner) DepositBTC(receiver common.Address) {

// send a donation to the TSS address to compensate for the funds minted automatically during pool creation
// and prevent accounting errors
// it also serves as gas fee for the TSS to send BTC to other addresses
_, err = r.SendToTSSFromDeployerWithMemo(0.11, utxos[2:4], []byte(constant.DonationMessage))
require.NoError(r, err)

Expand Down
4 changes: 2 additions & 2 deletions e2e/runner/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (r *E2ERunner) EnsureNoTrackers() {
require.Empty(r, res.OutboundTracker, "there should be no trackers at the end of the test")
}

// EnsureZeroBalanceAddressZEVM ensures that the balance of the address is zero in the ZEVM
func (r *E2ERunner) EnsureZeroBalanceAddressZEVM() {
// EnsureZeroBalanceAddressZEVM ensures that the balance of the restricted address is zero in the ZEVM
func (r *E2ERunner) EnsureZeroBalanceOnRestrictedAddressZEVM() {
restrictedAddress := ethcommon.HexToAddress(sample.RestrictedEVMAddressTest)

// ensure ZETA balance is zero
Expand Down
6 changes: 2 additions & 4 deletions e2e/runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ func (r *E2ERunner) RunE2ETest(e2eTest E2ETest, checkAccounting bool) error {
}
e2eTest.E2ETest(r, args)

//check supplies
// check zrc20 balance vs. supply
if checkAccounting {
if err := r.CheckZRC20ReserveAndSupply(); err != nil {
return err
}
r.CheckZRC20BalanceAndSupply()
}

r.Logger.Print("✅ completed in %s - %s", time.Since(startTime), e2eTest.Description)
Expand Down
6 changes: 3 additions & 3 deletions e2e/runner/setup_bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func (r *E2ERunner) SetupBitcoinAccounts(createWallet bool) {
r.Logger.Print("✅ Bitcoin account setup in %s", time.Since(startTime))
}()

// setup deployer account
// setup deployer address
r.SetupBtcAddress(r.Name, createWallet)

// import the TSS address to index TSS utxos and txs
// import the TSS address to index TSS utxos and transactions
err := r.BtcRPCClient.ImportAddress(r.BTCTSSAddress.EncodeAddress())
require.NoError(r, err)
r.Logger.Info("⚙️ imported BTC TSSAddress: %s", r.BTCTSSAddress.EncodeAddress())

// import deployer address to index deployer utxos and txs
// import deployer address to index deployer utxos and transactions
err = r.BtcRPCClient.ImportAddress(r.BTCDeployerAddress.EncodeAddress())
require.NoError(r, err)
r.Logger.Info("⚙️ imported BTCDeployerAddress: %s", r.BTCDeployerAddress.EncodeAddress())
Expand Down

0 comments on commit c051a04

Please sign in to comment.