Skip to content

Commit

Permalink
Merge branch 'develop' into authorizations-list-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD authored Jun 21, 2024
2 parents 6f8a62e + f9ca2be commit 6cf00b8
Show file tree
Hide file tree
Showing 21 changed files with 315 additions and 185 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* [2317](https://github.com/zeta-chain/node/pull/2317) - add ValidateOutbound method for cctx orchestrator
* [2340](https://github.com/zeta-chain/node/pull/2340) - add ValidateInbound method for cctx orchestrator
* [2344](https://github.com/zeta-chain/node/pull/2344) - group common data of EVM/Bitcoin signer and observer using base structs
* [2357](https://github.com/zeta-chain/node/pull/2357) - integrate base Signer structure into EVM/Bitcoin Signer

### Tests

Expand All @@ -76,6 +77,7 @@
* [2243](https://github.com/zeta-chain/node/pull/2243) - fix incorrect bitcoin outbound height in the CCTX outbound parameter
* [2256](https://github.com/zeta-chain/node/pull/2256) - fix rate limiter falsely included reverted non-withdraw cctxs
* [2327](https://github.com/zeta-chain/node/pull/2327) - partially cherry picked the fix to Bitcoin outbound dust amount
* [2362](https://github.com/zeta-chain/node/pull/2362) - set 1000 satoshis as minimum BTC amount that can be withdrawn from zEVM

### CI

Expand Down
5 changes: 0 additions & 5 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"io"
"strconv"
"strings"
"sync"
Expand All @@ -13,7 +12,6 @@ import (
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/zeta-chain/zetacore/pkg/chains"
Expand Down Expand Up @@ -61,7 +59,6 @@ func DebugCmd() *cobra.Command {
}
inboundHash := args[0]
var ballotIdentifier string
chainLogger := zerolog.New(io.Discard).Level(zerolog.Disabled)

// create a new zetacore client
client, err := zetacore.NewClient(
Expand Down Expand Up @@ -93,7 +90,6 @@ func DebugCmd() *cobra.Command {
Mu: &sync.Mutex{},
}
evmObserver.WithZetacoreClient(client)
evmObserver.WithLogger(chainLogger)
var ethRPC *ethrpc.EthRPC
var client *ethclient.Client
coinType := coin.CoinType_Cmd
Expand Down Expand Up @@ -172,7 +168,6 @@ func DebugCmd() *cobra.Command {
Mu: &sync.Mutex{},
}
btcObserver.WithZetacoreClient(client)
btcObserver.WithLogger(chainLogger)
btcObserver.WithChain(*chains.GetChainFromChainID(chainID))
connCfg := &rpcclient.ConnConfig{
Host: cfg.BitcoinConfig.RPCHost,
Expand Down
17 changes: 9 additions & 8 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,22 @@ func CreateZetacoreClient(
return client, nil
}

// CreateSignerMap creates a map of ChainSigners for all chains in the config
func CreateSignerMap(
appContext *context.AppContext,
tss interfaces.TSSSigner,
logger base.Logger,
ts *metrics.TelemetryServer,
) (map[int64]interfaces.ChainSigner, error) {
coreContext := appContext.ZetacoreContext()
zetacoreContext := appContext.ZetacoreContext()
signerMap := make(map[int64]interfaces.ChainSigner)

// EVM signers
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
continue
}
evmChainParams, found := coreContext.GetEVMChainParams(evmConfig.Chain.ChainId)
evmChainParams, found := zetacoreContext.GetEVMChainParams(evmConfig.Chain.ChainId)
if !found {
logger.Std.Error().Msgf("ChainParam not found for chain %s", evmConfig.Chain.String())
continue
Expand All @@ -77,15 +78,15 @@ func CreateSignerMap(
erc20CustodyAddress := ethcommon.HexToAddress(evmChainParams.Erc20CustodyContractAddress)
signer, err := evmsigner.NewSigner(
evmConfig.Chain,
evmConfig.Endpoint,
zetacoreContext,
tss,
ts,
logger,
evmConfig.Endpoint,
config.GetConnectorABI(),
config.GetERC20CustodyABI(),
mpiAddress,
erc20CustodyAddress,
coreContext,
logger,
ts)
erc20CustodyAddress)
if err != nil {
logger.Std.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String())
continue
Expand All @@ -95,7 +96,7 @@ func CreateSignerMap(
// BTC signer
btcChain, btcConfig, enabled := appContext.GetBTCChainAndConfig()
if enabled {
signer, err := btcsigner.NewSigner(btcConfig, tss, logger, ts, coreContext)
signer, err := btcsigner.NewSigner(btcChain, zetacoreContext, tss, ts, logger, btcConfig)
if err != nil {
logger.Std.Error().Err(err).Msgf("NewBTCSigner error for chain %s", btcChain.String())
} else {
Expand Down
15 changes: 15 additions & 0 deletions testutil/sample/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sample

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

// create a temporary directory for testing
func CreateTempDir(t *testing.T) string {
tempPath, err := os.MkdirTemp("", "tempdir-")
require.NoError(t, err)
return tempPath
}
14 changes: 10 additions & 4 deletions x/crosschain/keeper/evm_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/zeta-chain/zetacore/cmd/zetacored/config"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/coin"
"github.com/zeta-chain/zetacore/pkg/constant"
"github.com/zeta-chain/zetacore/x/crosschain/types"
fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
Expand Down Expand Up @@ -286,15 +287,20 @@ func ValidateZrc20WithdrawEvent(event *zrc20.ZRC20Withdrawal, chainID int64) err
// The event was parsed; that means the user has deposited tokens to the contract.

if chains.IsBitcoinChain(chainID) {
if event.Value.Cmp(big.NewInt(0)) <= 0 {
return fmt.Errorf("ParseZRC20WithdrawalEvent: invalid amount %s", event.Value.String())
if event.Value.Cmp(big.NewInt(constant.BTCWithdrawalDustAmount)) < 0 {
return errorsmod.Wrapf(
types.ErrInvalidWithdrawalAmount,
"withdraw amount %s is less than minimum amount %d",
event.Value.String(),
constant.BTCWithdrawalDustAmount,
)
}
addr, err := chains.DecodeBtcAddress(string(event.To), chainID)
if err != nil {
return fmt.Errorf("ParseZRC20WithdrawalEvent: invalid address %s: %s", event.To, err)
return errorsmod.Wrapf(types.ErrInvalidAddress, "invalid address %s", string(event.To))
}
if !chains.IsBtcAddressSupported(addr) {
return fmt.Errorf("ParseZRC20WithdrawalEvent: unsupported address %s", string(event.To))
return errorsmod.Wrapf(types.ErrInvalidAddress, "unsupported address %s", string(event.To))
}
}
return nil
Expand Down
16 changes: 12 additions & 4 deletions x/crosschain/keeper/evm_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/zeta-chain/zetacore/cmd/zetacored/config"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/constant"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
crosschainkeeper "github.com/zeta-chain/zetacore/x/crosschain/keeper"
Expand Down Expand Up @@ -164,14 +165,21 @@ func TestValidateZrc20WithdrawEvent(t *testing.T) {
require.NoError(t, err)
})

t.Run("unable to validate a event with an invalid amount", func(t *testing.T) {
t.Run("unable to validate a btc withdrawal event with an invalid amount", func(t *testing.T) {
btcMainNetWithdrawalEvent, err := crosschainkeeper.ParseZRC20WithdrawalEvent(
*sample.GetValidZRC20WithdrawToBTC(t).Logs[3],
)
require.NoError(t, err)
btcMainNetWithdrawalEvent.Value = big.NewInt(0)

// 1000 satoshis is the minimum amount that can be withdrawn
btcMainNetWithdrawalEvent.Value = big.NewInt(constant.BTCWithdrawalDustAmount)
err = crosschainkeeper.ValidateZrc20WithdrawEvent(btcMainNetWithdrawalEvent, chains.BitcoinMainnet.ChainId)
require.ErrorContains(t, err, "ParseZRC20WithdrawalEvent: invalid amount")
require.NoError(t, err)

// 999 satoshis cannot be withdrawn
btcMainNetWithdrawalEvent.Value = big.NewInt(constant.BTCWithdrawalDustAmount - 1)
err = crosschainkeeper.ValidateZrc20WithdrawEvent(btcMainNetWithdrawalEvent, chains.BitcoinMainnet.ChainId)
require.ErrorContains(t, err, "less than minimum amount")
})

t.Run("unable to validate a event with an invalid chain ID", func(t *testing.T) {
Expand Down Expand Up @@ -822,7 +830,7 @@ func TestKeeper_ProcessLogs(t *testing.T) {
}

err := k.ProcessLogs(ctx, block.Logs, sample.EthAddress(), "")
require.ErrorContains(t, err, "ParseZRC20WithdrawalEvent: invalid address")
require.ErrorContains(t, err, "invalid address")
cctxList := k.GetAllCrossChainTx(ctx)
require.Len(t, cctxList, 0)
})
Expand Down
1 change: 1 addition & 0 deletions x/crosschain/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ var (
ErrInvalidRateLimiterFlags = errorsmod.Register(ModuleName, 1152, "invalid rate limiter flags")
ErrMaxTxOutTrackerHashesReached = errorsmod.Register(ModuleName, 1153, "max tx out tracker hashes reached")
ErrInitiatitingOutbound = errorsmod.Register(ModuleName, 1154, "cannot initiate outbound")
ErrInvalidWithdrawalAmount = errorsmod.Register(ModuleName, 1155, "invalid withdrawal amount")
)
9 changes: 5 additions & 4 deletions zetaclient/chains/base/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/zetaclient/chains/base"
"github.com/zeta-chain/zetacore/zetaclient/config"
)
Expand All @@ -23,7 +24,7 @@ func TestInitLogger(t *testing.T) {
LogFormat: "json",
LogLevel: 1, // zerolog.InfoLevel,
ComplianceConfig: config.ComplianceConfig{
LogPath: createTempDir(t),
LogPath: sample.CreateTempDir(t),
},
},
fail: false,
Expand All @@ -34,7 +35,7 @@ func TestInitLogger(t *testing.T) {
LogFormat: "text",
LogLevel: 2, // zerolog.WarnLevel,
ComplianceConfig: config.ComplianceConfig{
LogPath: createTempDir(t),
LogPath: sample.CreateTempDir(t),
},
},
fail: false,
Expand All @@ -45,7 +46,7 @@ func TestInitLogger(t *testing.T) {
LogFormat: "unknown",
LogLevel: 3, // zerolog.ErrorLevel,
ComplianceConfig: config.ComplianceConfig{
LogPath: createTempDir(t),
LogPath: sample.CreateTempDir(t),
},
},
fail: false,
Expand All @@ -57,7 +58,7 @@ func TestInitLogger(t *testing.T) {
LogLevel: 4, // zerolog.DebugLevel,
LogSampler: true,
ComplianceConfig: config.ComplianceConfig{
LogPath: createTempDir(t),
LogPath: sample.CreateTempDir(t),
},
},
},
Expand Down
25 changes: 9 additions & 16 deletions zetaclient/chains/base/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ import (
"github.com/zeta-chain/zetacore/zetaclient/testutils/mocks"
)

// create a temporary directory for testing
func createTempDir(t *testing.T) string {
tempPath, err := os.MkdirTemp("", "tempdir-")
require.NoError(t, err)
return tempPath
}

// createObserver creates a new observer for testing
func createObserver(t *testing.T, dbPath string) *base.Observer {
// constructor parameters
Expand Down Expand Up @@ -62,7 +55,7 @@ func TestNewObserver(t *testing.T) {
tss := mocks.NewTSSMainnet()
blockCacheSize := base.DefaultBlockCacheSize
headersCacheSize := base.DefaultHeadersCacheSize
dbPath := createTempDir(t)
dbPath := sample.CreateTempDir(t)

// test cases
tests := []struct {
Expand Down Expand Up @@ -159,7 +152,7 @@ func TestNewObserver(t *testing.T) {
}

func TestObserverGetterAndSetter(t *testing.T) {
dbPath := createTempDir(t)
dbPath := sample.CreateTempDir(t)

t.Run("should be able to update chain", func(t *testing.T) {
ob := createObserver(t, dbPath)
Expand Down Expand Up @@ -258,7 +251,7 @@ func TestObserverGetterAndSetter(t *testing.T) {
}

func TestOpenDB(t *testing.T) {
dbPath := createTempDir(t)
dbPath := sample.CreateTempDir(t)
ob := createObserver(t, dbPath)

t.Run("should be able to open db", func(t *testing.T) {
Expand All @@ -277,7 +270,7 @@ func TestLoadLastBlockScanned(t *testing.T) {

t.Run("should be able to load last block scanned", func(t *testing.T) {
// create db and write 100 as last block scanned
dbPath := createTempDir(t)
dbPath := sample.CreateTempDir(t)
ob := createObserver(t, dbPath)
ob.WriteLastBlockScannedToDB(100)

Expand All @@ -289,7 +282,7 @@ func TestLoadLastBlockScanned(t *testing.T) {
})
t.Run("should use latest block if last block scanned not found", func(t *testing.T) {
// create empty db
dbPath := createTempDir(t)
dbPath := sample.CreateTempDir(t)
ob := createObserver(t, dbPath)

// read last block scanned
Expand All @@ -299,7 +292,7 @@ func TestLoadLastBlockScanned(t *testing.T) {
})
t.Run("should overwrite last block scanned if env var is set", func(t *testing.T) {
// create db and write 100 as last block scanned
dbPath := createTempDir(t)
dbPath := sample.CreateTempDir(t)
ob := createObserver(t, dbPath)
ob.WriteLastBlockScannedToDB(100)

Expand All @@ -322,7 +315,7 @@ func TestLoadLastBlockScanned(t *testing.T) {
})
t.Run("should return error on invalid env var", func(t *testing.T) {
// create db and write 100 as last block scanned
dbPath := createTempDir(t)
dbPath := sample.CreateTempDir(t)
ob := createObserver(t, dbPath)

// set invalid env var
Expand All @@ -338,7 +331,7 @@ func TestLoadLastBlockScanned(t *testing.T) {
func TestReadWriteLastBlockScannedToDB(t *testing.T) {
t.Run("should be able to write and read last block scanned to db", func(t *testing.T) {
// create db and write 100 as last block scanned
dbPath := createTempDir(t)
dbPath := sample.CreateTempDir(t)
ob := createObserver(t, dbPath)
err := ob.WriteLastBlockScannedToDB(100)
require.NoError(t, err)
Expand All @@ -349,7 +342,7 @@ func TestReadWriteLastBlockScannedToDB(t *testing.T) {
})
t.Run("should return error when last block scanned not found in db", func(t *testing.T) {
// create empty db
dbPath := createTempDir(t)
dbPath := sample.CreateTempDir(t)
ob := createObserver(t, dbPath)

lastScannedBlock, err := ob.ReadLastBlockScannedFromDB()
Expand Down
12 changes: 12 additions & 0 deletions zetaclient/chains/base/signer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package base

import (
"sync"

"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/zetaclient/chains/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/context"
Expand All @@ -24,6 +26,10 @@ type Signer struct {

// logger contains the loggers used by signer
logger Logger

// mu protects fields from concurrent access
// Note: base signer simply provides the mutex. It's the sub-struct's responsibility to use it to be thread-safe
mu *sync.Mutex
}

// NewSigner creates a new base signer
Expand All @@ -43,6 +49,7 @@ func NewSigner(
Std: logger.Std.With().Int64("chain", chain.ChainId).Str("module", "signer").Logger(),
Compliance: logger.Compliance,
},
mu: &sync.Mutex{},
}
}

Expand Down Expand Up @@ -94,3 +101,8 @@ func (s *Signer) WithTelemetryServer(ts *metrics.TelemetryServer) *Signer {
func (s *Signer) Logger() *Logger {
return &s.logger
}

// Mu returns the mutex for the signer
func (s *Signer) Mu() *sync.Mutex {
return s.mu
}
4 changes: 4 additions & 0 deletions zetaclient/chains/base/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ func TestSignerGetterAndSetter(t *testing.T) {
logger.Std.Info().Msg("print standard log")
logger.Compliance.Info().Msg("print compliance log")
})
t.Run("should be able to get mutex", func(t *testing.T) {
signer := createSigner(t)
require.NotNil(t, signer.Mu())
})
}
Loading

0 comments on commit 6cf00b8

Please sign in to comment.