Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Feb 22, 2024
1 parent 2230c1c commit 0b0be9c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 43 deletions.
2 changes: 1 addition & 1 deletion cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ 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,
Expand Down
3 changes: 3 additions & 0 deletions zetaclient/app_context/app_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type AppContext struct {
logger zerolog.Logger
}

// NewAppContext creates a new AppContext, containing global app structs
// like config, core context and logger
func NewAppContext(
coreContext *corecontext.ZetaCoreContext,
config *config.Config,
Expand All @@ -37,6 +39,7 @@ func (a *AppContext) Logger() zerolog.Logger {
return a.logger
}

// 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()
Expand Down
74 changes: 42 additions & 32 deletions zetaclient/core_context/zeta_core_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,62 @@ import (

type ZetaCoreContext struct {
coreContextLock *sync.RWMutex
Keygen *observertypes.Keygen
ChainsEnabled []common.Chain
EVMChainParams map[int64]*observertypes.ChainParams
BitcoinChainParams *observertypes.ChainParams
CurrentTssPubkey string
keygen *observertypes.Keygen
chainsEnabled []common.Chain
evmChainParams map[int64]*observertypes.ChainParams
bitcoinChainParams *observertypes.ChainParams
currentTssPubkey string
}

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 {
bitcoinChainParams = &observertypes.ChainParams{}
}
return &ZetaCoreContext{
coreContextLock: new(sync.RWMutex),
ChainsEnabled: []common.Chain{},
EVMChainParams: evmChainParams,
BitcoinChainParams: &observertypes.ChainParams{},
chainsEnabled: []common.Chain{},
evmChainParams: evmChainParams,
bitcoinChainParams: bitcoinChainParams,
}
}

func (c *ZetaCoreContext) GetKeygen() observertypes.Keygen {
c.coreContextLock.RLock()
defer c.coreContextLock.RUnlock()
copiedPubkeys := make([]string, len(c.Keygen.GranteePubkeys))
copy(copiedPubkeys, c.Keygen.GranteePubkeys)
copiedPubkeys := make([]string, len(c.keygen.GranteePubkeys))
copy(copiedPubkeys, c.keygen.GranteePubkeys)

return observertypes.Keygen{
Status: c.Keygen.Status,
Status: c.keygen.Status,
GranteePubkeys: copiedPubkeys,
BlockNumber: c.Keygen.BlockNumber,
BlockNumber: c.keygen.BlockNumber,
}
}

func (c *ZetaCoreContext) GetCurrentTssPubkey() string {
c.coreContextLock.RLock()
defer c.coreContextLock.RUnlock()
return c.CurrentTssPubkey
return c.currentTssPubkey
}

func (c *ZetaCoreContext) GetEnabledChains() []common.Chain {
c.coreContextLock.RLock()
defer c.coreContextLock.RUnlock()
copiedChains := make([]common.Chain, len(c.ChainsEnabled))
copy(copiedChains, c.ChainsEnabled)
copiedChains := make([]common.Chain, len(c.chainsEnabled))
copy(copiedChains, c.chainsEnabled)
return copiedChains
}

func (c *ZetaCoreContext) GetEVMChainParams(chainID int64) (*observertypes.ChainParams, bool) {
c.coreContextLock.RLock()
defer c.coreContextLock.RUnlock()
evmChainParams, found := c.EVMChainParams[chainID]
evmChainParams, found := c.evmChainParams[chainID]
return evmChainParams, found
}

Expand All @@ -72,8 +77,8 @@ func (c *ZetaCoreContext) GetAllEVMChainParams() map[int64]*observertypes.ChainP
defer c.coreContextLock.RUnlock()

// deep copy evm chain params
copied := make(map[int64]*observertypes.ChainParams, len(c.EVMChainParams))
for chainID, evmConfig := range c.EVMChainParams {
copied := make(map[int64]*observertypes.ChainParams, len(c.evmChainParams))
for chainID, evmConfig := range c.evmChainParams {
copied[chainID] = &observertypes.ChainParams{}
*copied[chainID] = *evmConfig
}
Expand All @@ -84,14 +89,14 @@ func (c *ZetaCoreContext) GetBTCChainParams() (common.Chain, *observertypes.Chai
c.coreContextLock.RLock()
defer c.coreContextLock.RUnlock()

if c.BitcoinChainParams == nil { // bitcoin is not enabled
if c.bitcoinChainParams == nil { // bitcoin is not enabled
return common.Chain{}, &observertypes.ChainParams{}, false
}
chain := common.GetChainFromChainID(c.BitcoinChainParams.ChainId)
chain := common.GetChainFromChainID(c.bitcoinChainParams.ChainId)
if chain == nil {
panic(fmt.Sprintf("BTCChain is missing for chainID %d", c.BitcoinChainParams.ChainId))
panic(fmt.Sprintf("BTCChain is missing for chainID %d", c.bitcoinChainParams.ChainId))
}
return *chain, c.BitcoinChainParams, true
return *chain, c.bitcoinChainParams, true
}

// Update updates core context and params for all chains
Expand All @@ -101,6 +106,7 @@ func (c *ZetaCoreContext) Update(
newChains []common.Chain,
evmChainParams map[int64]*observertypes.ChainParams,
btcChainParams *observertypes.ChainParams,
tssPubKey string,
init bool,
logger zerolog.Logger,
) {
Expand All @@ -117,36 +123,40 @@ func (c *ZetaCoreContext) Update(

// Add some warnings if chain list changes at runtime
if !init {
if len(c.ChainsEnabled) != len(newChains) {
if len(c.chainsEnabled) != len(newChains) {
logger.Warn().Msgf(
"UpdateChainParams: ChainsEnabled changed at runtime!! current: %v, new: %v",
c.ChainsEnabled,
c.chainsEnabled,
newChains,
)
} else {
for i, chain := range newChains {
if chain != c.ChainsEnabled[i] {
if chain != c.chainsEnabled[i] {
logger.Warn().Msgf(
"UpdateChainParams: ChainsEnabled changed at runtime!! current: %v, new: %v",
c.ChainsEnabled,
c.chainsEnabled,
newChains,
)
}
}
}
}
c.Keygen = keygen
c.ChainsEnabled = newChains
c.keygen = keygen
c.chainsEnabled = newChains
// update chain params for bitcoin if it has config in file
if c.BitcoinChainParams != nil && btcChainParams != nil {
c.BitcoinChainParams = btcChainParams
if c.bitcoinChainParams != nil && btcChainParams != nil {
c.bitcoinChainParams = btcChainParams
}
// update core params for evm chains we have configs in file
for _, params := range evmChainParams {
_, found := c.EVMChainParams[params.ChainId]
_, found := c.evmChainParams[params.ChainId]
if !found {
continue
}
c.EVMChainParams[params.ChainId] = params
c.evmChainParams[params.ChainId] = params
}

if tssPubKey != "" {
c.currentTssPubkey = tssPubKey
}
}
6 changes: 0 additions & 6 deletions zetaclient/evm/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,6 @@ func (ob *ChainClient) WithZetaClient(bridge *zetabridge.ZetaCoreBridge) {
ob.zetaClient = bridge
}

func (ob *ChainClient) WithParams(params observertypes.ChainParams) {
ob.Mu.Lock()
defer ob.Mu.Unlock()
ob.chainParams = params
}

func (ob *ChainClient) SetChainParams(params observertypes.ChainParams) {
ob.Mu.Lock()
defer ob.Mu.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions zetaclient/supplychecker/zeta_supply_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ func (zs *ZetaSupplyChecker) CheckZetaTokenSupply() error {
externalChainTotalSupply = externalChainTotalSupply.Add(totalSupplyInt)
}

ethConfig, ok := zs.coreContext.GetEVMChainParams(zs.ethereumChain.ChainId)
evmChainParams, ok := zs.coreContext.GetEVMChainParams(zs.ethereumChain.ChainId)

Check warning on line 133 in zetaclient/supplychecker/zeta_supply_checker.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/supplychecker/zeta_supply_checker.go#L133

Added line #L133 was not covered by tests
if !ok {
return fmt.Errorf("eth config not found for chain id %d", zs.ethereumChain.ChainId)
}
ethConnectorAddressString := ethConfig.ConnectorContractAddress
ethConnectorAddressString := evmChainParams.ConnectorContractAddress

Check warning on line 137 in zetaclient/supplychecker/zeta_supply_checker.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/supplychecker/zeta_supply_checker.go#L137

Added line #L137 was not covered by tests
ethConnectorAddress := ethcommon.HexToAddress(ethConnectorAddressString)
ethConnectorContract, err := evm.FetchConnectorContractEth(ethConnectorAddress, zs.evmClient[zs.ethereumChain.ChainId])
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions zetaclient/zetabridge/zetacore_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,16 @@ func (b *ZetaCoreBridge) UpdateZetaCoreContext(coreContext *corecontext.ZetaCore
if err != nil {
b.logger.Info().Msg("Unable to fetch keygen from zetabridge")
}
coreContext.Update(keyGen, newChains, newEVMParams, newBTCParams, init, b.logger)

tssPubKey := ""

Check warning on line 248 in zetaclient/zetabridge/zetacore_bridge.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetabridge/zetacore_bridge.go#L248

Added line #L248 was not covered by tests
tss, err := b.GetCurrentTss()
if err != nil {
b.logger.Debug().Err(err).Msg("Unable to fetch TSS from zetabridge")
} else {
coreContext.CurrentTssPubkey = tss.GetTssPubkey()
tssPubKey = tss.GetTssPubkey()

Check warning on line 253 in zetaclient/zetabridge/zetacore_bridge.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetabridge/zetacore_bridge.go#L253

Added line #L253 was not covered by tests
}

coreContext.Update(keyGen, newChains, newEVMParams, newBTCParams, tssPubKey, init, b.logger)

Check warning on line 256 in zetaclient/zetabridge/zetacore_bridge.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetabridge/zetacore_bridge.go#L256

Added line #L256 was not covered by tests
return nil
}

Expand Down

0 comments on commit 0b0be9c

Please sign in to comment.