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: move crosschain flags to core context in zetaclient #1914

Merged
merged 11 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [1848](https://github.com/zeta-chain/node/issues/1848) - create a method to observe deposits to tss address in one evm block
* [1885](https://github.com/zeta-chain/node/pull/1885) - change important metrics on port 8123 to be prometheus compatible
* [1863](https://github.com/zeta-chain/node/pull/1863) - remove duplicate ValidateChainParams function
* [1914](https://github.com/zeta-chain/node/pull/1914) - move crosschain flags to core context in zetaclient

### Features

Expand Down
17 changes: 14 additions & 3 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ func CreateSignerMap(
loggers clientcommon.ClientLogger,
ts *metrics.TelemetryServer,
) (map[int64]interfaces.ChainSigner, error) {
coreContext := appContext.ZetaCoreContext()
signerMap := make(map[int64]interfaces.ChainSigner)
// EVM signers
kevinssgh marked this conversation as resolved.
Show resolved Hide resolved
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
continue
}
evmChainParams, found := appContext.ZetaCoreContext().GetEVMChainParams(evmConfig.Chain.ChainId)
evmChainParams, found := coreContext.GetEVMChainParams(evmConfig.Chain.ChainId)
if !found {
loggers.Std.Error().Msgf("ChainParam not found for chain %s", evmConfig.Chain.String())
continue
Expand All @@ -71,7 +72,17 @@ func CreateSignerMap(
}
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, loggers, ts)
signer, err := evm.NewEVMSigner(
evmConfig.Chain,
evmConfig.Endpoint,
tss,
config.GetConnectorABI(),
config.GetERC20CustodyABI(),
mpiAddress,
erc20CustodyAddress,
coreContext,
loggers,
ts)
if err != nil {
loggers.Std.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String())
continue
Expand All @@ -81,7 +92,7 @@ func CreateSignerMap(
// BTC signer
btcChain, btcConfig, enabled := appContext.GetBTCChainAndConfig()
if enabled {
signer, err := bitcoin.NewBTCSigner(btcConfig, tss, loggers, ts)
signer, err := bitcoin.NewBTCSigner(btcConfig, tss, loggers, ts, coreContext)
if err != nil {
loggers.Std.Error().Err(err).Msgf("NewBTCSigner error for chain %s", btcChain.String())
} else {
Expand Down
13 changes: 7 additions & 6 deletions zetaclient/bitcoin/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"sync/atomic"
"time"

corecontext "github.com/zeta-chain/zetacore/zetaclient/core_context"

appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/config"
Expand Down Expand Up @@ -75,6 +77,7 @@
broadcastedTx map[string]string // key: chain-tss-nonce, value: outTx hash
utxos []btcjson.ListUnspentResult
params observertypes.ChainParams
coreContext *corecontext.ZetaCoreContext

db *gorm.DB
stop chan struct{}
Expand Down Expand Up @@ -166,6 +169,7 @@

ob.zetaClient = bridge
ob.Tss = tss
ob.coreContext = appcontext.ZetaCoreContext()
ob.includedTxHashes = make(map[string]bool)
ob.includedTxResults = make(map[string]*btcjson.GetTransactionResult)
ob.broadcastedTx = make(map[string]string)
Expand Down Expand Up @@ -327,7 +331,7 @@
for {
select {
case <-ticker.C():
err := ob.observeInTx()
err := ob.ObserveInTx()

Check warning on line 334 in zetaclient/bitcoin/bitcoin_client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_client.go#L334

Added line #L334 was not covered by tests
if err != nil {
ob.logger.WatchInTx.Error().Err(err).Msg("WatchInTx error observing in tx")
}
Expand Down Expand Up @@ -374,12 +378,9 @@
return err
}

func (ob *BTCChainClient) observeInTx() error {
func (ob *BTCChainClient) ObserveInTx() error {
// make sure inbound TXS / Send is enabled by the protocol
flags, err := ob.zetaClient.GetCrosschainFlags()
if err != nil {
return err
}
flags := ob.coreContext.GetCrossChainFlags()
if !flags.IsInboundEnabled {
return errors.New("inbound TXS / Send has been disabled by the protocol")
}
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/bitcoin/bitcoin_client_rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (suite *BitcoinClientTestSuite) SetupTest() {
PrivKey: privateKey,
}
appContext := appcontext.NewAppContext(&corecontext.ZetaCoreContext{}, config.Config{})
client, err := NewBitcoinClient(appContext, common.BtcRegtestChain(), nil, tss, "/tmp",
client, err := NewBitcoinClient(appContext, common.BtcRegtestChain(), nil, tss, tempSQLiteDbPath,
clientcommon.DefaultLoggers(), config.BTCConfig{}, nil)
suite.Require().NoError(err)
suite.BitcoinChainClient = client
Expand Down
43 changes: 40 additions & 3 deletions zetaclient/bitcoin/bitcoin_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,47 @@ import (
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/testutil/sample"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/config"
corecontext "github.com/zeta-chain/zetacore/zetaclient/core_context"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
"github.com/zeta-chain/zetacore/zetaclient/testutils/stub"
)

func MockBTCClientMainnet() *BTCChainClient {
cfg := config.NewConfig()
coreContext := corecontext.NewZetaCoreContext(cfg)

return &BTCChainClient{
chain: common.BtcMainnetChain(),
zetaClient: stub.NewMockZetaCoreBridge(),
Tss: stub.NewTSSMainnet(),
chain: common.BtcMainnetChain(),
zetaClient: stub.NewMockZetaCoreBridge(),
Tss: stub.NewTSSMainnet(),
coreContext: coreContext,
}
}

func TestNewBitcoinClient(t *testing.T) {
t.Run("should return error because zetacore doesn't update core context", func(t *testing.T) {
cfg := config.NewConfig()
coreContext := corecontext.NewZetaCoreContext(cfg)
appContext := appcontext.NewAppContext(coreContext, cfg)
chain := common.BtcMainnetChain()
bridge := stub.NewMockZetaCoreBridge()
tss := stub.NewMockTSS(sample.EthAddress().String(), "")
loggers := clientcommon.ClientLogger{}
btcCfg := cfg.BitcoinConfig
ts := metrics.NewTelemetryServer()

client, err := NewBitcoinClient(appContext, chain, bridge, tss, tempSQLiteDbPath, loggers, btcCfg, ts)
require.ErrorContains(t, err, "btc chains params not initialized")
require.Nil(t, client)
})
}

func TestConfirmationThreshold(t *testing.T) {
client := &BTCChainClient{Mu: &sync.Mutex{}}
t.Run("should return confirmations in chain param", func(t *testing.T) {
Expand Down Expand Up @@ -316,3 +344,12 @@ func TestCheckTSSVoutCancelled(t *testing.T) {
require.ErrorContains(t, err, "not match TSS address")
})
}

func TestBTCChainClient_ObserveInTx(t *testing.T) {
t.Run("should return error", func(t *testing.T) {
// create mainnet mock client
btcClient := MockBTCClientMainnet()
err := btcClient.ObserveInTx()
require.ErrorContains(t, err, "inbound TXS / Send has been disabled by the protocol")
})
}
13 changes: 7 additions & 6 deletions zetaclient/bitcoin/bitcoin_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"math/rand"
"time"

corecontext "github.com/zeta-chain/zetacore/zetaclient/core_context"

ethcommon "github.com/ethereum/go-ethereum/common"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
Expand Down Expand Up @@ -42,6 +44,7 @@
logger zerolog.Logger
loggerCompliance zerolog.Logger
ts *metrics.TelemetryServer
coreContext *corecontext.ZetaCoreContext
}

var _ interfaces.ChainSigner = &BTCSigner{}
Expand All @@ -50,7 +53,8 @@
cfg config.BTCConfig,
tssSigner interfaces.TSSSigner,
loggers clientcommon.ClientLogger,
ts *metrics.TelemetryServer) (*BTCSigner, error) {
ts *metrics.TelemetryServer,
coreContext *corecontext.ZetaCoreContext) (*BTCSigner, error) {

Check warning on line 57 in zetaclient/bitcoin/bitcoin_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_signer.go#L57

Added line #L57 was not covered by tests
connCfg := &rpcclient.ConnConfig{
Host: cfg.RPCHost,
User: cfg.RPCUsername,
Expand All @@ -70,6 +74,7 @@
logger: loggers.Std.With().Str("chain", "BTC").Str("module", "BTCSigner").Logger(),
loggerCompliance: loggers.Compliance,
ts: ts,
coreContext: coreContext,

Check warning on line 77 in zetaclient/bitcoin/bitcoin_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_signer.go#L77

Added line #L77 was not covered by tests
}, nil
}

Expand Down Expand Up @@ -290,11 +295,7 @@
logger.Error().Msgf("chain client is not a bitcoin client")
return
}
flags, err := zetaBridge.GetCrosschainFlags()
if err != nil {
logger.Error().Err(err).Msgf("cannot get crosschain flags")
return
}
flags := signer.coreContext.GetCrossChainFlags()

Check warning on line 298 in zetaclient/bitcoin/bitcoin_signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_signer.go#L298

Added line #L298 was not covered by tests
if !flags.IsOutboundEnabled {
logger.Info().Msgf("outbound is disabled")
return
Expand Down
16 changes: 11 additions & 5 deletions zetaclient/bitcoin/bitcoin_signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"sync"
"testing"

clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/metrics"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcjson"
Expand All @@ -24,7 +20,11 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/config"
corecontext "github.com/zeta-chain/zetacore/zetaclient/core_context"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
. "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -75,7 +75,13 @@ func (s *BTCSignerSuite) SetUpTest(c *C) {
tss := interfaces.TestSigner{
PrivKey: privateKey,
}
s.btcSigner, err = NewBTCSigner(config.BTCConfig{}, &tss, clientcommon.DefaultLoggers(), &metrics.TelemetryServer{})
cfg := config.NewConfig()
s.btcSigner, err = NewBTCSigner(
config.BTCConfig{},
&tss,
clientcommon.DefaultLoggers(),
&metrics.TelemetryServer{},
corecontext.NewZetaCoreContext(cfg))
c.Assert(err, IsNil)
}

Expand Down
14 changes: 13 additions & 1 deletion zetaclient/core_context/zeta_core_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ZetaCoreContext struct {
evmChainParams map[int64]*observertypes.ChainParams
bitcoinChainParams *observertypes.ChainParams
currentTssPubkey string
crossChainFlags observertypes.CrosschainFlags
}

// NewZetaCoreContext creates and returns new ZetaCoreContext
Expand All @@ -39,6 +40,7 @@ func NewZetaCoreContext(cfg config.Config) *ZetaCoreContext {
chainsEnabled: []common.Chain{},
evmChainParams: evmChainParams,
bitcoinChainParams: bitcoinChainParams,
crossChainFlags: observertypes.CrosschainFlags{},
}
}

Expand Down Expand Up @@ -106,6 +108,12 @@ func (c *ZetaCoreContext) GetBTCChainParams() (common.Chain, *observertypes.Chai
return *chain, c.bitcoinChainParams, true
}

func (c *ZetaCoreContext) GetCrossChainFlags() observertypes.CrosschainFlags {
c.coreContextLock.RLock()
defer c.coreContextLock.RUnlock()
return c.crossChainFlags
}

// Update updates core context and params for all chains
// this must be the ONLY function that writes to core context
func (c *ZetaCoreContext) Update(
Expand All @@ -114,6 +122,7 @@ func (c *ZetaCoreContext) Update(
evmChainParams map[int64]*observertypes.ChainParams,
btcChainParams *observertypes.ChainParams,
tssPubKey string,
crosschainFlags observertypes.CrosschainFlags,
init bool,
logger zerolog.Logger,
) {
Expand Down Expand Up @@ -148,8 +157,11 @@ func (c *ZetaCoreContext) Update(
}
}
}
c.keygen = *keygen
if keygen != nil {
c.keygen = *keygen
}
c.chainsEnabled = newChains
c.crossChainFlags = crosschainFlags
// update chain params for bitcoin if it has config in file
if c.bitcoinChainParams != nil && btcChainParams != nil {
c.bitcoinChainParams = btcChainParams
Expand Down
15 changes: 15 additions & 0 deletions zetaclient/core_context/zeta_core_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,19 @@ func TestUpdateZetaCoreContext(t *testing.T) {
}
tssPubKeyToUpdate := "tsspubkeytest"
loggers := clientcommon.DefaultLoggers()
crosschainFlags := observertypes.CrosschainFlags{
kevinssgh marked this conversation as resolved.
Show resolved Hide resolved
IsInboundEnabled: false,
IsOutboundEnabled: false,
GasPriceIncreaseFlags: nil,
BlockHeaderVerificationFlags: nil,
}
zetaContext.Update(
&keyGenToUpdate,
enabledChainsToUpdate,
evmChainParamsToUpdate,
btcChainParamsToUpdate,
tssPubKeyToUpdate,
crosschainFlags,
false,
loggers.Std,
)
Expand All @@ -154,6 +161,9 @@ func TestUpdateZetaCoreContext(t *testing.T) {
// assert evm chain params still empty because they were not specified in config
allEVMChainParams := zetaContext.GetAllEVMChainParams()
require.Empty(t, allEVMChainParams)

ccflags := zetaContext.GetCrossChainFlags()
kevinssgh marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, crosschainFlags, ccflags)
})

t.Run("should update core context after being created from config with evm and btc chain params", func(t *testing.T) {
Expand Down Expand Up @@ -210,13 +220,15 @@ func TestUpdateZetaCoreContext(t *testing.T) {
ChainId: testBtcChain.ChainId,
}
tssPubKeyToUpdate := "tsspubkeytest"
crosschainFlags := observertypes.CrosschainFlags{}
kevinssgh marked this conversation as resolved.
Show resolved Hide resolved
loggers := clientcommon.DefaultLoggers()
zetaContext.Update(
&keyGenToUpdate,
enabledChainsToUpdate,
evmChainParamsToUpdate,
btcChainParamsToUpdate,
tssPubKeyToUpdate,
crosschainFlags,
false,
loggers.Std,
)
Expand Down Expand Up @@ -248,6 +260,9 @@ func TestUpdateZetaCoreContext(t *testing.T) {
evmChainParams2, found := zetaContext.GetEVMChainParams(2)
require.True(t, found)
require.Equal(t, evmChainParamsToUpdate[2], evmChainParams2)

ccflags := zetaContext.GetCrossChainFlags()
kevinssgh marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, ccflags, crosschainFlags)
})
}

Expand Down
5 changes: 1 addition & 4 deletions zetaclient/evm/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,10 +907,7 @@

func (ob *ChainClient) observeInTX(sampledLogger zerolog.Logger) error {
// make sure inbound TXS / Send is enabled by the protocol
flags, err := ob.zetaClient.GetCrosschainFlags()
if err != nil {
return err
}
flags := ob.coreContext.GetCrossChainFlags()

Check warning on line 910 in zetaclient/evm/evm_client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/evm/evm_client.go#L910

Added line #L910 was not covered by tests
if !flags.IsInboundEnabled {
return errors.New("inbound TXS / Send has been disabled by the protocol")
}
Expand Down
Loading
Loading