Skip to content

Commit

Permalink
refactor: zetaclient re-organization (#1640)
Browse files Browse the repository at this point in the history
* initial refactor commit

* fix lint errors

* update changelog

* fix import cycle

* ran make generate

* run make generate again

* fix error

* renamed btc to bitcoin, removed klayton, and moved evm/utils functions to inbounds.go

* ran make generate and addressed comments

* moved dynamic ticker into types section

* use a different name for btc client

* ran make generate

* fix test file

---------

Co-authored-by: Charlie Chen <[email protected]>
  • Loading branch information
kevinssgh and ws4charlie authored Feb 12, 2024
1 parent b85ba73 commit ae22f1e
Show file tree
Hide file tree
Showing 57 changed files with 1,448 additions and 1,470 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
### Refactoring

* [1628](https://github.com/zeta-chain/node/pull/1628) optimize return and simplify code
* [1640](https://github.com/zeta-chain/node/pull/1640) reorganize zetaclient into subpackages

### Refactoring
* [1619](https://github.com/zeta-chain/node/pull/1619) - Add evm fee calculation to tss migration of evm chains
Expand Down
17 changes: 11 additions & 6 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"strings"
"sync"

"github.com/zeta-chain/zetacore/zetaclient/bitcoin"
"github.com/zeta-chain/zetacore/zetaclient/evm"
"github.com/zeta-chain/zetacore/zetaclient/keys"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
"github.com/zeta-chain/zetacore/zetaclient/zetabridge"

"github.com/btcsuite/btcd/rpcclient"
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
Expand All @@ -17,7 +23,6 @@ import (
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/testutil/sample"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient"
"github.com/zeta-chain/zetacore/zetaclient/config"
)

Expand Down Expand Up @@ -54,16 +59,16 @@ func DebugCmd() *cobra.Command {
var ballotIdentifier string
chainLogger := zerolog.New(io.Discard).Level(zerolog.Disabled)

telemetryServer := zetaclient.NewTelemetryServer()
telemetryServer := metrics.NewTelemetryServer()
go func() {
err := telemetryServer.Start()
if err != nil {
panic("telemetryServer error")
}
}()

bridge, err := zetaclient.NewZetaCoreBridge(
&zetaclient.Keys{OperatorAddress: sdk.MustAccAddressFromBech32(sample.AccAddress())},
bridge, err := zetabridge.NewZetaCoreBridge(
&keys.Keys{OperatorAddress: sdk.MustAccAddressFromBech32(sample.AccAddress())},
debugArgs.zetaNode,
"",
debugArgs.zetaChainID,
Expand All @@ -89,7 +94,7 @@ func DebugCmd() *cobra.Command {

if common.IsEVMChain(chain.ChainId) {

ob := zetaclient.EVMChainClient{
ob := evm.ChainClient{
Mu: &sync.Mutex{},
}
ob.WithZetaClient(bridge)
Expand Down Expand Up @@ -159,7 +164,7 @@ func DebugCmd() *cobra.Command {
}
fmt.Println("CoinType : ", coinType)
} else if common.IsBitcoinChain(chain.ChainId) {
obBtc := zetaclient.BitcoinChainClient{
obBtc := bitcoin.BTCChainClient{
Mu: &sync.Mutex{},
}
obBtc.WithZetaClient(bridge)
Expand Down
10 changes: 6 additions & 4 deletions cmd/zetaclientd/keygen_tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"fmt"
"time"

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

"github.com/rs/zerolog"
"github.com/tendermint/crypto/sha3"
"github.com/tendermint/tendermint/crypto/secp256k1"
Expand All @@ -15,17 +18,16 @@ import (
"github.com/zeta-chain/go-tss/p2p"
"github.com/zeta-chain/zetacore/common"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
mc "github.com/zeta-chain/zetacore/zetaclient"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
)

func GenerateTss(logger zerolog.Logger,
cfg *config.Config,
zetaBridge *mc.ZetaCoreBridge,
zetaBridge *zetabridge.ZetaCoreBridge,
peers p2p.AddrList,
priKey secp256k1.PrivKey,
ts *mc.TelemetryServer,
ts *metrics.TelemetryServer,
tssHistoricalList []observertypes.TSS,
metrics *metrics.Metrics,
tssPassword string,
Expand Down Expand Up @@ -63,7 +65,7 @@ func GenerateTss(logger zerolog.Logger,
// Set TSS block to 0 using genesis file to disable this feature
// Note : The TSS generation is done through the "hotkey" or "Zeta-clientGrantee" This key needs to be present on the machine for the TSS signing to happen .
// "ZetaClientGrantee" key is different from the "operator" key .The "Operator" key gives all zetaclient related permissions such as TSS generation ,reporting and signing, INBOUND and OUTBOUND vote signing, to the "ZetaClientGrantee" key.
// The votes to signify a successful TSS generation (Or unsuccessful) is signed by the operator key and broadcast to zetacore by the zetcalientGrantee key on behalf of the operator .
// The votes to signify a successful TSS generation (Or unsuccessful) is signed by the operator key and broadcast to zetabridge by the zetcalientGrantee key on behalf of the operator .
ticker := time.NewTicker(time.Second * 1)
triedKeygenAtBlock := false
lastBlock := int64(0)
Expand Down
7 changes: 4 additions & 3 deletions cmd/zetaclientd/p2p_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"time"

"github.com/zeta-chain/zetacore/zetaclient/metrics"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
libp2p "github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
Expand All @@ -21,7 +23,6 @@ import (
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/zeta-chain/go-tss/p2p"
"github.com/zeta-chain/zetacore/common/cosmos"
mc "github.com/zeta-chain/zetacore/zetaclient"
"github.com/zeta-chain/zetacore/zetaclient/config"
)

Expand All @@ -38,7 +39,7 @@ func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, bridgePk cry
}
startLogger.Warn().Msgf("my pubkey %s", pubkeyBech32)

var s *mc.TelemetryServer
var s *metrics.TelemetryServer
if len(peers) == 0 {
startLogger.Warn().Msg("No seed peer specified; assuming I'm the host")

Expand Down Expand Up @@ -83,7 +84,7 @@ func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, bridgePk cry
}
startLogger.Info().Msgf("host created: ID %s", host.ID().String())
if len(peers) == 0 {
s = mc.NewTelemetryServer()
s = metrics.NewTelemetryServer()
s.SetP2PID(host.ID().String())
go func() {
startLogger.Info().Msg("Starting TSS HTTP Server...")
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func start(_ *cobra.Command, _ []string) error {
waitForZetaCore(cfg, startLogger)
startLogger.Info().Msgf("ZetaCore is ready , Trying to connect to %s", cfg.Peer)

telemetryServer := mc.NewTelemetryServer()
telemetryServer := metrics2.NewTelemetryServer()
go func() {
err := telemetryServer.Start()
if err != nil {
Expand Down
44 changes: 25 additions & 19 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,30 @@ import (
"github.com/rs/zerolog"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/common/cosmos"
"github.com/zeta-chain/zetacore/zetaclient"
"github.com/zeta-chain/zetacore/zetaclient/authz"
"github.com/zeta-chain/zetacore/zetaclient/bitcoin"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/keys"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
"github.com/zeta-chain/zetacore/zetaclient/zetabridge"

"github.com/zeta-chain/zetacore/zetaclient/evm"
)

func CreateAuthzSigner(granter string, grantee sdk.AccAddress) {
zetaclient.SetupAuthZSignerList(granter, grantee)
authz.SetupAuthZSignerList(granter, grantee)
}

func CreateZetaBridge(cfg *config.Config, telemetry *zetaclient.TelemetryServer, hotkeyPassword string) (*zetaclient.ZetaCoreBridge, error) {
func CreateZetaBridge(cfg *config.Config, telemetry *metrics.TelemetryServer, hotkeyPassword string) (*zetabridge.ZetaCoreBridge, error) {
hotKey := cfg.AuthzHotkey
if cfg.HsmMode {
hotKey = cfg.HsmHotKey
}

chainIP := cfg.ZetaCoreURL

kb, _, err := zetaclient.GetKeyringKeybase(cfg, hotkeyPassword)
kb, _, err := keys.GetKeyringKeybase(cfg, hotkeyPassword)
if err != nil {
return nil, err
}
Expand All @@ -33,9 +39,9 @@ func CreateZetaBridge(cfg *config.Config, telemetry *zetaclient.TelemetryServer,
return nil, err
}

k := zetaclient.NewKeysWithKeybase(kb, granterAddreess, cfg.AuthzHotkey, hotkeyPassword)
k := keys.NewKeysWithKeybase(kb, granterAddreess, cfg.AuthzHotkey, hotkeyPassword)

bridge, err := zetaclient.NewZetaCoreBridge(k, chainIP, hotKey, cfg.ChainID, cfg.HsmMode, telemetry)
bridge, err := zetabridge.NewZetaCoreBridge(k, chainIP, hotKey, cfg.ChainID, cfg.HsmMode, telemetry)
if err != nil {
return nil, err
}
Expand All @@ -44,20 +50,20 @@ func CreateZetaBridge(cfg *config.Config, telemetry *zetaclient.TelemetryServer,
}

func CreateSignerMap(
tss zetaclient.TSSSigner,
tss interfaces.TSSSigner,
logger zerolog.Logger,
cfg *config.Config,
ts *zetaclient.TelemetryServer,
) (map[common.Chain]zetaclient.ChainSigner, error) {
signerMap := make(map[common.Chain]zetaclient.ChainSigner)
ts *metrics.TelemetryServer,
) (map[common.Chain]interfaces.ChainSigner, error) {
signerMap := make(map[common.Chain]interfaces.ChainSigner)
// EVM signers
for _, evmConfig := range cfg.GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
continue
}
mpiAddress := ethcommon.HexToAddress(evmConfig.ChainParams.ConnectorContractAddress)
erc20CustodyAddress := ethcommon.HexToAddress(evmConfig.ChainParams.Erc20CustodyContractAddress)
signer, err := zetaclient.NewEVMSigner(evmConfig.Chain, evmConfig.Endpoint, tss, config.GetConnectorABI(), config.GetERC20CustodyABI(), mpiAddress, erc20CustodyAddress, logger, ts)
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())
continue
Expand All @@ -67,7 +73,7 @@ func CreateSignerMap(
// BTC signer
btcChain, btcConfig, enabled := cfg.GetBTCConfig()
if enabled {
signer, err := zetaclient.NewBTCSigner(btcConfig, tss, logger, ts)
signer, err := bitcoin.NewBTCSigner(btcConfig, tss, logger, ts)
if err != nil {
logger.Error().Err(err).Msgf("NewBTCSigner error for chain %s", btcChain.String())
} else {
Expand All @@ -79,21 +85,21 @@ func CreateSignerMap(
}

func CreateChainClientMap(
bridge *zetaclient.ZetaCoreBridge,
tss zetaclient.TSSSigner,
bridge *zetabridge.ZetaCoreBridge,
tss interfaces.TSSSigner,
dbpath string,
metrics *metrics.Metrics,
logger zerolog.Logger,
cfg *config.Config,
ts *zetaclient.TelemetryServer,
) (map[common.Chain]zetaclient.ChainClient, error) {
clientMap := make(map[common.Chain]zetaclient.ChainClient)
ts *metrics.TelemetryServer,
) (map[common.Chain]interfaces.ChainClient, error) {
clientMap := make(map[common.Chain]interfaces.ChainClient)
// EVM clients
for _, evmConfig := range cfg.GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
continue
}
co, err := zetaclient.NewEVMChainClient(bridge, tss, dbpath, metrics, logger, cfg, *evmConfig, ts)
co, err := evm.NewEVMChainClient(bridge, tss, dbpath, metrics, logger, cfg, *evmConfig, ts)
if err != nil {
logger.Error().Err(err).Msgf("NewEVMChainClient error for chain %s", evmConfig.Chain.String())
continue
Expand All @@ -103,7 +109,7 @@ func CreateChainClientMap(
// BTC client
btcChain, btcConfig, enabled := cfg.GetBTCConfig()
if enabled {
co, err := zetaclient.NewBitcoinClient(btcChain, bridge, tss, dbpath, metrics, logger, btcConfig, ts)
co, err := bitcoin.NewBitcoinClient(btcChain, bridge, tss, dbpath, metrics, logger, btcConfig, ts)
if err != nil {
logger.Error().Err(err).Msgf("NewBitcoinClient error for chain %s", btcChain.String())

Expand Down
4 changes: 0 additions & 4 deletions common/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ func IsHeaderSupportedEvmChain(chainID int64) bool {
chainID == 56 // bsc mainnet
}

func (chain Chain) IsKlaytnChain() bool {
return chain.ChainId == 1001
}

// SupportMerkleProof returns true if the chain supports block header-based verification
func (chain Chain) SupportMerkleProof() bool {
return IsEVMChain(chain.ChainId) || IsBitcoinChain(chain.ChainId)
Expand Down
15 changes: 8 additions & 7 deletions contrib/localnet/orchestrator/smoketest/runner/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"math/big"
"time"

"github.com/zeta-chain/zetacore/common/bitcoin"

"github.com/zeta-chain/zetacore/contrib/localnet/orchestrator/smoketest/utils"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"

Expand All @@ -19,9 +21,8 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/rs/zerolog/log"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/common/bitcoin"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient"
zetabitcoin "github.com/zeta-chain/zetacore/zetaclient/bitcoin"
)

var blockHeaderBTCTimeout = 5 * time.Minute
Expand Down Expand Up @@ -54,7 +55,7 @@ func (sm *SmokeTestRunner) DepositBTCWithAmount(amount float64) (txHash *chainha
sm.Logger.Info(" spendableUTXOs: %d", spendableUTXOs)
sm.Logger.Info("Now sending two txs to TSS address...")

amount = amount + zetaclient.BtcDepositorFeeMin
amount = amount + zetabitcoin.BtcDepositorFeeMin
txHash, err = sm.SendToTSSFromDeployerToDeposit(sm.BTCTSSAddress, amount, utxos, sm.BtcRPCClient, sm.BTCDeployerAddress)
if err != nil {
panic(err)
Expand Down Expand Up @@ -100,12 +101,12 @@ func (sm *SmokeTestRunner) DepositBTC(testHeader bool) {
sm.Logger.Info("Now sending two txs to TSS address...")

// send two transactions to the TSS address
amount1 := 1.1 + zetaclient.BtcDepositorFeeMin
amount1 := 1.1 + zetabitcoin.BtcDepositorFeeMin
txHash1, err := sm.SendToTSSFromDeployerToDeposit(sm.BTCTSSAddress, amount1, utxos[:2], btc, sm.BTCDeployerAddress)
if err != nil {
panic(err)
}
amount2 := 0.05 + zetaclient.BtcDepositorFeeMin
amount2 := 0.05 + zetabitcoin.BtcDepositorFeeMin
txHash2, err := sm.SendToTSSFromDeployerToDeposit(sm.BTCTSSAddress, amount2, utxos[2:4], btc, sm.BTCDeployerAddress)
if err != nil {
panic(err)
Expand All @@ -118,7 +119,7 @@ func (sm *SmokeTestRunner) DepositBTC(testHeader bool) {
0.11,
utxos[4:5],
btc,
[]byte(zetaclient.DonationMessage),
[]byte(zetabitcoin.DonationMessage),
sm.BTCDeployerAddress,
)
if err != nil {
Expand Down Expand Up @@ -269,7 +270,7 @@ func (sm *SmokeTestRunner) SendToTSSFromDeployerWithMemo(
if err != nil {
panic(err)
}
events := zetaclient.FilterAndParseIncomingTx(
events := zetabitcoin.FilterAndParseIncomingTx(
[]btcjson.TxRawResult{*rawtx},
0,
sm.BTCTSSAddress.EncodeAddress(),
Expand Down
4 changes: 2 additions & 2 deletions contrib/localnet/orchestrator/smoketest/runner/setup_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"math/big"
"time"

"github.com/zeta-chain/zetacore/zetaclient"
"github.com/zeta-chain/zetacore/zetaclient/evm"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/zeta-chain/protocol-contracts/pkg/contracts/evm/erc20custody.sol"
Expand Down Expand Up @@ -63,7 +63,7 @@ func (sm *SmokeTestRunner) SetupEVM(contractsDeployed bool) {

// donate to the TSS address to avoid account errors because deploying gas token ZRC20 will automatically mint
// gas token on ZetaChain to initialize the pool
txDonation, err := sm.SendEther(sm.TSSAddress, big.NewInt(101000000000000000), []byte(zetaclient.DonationMessage))
txDonation, err := sm.SendEther(sm.TSSAddress, big.NewInt(101000000000000000), []byte(evm.DonationMessage))
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (

"github.com/zeta-chain/zetacore/contrib/localnet/orchestrator/smoketest/runner"
"github.com/zeta-chain/zetacore/contrib/localnet/orchestrator/smoketest/utils"
"github.com/zeta-chain/zetacore/zetaclient"
"github.com/zeta-chain/zetacore/zetaclient/evm"
)

// TestDonationEther tests donation of ether to the tss address
func TestDonationEther(sm *runner.SmokeTestRunner) {
txDonation, err := sm.SendEther(sm.TSSAddress, big.NewInt(100000000000000000), []byte(zetaclient.DonationMessage))
txDonation, err := sm.SendEther(sm.TSSAddress, big.NewInt(100000000000000000), []byte(evm.DonationMessage))
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit ae22f1e

Please sign in to comment.