Skip to content

Commit

Permalink
refactor: integrated base Observer structure into existing EVM/BTC ob…
Browse files Browse the repository at this point in the history
…servers (#2359)

* save local new files to remote

* initiated base observer

* move base to chains folder

* moved logger to base package

* added base signer and logger

* added changelog entry

* integrated base signer into evm/bitcoin; integrated base observer into evm

* integrated base observer to evm and bitcoin chain

* added changelog entry

* cherry pick base Signer structure integration

* updated PR number in changelog

* updated PR number in changelog

* cherry picked the integration of base Observer

* moved pure RPC methods to rpc package

* moved Mutex to base Observer struct

* type check on cached block, header

* update changelog PR number and added unit test for Stop() method

* replace magic numbers with constants

* updated method name in logging

* Move sqlite-mem db to testutils

* Add base signer's Lock & Unlock

---------

Co-authored-by: Dmitry <[email protected]>
  • Loading branch information
ws4charlie and swift1337 authored Jun 25, 2024
1 parent 852897f commit 3f32bd0
Show file tree
Hide file tree
Showing 30 changed files with 1,946 additions and 1,358 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* [2340](https://github.com/zeta-chain/node/pull/2340) - add ValidateInbound method for cctx orchestrator
* [2344](https://github.com/zeta-chain/node/pull/2344) - group common data of EVM/Bitcoin signer and observer using base structs
* [2357](https://github.com/zeta-chain/node/pull/2357) - integrate base Signer structure into EVM/Bitcoin Signer
* [2359](https://github.com/zeta-chain/node/pull/2359) - integrate base Observer structure into EVM/Bitcoin Observer
* [2375](https://github.com/zeta-chain/node/pull/2375) - improve & speedup code formatting

### Tests
Expand Down
9 changes: 2 additions & 7 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strconv"
"strings"
"sync"

"github.com/btcsuite/btcd/rpcclient"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -86,9 +85,7 @@ func DebugCmd() *cobra.Command {

// get ballot identifier according to the chain type
if chains.IsEVMChain(chain.ChainId) {
evmObserver := evmobserver.Observer{
Mu: &sync.Mutex{},
}
evmObserver := evmobserver.Observer{}
evmObserver.WithZetacoreClient(client)
var ethRPC *ethrpc.EthRPC
var client *ethclient.Client
Expand Down Expand Up @@ -164,9 +161,7 @@ func DebugCmd() *cobra.Command {
}
fmt.Println("CoinType : ", coinType)
} else if chains.IsBitcoinChain(chain.ChainId) {
btcObserver := btcobserver.Observer{
Mu: &sync.Mutex{},
}
btcObserver := btcobserver.Observer{}
btcObserver.WithZetacoreClient(client)
btcObserver.WithChain(*chains.GetChainFromChainID(chainID))
connCfg := &rpcclient.ConnConfig{
Expand Down
59 changes: 53 additions & 6 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package main

import (
"fmt"

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

"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"
Expand Down Expand Up @@ -116,32 +120,75 @@ func CreateChainObserverMap(
logger base.Logger,
ts *metrics.TelemetryServer,
) (map[int64]interfaces.ChainObserver, error) {
zetacoreContext := appContext.ZetacoreContext()
observerMap := make(map[int64]interfaces.ChainObserver)
// EVM observers
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
continue
}
_, found := appContext.ZetacoreContext().GetEVMChainParams(evmConfig.Chain.ChainId)
chainParams, found := zetacoreContext.GetEVMChainParams(evmConfig.Chain.ChainId)
if !found {
logger.Std.Error().Msgf("ChainParam not found for chain %s", evmConfig.Chain.String())
continue
}
co, err := evmobserver.NewObserver(appContext, zetacoreClient, tss, dbpath, logger, evmConfig, ts)

// create EVM client
evmClient, err := ethclient.Dial(evmConfig.Endpoint)
if err != nil {
logger.Std.Error().Err(err).Msgf("error dailing endpoint %s", evmConfig.Endpoint)
continue
}

// create EVM chain observer
observer, err := evmobserver.NewObserver(
evmConfig,
evmClient,
*chainParams,
zetacoreContext,
zetacoreClient,
tss,
dbpath,
logger,
ts,
)
if err != nil {
logger.Std.Error().Err(err).Msgf("NewObserver error for evm chain %s", evmConfig.Chain.String())
continue
}
observerMap[evmConfig.Chain.ChainId] = co
observerMap[evmConfig.Chain.ChainId] = observer
}

// BTC observer
_, chainParams, found := zetacoreContext.GetBTCChainParams()
if !found {
return nil, fmt.Errorf("bitcoin chains params not found")
}

// create BTC chain observer
btcChain, btcConfig, enabled := appContext.GetBTCChainAndConfig()
if enabled {
co, err := btcobserver.NewObserver(appContext, btcChain, zetacoreClient, tss, dbpath, logger, btcConfig, ts)
btcClient, err := btcrpc.NewRPCClient(btcConfig)
if err != nil {
logger.Std.Error().Err(err).Msgf("NewObserver error for bitcoin chain %s", btcChain.String())
logger.Std.Error().Err(err).Msgf("error creating rpc client for bitcoin chain %s", btcChain.String())
} else {
observerMap[btcChain.ChainId] = co
// create BTC chain observer
observer, err := btcobserver.NewObserver(
btcChain,
btcClient,
*chainParams,
zetacoreContext,
zetacoreClient,
tss,
dbpath,
logger,
ts,
)
if err != nil {
logger.Std.Error().Err(err).Msgf("NewObserver error for bitcoin chain %s", btcChain.String())
} else {
observerMap[btcChain.ChainId] = observer
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions testutil/sample/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"testing"

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand Down Expand Up @@ -60,6 +61,11 @@ func Hash() ethcommon.Hash {
return EthAddress().Hash()
}

// BtcHash returns a sample btc hash
func BtcHash() chainhash.Hash {
return chainhash.Hash(Hash())
}

// PubKey returns a sample account PubKey
func PubKey(r *rand.Rand) cryptotypes.PubKey {
seed := []byte(strconv.Itoa(r.Int()))
Expand Down
Loading

0 comments on commit 3f32bd0

Please sign in to comment.