Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/zeta-chain/node into orc…
Browse files Browse the repository at this point in the history
…hestrator-add-new-chain-at-runtime
  • Loading branch information
ws4charlie committed Jul 1, 2024
2 parents 9ccbae4 + 2d5519f commit eef51cc
Show file tree
Hide file tree
Showing 17 changed files with 339 additions and 313 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* [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
* [2395](https://github.com/zeta-chain/node/pull/2395) - converge AppContext with ZetaCoreContext in zetaclient

### Tests

Expand Down
281 changes: 142 additions & 139 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,163 +42,166 @@ func init() {
}

func DebugCmd() *cobra.Command {
cmd := &cobra.Command{
return &cobra.Command{
Use: "get-inbound-ballot [inboundHash] [chainID]",
Short: "provide txHash and chainID to get the ballot status for the txHash",
RunE: func(_ *cobra.Command, args []string) error {
cobra.ExactArgs(2)
cfg, err := config.Load(debugArgs.zetaCoreHome)
if err != nil {
return err
}
appContext := clientcontext.NewAppContext(cfg)
chainID, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}
inboundHash := args[0]
var ballotIdentifier string

// create a new zetacore client
client, err := zetacore.NewClient(
&keys.Keys{OperatorAddress: sdk.MustAccAddressFromBech32(sample.AccAddress())},
debugArgs.zetaNode,
"",
debugArgs.zetaChainID,
false,
nil)
if err != nil {
return err
}
chainParams, err := client.GetChainParams()
if err != nil {
return err
}
tssEthAddress, err := client.GetEthTssAddress()
if err != nil {
return err
}
chain := chains.GetChainFromChainID(chainID)
if chain == nil {
return fmt.Errorf("invalid chain id")
}
RunE: debugCmd,
}
}

// get ballot identifier according to the chain type
if chains.IsEVMChain(chain.ChainId) {
evmObserver := evmobserver.Observer{}
evmObserver.WithZetacoreClient(client)
var ethRPC *ethrpc.EthRPC
var client *ethclient.Client
coinType := coin.CoinType_Cmd
for chain, evmConfig := range cfg.GetAllEVMConfigs() {
if chainID == chain {
ethRPC = ethrpc.NewEthRPC(evmConfig.Endpoint)
client, err = ethclient.Dial(evmConfig.Endpoint)
if err != nil {
return err
}
evmObserver.WithEvmClient(client)
evmObserver.WithEvmJSONRPC(ethRPC)
evmObserver.WithChain(*chains.GetChainFromChainID(chainID))
}
}
hash := ethcommon.HexToHash(inboundHash)
tx, isPending, err := evmObserver.TransactionByHash(inboundHash)
if err != nil {
return fmt.Errorf("tx not found on chain %s , %d", err.Error(), chain.ChainId)
}
if isPending {
return fmt.Errorf("tx is still pending")
}
receipt, err := client.TransactionReceipt(context.Background(), hash)
if err != nil {
return fmt.Errorf("tx receipt not found on chain %s, %d", err.Error(), chain.ChainId)
}
func debugCmd(_ *cobra.Command, args []string) error {
cobra.ExactArgs(2)
cfg, err := config.Load(debugArgs.zetaCoreHome)
if err != nil {
return err
}

for _, chainParams := range chainParams {
if chainParams.ChainId == chainID {
evmObserver.SetChainParams(observertypes.ChainParams{
ChainId: chainID,
ConnectorContractAddress: chainParams.ConnectorContractAddress,
ZetaTokenContractAddress: chainParams.ZetaTokenContractAddress,
Erc20CustodyContractAddress: chainParams.Erc20CustodyContractAddress,
})
evmChainParams, found := appContext.GetExternalChainParams(chainID)
if !found {
return fmt.Errorf("missing chain params for chain %d", chainID)
}
evmChainParams.ZetaTokenContractAddress = chainParams.ZetaTokenContractAddress
if strings.EqualFold(tx.To, chainParams.ConnectorContractAddress) {
coinType = coin.CoinType_Zeta
} else if strings.EqualFold(tx.To, chainParams.Erc20CustodyContractAddress) {
coinType = coin.CoinType_ERC20
} else if strings.EqualFold(tx.To, tssEthAddress) {
coinType = coin.CoinType_Gas
}
}
}
appContext := clientcontext.NewAppContext(cfg)

switch coinType {
case coin.CoinType_Zeta:
ballotIdentifier, err = evmObserver.CheckAndVoteInboundTokenZeta(tx, receipt, false)
if err != nil {
return err
}

case coin.CoinType_ERC20:
ballotIdentifier, err = evmObserver.CheckAndVoteInboundTokenERC20(tx, receipt, false)
if err != nil {
return err
}

case coin.CoinType_Gas:
ballotIdentifier, err = evmObserver.CheckAndVoteInboundTokenGas(tx, receipt, false)
if err != nil {
return err
}
default:
fmt.Println("CoinType not detected")
}
fmt.Println("CoinType : ", coinType)
} else if chains.IsBitcoinChain(chain.ChainId) {
btcObserver := btcobserver.Observer{}
btcObserver.WithZetacoreClient(client)
btcObserver.WithChain(*chains.GetChainFromChainID(chainID))
connCfg := &rpcclient.ConnConfig{
Host: cfg.BitcoinConfig.RPCHost,
User: cfg.BitcoinConfig.RPCUsername,
Pass: cfg.BitcoinConfig.RPCPassword,
HTTPPostMode: true,
DisableTLS: true,
Params: cfg.BitcoinConfig.RPCParams,
}
chainID, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}

inboundHash := args[0]
var ballotIdentifier string

// create a new zetacore client
client, err := zetacore.NewClient(
&keys.Keys{OperatorAddress: sdk.MustAccAddressFromBech32(sample.AccAddress())},
debugArgs.zetaNode,
"",
debugArgs.zetaChainID,
false,
nil)
if err != nil {
return err
}
chainParams, err := client.GetChainParams()
if err != nil {
return err
}
tssEthAddress, err := client.GetEthTssAddress()
if err != nil {
return err
}
chain := chains.GetChainFromChainID(chainID)
if chain == nil {
return fmt.Errorf("invalid chain id")
}

btcClient, err := rpcclient.New(connCfg, nil)
// get ballot identifier according to the chain type
if chains.IsEVMChain(chain.ChainId) {
evmObserver := evmobserver.Observer{}
evmObserver.WithZetacoreClient(client)
var ethRPC *ethrpc.EthRPC
var client *ethclient.Client
coinType := coin.CoinType_Cmd
for chain, evmConfig := range cfg.GetAllEVMConfigs() {
if chainID == chain {
ethRPC = ethrpc.NewEthRPC(evmConfig.Endpoint)
client, err = ethclient.Dial(evmConfig.Endpoint)
if err != nil {
return err
}
btcObserver.WithBtcClient(btcClient)
ballotIdentifier, err = btcObserver.CheckReceiptForBtcTxHash(inboundHash, false)
if err != nil {
return err
evmObserver.WithEvmClient(client)
evmObserver.WithEvmJSONRPC(ethRPC)
evmObserver.WithChain(*chains.GetChainFromChainID(chainID))
}
}
hash := ethcommon.HexToHash(inboundHash)
tx, isPending, err := evmObserver.TransactionByHash(inboundHash)
if err != nil {
return fmt.Errorf("tx not found on chain %s , %d", err.Error(), chain.ChainId)
}
if isPending {
return fmt.Errorf("tx is still pending")
}
receipt, err := client.TransactionReceipt(context.Background(), hash)
if err != nil {
return fmt.Errorf("tx receipt not found on chain %s, %d", err.Error(), chain.ChainId)
}

for _, chainParams := range chainParams {
if chainParams.ChainId == chainID {
evmObserver.SetChainParams(observertypes.ChainParams{
ChainId: chainID,
ConnectorContractAddress: chainParams.ConnectorContractAddress,
ZetaTokenContractAddress: chainParams.ZetaTokenContractAddress,
Erc20CustodyContractAddress: chainParams.Erc20CustodyContractAddress,
})
evmChainParams, found := appContext.GetExternalChainParams(chainID)
if !found {
return fmt.Errorf("missing chain params for chain %d", chainID)
}
evmChainParams.ZetaTokenContractAddress = chainParams.ZetaTokenContractAddress
if strings.EqualFold(tx.To, chainParams.ConnectorContractAddress) {
coinType = coin.CoinType_Zeta
} else if strings.EqualFold(tx.To, chainParams.Erc20CustodyContractAddress) {
coinType = coin.CoinType_ERC20
} else if strings.EqualFold(tx.To, tssEthAddress) {
coinType = coin.CoinType_Gas
}
}
fmt.Println("BallotIdentifier : ", ballotIdentifier)
}

// query ballot
ballot, err := client.GetBallot(ballotIdentifier)
switch coinType {
case coin.CoinType_Zeta:
ballotIdentifier, err = evmObserver.CheckAndVoteInboundTokenZeta(tx, receipt, false)
if err != nil {
return err
}

for _, vote := range ballot.Voters {
fmt.Printf("%s : %s \n", vote.VoterAddress, vote.VoteType)
case coin.CoinType_ERC20:
ballotIdentifier, err = evmObserver.CheckAndVoteInboundTokenERC20(tx, receipt, false)
if err != nil {
return err
}
fmt.Println("BallotStatus : ", ballot.BallotStatus)

return nil
},
case coin.CoinType_Gas:
ballotIdentifier, err = evmObserver.CheckAndVoteInboundTokenGas(tx, receipt, false)
if err != nil {
return err
}
default:
fmt.Println("CoinType not detected")
}
fmt.Println("CoinType : ", coinType)
} else if chains.IsBitcoinChain(chain.ChainId) {
btcObserver := btcobserver.Observer{}
btcObserver.WithZetacoreClient(client)
btcObserver.WithChain(*chains.GetChainFromChainID(chainID))
connCfg := &rpcclient.ConnConfig{
Host: cfg.BitcoinConfig.RPCHost,
User: cfg.BitcoinConfig.RPCUsername,
Pass: cfg.BitcoinConfig.RPCPassword,
HTTPPostMode: true,
DisableTLS: true,
Params: cfg.BitcoinConfig.RPCParams,
}

btcClient, err := rpcclient.New(connCfg, nil)
if err != nil {
return err
}
btcObserver.WithBtcClient(btcClient)
ballotIdentifier, err = btcObserver.CheckReceiptForBtcTxHash(inboundHash, false)
if err != nil {
return err
}
}
fmt.Println("BallotIdentifier : ", ballotIdentifier)

// query ballot
ballot, err := client.GetBallot(ballotIdentifier)
if err != nil {
return err
}

for _, vote := range ballot.Voters {
fmt.Printf("%s : %s \n", vote.VoterAddress, vote.VoteType)
}
fmt.Println("BallotStatus : ", ballot.BallotStatus)

return cmd
return nil
}
8 changes: 1 addition & 7 deletions zetaclient/chains/base/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Observer struct {
// chainParams contains the dynamic chain parameters of the observed chain
chainParams observertypes.ChainParams

// appContext contains context data of zetaclient
// appContext contains context data for zetaclient & zetacore (e.g. supported chains)
appContext *context.AppContext

// zetacoreClient is the client to interact with ZetaChain
Expand Down Expand Up @@ -170,12 +170,6 @@ func (ob *Observer) AppContext() *context.AppContext {
return ob.appContext
}

// WithAppContext attaches a new app context to the observer.
func (ob *Observer) WithAppContext(context *context.AppContext) *Observer {
ob.appContext = context
return ob
}

// ZetacoreClient returns the zetacore client for the observer.
func (ob *Observer) ZetacoreClient() interfaces.ZetacoreClient {
return ob.zetacoreClient
Expand Down
8 changes: 0 additions & 8 deletions zetaclient/chains/base/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,6 @@ func TestObserverGetterAndSetter(t *testing.T) {
ob = ob.WithChainParams(newChainParams)
require.True(t, observertypes.ChainParamsEqual(newChainParams, ob.ChainParams()))
})
t.Run("should be able to update app context", func(t *testing.T) {
ob := createObserver(t)

// update app context
newAppContext := context.NewAppContext(config.NewConfig())
ob = ob.WithAppContext(newAppContext)
require.Equal(t, newAppContext, ob.AppContext())
})
t.Run("should be able to update zetacore client", func(t *testing.T) {
ob := createObserver(t)

Expand Down
6 changes: 0 additions & 6 deletions zetaclient/chains/base/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ func (s *Signer) AppContext() *context.AppContext {
return s.appContext
}

// WithAppContext attaches a new app context to the signer
func (s *Signer) WithAppContext(context *context.AppContext) *Signer {
s.appContext = context
return s
}

// Tss returns the tss signer for the signer
func (s *Signer) TSS() interfaces.TSSSigner {
return s.tss
Expand Down
8 changes: 0 additions & 8 deletions zetaclient/chains/base/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ func TestSignerGetterAndSetter(t *testing.T) {
signer = signer.WithChain(chains.BscMainnet)
require.Equal(t, newChain, signer.Chain())
})
t.Run("should be able to update app context", func(t *testing.T) {
signer := createSigner(t)

// update app context
newAppContext := context.NewAppContext(config.NewConfig())
signer = signer.WithAppContext(newAppContext)
require.Equal(t, newAppContext, signer.AppContext())
})
t.Run("should be able to update tss", func(t *testing.T) {
signer := createSigner(t)

Expand Down
Loading

0 comments on commit eef51cc

Please sign in to comment.