From 0a915e25a7a04ca4a7a0f61d8ab05fb4008bbc62 Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Wed, 11 Sep 2024 11:13:17 -0700 Subject: [PATCH 1/5] added bitcoin deposit and call e2e test --- cmd/zetae2e/local/local.go | 13 +++--- e2e/e2etests/e2etests.go | 9 ++++ e2e/e2etests/test_bitcoin_deposit_call.go | 56 +++++++++++++++++++++++ 3 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 e2e/e2etests/test_bitcoin_deposit_call.go diff --git a/cmd/zetae2e/local/local.go b/cmd/zetae2e/local/local.go index 098d8dab5c..13417ec841 100644 --- a/cmd/zetae2e/local/local.go +++ b/cmd/zetae2e/local/local.go @@ -282,6 +282,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) { bitcoinTests := []string{ e2etests.TestBitcoinDepositName, + e2etests.TestBitcoinDepositAndCallName, e2etests.TestBitcoinDepositRefundName, e2etests.TestBitcoinWithdrawSegWitName, e2etests.TestBitcoinWithdrawInvalidAddressName, @@ -289,12 +290,12 @@ func localE2ETest(cmd *cobra.Command, _ []string) { e2etests.TestCrosschainSwapName, } bitcoinAdvancedTests := []string{ - e2etests.TestBitcoinWithdrawTaprootName, - e2etests.TestBitcoinWithdrawLegacyName, - e2etests.TestBitcoinWithdrawMultipleName, - e2etests.TestBitcoinWithdrawP2SHName, - e2etests.TestBitcoinWithdrawP2WSHName, - e2etests.TestBitcoinWithdrawRestrictedName, + // e2etests.TestBitcoinWithdrawTaprootName, + // e2etests.TestBitcoinWithdrawLegacyName, + // e2etests.TestBitcoinWithdrawMultipleName, + // e2etests.TestBitcoinWithdrawP2SHName, + // e2etests.TestBitcoinWithdrawP2WSHName, + // e2etests.TestBitcoinWithdrawRestrictedName, } ethereumTests := []string{ e2etests.TestEtherWithdrawName, diff --git a/e2e/e2etests/e2etests.go b/e2e/e2etests/e2etests.go index d803bd0f31..9157ff8e20 100644 --- a/e2e/e2etests/e2etests.go +++ b/e2e/e2etests/e2etests.go @@ -68,6 +68,7 @@ const ( */ TestBitcoinDepositName = "bitcoin_deposit" TestBitcoinDepositRefundName = "bitcoin_deposit_refund" + TestBitcoinDepositAndCallName = "bitcoin_deposit_and_call" TestBitcoinWithdrawSegWitName = "bitcoin_withdraw_segwit" TestBitcoinWithdrawTaprootName = "bitcoin_withdraw_taproot" TestBitcoinWithdrawMultipleName = "bitcoin_withdraw_multiple" @@ -435,6 +436,14 @@ var AllE2ETests = []runner.E2ETest{ }, TestBitcoinDeposit, ), + runner.NewE2ETest( + TestBitcoinDepositAndCallName, + "deposit Bitcoin into ZEVM and call a contract", + []runner.ArgDefinition{ + {Description: "amount in btc", DefaultValue: "0.001"}, + }, + TestBitcoinDepositAndCall, + ), runner.NewE2ETest( TestBitcoinDepositRefundName, "deposit Bitcoin into ZEVM; expect refund", []runner.ArgDefinition{ diff --git a/e2e/e2etests/test_bitcoin_deposit_call.go b/e2e/e2etests/test_bitcoin_deposit_call.go new file mode 100644 index 0000000000..4780fda3ef --- /dev/null +++ b/e2e/e2etests/test_bitcoin_deposit_call.go @@ -0,0 +1,56 @@ +package e2etests + +import ( + "math/big" + + "github.com/stretchr/testify/require" + + "github.com/zeta-chain/node/e2e/runner" + "github.com/zeta-chain/node/e2e/utils" + testcontract "github.com/zeta-chain/node/testutil/contracts" + crosschaintypes "github.com/zeta-chain/node/x/crosschain/types" + zetabitcoin "github.com/zeta-chain/node/zetaclient/chains/bitcoin" +) + +func TestBitcoinDepositAndCall(r *runner.E2ERunner, args []string) { + // ARRANGE + // Given BTC address + r.SetBtcAddress(r.Name, false) + + // Given "Live" BTC network + stop := r.MineBlocksIfLocalBitcoin() + defer stop() + + // Given amount to send + require.Len(r, args, 1) + amount := parseFloat(r, args[0]) + amountTotal := amount + zetabitcoin.DefaultDepositorFee + + // Given a list of UTXOs + utxos, err := r.ListDeployerUTXOs() + require.NoError(r, err) + require.NotEmpty(r, utxos) + + // deploy an example contract in ZEVM + contractAddr, _, contract, err := testcontract.DeployExample(r.ZEVMAuth, r.ZEVMClient) + require.NoError(r, err) + r.Logger.Print("Bitcoin: Example contract deployed at: %s", contractAddr.String()) + + // ACT + // Send BTC to TSS address with a dummy memo + txHash, err := r.SendToTSSFromDeployerWithMemo(amountTotal, utxos, []byte("hello shatoshi")) + require.NoError(r, err) + require.NotEmpty(r, txHash) + r.Logger.Print("Bitcoin: Sent %f BTC to TSS address", amountTotal) + + // wait for the cctx to be mined + cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, txHash.String(), r.CctxClient, r.Logger, r.CctxTimeout) + r.Logger.CCTX(*cctx, "bitcoin_deposit_and_call") + utils.RequireCCTXStatus(r, cctx, crosschaintypes.CctxStatus_OutboundMined) + r.Logger.Print("Bitcoin: CCTX mined") + + // check if example contract has been called and value should be set to amount + amoutSats, err := zetabitcoin.GetSatoshis(amount) + require.NoError(r, err) + utils.MustHaveCalledExampleContract(r, contract, big.NewInt(amoutSats)) +} From 3665e396f993cbcc799c2e6139361585cc173dba Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Wed, 11 Sep 2024 11:35:26 -0700 Subject: [PATCH 2/5] add e2e test for bitcoin deposit and call --- e2e/e2etests/test_bitcoin_deposit_call.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/e2e/e2etests/test_bitcoin_deposit_call.go b/e2e/e2etests/test_bitcoin_deposit_call.go index 4780fda3ef..0f81e333b3 100644 --- a/e2e/e2etests/test_bitcoin_deposit_call.go +++ b/e2e/e2etests/test_bitcoin_deposit_call.go @@ -1,8 +1,6 @@ package e2etests import ( - "math/big" - "github.com/stretchr/testify/require" "github.com/zeta-chain/node/e2e/runner" @@ -32,13 +30,15 @@ func TestBitcoinDepositAndCall(r *runner.E2ERunner, args []string) { require.NotEmpty(r, utxos) // deploy an example contract in ZEVM - contractAddr, _, contract, err := testcontract.DeployExample(r.ZEVMAuth, r.ZEVMClient) + contractAddr, _, _, err := testcontract.DeployExample(r.ZEVMAuth, r.ZEVMClient) require.NoError(r, err) r.Logger.Print("Bitcoin: Example contract deployed at: %s", contractAddr.String()) // ACT // Send BTC to TSS address with a dummy memo - txHash, err := r.SendToTSSFromDeployerWithMemo(amountTotal, utxos, []byte("hello shatoshi")) + data := []byte("hello shatoshi") + memo := append(contractAddr.Bytes(), data...) + txHash, err := r.SendToTSSFromDeployerWithMemo(amountTotal, utxos, memo) require.NoError(r, err) require.NotEmpty(r, txHash) r.Logger.Print("Bitcoin: Sent %f BTC to TSS address", amountTotal) @@ -49,8 +49,8 @@ func TestBitcoinDepositAndCall(r *runner.E2ERunner, args []string) { utils.RequireCCTXStatus(r, cctx, crosschaintypes.CctxStatus_OutboundMined) r.Logger.Print("Bitcoin: CCTX mined") - // check if example contract has been called and value should be set to amount - amoutSats, err := zetabitcoin.GetSatoshis(amount) - require.NoError(r, err) - utils.MustHaveCalledExampleContract(r, contract, big.NewInt(amoutSats)) + // check if example contract has been called, 'bar' value should be set to amount + // amoutSats, err := zetabitcoin.GetSatoshis(amount) + // require.NoError(r, err) + // utils.MustHaveCalledExampleContract(r, contract, big.NewInt(amoutSats)) } From 92200e388f30f278aa1a7e87c2ef1561f168e5cb Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Tue, 17 Sep 2024 23:41:04 -0500 Subject: [PATCH 3/5] fix precision issue when depositing BTC to tss address --- cmd/zetae2e/local/local.go | 12 ++++++------ e2e/e2etests/test_bitcoin_deposit_call.go | 15 +++++++-------- e2e/runner/bitcoin.go | 5 ++++- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/cmd/zetae2e/local/local.go b/cmd/zetae2e/local/local.go index 13417ec841..2e24d815f2 100644 --- a/cmd/zetae2e/local/local.go +++ b/cmd/zetae2e/local/local.go @@ -290,12 +290,12 @@ func localE2ETest(cmd *cobra.Command, _ []string) { e2etests.TestCrosschainSwapName, } bitcoinAdvancedTests := []string{ - // e2etests.TestBitcoinWithdrawTaprootName, - // e2etests.TestBitcoinWithdrawLegacyName, - // e2etests.TestBitcoinWithdrawMultipleName, - // e2etests.TestBitcoinWithdrawP2SHName, - // e2etests.TestBitcoinWithdrawP2WSHName, - // e2etests.TestBitcoinWithdrawRestrictedName, + e2etests.TestBitcoinWithdrawTaprootName, + e2etests.TestBitcoinWithdrawLegacyName, + e2etests.TestBitcoinWithdrawMultipleName, + e2etests.TestBitcoinWithdrawP2SHName, + e2etests.TestBitcoinWithdrawP2WSHName, + e2etests.TestBitcoinWithdrawRestrictedName, } ethereumTests := []string{ e2etests.TestEtherWithdrawName, diff --git a/e2e/e2etests/test_bitcoin_deposit_call.go b/e2e/e2etests/test_bitcoin_deposit_call.go index 0f81e333b3..0f1fd6dc91 100644 --- a/e2e/e2etests/test_bitcoin_deposit_call.go +++ b/e2e/e2etests/test_bitcoin_deposit_call.go @@ -1,6 +1,8 @@ package e2etests import ( + "math/big" + "github.com/stretchr/testify/require" "github.com/zeta-chain/node/e2e/runner" @@ -30,27 +32,24 @@ func TestBitcoinDepositAndCall(r *runner.E2ERunner, args []string) { require.NotEmpty(r, utxos) // deploy an example contract in ZEVM - contractAddr, _, _, err := testcontract.DeployExample(r.ZEVMAuth, r.ZEVMClient) + contractAddr, _, contract, err := testcontract.DeployExample(r.ZEVMAuth, r.ZEVMClient) require.NoError(r, err) r.Logger.Print("Bitcoin: Example contract deployed at: %s", contractAddr.String()) // ACT // Send BTC to TSS address with a dummy memo - data := []byte("hello shatoshi") + data := []byte("hello satoshi") memo := append(contractAddr.Bytes(), data...) txHash, err := r.SendToTSSFromDeployerWithMemo(amountTotal, utxos, memo) require.NoError(r, err) - require.NotEmpty(r, txHash) - r.Logger.Print("Bitcoin: Sent %f BTC to TSS address", amountTotal) // wait for the cctx to be mined cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, txHash.String(), r.CctxClient, r.Logger, r.CctxTimeout) r.Logger.CCTX(*cctx, "bitcoin_deposit_and_call") utils.RequireCCTXStatus(r, cctx, crosschaintypes.CctxStatus_OutboundMined) - r.Logger.Print("Bitcoin: CCTX mined") // check if example contract has been called, 'bar' value should be set to amount - // amoutSats, err := zetabitcoin.GetSatoshis(amount) - // require.NoError(r, err) - // utils.MustHaveCalledExampleContract(r, contract, big.NewInt(amoutSats)) + amoutSats, err := zetabitcoin.GetSatoshis(amount) + require.NoError(r, err) + utils.MustHaveCalledExampleContract(r, contract, big.NewInt(amoutSats)) } diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index f861e8ce8f..10596e7e98 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -198,8 +198,11 @@ func (r *E2ERunner) SendToTSSFromDeployerWithMemo( scriptPubkeys[i] = utxo.ScriptPubKey } + // use static fee 0.0005 BTC to calculate change feeSats := btcutil.Amount(0.0005 * btcutil.SatoshiPerBitcoin) - amountSats := btcutil.Amount(amount * btcutil.SatoshiPerBitcoin) + amountInt, err := zetabitcoin.GetSatoshis(amount) + require.NoError(r, err) + amountSats := btcutil.Amount(amountInt) change := inputSats - feeSats - amountSats if change < 0 { From b0928b2da6f1df02cc88965b2119fbe329104fed Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Tue, 17 Sep 2024 23:46:43 -0500 Subject: [PATCH 4/5] add changelog entry --- changelog.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 2d0edbe190..9c069e0c56 100644 --- a/changelog.md +++ b/changelog.md @@ -34,7 +34,8 @@ * [2726](https://github.com/zeta-chain/node/pull/2726) - add e2e tests for deposit and call, deposit and revert * [2703](https://github.com/zeta-chain/node/pull/2703) - add e2e tests for stateful precompiled contracts * [2763](https://github.com/zeta-chain/node/pull/2763) - add V2 contracts migration test -* [2830] (https://github.com/zeta-chain/node/pull/2830) - extend staking precompile tests +* [2830](https://github.com/zeta-chain/node/pull/2830) - extend staking precompile tests +* [2895](https://github.com/zeta-chain/node/pull/2895) - add e2e test for bitcoin deposit and call ### Fixes From cc92e4938172580bf0bb7093328b05ad5145e6bf Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Tue, 17 Sep 2024 23:50:24 -0500 Subject: [PATCH 5/5] resolved changelog conflict --- changelog.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/changelog.md b/changelog.md index 8cf6ccfaa5..2c40bda19d 100644 --- a/changelog.md +++ b/changelog.md @@ -27,6 +27,7 @@ * [2867](https://github.com/zeta-chain/node/pull/2867) - skip precompiles test for tss migration * [2833](https://github.com/zeta-chain/node/pull/2833) - add e2e framework for TON blockchain * [2874](https://github.com/zeta-chain/node/pull/2874) - add support for multiple runs for precompile tests +* [2895](https://github.com/zeta-chain/node/pull/2895) - add e2e test for bitcoin deposit and call ### Fixes @@ -66,11 +67,7 @@ ### Tests * [2726](https://github.com/zeta-chain/node/pull/2726) - add e2e tests for deposit and call, deposit and revert -* [2703](https://github.com/zeta-chain/node/pull/2703) - add e2e tests for stateful precompiled contracts -* [2763](https://github.com/zeta-chain/node/pull/2763) - add V2 contracts migration test -* [2830](https://github.com/zeta-chain/node/pull/2830) - extend staking precompile tests * [2821](https://github.com/zeta-chain/node/pull/2821) - V2 protocol contracts migration e2e tests -* [2895](https://github.com/zeta-chain/node/pull/2895) - add e2e test for bitcoin deposit and call ### Fixes