Skip to content

Commit

Permalink
add more description for function; add unit test to ensure that stand…
Browse files Browse the repository at this point in the history
…ard memo is disabled for mainnet
  • Loading branch information
ws4charlie committed Oct 24, 2024
1 parent b2c149c commit 7e6368f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
4 changes: 4 additions & 0 deletions e2e/e2etests/test_bitcoin_std_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
11 changes: 8 additions & 3 deletions e2e/runner/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 24 additions & 6 deletions zetaclient/chains/bitcoin/observer/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,16 @@ func Test_DecodeEventMemoBytes(t *testing.T) {
// test cases
tests := []struct {
name string
chainID int64
event *observer.BTCInboundEvent
expectedMemoStd *memo.InboundMemo
expectedReceiver common.Address
donation bool
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(
Expand All @@ -150,22 +152,37 @@ 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"),
},
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(
Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 7e6368f

Please sign in to comment.