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

refactor: remove params from config and introduce app context #1774

Merged
merged 28 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
abeacc5
Remove params from config initial split
skosito Feb 14, 2024
9183345
Rename to coreparams
skosito Feb 14, 2024
53455c5
Cleanup and renaming
skosito Feb 14, 2024
b729e97
Merge branch 'develop' into refactor-remove-params-from-config
skosito Feb 15, 2024
c0af1c7
Rename core params to zetaCoreContext
skosito Feb 15, 2024
60ee088
Add missing core context in core observer
skosito Feb 15, 2024
639185f
Renamings
skosito Feb 16, 2024
f45a8bd
Group core context and config to app context
skosito Feb 16, 2024
e3c74c2
Add master logger to app context
skosito Feb 16, 2024
a970e74
Cleanup
skosito Feb 16, 2024
51d252f
Use app context to get join btc config and core context info
skosito Feb 16, 2024
f2a0c0e
Refactor observer app context
skosito Feb 16, 2024
5c4ec7f
Cleanup
skosito Feb 19, 2024
cd1e8d2
Fix some PR comments
skosito Feb 19, 2024
e977ee9
Merge branch 'develop' into refactor-remove-params-from-config
skosito Feb 21, 2024
ddea482
Fix PR comments
skosito Feb 21, 2024
eb6f0be
Merge branch 'develop' into refactor-remove-params-from-config
skosito Feb 21, 2024
3db710a
Fix evm chain params initialization
skosito Feb 21, 2024
2230c1c
Fix build
skosito Feb 21, 2024
0b0be9c
Cleanup
skosito Feb 22, 2024
1b46071
Merge branch 'develop' into refactor-remove-params-from-config
skosito Feb 22, 2024
935f69d
Merge branch 'develop' into refactor-remove-params-from-config
skosito Feb 23, 2024
0e2ebe0
PR comments
skosito Feb 23, 2024
adc6904
Merge branch 'develop' into refactor-remove-params-from-config
skosito Feb 27, 2024
6d94f69
Fixes after solving conflicts
skosito Feb 27, 2024
dac280a
Fix PR comments
skosito Feb 27, 2024
e58b904
Fix lint
skosito Feb 27, 2024
5f48d63
Merge branch 'develop' into refactor-remove-params-from-config
skosito Feb 27, 2024
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
5 changes: 3 additions & 2 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.NewZeraCoreContext()
chainID, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
Expand Down Expand Up @@ -128,8 +130,7 @@ func DebugCmd() *cobra.Command {
ZetaTokenContractAddress: chainParams.ZetaTokenContractAddress,
Erc20CustodyContractAddress: chainParams.Erc20CustodyContractAddress,
})
cfg.EVMChainConfigs[chainID].ZetaTokenContractAddress = chainParams.ZetaTokenContractAddress
ob.SetConfig(cfg)
coreContext.EVMChainParams[chainID].ZetaTokenContractAddress = chainParams.ZetaTokenContractAddress
skosito marked this conversation as resolved.
Show resolved Hide resolved
if strings.EqualFold(tx.To().Hex(), chainParams.ConnectorContractAddress) {
coinType = common.CoinType_Zeta
} else if strings.EqualFold(tx.To().Hex(), chainParams.Erc20CustodyContractAddress) {
Expand Down
25 changes: 12 additions & 13 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,11 @@ 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,
zetaBridge *zetabridge.ZetaCoreBridge,
peers p2p.AddrList,
priKey secp256k1.PrivKey,
Expand All @@ -32,21 +32,22 @@ func GenerateTss(logger zerolog.Logger,
metrics *metrics.Metrics,
tssPassword string,
hotkeyPassword string) (*mc.TSS, error) {
keygenLogger := logger.With().Str("module", "keygen").Logger()
keygenLogger := appContext.Logger().With().Str("module", "keygen").Logger()

// Bitcoin chain ID is currently used for using the correct signature format
// 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,
metrics,
Expand Down Expand Up @@ -74,7 +75,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 @@ -100,13 +101,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 @@ -150,9 +151,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
25 changes: 14 additions & 11 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"
metrics2 "github.com/zeta-chain/zetacore/zetaclient/metrics"
)

Expand Down Expand Up @@ -113,15 +115,16 @@ 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.NewZeraCoreContext(), cfg, masterLogger)
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)
// UpdateAppContext: A polling goroutine checks and updates core context at every height. Zetacore stores core context for all clients
go zetaBridge.UpdateAppContext(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 @@ -167,7 +170,7 @@ func start(_ *cobra.Command, _ []string) error {
}

telemetryServer.SetIPAddress(cfg.PublicIP)
tss, err := GenerateTss(masterLogger, cfg, zetaBridge, peers, priKey, telemetryServer, tssHistoricalList, metrics, tssKeyPass, hotkeyPass)
tss, err := GenerateTss(appContext, zetaBridge, peers, priKey, telemetryServer, tssHistoricalList, metrics, tssKeyPass, hotkeyPass)
if err != nil {
return err
}
Expand All @@ -182,8 +185,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().Keygen.Status != observerTypes.KeygenStatus_KeyGenSuccess {
skosito marked this conversation as resolved.
Show resolved Hide resolved
startLogger.Info().Msgf("Waiting for TSS Keygen to be a success, current status %s", appContext.ZetaCoreContext().Keygen.Status)
continue
}
break
Expand All @@ -201,7 +204,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().ChainsEnabled) == 0 {
skosito marked this conversation as resolved.
Show resolved Hide resolved
startLogger.Error().Msgf("No chains enabled in updated config %s ", cfg.String())
}

