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

feat: initiated feature of zetaclient-restricted-address #1790

Merged
merged 10 commits into from
Feb 26, 2024
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Features

*[1728] (https://github.com/zeta-chain/node/pull/1728) - allow aborted transactions to be refunded by minting tokens to zEvm.
*[1789] (https://github.com/zeta-chain/node/pull/1789) - block cross-chain transactions that involved banned addresses
lumtis marked this conversation as resolved.
Show resolved Hide resolved

### Refactor
* [1766](https://github.com/zeta-chain/node/pull/1766) - Refactors the `PostTxProcessing` EVM hook functionality to deal with invalid withdraw events
Expand Down
2 changes: 2 additions & 0 deletions cmd/zetaclientd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
)

var InitCmd = &cobra.Command{
Expand Down Expand Up @@ -95,6 +96,7 @@ func Initialize(_ *cobra.Command, _ []string) error {
configData.KeyringBackend = config.KeyringBackend(initArgs.KeyringBackend)
configData.HsmMode = initArgs.HsmMode
configData.HsmHotKey = initArgs.HsmHotKey
configData.ComplianceConfig = testutils.ComplianceConfigTest()

//Save config file
return config.Save(&configData, rootArgs.zetaCoreHome)
Expand Down
47 changes: 45 additions & 2 deletions cmd/zetaclientd/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package main

import (
"path/filepath"

"github.com/rs/zerolog/log"

ecdsakeygen "github.com/binance-chain/tss-lib/ecdsa/keygen"
"github.com/cosmos/cosmos-sdk/server"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/rs/zerolog"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/config"

"github.com/zeta-chain/zetacore/cmd"
Expand All @@ -20,6 +25,10 @@ import (
"github.com/zeta-chain/zetacore/app"
)

const (
ComplianceLogFile = "compliance.log"
)

var (
preParams *ecdsakeygen.LocalPreParams
)
Expand Down Expand Up @@ -51,19 +60,53 @@ func SetupConfigForTest() {

}

func InitLogger(cfg *config.Config) zerolog.Logger {
func InitLogger(cfg *config.Config) (clientcommon.ClientLogger, error) {
// open compliance log file
file, err := OpenComplianceLogFile(cfg)
if err != nil {
return clientcommon.DefaultLoggers(), err
}

var logger zerolog.Logger
var loggerCompliance zerolog.Logger
switch cfg.LogFormat {
case "json":
logger = zerolog.New(os.Stdout).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger()
loggerCompliance = zerolog.New(file).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger()
case "text":
logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger()
loggerCompliance = zerolog.New(file).Level(zerolog.Level(cfg.LogLevel)).With().Timestamp().Logger()
default:
logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339})
loggerCompliance = zerolog.New(file).With().Timestamp().Logger()
}

if cfg.LogSampler {
logger = logger.Sample(&zerolog.BasicSampler{N: 5})
}
return logger
log.Logger = logger // set global logger

return clientcommon.ClientLogger{
Std: log.Logger,
Compliance: loggerCompliance,
}, nil
}

func OpenComplianceLogFile(cfg *config.Config) (*os.File, error) {
// use zetacore home as default
logPath := cfg.ZetaCoreHome
if cfg.ComplianceConfig != nil && cfg.ComplianceConfig.LogPath != "" {
logPath = cfg.ComplianceConfig.LogPath
}

// clean file name
name := filepath.Join(logPath, ComplianceLogFile)
name, err := filepath.Abs(name)
if err != nil {
return nil, err
}
name = filepath.Clean(name)

// open (or create) compliance log file
return os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
}
18 changes: 12 additions & 6 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ func start(_ *cobra.Command, _ []string) error {
if err != nil {
return err
}
log.Logger = InitLogger(cfg)
loggers, err := InitLogger(cfg)
if err != nil {
log.Error().Err(err).Msg("InitLogger failed")
return err
}

//Wait until zetacore has started
if len(cfg.Peer) != 0 {
err := validatePeer(cfg.Peer)
Expand All @@ -67,9 +72,10 @@ func start(_ *cobra.Command, _ []string) error {
}
}

masterLogger := log.Logger
masterLogger := loggers.Std
startLogger := masterLogger.With().Str("module", "startup").Logger()

// Wait until zetacore is up
waitForZetaCore(cfg, startLogger)
startLogger.Info().Msgf("ZetaCore is ready , Trying to connect to %s", cfg.Peer)

Expand Down Expand Up @@ -218,8 +224,8 @@ func start(_ *cobra.Command, _ []string) error {
}
}

// CreateSignerMap: This creates a map of all signers for each chain . Each signer is responsible for signing transactions for a particular chain
signerMap, err := CreateSignerMap(tss, masterLogger, cfg, telemetryServer)
// CreateSignerMap: This creates a map of all signers for each chain. Each signer is responsible for signing transactions for a particular chain
signerMap, err := CreateSignerMap(tss, loggers, cfg, telemetryServer)
if err != nil {
log.Error().Err(err).Msg("CreateSignerMap")
return err
Expand All @@ -232,8 +238,8 @@ func start(_ *cobra.Command, _ []string) error {
}
dbpath := filepath.Join(userDir, ".zetaclient/chainobserver")

// CreateChainClientMap : This creates a map of all chain clients . Each chain client is responsible for listening to events on the chain and processing them
chainClientMap, err := CreateChainClientMap(zetaBridge, tss, dbpath, metrics, masterLogger, cfg, telemetryServer)
// CreateChainClientMap : This creates a map of all chain clients. Each chain client is responsible for listening to events on the chain and processing them
chainClientMap, err := CreateChainClientMap(zetaBridge, tss, dbpath, metrics, loggers, cfg, telemetryServer)
if err != nil {
startLogger.Err(err).Msg("CreateSignerMap")
return err
Expand Down
22 changes: 11 additions & 11 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/common/cosmos"
"github.com/zeta-chain/zetacore/zetaclient/authz"
"github.com/zeta-chain/zetacore/zetaclient/bitcoin"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/keys"
Expand Down Expand Up @@ -51,7 +51,7 @@ func CreateZetaBridge(cfg *config.Config, telemetry *metrics.TelemetryServer, ho

func CreateSignerMap(
tss interfaces.TSSSigner,
logger zerolog.Logger,
loggers clientcommon.ClientLogger,
cfg *config.Config,
ts *metrics.TelemetryServer,
) (map[common.Chain]interfaces.ChainSigner, error) {
Expand All @@ -63,19 +63,19 @@ func CreateSignerMap(
}
mpiAddress := ethcommon.HexToAddress(evmConfig.ChainParams.ConnectorContractAddress)
erc20CustodyAddress := ethcommon.HexToAddress(evmConfig.ChainParams.Erc20CustodyContractAddress)
signer, err := evm.NewEVMSigner(evmConfig.Chain, evmConfig.Endpoint, tss, config.GetConnectorABI(), config.GetERC20CustodyABI(), mpiAddress, erc20CustodyAddress, logger, ts)
signer, err := evm.NewEVMSigner(evmConfig.Chain, evmConfig.Endpoint, tss, config.GetConnectorABI(), config.GetERC20CustodyABI(), mpiAddress, erc20CustodyAddress, loggers, ts)
if err != nil {
logger.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String())
loggers.Std.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String())
continue
}
signerMap[evmConfig.Chain] = signer
}
// BTC signer
btcChain, btcConfig, enabled := cfg.GetBTCConfig()
if enabled {
signer, err := bitcoin.NewBTCSigner(btcConfig, tss, logger, ts)
signer, err := bitcoin.NewBTCSigner(btcConfig, tss, loggers, ts)
if err != nil {
logger.Error().Err(err).Msgf("NewBTCSigner error for chain %s", btcChain.String())
loggers.Std.Error().Err(err).Msgf("NewBTCSigner error for chain %s", btcChain.String())
} else {
signerMap[btcChain] = signer
}
Expand All @@ -89,7 +89,7 @@ func CreateChainClientMap(
tss interfaces.TSSSigner,
dbpath string,
metrics *metrics.Metrics,
logger zerolog.Logger,
loggers clientcommon.ClientLogger,
cfg *config.Config,
ts *metrics.TelemetryServer,
) (map[common.Chain]interfaces.ChainClient, error) {
Expand All @@ -99,19 +99,19 @@ func CreateChainClientMap(
if evmConfig.Chain.IsZetaChain() {
continue
}
co, err := evm.NewEVMChainClient(bridge, tss, dbpath, metrics, logger, cfg, *evmConfig, ts)
co, err := evm.NewEVMChainClient(bridge, tss, dbpath, metrics, loggers, cfg, *evmConfig, ts)
if err != nil {
logger.Error().Err(err).Msgf("NewEVMChainClient error for chain %s", evmConfig.Chain.String())
loggers.Std.Error().Err(err).Msgf("NewEVMChainClient error for chain %s", evmConfig.Chain.String())
continue
}
clientMap[evmConfig.Chain] = co
}
// BTC client
btcChain, btcConfig, enabled := cfg.GetBTCConfig()
if enabled {
co, err := bitcoin.NewBitcoinClient(btcChain, bridge, tss, dbpath, metrics, logger, btcConfig, ts)
co, err := bitcoin.NewBitcoinClient(btcChain, bridge, tss, dbpath, metrics, loggers, btcConfig, ts)
if err != nil {
logger.Error().Err(err).Msgf("NewBitcoinClient error for chain %s", btcChain.String())
loggers.Std.Error().Err(err).Msgf("NewBitcoinClient error for chain %s", btcChain.String())

} else {
clientMap[btcChain] = co
Expand Down
23 changes: 23 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,26 @@
}
return nil, fmt.Errorf("cannot convert hash to bytes for chain %d", chainID)
}

// ParseAddressAndData parses the message string into an address and data
// message is hex encoded byte array
// [ contractAddress calldata ]
// [ 20B, variable]
func ParseAddressAndData(message string) (ethcommon.Address, []byte, error) {
if len(message) == 0 {
return ethcommon.Address{}, nil, nil

Check warning on line 51 in common/utils.go

View check run for this annotation

Codecov / codecov/patch

common/utils.go#L49-L51

Added lines #L49 - L51 were not covered by tests
}

data, err := hex.DecodeString(message)
if err != nil {
return ethcommon.Address{}, nil, fmt.Errorf("message should be a hex encoded string: " + err.Error())

Check warning on line 56 in common/utils.go

View check run for this annotation

Codecov / codecov/patch

common/utils.go#L54-L56

Added lines #L54 - L56 were not covered by tests
}

if len(data) < 20 {
return ethcommon.Address{}, data, nil

Check warning on line 60 in common/utils.go

View check run for this annotation

Codecov / codecov/patch

common/utils.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}

address := ethcommon.BytesToAddress(data[:20])
data = data[20:]
return address, data, nil

Check warning on line 65 in common/utils.go

View check run for this annotation

Codecov / codecov/patch

common/utils.go#L63-L65

Added lines #L63 - L65 were not covered by tests
}
25 changes: 1 addition & 24 deletions x/crosschain/keeper/evm_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (k Keeper) HandleEVMDeposit(
}
} else {
// cointype is Gas or ERC20; then it could be a ZRC20 deposit/depositAndCall cctx.
parsedAddress, data, err := parseAddressAndData(msg.Message)
parsedAddress, data, err := common.ParseAddressAndData(msg.Message)
if err != nil {
return false, errors.Wrap(types.ErrUnableToParseAddress, err.Error())
}
Expand Down Expand Up @@ -110,26 +110,3 @@ func errShouldRevertCctx(err error) bool {
errors.Is(err, fungibletypes.ErrCallNonContract) ||
errors.Is(err, fungibletypes.ErrPausedZRC20)
}

// parseAddressAndData parses the message string into an address and data
// message is hex encoded byte array
// [ contractAddress calldata ]
// [ 20B, variable]
func parseAddressAndData(message string) (ethcommon.Address, []byte, error) {
if len(message) == 0 {
return ethcommon.Address{}, nil, nil
}

data, err := hex.DecodeString(message)
if err != nil {
return ethcommon.Address{}, nil, fmt.Errorf("message should be a hex encoded string: " + err.Error())
}

if len(data) < 20 {
return ethcommon.Address{}, data, nil
}

address := ethcommon.BytesToAddress(data[:20])
data = data[20:]
return address, data, nil
}
Loading
Loading