From 7e6368f3ed07f0a3afaa55f2270a65a0da79746a Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Thu, 24 Oct 2024 17:38:53 -0500 Subject: [PATCH] add more description for function; add unit test to ensure that standard memo is disabled for mainnet --- e2e/e2etests/test_bitcoin_std_deposit.go | 4 +++ e2e/runner/bitcoin.go | 11 +++++-- .../chains/bitcoin/observer/event_test.go | 30 +++++++++++++++---- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/e2e/e2etests/test_bitcoin_std_deposit.go b/e2e/e2etests/test_bitcoin_std_deposit.go index a98a34a3f1..fa123b94cd 100644 --- a/e2e/e2etests/test_bitcoin_std_deposit.go +++ b/e2e/e2etests/test_bitcoin_std_deposit.go @@ -17,6 +17,10 @@ func TestBitcoinStdMemoDeposit(r *runner.E2ERunner, args []string) { // setup deployer BTC address r.SetBtcAddress(r.Name, false) + // start mining blocks if local bitcoin + stop := r.MineBlocksIfLocalBitcoin() + defer stop() + // parse amount to deposit require.Len(r, args, 1) amount := parseFloat(r, args[0]) diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index a1ff30c084..3d65589fa5 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -77,7 +77,7 @@ func (r *E2ERunner) GetTop20UTXOsForTssAddress() ([]btcjson.ListUnspentResult, e return utxos, nil } -// DepositBTCWithAmount deposits BTC into ZetaChain with a specific amount +// DepositBTCWithAmount deposits BTC into ZetaChain with a specific amount and memo func (r *E2ERunner) DepositBTCWithAmount(amount float64, memo *memo.InboundMemo) *chainhash.Hash { // list deployer utxos utxos, err := r.ListDeployerUTXOs() @@ -176,16 +176,21 @@ func (r *E2ERunner) DepositBTC() { } // DepositBTCWithLegacyMemo deposits BTC from the deployer address to the TSS using legacy memo +// +// The legacy memo layout: [20-byte receiver] + [payload] func (r *E2ERunner) DepositBTCWithLegacyMemo( amount float64, inputUTXOs []btcjson.ListUnspentResult, ) (*chainhash.Hash, error) { r.Logger.Info("⏳ depositing BTC into ZEVM with legacy memo") - return r.SendToTSSFromDeployerWithMemo(amount, inputUTXOs, r.EVMAddress().Bytes()) + // payload is not needed for pure deposit + memoBytes := r.EVMAddress().Bytes() + + return r.SendToTSSFromDeployerWithMemo(amount, inputUTXOs, memoBytes) } -// DepositBTCWithStandardMemo deposits BTC from the deployer address to the TSS using standard memo +// DepositBTCWithStandardMemo deposits BTC from the deployer address to the TSS using standard `InboundMemo` struct func (r *E2ERunner) DepositBTCWithStandardMemo( amount float64, inputUTXOs []btcjson.ListUnspentResult, diff --git a/zetaclient/chains/bitcoin/observer/event_test.go b/zetaclient/chains/bitcoin/observer/event_test.go index 3c17e8c5ed..5ed8e9b103 100644 --- a/zetaclient/chains/bitcoin/observer/event_test.go +++ b/zetaclient/chains/bitcoin/observer/event_test.go @@ -121,6 +121,7 @@ func Test_DecodeEventMemoBytes(t *testing.T) { // test cases tests := []struct { name string + chainID int64 event *observer.BTCInboundEvent expectedMemoStd *memo.InboundMemo expectedReceiver common.Address @@ -128,7 +129,8 @@ func Test_DecodeEventMemoBytes(t *testing.T) { errMsg string }{ { - name: "should decode standard memo bytes successfully", + name: "should decode standard memo bytes successfully", + chainID: chains.BitcoinTestnet.ChainId, event: &observer.BTCInboundEvent{ // a deposit and call MemoBytes: testutil.HexToBytes( @@ -150,7 +152,8 @@ func Test_DecodeEventMemoBytes(t *testing.T) { }, }, { - name: "should fall back to legacy memo successfully", + name: "should fall back to legacy memo successfully", + chainID: chains.BitcoinTestnet.ChainId, event: &observer.BTCInboundEvent{ // raw address + payload MemoBytes: testutil.HexToBytes(t, "2d07a9cbd57dcca3e2cf966c88bc874445b6e3b668656c6c6f207361746f736869"), @@ -158,14 +161,28 @@ func Test_DecodeEventMemoBytes(t *testing.T) { expectedReceiver: common.HexToAddress("0x2D07A9CBd57DCca3E2cF966C88Bc874445b6E3B6"), }, { - name: "should do nothing for donation message", + name: "should disable standard memo for Bitcoin mainnet", + chainID: chains.BitcoinMainnet.ChainId, + event: &observer.BTCInboundEvent{ + // a deposit and call + MemoBytes: testutil.HexToBytes( + t, + "5a0110032d07a9cbd57dcca3e2cf966c88bc874445b6e3b60d68656c6c6f207361746f736869", + ), + }, + expectedReceiver: common.HexToAddress("0x5A0110032d07A9cbd57dcCa3e2Cf966c88bC8744"), + }, + { + name: "should do nothing for donation message", + chainID: chains.BitcoinTestnet.ChainId, event: &observer.BTCInboundEvent{ MemoBytes: []byte(constant.DonationMessage), }, donation: true, }, { - name: "should return error if standard memo contains improper data", + name: "should return error if standard memo contains improper data", + chainID: chains.BitcoinTestnet.ChainId, event: &observer.BTCInboundEvent{ // a deposit and call, receiver is empty ZEVM address MemoBytes: testutil.HexToBytes( @@ -176,7 +193,8 @@ func Test_DecodeEventMemoBytes(t *testing.T) { errMsg: "standard memo contains improper data", }, { - name: "should return error if standard memo validation failed", + name: "should return error if standard memo validation failed", + chainID: chains.BitcoinTestnet.ChainId, event: &observer.BTCInboundEvent{ // a no asset call opCode passed, not supported at the moment MemoBytes: testutil.HexToBytes( @@ -190,7 +208,7 @@ func Test_DecodeEventMemoBytes(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := tt.event.DecodeMemoBytes(chains.BitcoinTestnet.ChainId) + err := tt.event.DecodeMemoBytes(tt.chainID) if tt.errMsg != "" { require.Contains(t, err.Error(), tt.errMsg) return