Skip to content

Commit

Permalink
Cleanup pointers from config
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Mar 4, 2024
1 parent 52de439 commit 5086f8c
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 110 deletions.
2 changes: 1 addition & 1 deletion cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func DebugCmd() *cobra.Command {
ob.WithLogger(chainLogger)
client := &ethclient.Client{}
coinType := common.CoinType_Cmd
for chain, evmConfig := range cfg.GetAllEVMConfigs() {
for chain, evmConfig := range cfg.EVMChainConfigs {
if chainID == chain {
client, err = ethclient.Dial(evmConfig.Endpoint)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/zetaclientd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func SetupConfigForTest() {

}

func InitLogger(cfg *config.Config) (clientcommon.ClientLogger, error) {
func InitLogger(cfg config.Config) (clientcommon.ClientLogger, error) {
// open compliance log file
file, err := OpenComplianceLogFile(cfg)
if err != nil {
Expand Down Expand Up @@ -92,10 +92,10 @@ func InitLogger(cfg *config.Config) (clientcommon.ClientLogger, error) {
}, nil
}

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/zetaclientd/p2p_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/zeta-chain/zetacore/zetaclient/config"
)

func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, bridgePk cryptotypes.PrivKey, cfg *config.Config) error {
func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, bridgePk cryptotypes.PrivKey, cfg config.Config) error {

startLogger.Warn().Msg("P2P Diagnostic mode enabled")
startLogger.Warn().Msgf("seed peer: %s", peers)
Expand Down
15 changes: 7 additions & 8 deletions cmd/zetaclientd/start_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
"google.golang.org/grpc"
)

func waitForZetaCore(configData *config.Config, logger zerolog.Logger) {
func waitForZetaCore(config config.Config, logger zerolog.Logger) {
// wait until zetacore is up
logger.Debug().Msg("Waiting for ZetaCore to open 9090 port...")
for {
_, err := grpc.Dial(
fmt.Sprintf("%s:9090", configData.ZetaCoreURL),
fmt.Sprintf("%s:9090", config.ZetaCoreURL),
grpc.WithInsecure(),
)
if err != nil {
Expand Down Expand Up @@ -54,20 +54,19 @@ func validatePeer(seedPeer string) error {
// maskCfg sensitive fields are masked, currently only the EVM endpoints and bitcoin credentials,
//
// other fields can be added.
func maskCfg(cfg *config.Config) string {
func maskCfg(cfg config.Config) string {
maskedCfg := config.NewConfig()

// Deep copy since cfg contains some references
*maskedCfg = *cfg
maskedCfg.BitcoinConfig = &config.BTCConfig{
maskedCfg = cfg
maskedCfg.BitcoinConfig = config.BTCConfig{
RPCUsername: cfg.BitcoinConfig.RPCUsername,
RPCPassword: cfg.BitcoinConfig.RPCPassword,
RPCHost: cfg.BitcoinConfig.RPCHost,
RPCParams: cfg.BitcoinConfig.RPCParams,
}
maskedCfg.EVMChainConfigs = map[int64]*config.EVMConfig{}
maskedCfg.EVMChainConfigs = map[int64]config.EVMConfig{}
for key, val := range cfg.EVMChainConfigs {
maskedCfg.EVMChainConfigs[key] = &config.EVMConfig{
maskedCfg.EVMChainConfigs[key] = config.EVMConfig{
Chain: val.Chain,
Endpoint: val.Endpoint,
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func CreateAuthzSigner(granter string, grantee sdk.AccAddress) {
authz.SetupAuthZSignerList(granter, grantee)
}

func CreateZetaBridge(cfg *config.Config, telemetry *metrics.TelemetryServer, hotkeyPassword string) (*zetabridge.ZetaCoreBridge, error) {
func CreateZetaBridge(cfg config.Config, telemetry *metrics.TelemetryServer, hotkeyPassword string) (*zetabridge.ZetaCoreBridge, error) {
hotKey := cfg.AuthzHotkey
if cfg.HsmMode {
hotKey = cfg.HsmHotKey
Expand Down Expand Up @@ -58,7 +58,7 @@ func CreateSignerMap(
) (map[common.Chain]interfaces.ChainSigner, error) {
signerMap := make(map[common.Chain]interfaces.ChainSigner)
// EVM signers
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
for _, evmConfig := range appContext.Config().EVMChainConfigs {
if evmConfig.Chain.IsZetaChain() {
continue
}
Expand Down Expand Up @@ -100,11 +100,11 @@ func CreateChainClientMap(
) (map[common.Chain]interfaces.ChainClient, error) {
clientMap := make(map[common.Chain]interfaces.ChainClient)
// EVM clients
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
for _, evmConfig := range appContext.Config().EVMChainConfigs {
if evmConfig.Chain.IsZetaChain() {
continue
}
co, err := evm.NewEVMChainClient(appContext, bridge, tss, dbpath, loggers, *evmConfig, ts)
co, err := evm.NewEVMChainClient(appContext, bridge, tss, dbpath, loggers, evmConfig, ts)
if err != nil {
loggers.Std.Error().Err(err).Msgf("NewEVMChainClient error for chain %s", evmConfig.Chain.String())
continue
Expand Down
15 changes: 7 additions & 8 deletions zetaclient/app_context/app_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,35 @@ import (
// AppContext contains global app structs like config, core context and logger
type AppContext struct {
coreContext *corecontext.ZetaCoreContext
config *config.Config
config config.Config
}

// NewAppContext creates and returns new AppContext
func NewAppContext(
coreContext *corecontext.ZetaCoreContext,
config *config.Config,
config config.Config,
) *AppContext {
return &AppContext{
coreContext: coreContext,
config: config,
}
}

func (a *AppContext) Config() *config.Config {
func (a AppContext) Config() config.Config {
return a.config
}

func (a *AppContext) ZetaCoreContext() *corecontext.ZetaCoreContext {
func (a AppContext) ZetaCoreContext() *corecontext.ZetaCoreContext {
return a.coreContext
}

// GetBTCChainAndConfig returns btc chain and config if enabled
func (a *AppContext) GetBTCChainAndConfig() (common.Chain, config.BTCConfig, bool) {
btcConfig, configEnabled := a.Config().GetBTCConfig()
func (a AppContext) GetBTCChainAndConfig() (common.Chain, config.BTCConfig, bool) {
btcChain, _, paramsEnabled := a.ZetaCoreContext().GetBTCChainParams()

if !configEnabled || !paramsEnabled {
if !a.config.BTCEnabled() || !paramsEnabled {
return common.Chain{}, config.BTCConfig{}, false
}

return btcChain, btcConfig, true
return btcChain, a.config.BitcoinConfig, true
}
2 changes: 1 addition & 1 deletion zetaclient/bitcoin/bitcoin_client_rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (suite *BitcoinClientTestSuite) SetupTest() {
tss := interfaces.TestSigner{
PrivKey: privateKey,
}
appContext := appcontext.NewAppContext(&corecontext.ZetaCoreContext{}, &config.Config{})
appContext := appcontext.NewAppContext(&corecontext.ZetaCoreContext{}, config.Config{})
client, err := NewBitcoinClient(appContext, common.BtcRegtestChain(), nil, tss, "/tmp", clientcommon.DefaultLoggers(), nil)
suite.Require().NoError(err)
suite.BitcoinChainClient = client
Expand Down
4 changes: 2 additions & 2 deletions zetaclient/common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func TestCctxRestricted(t *testing.T) {
require.NoError(t, err)

// create config
cfg := &config.Config{
ComplianceConfig: &config.ComplianceConfig{},
cfg := config.Config{
ComplianceConfig: config.ComplianceConfig{},
}

t.Run("should return true if sender is restricted", func(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions zetaclient/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,32 @@ func Save(config *Config, path string) error {
}

// Load loads ZetaClient config from a filepath
func Load(path string) (*Config, error) {
func Load(path string) (Config, error) {
// retrieve file
file := filepath.Join(path, folder, filename)
file, err := filepath.Abs(file)
if err != nil {
return nil, err
return Config{}, err
}
file = filepath.Clean(file)

// read config
cfg := NewConfig()
input, err := os.ReadFile(file)
if err != nil {
return nil, err
return Config{}, err
}
err = json.Unmarshal(input, &cfg)
if err != nil {
return nil, err
return Config{}, err
}

// read keyring backend and use test by default
if cfg.KeyringBackend == KeyringBackendUndefined {
cfg.KeyringBackend = KeyringBackendTest
}
if cfg.KeyringBackend != KeyringBackendFile && cfg.KeyringBackend != KeyringBackendTest {
return nil, fmt.Errorf("invalid keyring backend %s", cfg.KeyringBackend)
return Config{}, fmt.Errorf("invalid keyring backend %s", cfg.KeyringBackend)
}

// fields sanitization
Expand All @@ -75,7 +75,7 @@ func Load(path string) (*Config, error) {
return cfg, nil
}

func LoadComplianceConfig(cfg *Config) {
func LoadComplianceConfig(cfg Config) {
restrictedAddressBook = cfg.GetRestrictedAddressBook()
}

Expand Down
4 changes: 2 additions & 2 deletions zetaclient/config/config_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ func New() Config {
}
}

var bitcoinConfigRegnet = &BTCConfig{
var bitcoinConfigRegnet = BTCConfig{
RPCUsername: "e2e",
RPCPassword: "123",
RPCHost: "bitcoin:18443",
RPCParams: "regtest",
}

var evmChainsConfigs = map[int64]*EVMConfig{
var evmChainsConfigs = map[int64]EVMConfig{
common.EthChain().ChainId: {
Chain: common.EthChain(),
},
Expand Down
69 changes: 14 additions & 55 deletions zetaclient/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"strings"
"sync"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/zeta-chain/zetacore/common"
Expand Down Expand Up @@ -51,8 +50,6 @@ type ComplianceConfig struct {
// TODO: use snake case for json fields
// https://github.com/zeta-chain/node/issues/1020
type Config struct {
cfgLock *sync.RWMutex `json:"-"`

Peer string `json:"Peer"`
PublicIP string `json:"PublicIP"`
LogFormat string `json:"LogFormat"`
Expand All @@ -73,79 +70,41 @@ type Config struct {
HsmMode bool `json:"HsmMode"`
HsmHotKey string `json:"HsmHotKey"`

EVMChainConfigs map[int64]*EVMConfig `json:"EVMChainConfigs"`
BitcoinConfig *BTCConfig `json:"BitcoinConfig"`
EVMChainConfigs map[int64]EVMConfig `json:"EVMChainConfigs"`
BitcoinConfig BTCConfig `json:"BitcoinConfig"`

// compliance config
ComplianceConfig *ComplianceConfig `json:"ComplianceConfig"`
ComplianceConfig ComplianceConfig `json:"ComplianceConfig"`
}

func NewConfig() *Config {
return &Config{
cfgLock: &sync.RWMutex{},
}
func NewConfig() Config {
return Config{}
}

func (c Config) BTCEnabled() bool {
return c.BitcoinConfig != BTCConfig{}
}

func (c *Config) String() string {
c.cfgLock.RLock()
defer c.cfgLock.RUnlock()
func (c Config) String() string {
s, err := json.MarshalIndent(c, "", "\t")
if err != nil {
return ""
}
return string(s)
}

func (c *Config) GetEVMConfig(chainID int64) (EVMConfig, bool) {
c.cfgLock.RLock()
defer c.cfgLock.RUnlock()
evmCfg, found := c.EVMChainConfigs[chainID]
return *evmCfg, found
}

func (c *Config) GetAllEVMConfigs() map[int64]*EVMConfig {
c.cfgLock.RLock()
defer c.cfgLock.RUnlock()

// deep copy evm configs
copied := make(map[int64]*EVMConfig, len(c.EVMChainConfigs))
for chainID, evmConfig := range c.EVMChainConfigs {
copied[chainID] = &EVMConfig{}
*copied[chainID] = *evmConfig
}
return copied
}

func (c *Config) GetBTCConfig() (BTCConfig, bool) {
c.cfgLock.RLock()
defer c.cfgLock.RUnlock()

if c.BitcoinConfig == nil { // bitcoin is not enabled
return BTCConfig{}, false
}
return *c.BitcoinConfig, true
}

// GetRestrictedAddressBook returns a map of restricted addresses
// Note: the restricted address book contains both ETH and BTC addresses
func (c *Config) GetRestrictedAddressBook() map[string]bool {
func (c Config) GetRestrictedAddressBook() map[string]bool {
restrictedAddresses := make(map[string]bool)
if c.ComplianceConfig != nil {
for _, address := range c.ComplianceConfig.RestrictedAddresses {
if address != "" {
restrictedAddresses[strings.ToLower(address)] = true
}
for _, address := range c.ComplianceConfig.RestrictedAddresses {
if address != "" {
restrictedAddresses[strings.ToLower(address)] = true
}
}
return restrictedAddresses
}

func (c *Config) GetKeyringBackend() KeyringBackend {
c.cfgLock.RLock()
defer c.cfgLock.RUnlock()
return c.KeyringBackend
}

// ValidateChainParams performs some basic checks on core params
func ValidateChainParams(chainParams *observertypes.ChainParams) error {
if chainParams == nil {
Expand Down
5 changes: 2 additions & 3 deletions zetaclient/core_context/zeta_core_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ type ZetaCoreContext struct {

// NewZetaCoreContext creates and returns new ZetaCoreContext
// it is initializing chain params from provided config
func NewZetaCoreContext(cfg *config.Config) *ZetaCoreContext {
func NewZetaCoreContext(cfg config.Config) *ZetaCoreContext {
evmChainParams := make(map[int64]*observertypes.ChainParams)
for _, e := range cfg.EVMChainConfigs {
evmChainParams[e.Chain.ChainId] = &observertypes.ChainParams{}
}
var bitcoinChainParams *observertypes.ChainParams
_, found := cfg.GetBTCConfig()
if found {
if cfg.BTCEnabled() {
bitcoinChainParams = &observertypes.ChainParams{}
}
return &ZetaCoreContext{
Expand Down
Loading

0 comments on commit 5086f8c

Please sign in to comment.