Skip to content

Commit

Permalink
Implement signers provision / deprovision based on AppContext state
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Jul 17, 2024
1 parent a96898f commit 232b48e
Show file tree
Hide file tree
Showing 9 changed files with 552 additions and 103 deletions.
7 changes: 4 additions & 3 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,11 @@ 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(ctx, appContext, tss, logger, 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 := orchestrator.CreateSignerMap(ctx, tss, logger, telemetryServer)
if err != nil {
log.Error().Err(err).Msg("CreateSignerMap")
log.Error().Err(err).Msg("Unable to create signer map")
return err
}

Expand Down
66 changes: 0 additions & 66 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/rs/zerolog"

"github.com/zeta-chain/zetacore/zetaclient/authz"
"github.com/zeta-chain/zetacore/zetaclient/chains/base"
btcobserver "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin/observer"
btcrpc "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin/rpc"
btcsigner "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin/signer"
evmobserver "github.com/zeta-chain/zetacore/zetaclient/chains/evm/observer"
evmsigner "github.com/zeta-chain/zetacore/zetaclient/chains/evm/signer"
"github.com/zeta-chain/zetacore/zetaclient/chains/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/context"
Expand Down Expand Up @@ -56,69 +53,6 @@ func CreateZetacoreClient(cfg config.Config, hotkeyPassword string, logger zerol
return client, nil
}

// CreateSignerMap creates a map of ChainSigners for all chains in the config
func CreateSignerMap(
ctx gocontext.Context,
appContext *context.AppContext,
tss interfaces.TSSSigner,
logger base.Logger,
ts *metrics.TelemetryServer,
) (map[int64]interfaces.ChainSigner, error) {
signerMap := make(map[int64]interfaces.ChainSigner)

// EVM signers
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
continue
}
evmChainParams, found := appContext.GetEVMChainParams(evmConfig.Chain.ChainId)
if !found {
logger.Std.Error().Msgf("ChainParam not found for chain %s", evmConfig.Chain.String())
continue
}

chainName := evmConfig.Chain.ChainName.String()
mpiAddress := ethcommon.HexToAddress(evmChainParams.ConnectorContractAddress)
erc20CustodyAddress := ethcommon.HexToAddress(evmChainParams.Erc20CustodyContractAddress)

signer, err := evmsigner.NewSigner(
ctx,
evmConfig.Chain,
tss,
ts,
logger,
evmConfig.Endpoint,
config.GetConnectorABI(),
config.GetERC20CustodyABI(),
mpiAddress,
erc20CustodyAddress,
)
if err != nil {
logger.Std.Error().Err(err).Msgf("NewSigner error for EVM chain %q", chainName)
continue
}

signerMap[evmConfig.Chain.ChainId] = signer
logger.Std.Info().Msgf("NewSigner succeeded for EVM chain %q", chainName)
}

// BTC signer
btcChain, btcConfig, btcEnabled := appContext.GetBTCChainAndConfig()
if btcEnabled {
chainName := btcChain.ChainName.String()

signer, err := btcsigner.NewSigner(btcChain, tss, ts, logger, btcConfig)
if err != nil {
logger.Std.Error().Err(err).Msgf("NewSigner error for BTC chain %q", chainName)
} else {
signerMap[btcChain.ChainId] = signer
logger.Std.Info().Msgf("NewSigner succeeded for BTC chain %q", chainName)
}
}

return signerMap, nil
}

