Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check Solana account rent exempt in zetacore to avoid outbound failure #2545

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
* [2396](https://github.com/zeta-chain/node/issues/2386) - special handle bitcoin testnet gas price estimator
* [2434](https://github.com/zeta-chain/node/pull/2434) - the default database when running `zetacored init` is now pebbledb
* [2481](https://github.com/zeta-chain/node/pull/2481) - increase gas limit inbound and outbound vote message to 500k
* [2545](https://github.com/zeta-chain/node/pull/2545) - check solana minimum rent exempt to avoid outbound failure

### CI

Expand Down
6 changes: 6 additions & 0 deletions pkg/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ const (
CmdMigrateTssFunds = "cmd_migrate_tss_funds"

// BTCWithdrawalDustAmount is the minimum satoshis that can be withdrawn from zEVM to avoid outbound dust output
// The Bitcoin protocol sets a minimum output value to 546 satoshis (dust limit) but we set it to 1000 satoshis
BTCWithdrawalDustAmount = 1000

// SolanaWalletRentExempt is the minimum balance for a Solana wallet account to become rent exempt
// The Solana protocol sets minimum rent exempt to 890880 lamports but we set it to 1_000_000 lamports (0.001 SOL)
// The number 890880 comes from CLI command `solana rent 0` and has been verified on devnet gateway program
SolanaWalletRentExempt = 1_000_000
lumtis marked this conversation as resolved.
Show resolved Hide resolved
)
12 changes: 12 additions & 0 deletions testutil/sample/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"math/big"
"math/rand"
"strings"
"testing"
Expand All @@ -13,6 +14,7 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
zrc20 "github.com/zeta-chain/protocol-contracts/pkg/contracts/zevm/zrc20.sol"

"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/coin"
Expand Down Expand Up @@ -295,6 +297,16 @@ func InboundVote(coinType coin.CoinType, from, to int64) types.MsgVoteInbound {
}
}

func ZRC20Withdrawal(to []byte, value *big.Int) *zrc20.ZRC20Withdrawal {
return &zrc20.ZRC20Withdrawal{
From: EthAddress(),
To: to,
Value: value,
Gasfee: big.NewInt(Int64InRange(100000, 10000000)),
ProtocolFlatFee: big.NewInt(Int64InRange(100000, 10000000)),
}
}
ws4charlie marked this conversation as resolved.
Show resolved Hide resolved

// receiver is 1EYVvXLusCxtVuEwoYvWRyN5EZTXwPVvo3
func GetInvalidZRC20WithdrawToExternal(t *testing.T) (receipt ethtypes.Receipt) {
block := "{\n \"type\": \"0x2\",\n \"root\": \"0x\",\n \"status\": \"0x1\",\n \"cumulativeGasUsed\": \"0x4e7a38\",\n \"logsBloom\": \"0x00000000000000000000010000020000000000000000000000000000000000020000000100000000000000000000000080000000000000000000000400200000200000000002000000000008000000000000000000000000000000000000000000000000020000000000000000800800000040000000000000000010000000000000000000000000000000000000000000000000000004000000000000000000020000000000000000000000000000000000000000000000000000000000010000000002000000000000000000000000000000000000000000000000000020000010000000000000000001000000000000000000040200000000000000000000\",\n \"logs\": [\n {\n \"address\": \"0x13a0c5930c028511dc02665e7285134b6d11a5f4\",\n \"topics\": [\n \"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef\",\n \"0x000000000000000000000000313e74f7755afbae4f90e02ca49f8f09ff934a37\",\n \"0x000000000000000000000000735b14bb79463307aacbed86daf3322b1e6226ab\"\n ],\n \"data\": \"0x0000000000000000000000000000000000000000000000000000000000003790\",\n \"blockNumber\": \"0x1a2ad3\",\n \"transactionHash\": \"0x81126c18c7ca7d1fb7ded6644a87802e91bf52154ee4af7a5b379354e24fb6e0\",\n \"transactionIndex\": \"0x10\",\n \"blockHash\": \"0x5cb338544f64a226f4bfccb7a8d977f861c13ad73f7dd4317b66b00dd95de51c\",\n \"logIndex\": \"0x46\",\n \"removed\": false\n },\n {\n \"address\": \"0x13a0c5930c028511dc02665e7285134b6d11a5f4\",\n \"topics\": [\n \"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925\",\n \"0x000000000000000000000000313e74f7755afbae4f90e02ca49f8f09ff934a37\",\n \"0x00000000000000000000000013a0c5930c028511dc02665e7285134b6d11a5f4\"\n ],\n \"data\": \"0x00000000000000000000000000000000000000000000000000000000006a1217\",\n \"blockNumber\": \"0x1a2ad3\",\n \"transactionHash\": \"0x81126c18c7ca7d1fb7ded6644a87802e91bf52154ee4af7a5b379354e24fb6e0\",\n \"transactionIndex\": \"0x10\",\n \"blockHash\": \"0x5cb338544f64a226f4bfccb7a8d977f861c13ad73f7dd4317b66b00dd95de51c\",\n \"logIndex\": \"0x47\",\n \"removed\": false\n },\n {\n \"address\": \"0x13a0c5930c028511dc02665e7285134b6d11a5f4\",\n \"topics\": [\n \"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef\",\n \"0x000000000000000000000000313e74f7755afbae4f90e02ca49f8f09ff934a37\",\n \"0x0000000000000000000000000000000000000000000000000000000000000000\"\n ],\n \"data\": \"0x00000000000000000000000000000000000000000000000000000000006a0c70\",\n \"blockNumber\": \"0x1a2ad3\",\n \"transactionHash\": \"0x81126c18c7ca7d1fb7ded6644a87802e91bf52154ee4af7a5b379354e24fb6e0\",\n \"transactionIndex\": \"0x10\",\n \"blockHash\": \"0x5cb338544f64a226f4bfccb7a8d977f861c13ad73f7dd4317b66b00dd95de51c\",\n \"logIndex\": \"0x48\",\n \"removed\": false\n },\n {\n \"address\": \"0x13a0c5930c028511dc02665e7285134b6d11a5f4\",\n \"topics\": [\n \"0x9ffbffc04a397460ee1dbe8c9503e098090567d6b7f4b3c02a8617d800b6d955\",\n \"0x000000000000000000000000313e74f7755afbae4f90e02ca49f8f09ff934a37\"\n ],\n \"data\": \"0x000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000006a0c700000000000000000000000000000000000000000000000000000000000003790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000223145595676584c7573437874567545776f59765752794e35455a5458775056766f33000000000000000000000000000000000000000000000000000000000000\",\n \"blockNumber\": \"0x1a2ad3\",\n \"transactionHash\": \"0x81126c18c7ca7d1fb7ded6644a87802e91bf52154ee4af7a5b379354e24fb6e0\",\n \"transactionIndex\": \"0x10\",\n \"blockHash\": \"0x5cb338544f64a226f4bfccb7a8d977f861c13ad73f7dd4317b66b00dd95de51c\",\n \"logIndex\": \"0x49\",\n \"removed\": false\n }\n ],\n \"transactionHash\": \"0x81126c18c7ca7d1fb7ded6644a87802e91bf52154ee4af7a5b379354e24fb6e0\",\n \"contractAddress\": \"0x0000000000000000000000000000000000000000\",\n \"gasUsed\": \"0x12521\",\n \"blockHash\": \"0x5cb338544f64a226f4bfccb7a8d977f861c13ad73f7dd4317b66b00dd95de51c\",\n \"blockNumber\": \"0x1a2ad3\",\n \"transactionIndex\": \"0x10\"\n}\n"
Expand Down
19 changes: 17 additions & 2 deletions x/crosschain/keeper/evm_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,12 @@ func (k Keeper) ProcessZetaSentEvent(
func (k Keeper) ValidateZrc20WithdrawEvent(ctx sdk.Context, event *zrc20.ZRC20Withdrawal, chainID int64) error {
// The event was parsed; that means the user has deposited tokens to the contract.

if chains.IsBitcoinChain(chainID, k.GetAuthorityKeeper().GetAdditionalChainList(ctx)) {
additionalChains := k.GetAuthorityKeeper().GetAdditionalChainList(ctx)
if chains.IsBitcoinChain(chainID, additionalChains) {
if event.Value.Cmp(big.NewInt(constant.BTCWithdrawalDustAmount)) < 0 {
return errorsmod.Wrapf(
types.ErrInvalidWithdrawalAmount,
"withdraw amount %s is less than minimum amount %d",
"withdraw amount %s is less than dust amount %d",
event.Value.String(),
constant.BTCWithdrawalDustAmount,
)
Expand All @@ -292,7 +293,21 @@ func (k Keeper) ValidateZrc20WithdrawEvent(ctx sdk.Context, event *zrc20.ZRC20Wi
if !chains.IsBtcAddressSupported(addr) {
return errorsmod.Wrapf(types.ErrInvalidAddress, "unsupported address %s", string(event.To))
}
} else if chains.IsSolanaChain(chainID, additionalChains) {
if event.Value.Cmp(big.NewInt(constant.SolanaWalletRentExempt)) < 0 {
return errorsmod.Wrapf(
types.ErrInvalidWithdrawalAmount,
"withdraw amount %s is less than rent exempt %d",
event.Value.String(),
constant.SolanaWalletRentExempt,
)
}
_, err := chains.DecodeSolanaWalletAddress(string(event.To))
if err != nil {
return errorsmod.Wrapf(types.ErrInvalidAddress, "invalid address %s", string(event.To))
}
ws4charlie marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

Expand Down
33 changes: 32 additions & 1 deletion x/crosschain/keeper/evm_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestValidateZrc20WithdrawEvent(t *testing.T) {
// 999 satoshis cannot be withdrawn
btcMainNetWithdrawalEvent.Value = big.NewInt(constant.BTCWithdrawalDustAmount - 1)
err = k.ValidateZrc20WithdrawEvent(ctx, btcMainNetWithdrawalEvent, chains.BitcoinMainnet.ChainId)
require.ErrorContains(t, err, "less than minimum amount")
require.ErrorContains(t, err, "less than dust amount")
})

t.Run("unable to validate a event with an invalid chain ID", func(t *testing.T) {
Expand All @@ -204,6 +204,37 @@ func TestValidateZrc20WithdrawEvent(t *testing.T) {
err = k.ValidateZrc20WithdrawEvent(ctx, btcMainNetWithdrawalEvent, chains.BitcoinMainnet.ChainId)
require.ErrorContains(t, err, "unsupported address")
})

t.Run("unable to validate an event with an invalid solana address", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)

// create a withdrawal event with an invalid address (contains invalid character 'l')
to := []byte("DCAK36VfExkPdAkYUQg6ewgxyinvcEyPLyHjRbmveKFl")
value := big.NewInt(constant.SolanaWalletRentExempt)
solWithdrawalEvent := sample.ZRC20Withdrawal(to, value)

err := k.ValidateZrc20WithdrawEvent(ctx, solWithdrawalEvent, chains.SolanaMainnet.ChainId)
require.ErrorContains(t, err, "invalid address")
})

t.Run("unable to validate a solana withdrawal event with an invalid amount", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)

// 1000000 lamports is the minimum amount (rent exempt) that can be withdrawn
chainID := chains.SolanaMainnet.ChainId
to := []byte(sample.SolanaAddress(t))
value := big.NewInt(constant.SolanaWalletRentExempt)
solWithdrawalEvent := sample.ZRC20Withdrawal(to, value)

// 1000000 lamports can be withdrawn
err := k.ValidateZrc20WithdrawEvent(ctx, solWithdrawalEvent, chainID)
require.NoError(t, err)

// 999999 lamports cannot be withdrawn
solWithdrawalEvent.Value = big.NewInt(constant.SolanaWalletRentExempt - 1)
err = k.ValidateZrc20WithdrawEvent(ctx, solWithdrawalEvent, chainID)
require.ErrorContains(t, err, "less than rent exempt")
})
}

func TestKeeper_ProcessZRC20WithdrawalEvent(t *testing.T) {
Expand Down
Loading