Skip to content

Commit

Permalink
feat(zetaclient): propagate context across codebase & refactor zetaco…
Browse files Browse the repository at this point in the history
…re client (#2428)

* Implement `retry` package

* Refactor zetacore query methods

* Refactor zetacore broadcast.go

* Refactor zetacore client construction

* Refactor zetacore client [WIP]

* Fix debug cli command

* Add ctx to evm observer

* Add ctx to evm observer & signer

* Add `bg` package

* Add ctx to btc observer

* Add ctx to supply checker

* Add ctx to orchestrator & signers

* Fix lint errors

* Improve zetacore client configuration. Fix zetacore tests [WIP]

* Fix zetacore client test cases

* Fix other test cases

* Resolve merge conflicts

* Update changelog

* Address PR comments [1]

* Address PR comments [2]

* Remove logger pointer from bg package

* Minor fix

* Minor code improvement

* Converge config.New and config.NewConfig

* Improve NewSigner logging

* Add zctx.Copy()

* Refactor Orchestrator shutdown logic

* Fix retrier logic for monitors

* Minor fix

* Fix gosec

* Update e2e readme

* Fix BTC outbound typo

* Fix typo

* Address PR comments

* Add test cases for `bg`

* Handle ctx errors in `retry`
  • Loading branch information
swift1337 authored Jul 12, 2024
1 parent 98a408f commit 59d5ba9
Show file tree
Hide file tree
Showing 72 changed files with 4,347 additions and 2,632 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
* [2375](https://github.com/zeta-chain/node/pull/2375) - improve & speedup code formatting
* [2380](https://github.com/zeta-chain/node/pull/2380) - use `ChainInfo` in `authority` to allow dynamically support new chains
* [2395](https://github.com/zeta-chain/node/pull/2395) - converge AppContext with ZetaCoreContext in zetaclient
* [2428](https://github.com/zeta-chain/node/pull/2428) - propagate context across codebase & refactor zetacore client

### Tests

Expand Down
22 changes: 12 additions & 10 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
btcobserver "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin/observer"
evmobserver "github.com/zeta-chain/zetacore/zetaclient/chains/evm/observer"
"github.com/zeta-chain/zetacore/zetaclient/config"
clientcontext "github.com/zeta-chain/zetacore/zetaclient/context"
zctx "github.com/zeta-chain/zetacore/zetaclient/context"
"github.com/zeta-chain/zetacore/zetaclient/keys"
"github.com/zeta-chain/zetacore/zetaclient/zetacore"
)
Expand Down Expand Up @@ -57,7 +57,8 @@ func debugCmd(_ *cobra.Command, args []string) error {
return err
}

appContext := clientcontext.New(cfg, zerolog.Nop())
appContext := zctx.New(cfg, zerolog.Nop())
ctx := zctx.WithAppContext(context.Background(), appContext)

chainID, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
Expand All @@ -74,15 +75,16 @@ func debugCmd(_ *cobra.Command, args []string) error {
"",
debugArgs.zetaChainID,
false,
nil)
zerolog.Nop(),
)
if err != nil {
return err
}
chainParams, err := client.GetChainParams()
chainParams, err := client.GetChainParams(ctx)
if err != nil {
return err
}
tssEthAddress, err := client.GetEthTssAddress()
tssEthAddress, err := client.GetEVMTSSAddress(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -148,19 +150,19 @@ func debugCmd(_ *cobra.Command, args []string) error {

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

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

case coin.CoinType_Gas:
ballotIdentifier, err = evmObserver.CheckAndVoteInboundTokenGas(tx, receipt, false)
ballotIdentifier, err = evmObserver.CheckAndVoteInboundTokenGas(ctx, tx, receipt, false)
if err != nil {
return err
}
Expand All @@ -186,15 +188,15 @@ func debugCmd(_ *cobra.Command, args []string) error {
return err
}
btcObserver.WithBtcClient(btcClient)
ballotIdentifier, err = btcObserver.CheckReceiptForBtcTxHash(inboundHash, false)
ballotIdentifier, err = btcObserver.CheckReceiptForBtcTxHash(ctx, inboundHash, false)
if err != nil {
return err
}
}
fmt.Println("BallotIdentifier : ", ballotIdentifier)

// query ballot
ballot, err := client.GetBallot(ballotIdentifier)
ballot, err := client.GetBallot(ctx, ballotIdentifier)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetaclientd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Initialize(_ *cobra.Command, _ []string) error {
}

//Create new config struct
configData := config.New()
configData := config.New(true)

//Validate Peer eg. /ip4/172.0.2.1/tcp/6668/p2p/16Uiu2HAmACG5DtqmQsHtXg4G2sLS65ttv84e7MrL4kapkjfmhxAp
if len(initArgs.peer) != 0 {
Expand Down
45 changes: 32 additions & 13 deletions cmd/zetaclientd/keygen_tss.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -16,35 +17,42 @@ import (

"github.com/zeta-chain/zetacore/pkg/chains"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient/context"
zctx "github.com/zeta-chain/zetacore/zetaclient/context"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
mc "github.com/zeta-chain/zetacore/zetaclient/tss"
"github.com/zeta-chain/zetacore/zetaclient/zetacore"
)

func GenerateTss(
appContext *context.AppContext,
ctx context.Context,
logger zerolog.Logger,
client *zetacore.Client,
peers p2p.AddrList,
priKey secp256k1.PrivKey,
ts *metrics.TelemetryServer,
tssHistoricalList []observertypes.TSS,
tssPassword string,
hotkeyPassword string) (*mc.TSS, error) {
hotkeyPassword string,
) (*mc.TSS, error) {
app, err := zctx.FromContext(ctx)
if err != nil {
return nil, err
}

keygenLogger := logger.With().Str("module", "keygen").Logger()

// Bitcoin chain ID is currently used for using the correct signature format
// TODO: remove this once we have a better way to determine the signature format
// https://github.com/zeta-chain/node/issues/1397
bitcoinChainID := chains.BitcoinRegtest.ChainId
btcChain, _, btcEnabled := appContext.GetBTCChainAndConfig()
btcChain, _, btcEnabled := app.GetBTCChainAndConfig()
if btcEnabled {
bitcoinChainID = btcChain.ChainId
}

tss, err := mc.NewTSS(
appContext,
ctx,
app,
peers,
priKey,
preParams,
Expand Down Expand Up @@ -74,7 +82,7 @@ func GenerateTss(
// This loop will try keygen at the keygen block and then wait for keygen to be successfully reported by all nodes before breaking out of the loop.
// If keygen is unsuccessful, it will reset the triedKeygenAtBlock flag and try again at a new keygen block.

keyGen := appContext.GetKeygen()
keyGen := app.GetKeygen()
if keyGen.Status == observertypes.KeygenStatus_KeyGenSuccess {
return tss, nil
}
Expand All @@ -86,7 +94,7 @@ func GenerateTss(
// Try generating TSS at keygen block , only when status is pending keygen and generation has not been tried at the block
if keyGen.Status == observertypes.KeygenStatus_PendingKeygen {
// Return error if RPC is not working
currentBlock, err := client.GetBlockHeight()
currentBlock, err := client.GetBlockHeight(ctx)
if err != nil {
keygenLogger.Error().Err(err).Msg("GetBlockHeight RPC error")
continue
Expand All @@ -101,16 +109,21 @@ func GenerateTss(
if currentBlock > lastBlock {
lastBlock = currentBlock
keygenLogger.Info().
Msgf("Waiting For Keygen Block to arrive or new keygen block to be set. Keygen Block : %d Current Block : %d ChainID %s ", keyGen.BlockNumber, currentBlock, appContext.Config().ChainID)
Msgf("Waiting For Keygen Block to arrive or new keygen block to be set. Keygen Block : %d Current Block : %d ChainID %s ", keyGen.BlockNumber, currentBlock, app.Config().ChainID)
}
continue
}
// Try keygen only once at a particular block, irrespective of whether it is successful or failure
triedKeygenAtBlock = true
err = keygenTss(keyGen, tss, keygenLogger)
err = keygenTss(ctx, keyGen, tss, keygenLogger)
if err != nil {
keygenLogger.Error().Err(err).Msg("keygenTss error")
tssFailedVoteHash, err := client.SetTSS("", keyGen.BlockNumber, chains.ReceiveStatus_failed)
tssFailedVoteHash, err := client.PostVoteTSS(
ctx,
"",
keyGen.BlockNumber,
chains.ReceiveStatus_failed,
)
if err != nil {
keygenLogger.Error().Err(err).Msg("Failed to broadcast Failed TSS Vote to zetacore")
return nil, err
Expand All @@ -128,7 +141,8 @@ func GenerateTss(
}

// If TSS is successful , broadcast the vote to zetacore and set Pubkey
tssSuccessVoteHash, err := client.SetTSS(
tssSuccessVoteHash, err := client.PostVoteTSS(
ctx,
newTss.CurrentPubkey,
keyGen.BlockNumber,
chains.ReceiveStatus_success,
Expand All @@ -155,7 +169,7 @@ func GenerateTss(
return nil, errors.New("unexpected state for TSS generation")
}

func keygenTss(keyGen observertypes.Keygen, tss *mc.TSS, keygenLogger zerolog.Logger) error {
func keygenTss(ctx context.Context, keyGen observertypes.Keygen, tss *mc.TSS, keygenLogger zerolog.Logger) error {
keygenLogger.Info().Msgf("Keygen at blocknum %d , TSS signers %s ", keyGen.BlockNumber, keyGen.GranteePubkeys)
var req keygen.Request
req = keygen.NewRequest(keyGen.GranteePubkeys, keyGen.BlockNumber, "0.14.0")
Expand All @@ -168,7 +182,12 @@ func keygenTss(keyGen observertypes.Keygen, tss *mc.TSS, keygenLogger zerolog.Lo
return err
}
index := fmt.Sprintf("keygen-%s-%d", digest, keyGen.BlockNumber)
zetaHash, err := tss.ZetacoreClient.PostBlameData(&res.Blame, tss.ZetacoreClient.Chain().ChainId, index)
zetaHash, err := tss.ZetacoreClient.PostVoteBlameData(
ctx,
&res.Blame,
tss.ZetacoreClient.Chain().ChainId,
index,
)
if err != nil {
keygenLogger.Error().Err(err).Msg("error sending blame data to core")
return err
Expand Down
Loading

0 comments on commit 59d5ba9

Please sign in to comment.