// CreateChainObserverMap creates a map of ChainObservers for all chains in the config
func CreateChainObserverMap(
ctx gocontext.Context,
Expand Down
17 changes: 17 additions & 0 deletions pkg/ptr/ptr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Package ptr provides helper functions for working with pointers.
package ptr

// Ptr returns a pointer to the value passed in.
func Ptr[T any](value T) *T {
return &value
}

// Deref returns the value of the pointer passed in, or the zero value of the type if the pointer is nil.
func Deref[T any](value *T) T {
var out T
if value != nil {
out = *value
}

return out
}
3 changes: 2 additions & 1 deletion zetaclient/chains/bitcoin/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"

"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/coin"
Expand Down Expand Up @@ -77,7 +78,7 @@ func NewSigner(
}
client, err := rpcclient.New(connCfg, nil)
if err != nil {
return nil, fmt.Errorf("error creating bitcoin rpc client: %s", err)
return nil, errors.Wrap(err, "unable to create bitcoin rpc client")
}

return &Signer{
Expand Down
65 changes: 33 additions & 32 deletions zetaclient/context/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,15 @@ type AppContext struct {
mu sync.RWMutex
}

// New creates and returns new AppContext
// New creates and returns new empty AppContext
func New(cfg config.Config, logger zerolog.Logger) *AppContext {
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 &AppContext{
config: cfg,
logger: logger.With().Str("module", "appcontext").Logger(),

chainsEnabled: []chains.Chain{},
evmChainParams: evmChainParams,
bitcoinChainParams: bitcoinChainParams,
evmChainParams: map[int64]*observertypes.ChainParams{},
bitcoinChainParams: nil,
crosschainFlags: observertypes.CrosschainFlags{},
blockHeaderEnabledChains: []lightclienttypes.HeaderSupportedChain{},

Expand All @@ -72,14 +61,17 @@ func (a *AppContext) Config() config.Config {

// GetBTCChainAndConfig returns btc chain and config if enabled
func (a *AppContext) GetBTCChainAndConfig() (chains.Chain, config.BTCConfig, bool) {
btcConfig, configEnabled := a.Config().GetBTCConfig()
btcChain, _, paramsEnabled := a.GetBTCChainParams()
cfg, configEnabled := a.Config().GetBTCConfig()
if !configEnabled {
return chains.Chain{}, config.BTCConfig{}, false
}

if !configEnabled || !paramsEnabled {
chain, _, paramsEnabled := a.GetBTCChainParams()
if !paramsEnabled {
return chains.Chain{}, config.BTCConfig{}, false
}

return btcChain, btcConfig, true
return chain, cfg, true
}

// IsOutboundObservationEnabled returns true if the chain is supported and outbound flag is enabled
Expand Down Expand Up @@ -173,7 +165,8 @@ func (a *AppContext) GetBTCChainParams() (chains.Chain, *observertypes.ChainPara
a.mu.RLock()
defer a.mu.RUnlock()

if a.bitcoinChainParams == nil { // bitcoin is not enabled
// bitcoin is not enabled
if a.bitcoinChainParams == nil {
return chains.Chain{}, nil, false
}

Expand Down Expand Up @@ -235,15 +228,15 @@ func (a *AppContext) Update(
blockHeaderEnabledChains []lightclienttypes.HeaderSupportedChain,
init bool,
) {
if len(newChains) == 0 {
a.logger.Warn().Msg("UpdateChainParams: No chains enabled in ZeroCore")
}

// Ignore whatever order zetacore organizes chain list in state
sort.SliceStable(newChains, func(i, j int) bool {
return newChains[i].ChainId < newChains[j].ChainId
})

if len(newChains) == 0 {
a.logger.Warn().Msg("UpdateChainParams: No chains enabled in ZeroCore")
}

a.mu.Lock()
defer a.mu.Unlock()

Expand All @@ -252,7 +245,7 @@ func (a *AppContext) Update(
a.logger.Warn().
Interface("chains.current", a.chainsEnabled).
Interface("chains.new", newChains).
Msg("UpdateChainParams: ChainsEnabled changed at runtime!")
Msg("ChainsEnabled changed at runtime!")
}

if keygen != nil {
Expand All @@ -264,18 +257,26 @@ func (a *AppContext) Update(
a.additionalChain = additionalChains
a.blockHeaderEnabledChains = blockHeaderEnabledChains

// update chain params for bitcoin if it has config in file
if a.bitcoinChainParams != nil && btcChainParams != nil {
a.bitcoinChainParams = btcChainParams
}

// update core params for evm chains we have configs in file
for _, params := range evmChainParams {
_, found := a.evmChainParams[params.ChainId]
freshEvmChainParams := make(map[int64]*observertypes.ChainParams)
for _, cp := range evmChainParams {
_, found := a.config.EVMChainConfigs[cp.ChainId]
if !found {
a.logger.Warn().
Int64("chain.id", cp.ChainId).
Msg("Encountered EVM ChainParams that are not present in the config file")

continue
}
a.evmChainParams[params.ChainId] = params

freshEvmChainParams[cp.ChainId] = cp
}

a.evmChainParams = freshEvmChainParams

// update chain params for bitcoin if it has config in file
if btcChainParams != nil {
a.bitcoinChainParams = btcChainParams
}

if tssPubKey != "" {
Expand Down
Loading

0 comments on commit 232b48e

Please sign in to comment.