Skip to content

Commit

Permalink
refactor: remove params from config and introduce app context (#1774)
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito authored Feb 27, 2024
1 parent 3ba44a1 commit cfd27a7
Show file tree
Hide file tree
Showing 20 changed files with 362 additions and 266 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Refactor

* [1783](https://github.com/zeta-chain/node/pull/1783) - refactor zetaclient metrics naming and structure
* [1774](https://github.com/zeta-chain/node/pull/1774) - split params and config in zetaclient

### Tests

Expand Down
11 changes: 8 additions & 3 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"

"github.com/zeta-chain/zetacore/zetaclient/bitcoin"
corecontext "github.com/zeta-chain/zetacore/zetaclient/core_context"
"github.com/zeta-chain/zetacore/zetaclient/evm"
"github.com/zeta-chain/zetacore/zetaclient/keys"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
Expand Down Expand Up @@ -51,6 +52,7 @@ func DebugCmd() *cobra.Command {
if err != nil {
return err
}
coreContext := corecontext.NewZetaCoreContext(cfg)
chainID, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
Expand Down Expand Up @@ -122,14 +124,17 @@ func DebugCmd() *cobra.Command {

for _, chainParams := range chainParams {
if chainParams.ChainId == chainID {
ob.WithParams(observertypes.ChainParams{
ob.SetChainParams(observertypes.ChainParams{
ChainId: chainID,
ConnectorContractAddress: chainParams.ConnectorContractAddress,
ZetaTokenContractAddress: chainParams.ZetaTokenContractAddress,
Erc20CustodyContractAddress: chainParams.Erc20CustodyContractAddress,
})
cfg.EVMChainConfigs[chainID].ZetaTokenContractAddress = chainParams.ZetaTokenContractAddress
ob.SetConfig(cfg)
evmChainParams, found := coreContext.GetEVMChainParams(chainID)
if !found {
return fmt.Errorf("missing chain params for chain %d", chainID)
}
evmChainParams.ZetaTokenContractAddress = chainParams.ZetaTokenContractAddress
if strings.EqualFold(tx.To().Hex(), chainParams.ConnectorContractAddress) {
coinType = common.CoinType_Zeta
} else if strings.EqualFold(tx.To().Hex(), chainParams.Erc20CustodyContractAddress) {
Expand Down
24 changes: 12 additions & 12 deletions cmd/zetaclientd/keygen_tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"time"

appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
mc "github.com/zeta-chain/zetacore/zetaclient/tss"
"github.com/zeta-chain/zetacore/zetaclient/zetabridge"

Expand All @@ -18,12 +19,12 @@ import (
"github.com/zeta-chain/go-tss/p2p"
"github.com/zeta-chain/zetacore/common"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
)

func GenerateTss(logger zerolog.Logger,
cfg *config.Config,
func GenerateTss(
appContext *appcontext.AppContext,
logger zerolog.Logger,
zetaBridge *zetabridge.ZetaCoreBridge,
peers p2p.AddrList,
priKey secp256k1.PrivKey,
Expand All @@ -37,15 +38,16 @@ func GenerateTss(logger zerolog.Logger,
// TODO: remove this once we have a better way to determine the signature format
// https://github.com/zeta-chain/node/issues/1397
bitcoinChainID := common.BtcRegtestChain().ChainId
if cfg.BitcoinConfig != nil {
bitcoinChainID = cfg.BitcoinConfig.ChainId
btcChain, _, btcEnabled := appContext.GetBTCChainAndConfig()
if btcEnabled {
bitcoinChainID = btcChain.ChainId
}

tss, err := mc.NewTSS(
appContext,
peers,
priKey,
preParams,
cfg,
zetaBridge,
tssHistoricalList,
bitcoinChainID,
Expand All @@ -72,7 +74,7 @@ func GenerateTss(logger zerolog.Logger,
// This loop will try keygen at the keygen block and then wait for keygen to be successfully reported by all nodes before breaking out of the loop.
// If keygen is unsuccessful, it will reset the triedKeygenAtBlock flag and try again at a new keygen block.

keyGen := cfg.GetKeygen()
keyGen := appContext.ZetaCoreContext().GetKeygen()
if keyGen.Status == observertypes.KeygenStatus_KeyGenSuccess {
return tss, nil
}
Expand All @@ -98,13 +100,13 @@ func GenerateTss(logger zerolog.Logger,
if currentBlock != keyGen.BlockNumber {
if currentBlock > lastBlock {
lastBlock = currentBlock
keygenLogger.Info().Msgf("Waiting For Keygen Block to arrive or new keygen block to be set. Keygen Block : %d Current Block : %d ChainID %s ", keyGen.BlockNumber, currentBlock, cfg.ChainID)
keygenLogger.Info().Msgf("Waiting For Keygen Block to arrive or new keygen block to be set. Keygen Block : %d Current Block : %d ChainID %s ", keyGen.BlockNumber, currentBlock, appContext.Config().ChainID)
}
continue
}
// Try keygen only once at a particular block, irrespective of whether it is successful or failure
triedKeygenAtBlock = true
err = keygenTss(cfg, tss, keygenLogger)
err = keygenTss(keyGen, tss, keygenLogger)
if err != nil {
keygenLogger.Error().Err(err).Msg("keygenTss error")
tssFailedVoteHash, err := zetaBridge.SetTSS("", keyGen.BlockNumber, common.ReceiveStatus_Failed)
Expand Down Expand Up @@ -147,9 +149,7 @@ func GenerateTss(logger zerolog.Logger,
return nil, errors.New("unexpected state for TSS generation")
}

func keygenTss(cfg *config.Config, tss *mc.TSS, keygenLogger zerolog.Logger) error {

keyGen := cfg.GetKeygen()
func keygenTss(keyGen observertypes.Keygen, tss *mc.TSS, keygenLogger zerolog.Logger) error {
keygenLogger.Info().Msgf("Keygen at blocknum %d , TSS signers %s ", keyGen.BlockNumber, keyGen.GranteePubkeys)
var req keygen.Request
req = keygen.NewRequest(keyGen.GranteePubkeys, keyGen.BlockNumber, "0.14.0")
Expand Down
30 changes: 16 additions & 14 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"github.com/zeta-chain/zetacore/common"
observerTypes "github.com/zeta-chain/zetacore/x/observer/types"
mc "github.com/zeta-chain/zetacore/zetaclient"
appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
"github.com/zeta-chain/zetacore/zetaclient/config"
corecontext "github.com/zeta-chain/zetacore/zetaclient/core_context"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
)

Expand Down Expand Up @@ -119,15 +121,15 @@ func start(_ *cobra.Command, _ []string) error {
startLogger.Debug().Msgf("CreateAuthzSigner is ready")

// Initialize core parameters from zetacore
err = zetaBridge.UpdateConfigFromCore(cfg, true)
appContext := appcontext.NewAppContext(corecontext.NewZetaCoreContext(cfg), cfg)
err = zetaBridge.UpdateZetaCoreContext(appContext.ZetaCoreContext(), true)
if err != nil {
startLogger.Error().Err(err).Msg("Error getting core parameters")
return err
}
startLogger.Info().Msgf("Config is updated from ZetaCore %s", maskCfg(cfg))

// ConfigUpdater: A polling goroutine checks and updates core parameters at every height. Zetacore stores core parameters for all clients
go zetaBridge.ConfigUpdater(cfg)
go zetaBridge.CoreContextUpdater(appContext)

// Generate TSS address . The Tss address is generated through Keygen ceremony. The TSS key is used to sign all outbound transactions .
// The bridgePk is private key for the Hotkey. The Hotkey is used to sign all inbound transactions
Expand Down Expand Up @@ -173,7 +175,7 @@ func start(_ *cobra.Command, _ []string) error {
}

telemetryServer.SetIPAddress(cfg.PublicIP)
tss, err := GenerateTss(masterLogger, cfg, zetaBridge, peers, priKey, telemetryServer, tssHistoricalList, tssKeyPass, hotkeyPass)
tss, err := GenerateTss(appContext, masterLogger, zetaBridge, peers, priKey, telemetryServer, tssHistoricalList, tssKeyPass, hotkeyPass)
if err != nil {
return err
}
Expand All @@ -188,8 +190,8 @@ func start(_ *cobra.Command, _ []string) error {
// For existing keygen, this should directly proceed to the next step
ticker := time.NewTicker(time.Second * 1)
for range ticker.C {
if cfg.Keygen.Status != observerTypes.KeygenStatus_KeyGenSuccess {
startLogger.Info().Msgf("Waiting for TSS Keygen to be a success, current status %s", cfg.Keygen.Status)
if appContext.ZetaCoreContext().GetKeygen().Status != observerTypes.KeygenStatus_KeyGenSuccess {
startLogger.Info().Msgf("Waiting for TSS Keygen to be a success, current status %s", appContext.ZetaCoreContext().GetKeygen().Status)
continue
}
break
Expand All @@ -207,7 +209,7 @@ func start(_ *cobra.Command, _ []string) error {
// Defensive check: Make sure the tss address is set to the current TSS address and not the newly generated one
tss.CurrentPubkey = currentTss.TssPubkey
startLogger.Info().Msgf("Current TSS address \n ETH : %s \n BTC : %s \n PubKey : %s ", tss.EVMAddress(), tss.BTCAddress(), tss.CurrentPubkey)
if len(cfg.ChainsEnabled) == 0 {
if len(appContext.ZetaCoreContext().GetEnabledChains()) == 0 {
startLogger.Error().Msgf("No chains enabled in updated config %s ", cfg.String())
}

Expand All @@ -224,8 +226,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, loggers, 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(appContext, tss, loggers, telemetryServer)
if err != nil {
log.Error().Err(err).Msg("CreateSignerMap")
return err
Expand All @@ -238,10 +240,10 @@ 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, loggers, 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(appContext, zetaBridge, tss, dbpath, loggers, telemetryServer)
if err != nil {
startLogger.Err(err).Msg("CreateSignerMap")
startLogger.Err(err).Msg("CreateChainClientMap")
return err
}

Expand All @@ -255,8 +257,8 @@ func start(_ *cobra.Command, _ []string) error {
}

// CreateCoreObserver : Core observer wraps the zetacore bridge and adds the client and signer maps to it . This is the high level object used for CCTX interactions
mo1 := mc.NewCoreObserver(zetaBridge, signerMap, chainClientMap, masterLogger, cfg, telemetryServer)
mo1.MonitorCore()
mo1 := mc.NewCoreObserver(zetaBridge, signerMap, chainClientMap, masterLogger, telemetryServer)
mo1.MonitorCore(appContext)

// start zeta supply checker
// TODO: enable
Expand Down
5 changes: 2 additions & 3 deletions cmd/zetaclientd/start_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ func maskCfg(cfg *config.Config) string {
maskedCfg.EVMChainConfigs = map[int64]*config.EVMConfig{}
for key, val := range cfg.EVMChainConfigs {
maskedCfg.EVMChainConfigs[key] = &config.EVMConfig{
ChainParams: val.ChainParams,
Chain: val.Chain,
Endpoint: val.Endpoint,
Chain: val.Chain,
Endpoint: val.Endpoint,
}
}

Expand Down
26 changes: 16 additions & 10 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/common/cosmos"
appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
"github.com/zeta-chain/zetacore/zetaclient/authz"
"github.com/zeta-chain/zetacore/zetaclient/bitcoin"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
Expand Down Expand Up @@ -50,19 +51,24 @@ func CreateZetaBridge(cfg *config.Config, telemetry *metrics.TelemetryServer, ho
}

func CreateSignerMap(
appContext *appcontext.AppContext,
tss interfaces.TSSSigner,
loggers clientcommon.ClientLogger,
cfg *config.Config,
ts *metrics.TelemetryServer,
) (map[common.Chain]interfaces.ChainSigner, error) {
signerMap := make(map[common.Chain]interfaces.ChainSigner)
// EVM signers
for _, evmConfig := range cfg.GetAllEVMConfigs() {
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
continue
}
mpiAddress := ethcommon.HexToAddress(evmConfig.ChainParams.ConnectorContractAddress)
erc20CustodyAddress := ethcommon.HexToAddress(evmConfig.ChainParams.Erc20CustodyContractAddress)
evmChainParams, found := appContext.ZetaCoreContext().GetEVMChainParams(evmConfig.Chain.ChainId)
if !found {
loggers.Std.Error().Msgf("ChainParam not found for chain %s", evmConfig.Chain.String())
continue
}
mpiAddress := ethcommon.HexToAddress(evmChainParams.ConnectorContractAddress)
erc20CustodyAddress := ethcommon.HexToAddress(evmChainParams.Erc20CustodyContractAddress)
signer, err := evm.NewEVMSigner(evmConfig.Chain, evmConfig.Endpoint, tss, config.GetConnectorABI(), config.GetERC20CustodyABI(), mpiAddress, erc20CustodyAddress, loggers, ts)
if err != nil {
loggers.Std.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String())
Expand All @@ -71,7 +77,7 @@ func CreateSignerMap(
signerMap[evmConfig.Chain] = signer
}
// BTC signer
btcChain, btcConfig, enabled := cfg.GetBTCConfig()
btcChain, btcConfig, enabled := appContext.GetBTCChainAndConfig()
if enabled {
signer, err := bitcoin.NewBTCSigner(btcConfig, tss, loggers, ts)
if err != nil {
Expand All @@ -85,30 +91,30 @@ func CreateSignerMap(
}

func CreateChainClientMap(
appContext *appcontext.AppContext,
bridge *zetabridge.ZetaCoreBridge,
tss interfaces.TSSSigner,
dbpath string,
loggers clientcommon.ClientLogger,
cfg *config.Config,
ts *metrics.TelemetryServer,
) (map[common.Chain]interfaces.ChainClient, error) {
clientMap := make(map[common.Chain]interfaces.ChainClient)
// EVM clients
for _, evmConfig := range cfg.GetAllEVMConfigs() {
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
continue
}
co, err := evm.NewEVMChainClient(bridge, tss, dbpath, loggers, cfg, *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
}
clientMap[evmConfig.Chain] = co
}
// BTC client
btcChain, btcConfig, enabled := cfg.GetBTCConfig()
btcChain, _, enabled := appContext.GetBTCChainAndConfig()
if enabled {
co, err := bitcoin.NewBitcoinClient(btcChain, bridge, tss, dbpath, loggers, btcConfig, ts)
co, err := bitcoin.NewBitcoinClient(appContext, btcChain, bridge, tss, dbpath, loggers, ts)
if err != nil {
loggers.Std.Error().Err(err).Msgf("NewBitcoinClient error for chain %s", btcChain.String())

Expand Down
44 changes: 44 additions & 0 deletions zetaclient/app_context/app_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package appcontext

import (
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/zetaclient/config"
corecontext "github.com/zeta-chain/zetacore/zetaclient/core_context"
)

// AppContext contains global app structs like config, core context and logger
type AppContext struct {
coreContext *corecontext.ZetaCoreContext
config *config.Config
}

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

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

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()
btcChain, _, paramsEnabled := a.ZetaCoreContext().GetBTCChainParams()

if !configEnabled || !paramsEnabled {
return common.Chain{}, config.BTCConfig{}, false
}

return btcChain, btcConfig, true
}
Loading

0 comments on commit cfd27a7

Please sign in to comment.