Expand All @@ -219,7 +222,7 @@ 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)
signerMap, err := CreateSignerMap(appContext, tss, telemetryServer)
if err != nil {
log.Error().Err(err).Msg("CreateSignerMap")
return err
Expand All @@ -233,7 +236,7 @@ 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)
chainClientMap, err := CreateChainClientMap(appContext, zetaBridge, tss, dbpath, metrics, telemetryServer)
if err != nil {
startLogger.Err(err).Msg("CreateSignerMap")
skosito marked this conversation as resolved.
Show resolved Hide resolved
return err
Expand All @@ -249,8 +252,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, metrics, masterLogger, cfg, telemetryServer)
mo1.MonitorCore()
mo1 := mc.NewCoreObserver(appContext, zetaBridge, signerMap, chainClientMap, metrics, 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
31 changes: 18 additions & 13 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ 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"
appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
"github.com/zeta-chain/zetacore/zetaclient/authz"
"github.com/zeta-chain/zetacore/zetaclient/bitcoin"
"github.com/zeta-chain/zetacore/zetaclient/config"
Expand Down Expand Up @@ -50,19 +50,24 @@ func CreateZetaBridge(cfg *config.Config, telemetry *metrics.TelemetryServer, ho
}

func CreateSignerMap(
appContext *appcontext.AppContext,
tss interfaces.TSSSigner,
logger zerolog.Logger,
cfg *config.Config,
ts *metrics.TelemetryServer,
) (map[common.Chain]interfaces.ChainSigner, error) {
signerMap := make(map[common.Chain]interfaces.ChainSigner)
logger := appContext.Logger()
// 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 {
logger.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, logger, ts)
if err != nil {
logger.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String())
Expand All @@ -71,7 +76,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, logger, ts)
if err != nil {
Expand All @@ -85,31 +90,31 @@ func CreateSignerMap(
}

func CreateChainClientMap(
appContext *appcontext.AppContext,
bridge *zetabridge.ZetaCoreBridge,
tss interfaces.TSSSigner,
dbpath string,
metrics *metrics.Metrics,
logger zerolog.Logger,
cfg *config.Config,
ts *metrics.TelemetryServer,
) (map[common.Chain]interfaces.ChainClient, error) {
clientMap := make(map[common.Chain]interfaces.ChainClient)
logger := appContext.Logger()
// 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, metrics, logger, cfg, *evmConfig, ts)
co, err := evm.NewEVMChainClient(appContext, bridge, tss, dbpath, metrics, *evmConfig, ts)
if err != nil {
logger.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, metrics, logger, btcConfig, ts)
co, err := bitcoin.NewBitcoinClient(appContext, btcChain, bridge, tss, dbpath, metrics, ts)
if err != nil {
logger.Error().Err(err).Msgf("NewBitcoinClient error for chain %s", btcChain.String())

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

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

type AppContext struct {
lumtis marked this conversation as resolved.
Show resolved Hide resolved
skosito marked this conversation as resolved.
Show resolved Hide resolved
coreContext *corecontext.ZeraCoreContext
config *config.Config
logger zerolog.Logger
}

func NewAppContext(
coreContext *corecontext.ZeraCoreContext,
config *config.Config,
lumtis marked this conversation as resolved.
Show resolved Hide resolved
logger zerolog.Logger,
) *AppContext {
return &AppContext{
coreContext: coreContext,
config: config,
logger: logger,
}
}

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

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

func (a *AppContext) Logger() zerolog.Logger {
return a.logger
}

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
Loading