Skip to content

Commit

Permalink
refactor(zetaclient)!: improve AppContext (zeta-chain#2568)
Browse files Browse the repository at this point in the history
* Implement chain registry

* Rewrite test-cases for AppContext

* Drop `supplychecker`

* Refactor app ctx Update worker

* Refactor orchestrator

* Refactor observer&signer; DROP postBlockHeaders

* Fix test cases [1]

* Update changelog

* Allow Zeta Chain in appContext; address PR comments [1]

* Fix app context update

* Check for `chain.IsZeta()`

* Add AppContext.FilterChains

* Fix test cases [2]

* Fix test cases [3]

* Address PR comments [1]

* Address PR comments [2]

* Add tests for `slices`

* Fix e2e tests [1]

* Fix e2e tests [2]

* Resolve conflicts, converge codebase between PRs

* Add lodash; remove slices pkg

* Address PR comments

* Minor logging fix

* Address PR comments
  • Loading branch information
swift1337 authored Aug 2, 2024
1 parent 65ff18f commit b4251e1
Show file tree
Hide file tree
Showing 58 changed files with 1,634 additions and 2,485 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
* [2542](https://github.com/zeta-chain/node/pull/2542) - adjust permissions to be more restrictive
* [2572](https://github.com/zeta-chain/node/pull/2572) - turn off IBC modules
* [2556](https://github.com/zeta-chain/node/pull/2556) - refactor migrator length check to use consensus type
* [2568](https://github.com/zeta-chain/node/pull/2568) - improve AppContext by converging chains, chainParams, enabledChains, and additionalChains into a single zctx.Chain

### Tests

Expand Down
93 changes: 46 additions & 47 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"context"
"fmt"
"os"
"strconv"
"strings"

"cosmossdk.io/errors"
"github.com/btcsuite/btcd/rpcclient"
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
Expand All @@ -14,10 +16,8 @@ import (
"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/coin"
"github.com/zeta-chain/zetacore/testutil/sample"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
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"
Expand All @@ -35,11 +35,14 @@ type debugArguments struct {
}

func init() {
RootCmd.AddCommand(DebugCmd())
DebugCmd().Flags().
StringVar(&debugArgs.zetaCoreHome, "core-home", "/Users/tanmay/.zetacored", "peer address, e.g. /dns/tss1/tcp/6668/ipfs/16Uiu2HAmACG5DtqmQsHtXg4G2sLS65ttv84e7MrL4kapkjfmhxAp")
DebugCmd().Flags().StringVar(&debugArgs.zetaNode, "node", "46.4.15.110", "public ip address")
DebugCmd().Flags().StringVar(&debugArgs.zetaChainID, "chain-id", "athens_7001-1", "pre-params file path")
defaultHomeDir := os.ExpandEnv("$HOME/.zetacored")

cmd := DebugCmd()
cmd.Flags().StringVar(&debugArgs.zetaCoreHome, "core-home", defaultHomeDir, "zetacore home directory")
cmd.Flags().StringVar(&debugArgs.zetaNode, "node", "46.4.15.110", "public ip address")
cmd.Flags().StringVar(&debugArgs.zetaChainID, "chain-id", "athens_7001-1", "pre-params file path")

RootCmd.AddCommand(cmd)
}

func DebugCmd() *cobra.Command {
Expand All @@ -54,20 +57,16 @@ func debugCmd(_ *cobra.Command, args []string) error {
cobra.ExactArgs(2)
cfg, err := config.Load(debugArgs.zetaCoreHome)
if err != nil {
return err
return errors.Wrap(err, "failed to load config")
}

appContext := zctx.New(cfg, zerolog.Nop())
ctx := zctx.WithAppContext(context.Background(), appContext)
inboundHash := args[0]

chainID, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
return errors.Wrap(err, "failed to parse chain id")
}

inboundHash := args[0]
var ballotIdentifier string

// create a new zetacore client
client, err := zetacore.NewClient(
&keys.Keys{OperatorAddress: sdk.MustAccAddressFromBech32(sample.AccAddress())},
Expand All @@ -80,21 +79,30 @@ func debugCmd(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
chainParams, err := client.GetChainParams(ctx)
if err != nil {
return err

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

if err := client.UpdateAppContext(ctx, appContext, zerolog.Nop()); err != nil {
return errors.Wrap(err, "failed to update app context")
}

var ballotIdentifier string

tssEthAddress, err := client.GetEVMTSSAddress(ctx)
if err != nil {
return err
}
chain, found := chains.GetChainFromChainID(chainID, appContext.GetAdditionalChains())
if !found {
return fmt.Errorf("invalid chain id")

chain, err := appContext.GetChain(chainID)
if err != nil {
return err
}

chainProto := chain.RawChain()

// get ballot identifier according to the chain type
if chains.IsEVMChain(chain.ChainId, appContext.GetAdditionalChains()) {
if chain.IsEVM() {
evmObserver := evmobserver.Observer{}
evmObserver.WithZetacoreClient(client)
var ethRPC *ethrpc.EthRPC
Expand All @@ -109,43 +117,34 @@ func debugCmd(_ *cobra.Command, args []string) error {
}
evmObserver.WithEvmClient(client)
evmObserver.WithEvmJSONRPC(ethRPC)
evmObserver.WithChain(chain)
evmObserver.WithChain(*chainProto)
}
}
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)
return fmt.Errorf("tx not found on chain %s, %d", err.Error(), chain.ID())
}

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)
return fmt.Errorf("tx receipt not found on chain %s, %d", err.Error(), chain.ID())
}

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.GetEVMChainParams(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
}
}
params := chain.Params()

evmObserver.SetChainParams(*params)

if strings.EqualFold(tx.To, params.ConnectorContractAddress) {
coinType = coin.CoinType_Zeta
} else if strings.EqualFold(tx.To, params.Erc20CustodyContractAddress) {
coinType = coin.CoinType_ERC20
} else if strings.EqualFold(tx.To, tssEthAddress) {
coinType = coin.CoinType_Gas
}

switch coinType {
Expand All @@ -170,10 +169,10 @@ func debugCmd(_ *cobra.Command, args []string) error {
fmt.Println("CoinType not detected")
}
fmt.Println("CoinType : ", coinType)
} else if chains.IsBitcoinChain(chain.ChainId, appContext.GetAdditionalChains()) {
} else if chain.IsUTXO() {
btcObserver := btcobserver.Observer{}
btcObserver.WithZetacoreClient(client)
btcObserver.WithChain(chain)
btcObserver.WithChain(*chainProto)
connCfg := &rpcclient.ConnConfig{
Host: cfg.BitcoinConfig.RPCHost,
User: cfg.BitcoinConfig.RPCUsername,
Expand Down
33 changes: 21 additions & 12 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra"

"github.com/zeta-chain/zetacore/pkg/authz"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/constant"
observerTypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient/chains/base"
Expand Down Expand Up @@ -143,11 +142,11 @@ func start(_ *cobra.Command, _ []string) error {
startLogger.Debug().Msgf("CreateAuthzSigner is ready")

// Initialize core parameters from zetacore
err = zetacoreClient.UpdateAppContext(ctx, appContext, true, startLogger)
if err != nil {
if err = zetacoreClient.UpdateAppContext(ctx, appContext, startLogger); err != nil {
startLogger.Error().Err(err).Msg("Error getting core parameters")
return err
}

startLogger.Info().Msgf("Config is updated from zetacore %s", maskCfg(cfg))

go zetacoreClient.UpdateAppContextWorker(ctx, appContext)
Expand Down Expand Up @@ -214,16 +213,21 @@ func start(_ *cobra.Command, _ []string) error {
return err
}

bitcoinChainID := chains.BitcoinRegtest.ChainId
btcChain, _, btcEnabled := appContext.GetBTCChainAndConfig()
if btcEnabled {
bitcoinChainID = btcChain.ChainId
btcChains := appContext.FilterChains(zctx.Chain.IsUTXO)
switch {
case len(btcChains) == 0:
return errors.New("no BTC chains found")
case len(btcChains) > 1:
// In the future we might support multiple UTXO chains;
// right now we only support BTC. Let's make sure there are no surprises.
return errors.New("more than one BTC chain found")
}

tss, err := mc.NewTSS(
ctx,
zetacoreClient,
tssHistoricalList,
bitcoinChainID,
btcChains[0].ID(),
hotkeyPass,
server,
)
Expand Down Expand Up @@ -263,11 +267,16 @@ func start(_ *cobra.Command, _ []string) error {
tss.CurrentPubkey = currentTss.TssPubkey
if tss.EVMAddress() == (ethcommon.Address{}) || tss.BTCAddress() == "" {
startLogger.Error().Msg("TSS address is not set in zetacore")
} else {
startLogger.Info().
Str("tss.eth", tss.EVMAddress().String()).
Str("tss.btc", tss.BTCAddress()).
Str("tss.pub_key", tss.CurrentPubkey).
Msg("Current TSS")
}
startLogger.Info().
Msgf("Current TSS address \n ETH : %s \n BTC : %s \n PubKey : %s ", tss.EVMAddress(), tss.BTCAddress(), tss.CurrentPubkey)
if len(appContext.GetEnabledChains()) == 0 {
startLogger.Error().Msgf("No chains enabled in updated config %s ", cfg.String())

if len(appContext.ListChainIDs()) == 0 {
startLogger.Error().Interface("config", cfg).Msgf("No chains in updated config")
}

isObserver, err := isObserverNode(ctx, zetacoreClient)
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetae2e/local/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func adminTestRoutine(

// depositing the necessary tokens on ZetaChain
txZetaDeposit := adminRunner.DepositZeta()
txEtherDeposit := adminRunner.DepositEther(false)
txEtherDeposit := adminRunner.DepositEther()
txERC20Deposit := adminRunner.DepositERC20()
adminRunner.WaitForMinedCCTX(txZetaDeposit)
adminRunner.WaitForMinedCCTX(txEtherDeposit)
Expand Down
5 changes: 2 additions & 3 deletions cmd/zetae2e/local/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func bitcoinTestRoutine(
deployerRunner *runner.E2ERunner,
verbose bool,
initBitcoinNetwork bool,
testHeader bool,
testNames ...string,
) func() error {
return func() (err error) {
Expand All @@ -42,14 +41,14 @@ func bitcoinTestRoutine(
bitcoinRunner.WaitForTxReceiptOnEvm(txERC20Send)

// depositing the necessary tokens on ZetaChain
txEtherDeposit := bitcoinRunner.DepositEther(false)
txEtherDeposit := bitcoinRunner.DepositEther()
txERC20Deposit := bitcoinRunner.DepositERC20()

bitcoinRunner.WaitForMinedCCTX(txEtherDeposit)
bitcoinRunner.WaitForMinedCCTX(txERC20Deposit)

bitcoinRunner.SetupBitcoinAccount(initBitcoinNetwork)
bitcoinRunner.DepositBTC(testHeader)
bitcoinRunner.DepositBTC()

// run bitcoin test
// Note: due to the extensive block generation in Bitcoin localnet, block header test is run first
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetae2e/local/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func erc20TestRoutine(
erc20Runner.WaitForTxReceiptOnEvm(txERC20Send)

// depositing the necessary tokens on ZetaChain
txEtherDeposit := erc20Runner.DepositEther(false)
txEtherDeposit := erc20Runner.DepositEther()
txERC20Deposit := erc20Runner.DepositERC20()
erc20Runner.WaitForMinedCCTX(txEtherDeposit)
erc20Runner.WaitForMinedCCTX(txERC20Deposit)
Expand Down
3 changes: 1 addition & 2 deletions cmd/zetae2e/local/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func ethereumTestRoutine(
conf config.Config,
deployerRunner *runner.E2ERunner,
verbose bool,
testHeader bool,
testNames ...string,
) func() error {
return func() (err error) {
Expand All @@ -36,7 +35,7 @@ func ethereumTestRoutine(
startTime := time.Now()

// depositing the necessary tokens on ZetaChain
txEtherDeposit := ethereumRunner.DepositEther(testHeader)
txEtherDeposit := ethereumRunner.DepositEther()
ethereumRunner.WaitForMinedCCTX(txEtherDeposit)

// run ethereum test
Expand Down
7 changes: 2 additions & 5 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,11 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
ethereumTests = append(ethereumTests, ethereumAdvancedTests...)
}

// skip the header proof test if we run light test or skipHeaderProof is enabled
testHeader := !light && !skipHeaderProof

eg.Go(erc20TestRoutine(conf, deployerRunner, verbose, erc20Tests...))
eg.Go(zetaTestRoutine(conf, deployerRunner, verbose, zetaTests...))
eg.Go(zevmMPTestRoutine(conf, deployerRunner, verbose, zevmMPTests...))
eg.Go(bitcoinTestRoutine(conf, deployerRunner, verbose, !skipBitcoinSetup, testHeader, bitcoinTests...))
eg.Go(ethereumTestRoutine(conf, deployerRunner, verbose, testHeader, ethereumTests...))
eg.Go(bitcoinTestRoutine(conf, deployerRunner, verbose, !skipBitcoinSetup, bitcoinTests...))
eg.Go(ethereumTestRoutine(conf, deployerRunner, verbose, ethereumTests...))
}

if testAdmin {
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetae2e/local/performance.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func ethereumWithdrawPerformanceRoutine(
startTime := time.Now()

// depositing the necessary tokens on ZetaChain
txEtherDeposit := r.DepositEther(false)
txEtherDeposit := r.DepositEther()
r.WaitForMinedCCTX(txEtherDeposit)

tests, err := r.GetE2ETestsToRunByName(
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetae2e/local/zeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func zetaTestRoutine(

// depositing the necessary tokens on ZetaChain
txZetaDeposit := zetaRunner.DepositZeta()
txEtherDeposit := zetaRunner.DepositEther(false)
txEtherDeposit := zetaRunner.DepositEther()
zetaRunner.WaitForMinedCCTX(txZetaDeposit)
zetaRunner.WaitForMinedCCTX(txEtherDeposit)

Expand Down
2 changes: 1 addition & 1 deletion cmd/zetae2e/local/zevm_mp.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func zevmMPTestRoutine(

// depositing the necessary tokens on ZetaChain
txZetaDeposit := zevmMPRunner.DepositZeta()
txEtherDeposit := zevmMPRunner.DepositEther(false)
txEtherDeposit := zevmMPRunner.DepositEther()
zevmMPRunner.WaitForMinedCCTX(txZetaDeposit)
zevmMPRunner.WaitForMinedCCTX(txEtherDeposit)

Expand Down
2 changes: 1 addition & 1 deletion cmd/zetae2e/stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func StressTest(cmd *cobra.Command, _ []string) {
e2eTest.SetZEVMContracts()

// deposit on ZetaChain
e2eTest.DepositEther(false)
e2eTest.DepositEther()
e2eTest.DepositZeta()
case "TESTNET":
ethZRC20Addr := must(e2eTest.SystemContract.GasCoinZRC20ByChainId(&bind.CallOpts{}, big.NewInt(5)))
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_eth_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestEtherDeposit(r *runner.E2ERunner, args []string) {
amount, ok := big.NewInt(0).SetString(args[0], 10)
require.True(r, ok, "Invalid amount specified for TestEtherDeposit.")

hash := r.DepositEtherWithAmount(false, amount) // in wei
hash := r.DepositEtherWithAmount(amount) // in wei
// wait for the cctx to be mined
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, hash.Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
r.Logger.CCTX(*cctx, "deposit")
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_migrate_chain_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestMigrateChainSupport(r *runner.E2ERunner, _ []string) {
// deposit Ethers and ERC20 on ZetaChain
etherAmount := big.NewInt(1e18)
etherAmount = etherAmount.Mul(etherAmount, big.NewInt(10))
txEtherDeposit := newRunner.DepositEtherWithAmount(false, etherAmount)
txEtherDeposit := newRunner.DepositEtherWithAmount(etherAmount)
newRunner.WaitForMinedCCTX(txEtherDeposit)

// perform withdrawals on the new chain
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_stress_eth_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestStressEtherDeposit(r *runner.E2ERunner, args []string) {
// send the deposits
for i := 0; i < numDeposits; i++ {
i := i
hash := r.DepositEtherWithAmount(false, depositAmount)
hash := r.DepositEtherWithAmount(depositAmount)
r.Logger.Print("index %d: starting deposit, tx hash: %s", i, hash.Hex())

eg.Go(func() error { return monitorEtherDeposit(r, hash, i, time.Now()) })
Expand Down
Loading

0 comments on commit b4251e1

Please sign in to comment.