-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate standard memo to Bitcoin inbound and E2E tests (#3025)
* initial commit of btc revert address * add e2e tests for bitcoin standard memo deposit * add more unit tests * disable standard memo for Bitcoin mainnet * fix gosec * uncomment e2e tests * a few renamings; better comments and unit test * code refactor to make DecodeMemoBytes more redable * add more description for function; add unit test to ensure that standard memo is disabled for mainnet * revert func Processability() to pointer receiver
- Loading branch information
1 parent
a6f1e44
commit 467d691
Showing
33 changed files
with
1,570 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package e2etests | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/zeta-chain/node/e2e/runner" | ||
"github.com/zeta-chain/node/e2e/utils" | ||
"github.com/zeta-chain/node/testutil/sample" | ||
zetabitcoin "github.com/zeta-chain/node/zetaclient/chains/bitcoin" | ||
) | ||
|
||
func TestBitcoinDepositAndCallRevert(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]) | ||
amount += zetabitcoin.DefaultDepositorFee | ||
|
||
// Given a list of UTXOs | ||
utxos, err := r.ListDeployerUTXOs() | ||
require.NoError(r, err) | ||
require.NotEmpty(r, utxos) | ||
|
||
// ACT | ||
// Send BTC to TSS address with a dummy memo | ||
// zetacore should revert cctx if call is made on a non-existing address | ||
nonExistReceiver := sample.EthAddress() | ||
badMemo := append(nonExistReceiver.Bytes(), []byte("gibberish-memo")...) | ||
txHash, err := r.SendToTSSFromDeployerWithMemo(amount, utxos, badMemo) | ||
require.NoError(r, err) | ||
require.NotEmpty(r, txHash) | ||
|
||
// ASSERT | ||
// Now we want to make sure refund TX is completed. | ||
cctx := utils.WaitCctxRevertedByInboundHash(r.Ctx, r, txHash.String(), r.CctxClient) | ||
|
||
// Check revert tx receiver address and amount | ||
receiver, value := r.QueryOutboundReceiverAndAmount(cctx.OutboundParams[1].Hash) | ||
assert.Equal(r, r.BTCDeployerAddress.EncodeAddress(), receiver) | ||
assert.Positive(r, value) | ||
|
||
r.Logger.Info("Sent %f BTC to TSS with invalid memo, got refund of %d satoshis", amount, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package e2etests | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/zeta-chain/node/e2e/runner" | ||
"github.com/zeta-chain/node/pkg/constant" | ||
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types" | ||
zetabitcoin "github.com/zeta-chain/node/zetaclient/chains/bitcoin" | ||
) | ||
|
||
func TestBitcoinDonation(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) | ||
|
||
// ACT | ||
// Send BTC to TSS address with donation message | ||
memo := []byte(constant.DonationMessage) | ||
txHash, err := r.SendToTSSFromDeployerWithMemo(amountTotal, utxos, memo) | ||
require.NoError(r, err) | ||
|
||
// ASSERT after 4 Zeta blocks | ||
time.Sleep(constant.ZetaBlockTime * 4) | ||
req := &crosschaintypes.QueryInboundHashToCctxDataRequest{InboundHash: txHash.String()} | ||
_, err = r.CctxClient.InTxHashToCctxData(r.Ctx, req) | ||
require.Error(r, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package e2etests | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/zeta-chain/node/e2e/runner" | ||
"github.com/zeta-chain/node/e2e/utils" | ||
"github.com/zeta-chain/node/pkg/memo" | ||
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types" | ||
"github.com/zeta-chain/node/zetaclient/chains/bitcoin" | ||
) | ||
|
||
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]) | ||
|
||
// get ERC20 BTC balance before deposit | ||
balanceBefore, err := r.BTCZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress()) | ||
require.NoError(r, err) | ||
r.Logger.Info("runner balance of BTC before deposit: %d satoshis", balanceBefore) | ||
|
||
// create standard memo with receiver address | ||
memo := &memo.InboundMemo{ | ||
Header: memo.Header{ | ||
Version: 0, | ||
EncodingFmt: memo.EncodingFmtCompactShort, | ||
OpCode: memo.OpCodeDeposit, | ||
}, | ||
FieldsV0: memo.FieldsV0{ | ||
Receiver: r.EVMAddress(), // to deployer self | ||
}, | ||
} | ||
|
||
// deposit BTC with standard memo | ||
txHash := r.DepositBTCWithAmount(amount, memo) | ||
|
||
// 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_std_memo_deposit") | ||
utils.RequireCCTXStatus(r, cctx, crosschaintypes.CctxStatus_OutboundMined) | ||
|
||
// get ERC20 BTC balance after deposit | ||
balanceAfter, err := r.BTCZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress()) | ||
require.NoError(r, err) | ||
r.Logger.Info("runner balance of BTC after deposit: %d satoshis", balanceAfter) | ||
|
||
// the runner balance should be increased by the deposit amount | ||
amountIncreased := new(big.Int).Sub(balanceAfter, balanceBefore) | ||
amountSatoshis, err := bitcoin.GetSatoshis(amount) | ||
require.NoError(r, err) | ||
require.Equal(r, uint64(amountSatoshis), amountIncreased.Uint64()) | ||
} |
Oops, something went wrong.