Skip to content

Commit fcbb639

Browse files
authored
test: e2e tests for zetaclient restricted addresses (#1791)
* initiated feature of zetaclient-banned-address * fix some issues in e2e tests * added e2e tests for banned addresses * fix gosec issues * some unit tests, comments and refactor * unified log prints for restricted address detection * renamed tests and variables * move restricted address to Unreleased section * move restricted address to Unreleased section * improved compliance log prints * removed unnecessary tests and made bitcoin withdrawal to restricted address a separate test * made OFAC tests separate and moved them to bottom of list * fix nosec false positive
1 parent 85eda62 commit fcbb639

15 files changed

+140
-25
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
### Tests
1616

1717
* [1767](https://github.com/zeta-chain/node/pull/1767) - add unit tests for emissions module begin blocker
18+
* [1791](https://github.com/zeta-chain/node/pull/1791) - add e2e tests for feature of restricted address
1819

1920
### Chores
2021

cmd/zetae2e/local/bitcoin.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func bitcoinTestRoutine(
6969
e2etests.TestBitcoinWithdrawName,
7070
e2etests.TestZetaWithdrawBTCRevertName,
7171
e2etests.TestCrosschainSwapName,
72+
e2etests.TestBitcoinWithdrawRestrictedName,
7273
); err != nil {
7374
return fmt.Errorf("bitcoin tests failed: %v", err)
7475
}

cmd/zetae2e/local/erc20.go

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func erc20TestRoutine(
6666
e2etests.TestMultipleWithdrawsName,
6767
e2etests.TestERC20DepositAndCallRefundName,
6868
e2etests.TestZRC20SwapName,
69+
e2etests.TestERC20DepositRestrictedName,
6970
); err != nil {
7071
return fmt.Errorf("erc20 tests failed: %v", err)
7172
}

cmd/zetae2e/local/ethereum.go

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func ethereumTestRoutine(
5959
e2etests.TestContextUpgradeName,
6060
e2etests.TestEtherDepositAndCallName,
6161
e2etests.TestDepositAndCallRefundName,
62+
e2etests.TestEtherWithdrawRestrictedName,
6263
); err != nil {
6364
return fmt.Errorf("ethereum tests failed: %v", err)
6465
}

cmd/zetae2e/local/zeta.go

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func zetaTestRoutine(
6363
e2etests.TestMessagePassingName,
6464
e2etests.TestMessagePassingRevertFailName,
6565
e2etests.TestMessagePassingRevertSuccessName,
66+
e2etests.TestZetaDepositRestrictedName,
6667
); err != nil {
6768
return fmt.Errorf("zeta tests failed: %v", err)
6869
}

e2e/e2etests/e2etests.go

+31-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
TestMessagePassingName = "message_passing"
1717
TestZRC20SwapName = "zrc20_swap"
1818
TestBitcoinWithdrawName = "bitcoin_withdraw"
19+
TestBitcoinWithdrawRestrictedName = "bitcoin_withdraw_restricted"
1920
TestCrosschainSwapName = "crosschain_swap"
2021
TestMessagePassingRevertFailName = "message_passing_revert_fail"
2122
TestMessagePassingRevertSuccessName = "message_passing_revert_success"
@@ -26,12 +27,16 @@ const (
2627
TestDepositEtherLiquidityCapName = "deposit_eth_liquidity_cap"
2728
TestMyTestName = "my_test"
2829

29-
TestERC20WithdrawName = "erc20_withdraw"
30-
TestERC20DepositName = "erc20_deposit"
31-
TestEtherDepositName = "eth_deposit"
32-
TestEtherWithdrawName = "eth_withdraw"
33-
TestBitcoinDepositName = "bitcoin_deposit"
34-
TestZetaDepositName = "zeta_deposit"
30+
TestERC20WithdrawName = "erc20_withdraw"
31+
TestERC20DepositName = "erc20_deposit"
32+
// #nosec G101: Potential hardcoded credentials (gosec), not a credential
33+
TestERC20DepositRestrictedName = "erc20_deposit_restricted"
34+
TestEtherDepositName = "eth_deposit"
35+
TestEtherWithdrawName = "eth_withdraw"
36+
TestEtherWithdrawRestrictedName = "eth_withdraw_restricted"
37+
TestBitcoinDepositName = "bitcoin_deposit"
38+
TestZetaDepositName = "zeta_deposit"
39+
TestZetaDepositRestrictedName = "zeta_deposit_restricted"
3540

3641
TestDonationEtherName = "donation_ether"
3742

@@ -188,4 +193,24 @@ var AllE2ETests = []runner.E2ETest{
188193
"stress test BTC deposit",
189194
TestStressBTCDeposit,
190195
},
196+
{
197+
TestZetaDepositRestrictedName,
198+
"deposit ZETA from Ethereum to ZEVM restricted address",
199+
TestZetaDepositRestricted,
200+
},
201+
{
202+
TestERC20DepositRestrictedName,
203+
"deposit ERC20 into ZEVM restricted address",
204+
TestERC20DepositRestricted,
205+
},
206+
{
207+
TestEtherWithdrawRestrictedName,
208+
"withdraw Ether from ZEVM to restricted address",
209+
TestEtherWithdrawRestricted,
210+
},
211+
{
212+
TestBitcoinWithdrawRestrictedName,
213+
"withdraw Bitcoin from ZEVM to restricted address",
214+
TestBitcoinWithdrawRestricted,
215+
},
191216
}

e2e/e2etests/test_bitcoin_withdraw.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ import (
44
"fmt"
55
"math/big"
66

7+
"github.com/btcsuite/btcd/btcjson"
78
"github.com/btcsuite/btcd/chaincfg/chainhash"
89
"github.com/btcsuite/btcutil"
10+
"github.com/zeta-chain/zetacore/common"
911
"github.com/zeta-chain/zetacore/e2e/runner"
1012
"github.com/zeta-chain/zetacore/e2e/utils"
13+
"github.com/zeta-chain/zetacore/zetaclient/testutils"
1114
)
1215

1316
func TestBitcoinWithdraw(r *runner.E2ERunner) {
14-
// withdraw 0.1 BTC from ZRC20 to BTC address
15-
// first, approve the ZRC20 contract to spend 1 BTC from the deployer address
17+
// withdraw 0.01 BTC from ZRC20 to BTC address
1618
WithdrawBitcoin(r)
1719
}
1820

19-
func WithdrawBitcoin(r *runner.E2ERunner) {
20-
amount := big.NewInt(0.1 * btcutil.SatoshiPerBitcoin)
21+
func TestBitcoinWithdrawRestricted(r *runner.E2ERunner) {
22+
// withdraw 0.01 BTC from ZRC20 to BTC restricted address
23+
WithdrawBitcoinRestricted(r)
24+
}
2125

22-
// approve the ZRC20 contract to spend 1 BTC from the deployer address
26+
func withdrawBTCZRC20(r *runner.E2ERunner, to btcutil.Address, amount *big.Int) *btcjson.TxRawResult {
27+
// approve the ZRC20 contract to spend 'amount' of BTC from the deployer address
2328
tx, err := r.BTCZRC20.Approve(r.ZevmAuth, r.BTCZRC20Addr, big.NewInt(amount.Int64()*2)) // approve more to cover withdraw fee
2429
if err != nil {
2530
panic(err)
@@ -32,8 +37,8 @@ func WithdrawBitcoin(r *runner.E2ERunner) {
3237
// mine blocks
3338
stop := r.MineBlocks()
3439

35-
// withdraw 0.1 BTC from ZRC20 to BTC address
36-
tx, err = r.BTCZRC20.Withdraw(r.ZevmAuth, []byte(r.BTCDeployerAddress.EncodeAddress()), amount)
40+
// withdraw 'amount' of BTC from ZRC20 to BTC address
41+
tx, err = r.BTCZRC20.Withdraw(r.ZevmAuth, []byte(to.EncodeAddress()), amount)
3742
if err != nil {
3843
panic(err)
3944
}
@@ -43,7 +48,7 @@ func WithdrawBitcoin(r *runner.E2ERunner) {
4348
}
4449

4550
// mine 10 blocks to confirm the withdraw tx
46-
_, err = r.BtcRPCClient.GenerateToAddress(10, r.BTCDeployerAddress, nil)
51+
_, err = r.BtcRPCClient.GenerateToAddress(10, to, nil)
4752
if err != nil {
4853
panic(err)
4954
}
@@ -75,6 +80,29 @@ func WithdrawBitcoin(r *runner.E2ERunner) {
7580

7681
// stop mining
7782
stop <- struct{}{}
83+
84+
return rawTx
85+
}
86+
87+
func WithdrawBitcoin(r *runner.E2ERunner) {
88+
amount := big.NewInt(0.01 * btcutil.SatoshiPerBitcoin)
89+
withdrawBTCZRC20(r, r.BTCDeployerAddress, amount)
90+
}
91+
92+
func WithdrawBitcoinRestricted(r *runner.E2ERunner) {
93+
amount := big.NewInt(0.01 * btcutil.SatoshiPerBitcoin)
94+
95+
// use restricted BTC P2WPKH address
96+
addressRestricted, err := common.DecodeBtcAddress(testutils.RestrictedBtcAddressTest, common.BtcRegtestChain().ChainId)
97+
if err != nil {
98+
panic(err)
99+
}
100+
101+
// the cctx should be cancelled
102+
rawTx := withdrawBTCZRC20(r, addressRestricted, amount)
103+
if len(rawTx.Vout) != 2 {
104+
panic(fmt.Errorf("BTC cancelled outtx rawTx.Vout should have 2 outputs"))
105+
}
78106
}
79107

80108
// WithdrawBitcoinMultipleTimes ...

e2e/e2etests/test_crosschain_swap.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestCrosschainSwap(r *runner.E2ERunner) {
8989

9090
r.Logger.Info("***** First test: USDT -> BTC")
9191
// Should deposit USDT for swap, swap for BTC and withdraw BTC
92-
txHash := r.DepositERC20WithAmountAndMessage(big.NewInt(8e7), msg)
92+
txHash := r.DepositERC20WithAmountAndMessage(r.DeployerAddress, big.NewInt(8e7), msg)
9393
cctx1 := utils.WaitCctxMinedByInTxHash(r.Ctx, txHash.Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
9494

9595
// check the cctx status

e2e/e2etests/test_erc20_deposit.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ package e2etests
33
import (
44
"math/big"
55

6+
ethcommon "github.com/ethereum/go-ethereum/common"
67
"github.com/zeta-chain/zetacore/e2e/runner"
78
"github.com/zeta-chain/zetacore/e2e/utils"
9+
"github.com/zeta-chain/zetacore/zetaclient/testutils"
810
)
911

1012
func TestERC20Deposit(r *runner.E2ERunner) {
11-
hash := r.DepositERC20WithAmountAndMessage(big.NewInt(100000), []byte{})
13+
hash := r.DepositERC20WithAmountAndMessage(r.DeployerAddress, big.NewInt(100000), []byte{})
1214

1315
// wait for the cctx to be mined
1416
cctx := utils.WaitCctxMinedByInTxHash(r.Ctx, hash.Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
1517
r.Logger.CCTX(*cctx, "deposit")
1618
}
19+
20+
func TestERC20DepositRestricted(r *runner.E2ERunner) {
21+
// deposit ERC20 to restricted address
22+
r.DepositERC20WithAmountAndMessage(ethcommon.HexToAddress(testutils.RestrictedEVMAddressTest), big.NewInt(100000), []byte{})
23+
}

e2e/e2etests/test_erc20_refund.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func TestERC20DepositAndCallRefund(r *runner.E2ERunner) {
137137

138138
func createZetaERC20LiquidityPool(r *runner.E2ERunner) error {
139139
amount := big.NewInt(1e10)
140-
txHash := r.DepositERC20WithAmountAndMessage(amount, []byte{})
140+
txHash := r.DepositERC20WithAmountAndMessage(r.DeployerAddress, amount, []byte{})
141141
utils.WaitCctxMinedByInTxHash(r.Ctx, txHash.Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
142142

143143
tx, err := r.USDTZRC20.Approve(r.ZevmAuth, r.UniswapV2RouterAddr, big.NewInt(1e10))

e2e/e2etests/test_eth_withdraw.go

+43
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package e2etests
33
import (
44
"math/big"
55

6+
ethcommon "github.com/ethereum/go-ethereum/common"
67
"github.com/zeta-chain/zetacore/e2e/runner"
78
"github.com/zeta-chain/zetacore/e2e/utils"
89
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
10+
"github.com/zeta-chain/zetacore/zetaclient/testutils"
911
)
1012

1113
// TestEtherWithdraw tests the withdraw of ether
@@ -44,3 +46,44 @@ func TestEtherWithdraw(r *runner.E2ERunner) {
4446
panic("cctx status is not outbound mined")
4547
}
4648
}
49+
50+
// TestEtherWithdrawRestricted tests the withdrawal to a restricted receiver address
51+
func TestEtherWithdrawRestricted(r *runner.E2ERunner) {
52+
// approve
53+
tx, err := r.ETHZRC20.Approve(r.ZevmAuth, r.ETHZRC20Addr, big.NewInt(1e18))
54+
if err != nil {
55+
panic(err)
56+
}
57+
r.Logger.EVMTransaction(*tx, "approve")
58+
59+
receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZevmClient, tx, r.Logger, r.ReceiptTimeout)
60+
if receipt.Status == 0 {
61+
panic("approve failed")
62+
}
63+
r.Logger.EVMReceipt(*receipt, "approve")
64+
65+
// withdraw
66+
restrictedAddress := ethcommon.HexToAddress(testutils.RestrictedEVMAddressTest)
67+
tx, err = r.ETHZRC20.Withdraw(r.ZevmAuth, restrictedAddress.Bytes(), big.NewInt(100000))
68+
if err != nil {
69+
panic(err)
70+
}
71+
r.Logger.EVMTransaction(*tx, "withdraw to restricted address")
72+
73+
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZevmClient, tx, r.Logger, r.ReceiptTimeout)
74+
if receipt.Status == 0 {
75+
panic("withdraw failed")
76+
}
77+
r.Logger.EVMReceipt(*receipt, "withdraw")
78+
r.Logger.ZRC20Withdrawal(r.ETHZRC20, *receipt, "withdraw")
79+
80+
// verify the withdraw value
81+
cctx := utils.WaitCctxMinedByInTxHash(r.Ctx, receipt.TxHash.Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
82+
r.Logger.CCTX(*cctx, "withdraw")
83+
if cctx.CctxStatus.Status != crosschaintypes.CctxStatus_OutboundMined {
84+
panic("cctx status is not outbound mined")
85+
}
86+
87+
// the cctx should be cancelled with zero value
88+
verifyTransferAmountFromCCTX(r, cctx, 0)
89+
}

e2e/e2etests/test_stress_eth_deposit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestStressEtherDeposit(r *runner.E2ERunner) {
2525
// send the deposits
2626
for i := 0; i < numDeposits; i++ {
2727
i := i
28-
hash := r.DepositERC20WithAmountAndMessage(big.NewInt(100000), []byte{})
28+
hash := r.DepositERC20WithAmountAndMessage(r.DeployerAddress, big.NewInt(100000), []byte{})
2929
r.Logger.Print("index %d: starting deposit, tx hash: %s", i, hash.Hex())
3030

3131
eg.Go(func() error {

e2e/e2etests/test_zeta_deposit.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@ package e2etests
33
import (
44
"math/big"
55

6+
ethcommon "github.com/ethereum/go-ethereum/common"
67
"github.com/zeta-chain/zetacore/e2e/runner"
78
"github.com/zeta-chain/zetacore/e2e/utils"
9+
"github.com/zeta-chain/zetacore/zetaclient/testutils"
810
)
911

1012
func TestZetaDeposit(r *runner.E2ERunner) {
1113
// Deposit 1 Zeta
12-
hash := r.DepositZetaWithAmount(big.NewInt(1e18))
14+
hash := r.DepositZetaWithAmount(r.DeployerAddress, big.NewInt(1e18))
1315

1416
// wait for the cctx to be mined
1517
cctx := utils.WaitCctxMinedByInTxHash(r.Ctx, hash.Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
1618
r.Logger.CCTX(*cctx, "deposit")
1719
}
20+
21+
func TestZetaDepositRestricted(r *runner.E2ERunner) {
22+
// Deposit 1 Zeta to restricted address
23+
r.DepositZetaWithAmount(ethcommon.HexToAddress(testutils.RestrictedEVMAddressTest), big.NewInt(1e18))
24+
}

e2e/runner/evm.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ func (runner *E2ERunner) SendUSDTOnEvm(address ethcommon.Address, amountUSDT int
7272
func (runner *E2ERunner) DepositERC20() ethcommon.Hash {
7373
runner.Logger.Print("⏳ depositing ERC20 into ZEVM")
7474

75-
return runner.DepositERC20WithAmountAndMessage(big.NewInt(1e18), []byte{})
75+
return runner.DepositERC20WithAmountAndMessage(runner.DeployerAddress, big.NewInt(1e18), []byte{})
7676
}
7777

78-
func (runner *E2ERunner) DepositERC20WithAmountAndMessage(amount *big.Int, msg []byte) ethcommon.Hash {
78+
func (runner *E2ERunner) DepositERC20WithAmountAndMessage(to ethcommon.Address, amount *big.Int, msg []byte) ethcommon.Hash {
7979
// reset allowance, necessary for USDT
8080
tx, err := runner.USDTERC20.Approve(runner.GoerliAuth, runner.ERC20CustodyAddr, big.NewInt(0))
8181
if err != nil {
@@ -97,7 +97,7 @@ func (runner *E2ERunner) DepositERC20WithAmountAndMessage(amount *big.Int, msg [
9797
}
9898
runner.Logger.Info("USDT Approve receipt tx hash: %s", tx.Hash().Hex())
9999

100-
tx, err = runner.ERC20Custody.Deposit(runner.GoerliAuth, runner.DeployerAddress.Bytes(), runner.USDTERC20Addr, amount, msg)
100+
tx, err = runner.ERC20Custody.Deposit(runner.GoerliAuth, to.Bytes(), runner.USDTERC20Addr, amount, msg)
101101
runner.Logger.Print("TX: %v", tx)
102102
if err != nil {
103103
panic(err)

e2e/runner/zeta.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ func (runner *E2ERunner) DepositZeta() ethcommon.Hash {
5151
amount := big.NewInt(1e18)
5252
amount = amount.Mul(amount, big.NewInt(100)) // 100 Zeta
5353

54-
return runner.DepositZetaWithAmount(amount)
54+
return runner.DepositZetaWithAmount(runner.DeployerAddress, amount)
5555
}
5656

5757
// DepositZetaWithAmount deposits ZETA on ZetaChain from the ZETA smart contract on EVM with the specified amount
58-
func (runner *E2ERunner) DepositZetaWithAmount(amount *big.Int) ethcommon.Hash {
58+
func (runner *E2ERunner) DepositZetaWithAmount(to ethcommon.Address, amount *big.Int) ethcommon.Hash {
5959
tx, err := runner.ZetaEth.Approve(runner.GoerliAuth, runner.ConnectorEthAddr, amount)
6060
if err != nil {
6161
panic(err)
@@ -78,7 +78,7 @@ func (runner *E2ERunner) DepositZetaWithAmount(amount *big.Int) ethcommon.Hash {
7878
// TODO: allow user to specify destination chain id
7979
// https://github.com/zeta-chain/node-private/issues/41
8080
DestinationChainId: zetaChainID,
81-
DestinationAddress: runner.DeployerAddress.Bytes(),
81+
DestinationAddress: to.Bytes(),
8282
DestinationGasLimit: big.NewInt(250_000),
8383
Message: nil,
8484
ZetaValueAndGas: amount,

0 commit comments

Comments
 (0)