Skip to content

Commit 2d1baa7

Browse files
swift1337gartnera
authored andcommitted
refactor(zetaclient)!: improve AppContext (#2568)
* 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 tmp
1 parent 4e3d480 commit 2d1baa7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1632
-2474
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [2524](https://github.com/zeta-chain/node/pull/2524) - add inscription envolop parsing
66
* [2533](https://github.com/zeta-chain/node/pull/2533) - parse memo from both OP_RETURN and inscription
7+
* [2568](https://github.com/zeta-chain/node/pull/2568) - improve AppContext by converging chains, chainParams, enabledChains, and additionalChains into a single zctx.Chain
78

89

910
## v19.0.1

cmd/zetaclientd/debug.go

+46-47
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"strconv"
78
"strings"
89

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

17-
"github.com/zeta-chain/zetacore/pkg/chains"
1819
"github.com/zeta-chain/zetacore/pkg/coin"
1920
"github.com/zeta-chain/zetacore/testutil/sample"
20-
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
2121
btcobserver "github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin/observer"
2222
evmobserver "github.com/zeta-chain/zetacore/zetaclient/chains/evm/observer"
2323
"github.com/zeta-chain/zetacore/zetaclient/config"
@@ -35,11 +35,14 @@ type debugArguments struct {
3535
}
3636

3737
func init() {
38-
RootCmd.AddCommand(DebugCmd())
39-
DebugCmd().Flags().
40-
StringVar(&debugArgs.zetaCoreHome, "core-home", "/Users/tanmay/.zetacored", "peer address, e.g. /dns/tss1/tcp/6668/ipfs/16Uiu2HAmACG5DtqmQsHtXg4G2sLS65ttv84e7MrL4kapkjfmhxAp")
41-
DebugCmd().Flags().StringVar(&debugArgs.zetaNode, "node", "46.4.15.110", "public ip address")
42-
DebugCmd().Flags().StringVar(&debugArgs.zetaChainID, "chain-id", "athens_7001-1", "pre-params file path")
38+
defaultHomeDir := os.ExpandEnv("$HOME/.zetacored")
39+
40+
cmd := DebugCmd()
41+
cmd.Flags().StringVar(&debugArgs.zetaCoreHome, "core-home", defaultHomeDir, "zetacore home directory")
42+
cmd.Flags().StringVar(&debugArgs.zetaNode, "node", "46.4.15.110", "public ip address")
43+
cmd.Flags().StringVar(&debugArgs.zetaChainID, "chain-id", "athens_7001-1", "pre-params file path")
44+
45+
RootCmd.AddCommand(cmd)
4346
}
4447

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

60-
appContext := zctx.New(cfg, zerolog.Nop())
61-
ctx := zctx.WithAppContext(context.Background(), appContext)
63+
inboundHash := args[0]
6264

6365
chainID, err := strconv.ParseInt(args[1], 10, 64)
6466
if err != nil {
65-
return err
67+
return errors.Wrap(err, "failed to parse chain id")
6668
}
6769

68-
inboundHash := args[0]
69-
var ballotIdentifier string
70-
7170
// create a new zetacore client
7271
client, err := zetacore.NewClient(
7372
&keys.Keys{OperatorAddress: sdk.MustAccAddressFromBech32(sample.AccAddress())},
@@ -80,21 +79,30 @@ func debugCmd(_ *cobra.Command, args []string) error {
8079
if err != nil {
8180
return err
8281
}
83-
chainParams, err := client.GetChainParams(ctx)
84-
if err != nil {
85-
return err
82+
83+
appContext := zctx.New(cfg, zerolog.Nop())
84+
ctx := zctx.WithAppContext(context.Background(), appContext)
85+
86+
if err := client.UpdateAppContext(ctx, appContext, zerolog.Nop()); err != nil {
87+
return errors.Wrap(err, "failed to update app context")
8688
}
89+
90+
var ballotIdentifier string
91+
8792
tssEthAddress, err := client.GetEVMTSSAddress(ctx)
8893
if err != nil {
8994
return err
9095
}
91-
chain, found := chains.GetChainFromChainID(chainID, appContext.GetAdditionalChains())
92-
if !found {
93-
return fmt.Errorf("invalid chain id")
96+
97+
chain, err := appContext.GetChain(chainID)
98+
if err != nil {
99+
return err
94100
}
95101

102+
chainProto := chain.RawChain()
103+
96104
// get ballot identifier according to the chain type
97-
if chains.IsEVMChain(chain.ChainId, appContext.GetAdditionalChains()) {
105+
if chain.IsEVM() {
98106
evmObserver := evmobserver.Observer{}
99107
evmObserver.WithZetacoreClient(client)
100108
var ethRPC *ethrpc.EthRPC
@@ -109,43 +117,34 @@ func debugCmd(_ *cobra.Command, args []string) error {
109117
}
110118
evmObserver.WithEvmClient(client)
111119
evmObserver.WithEvmJSONRPC(ethRPC)
112-
evmObserver.WithChain(chain)
120+
evmObserver.WithChain(*chainProto)
113121
}
114122
}
115123
hash := ethcommon.HexToHash(inboundHash)
116124
tx, isPending, err := evmObserver.TransactionByHash(inboundHash)
117125
if err != nil {
118-
return fmt.Errorf("tx not found on chain %s , %d", err.Error(), chain.ChainId)
126+
return fmt.Errorf("tx not found on chain %s, %d", err.Error(), chain.ID())
119127
}
128+
120129
if isPending {
121130
return fmt.Errorf("tx is still pending")
122131
}
132+
123133
receipt, err := client.TransactionReceipt(context.Background(), hash)
124134
if err != nil {
125-
return fmt.Errorf("tx receipt not found on chain %s, %d", err.Error(), chain.ChainId)
135+
return fmt.Errorf("tx receipt not found on chain %s, %d", err.Error(), chain.ID())
126136
}
127137

128-
for _, chainParams := range chainParams {
129-
if chainParams.ChainId == chainID {
130-
evmObserver.SetChainParams(observertypes.ChainParams{
131-
ChainId: chainID,
132-
ConnectorContractAddress: chainParams.ConnectorContractAddress,
133-
ZetaTokenContractAddress: chainParams.ZetaTokenContractAddress,
134-
Erc20CustodyContractAddress: chainParams.Erc20CustodyContractAddress,
135-
})
136-
evmChainParams, found := appContext.GetEVMChainParams(chainID)
137-
if !found {
138-
return fmt.Errorf("missing chain params for chain %d", chainID)
139-
}
140-
evmChainParams.ZetaTokenContractAddress = chainParams.ZetaTokenContractAddress
141-
if strings.EqualFold(tx.To, chainParams.ConnectorContractAddress) {
142-
coinType = coin.CoinType_Zeta
143-
} else if strings.EqualFold(tx.To, chainParams.Erc20CustodyContractAddress) {
144-
coinType = coin.CoinType_ERC20
145-
} else if strings.EqualFold(tx.To, tssEthAddress) {
146-
coinType = coin.CoinType_Gas
147-
}
148-
}
138+
params := chain.Params()
139+
140+
evmObserver.SetChainParams(*params)
141+
142+
if strings.EqualFold(tx.To, params.ConnectorContractAddress) {
143+
coinType = coin.CoinType_Zeta
144+
} else if strings.EqualFold(tx.To, params.Erc20CustodyContractAddress) {
145+
coinType = coin.CoinType_ERC20
146+
} else if strings.EqualFold(tx.To, tssEthAddress) {
147+
coinType = coin.CoinType_Gas
149148
}
150149

151150
switch coinType {
@@ -170,10 +169,10 @@ func debugCmd(_ *cobra.Command, args []string) error {
170169
fmt.Println("CoinType not detected")
171170
}
172171
fmt.Println("CoinType : ", coinType)
173-
} else if chains.IsBitcoinChain(chain.ChainId, appContext.GetAdditionalChains()) {
172+
} else if chain.IsUTXO() {
174173
btcObserver := btcobserver.Observer{}
175174
btcObserver.WithZetacoreClient(client)
176-
btcObserver.WithChain(chain)
175+
btcObserver.WithChain(*chainProto)
177176
connCfg := &rpcclient.ConnConfig{
178177
Host: cfg.BitcoinConfig.RPCHost,
179178
User: cfg.BitcoinConfig.RPCUsername,

cmd/zetaclientd/start.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/spf13/cobra"
2222

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

145144
// Initialize core parameters from zetacore
146-
err = zetacoreClient.UpdateAppContext(ctx, appContext, true, startLogger)
147-
if err != nil {
145+
if err = zetacoreClient.UpdateAppContext(ctx, appContext, startLogger); err != nil {
148146
startLogger.Error().Err(err).Msg("Error getting core parameters")
149147
return err
150148
}
149+
151150
startLogger.Info().Msgf("Config is updated from zetacore %s", maskCfg(cfg))
152151

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

217-
bitcoinChainID := chains.BitcoinRegtest.ChainId
218-
btcChain, _, btcEnabled := appContext.GetBTCChainAndConfig()
219-
if btcEnabled {
220-
bitcoinChainID = btcChain.ChainId
216+
btcChains := appContext.FilterChains(zctx.Chain.IsUTXO)
217+
switch {
218+
case len(btcChains) == 0:
219+
return errors.New("no BTC chains found")
220+
case len(btcChains) > 1:
221+
// In the future we might support multiple UTXO chains;
222+
// right now we only support BTC. Let's make sure there are no surprises.
223+
return errors.New("more than one BTC chain found")
221224
}
225+
222226
tss, err := mc.NewTSS(
223227
ctx,
224228
zetacoreClient,
225229
tssHistoricalList,
226-
bitcoinChainID,
230+
btcChains[0].ID(),
227231
hotkeyPass,
228232
server,
229233
)
@@ -263,11 +267,16 @@ func start(_ *cobra.Command, _ []string) error {
263267
tss.CurrentPubkey = currentTss.TssPubkey
264268
if tss.EVMAddress() == (ethcommon.Address{}) || tss.BTCAddress() == "" {
265269
startLogger.Error().Msg("TSS address is not set in zetacore")
270+
} else {
271+
startLogger.Info().
272+
Str("tss.eth", tss.EVMAddress().String()).
273+
Str("tss.btc", tss.BTCAddress()).
274+
Str("tss.pub_key", tss.CurrentPubkey).
275+
Msg("Current TSS")
266276
}
267-
startLogger.Info().
268-
Msgf("Current TSS address \n ETH : %s \n BTC : %s \n PubKey : %s ", tss.EVMAddress(), tss.BTCAddress(), tss.CurrentPubkey)
269-
if len(appContext.GetEnabledChains()) == 0 {
270-
startLogger.Error().Msgf("No chains enabled in updated config %s ", cfg.String())
277+
278+
if len(appContext.ListChainIDs()) == 0 {
279+
startLogger.Error().Interface("config", cfg).Msgf("No chains in updated config")
271280
}
272281

273282
isObserver, err := isObserverNode(ctx, zetacoreClient)

cmd/zetae2e/local/admin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func adminTestRoutine(
4545

4646
// depositing the necessary tokens on ZetaChain
4747
txZetaDeposit := adminRunner.DepositZeta()
48-
txEtherDeposit := adminRunner.DepositEther(false)
48+
txEtherDeposit := adminRunner.DepositEther()
4949
txERC20Deposit := adminRunner.DepositERC20()
5050
adminRunner.WaitForMinedCCTX(txZetaDeposit)
5151
adminRunner.WaitForMinedCCTX(txEtherDeposit)

cmd/zetae2e/local/bitcoin.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func bitcoinTestRoutine(
1717
deployerRunner *runner.E2ERunner,
1818
verbose bool,
1919
initBitcoinNetwork bool,
20-
testHeader bool,
2120
testNames ...string,
2221
) func() error {
2322
return func() (err error) {
@@ -42,14 +41,14 @@ func bitcoinTestRoutine(
4241
bitcoinRunner.WaitForTxReceiptOnEvm(txERC20Send)
4342

4443
// depositing the necessary tokens on ZetaChain
45-
txEtherDeposit := bitcoinRunner.DepositEther(false)
44+
txEtherDeposit := bitcoinRunner.DepositEther()
4645
txERC20Deposit := bitcoinRunner.DepositERC20()
4746

4847
bitcoinRunner.WaitForMinedCCTX(txEtherDeposit)
4948
bitcoinRunner.WaitForMinedCCTX(txERC20Deposit)
5049

5150
bitcoinRunner.SetupBitcoinAccount(initBitcoinNetwork)
52-
bitcoinRunner.DepositBTC(testHeader)
51+
bitcoinRunner.DepositBTC()
5352

5453
// run bitcoin test
5554
// Note: due to the extensive block generation in Bitcoin localnet, block header test is run first

cmd/zetae2e/local/erc20.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func erc20TestRoutine(
4141
erc20Runner.WaitForTxReceiptOnEvm(txERC20Send)
4242

4343
// depositing the necessary tokens on ZetaChain
44-
txEtherDeposit := erc20Runner.DepositEther(false)
44+
txEtherDeposit := erc20Runner.DepositEther()
4545
txERC20Deposit := erc20Runner.DepositERC20()
4646
erc20Runner.WaitForMinedCCTX(txEtherDeposit)
4747
erc20Runner.WaitForMinedCCTX(txERC20Deposit)

cmd/zetae2e/local/ethereum.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ func ethereumTestRoutine(
1616
conf config.Config,
1717
deployerRunner *runner.E2ERunner,
1818
verbose bool,
19-
testHeader bool,
2019
testNames ...string,
2120
) func() error {
2221
return func() (err error) {
@@ -36,7 +35,7 @@ func ethereumTestRoutine(
3635
startTime := time.Now()
3736

3837
// depositing the necessary tokens on ZetaChain
39-
txEtherDeposit := ethereumRunner.DepositEther(testHeader)
38+
txEtherDeposit := ethereumRunner.DepositEther()
4039
ethereumRunner.WaitForMinedCCTX(txEtherDeposit)
4140

4241
// run ethereum test

cmd/zetae2e/local/local.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,11 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
283283
ethereumTests = append(ethereumTests, ethereumAdvancedTests...)
284284
}
285285

286-
// skip the header proof test if we run light test or skipHeaderProof is enabled
287-
testHeader := !light && !skipHeaderProof
288-
289286
eg.Go(erc20TestRoutine(conf, deployerRunner, verbose, erc20Tests...))
290287
eg.Go(zetaTestRoutine(conf, deployerRunner, verbose, zetaTests...))
291288
eg.Go(zevmMPTestRoutine(conf, deployerRunner, verbose, zevmMPTests...))
292-
eg.Go(bitcoinTestRoutine(conf, deployerRunner, verbose, !skipBitcoinSetup, testHeader, bitcoinTests...))
293-
eg.Go(ethereumTestRoutine(conf, deployerRunner, verbose, testHeader, ethereumTests...))
289+
eg.Go(bitcoinTestRoutine(conf, deployerRunner, verbose, !skipBitcoinSetup, bitcoinTests...))
290+
eg.Go(ethereumTestRoutine(conf, deployerRunner, verbose, ethereumTests...))
294291
}
295292

296293
if testAdmin {

cmd/zetae2e/local/performance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func ethereumWithdrawPerformanceRoutine(
8686
startTime := time.Now()
8787

8888
// depositing the necessary tokens on ZetaChain
89-
txEtherDeposit := r.DepositEther(false)
89+
txEtherDeposit := r.DepositEther()
9090
r.WaitForMinedCCTX(txEtherDeposit)
9191

9292
tests, err := r.GetE2ETestsToRunByName(

cmd/zetae2e/local/zeta.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func zetaTestRoutine(
4141

4242
// depositing the necessary tokens on ZetaChain
4343
txZetaDeposit := zetaRunner.DepositZeta()
44-
txEtherDeposit := zetaRunner.DepositEther(false)
44+
txEtherDeposit := zetaRunner.DepositEther()
4545
zetaRunner.WaitForMinedCCTX(txZetaDeposit)
4646
zetaRunner.WaitForMinedCCTX(txEtherDeposit)
4747

cmd/zetae2e/local/zevm_mp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func zevmMPTestRoutine(
4141

4242
// depositing the necessary tokens on ZetaChain
4343
txZetaDeposit := zevmMPRunner.DepositZeta()
44-
txEtherDeposit := zevmMPRunner.DepositEther(false)
44+
txEtherDeposit := zevmMPRunner.DepositEther()
4545
zevmMPRunner.WaitForMinedCCTX(txZetaDeposit)
4646
zevmMPRunner.WaitForMinedCCTX(txEtherDeposit)
4747

cmd/zetae2e/stress.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func StressTest(cmd *cobra.Command, _ []string) {
144144
e2eTest.SetZEVMContracts()
145145

146146
// deposit on ZetaChain
147-
e2eTest.DepositEther(false)
147+
e2eTest.DepositEther()
148148
e2eTest.DepositZeta()
149149
case "TESTNET":
150150
ethZRC20Addr := must(e2eTest.SystemContract.GasCoinZRC20ByChainId(&bind.CallOpts{}, big.NewInt(5)))

e2e/e2etests/test_eth_deposit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestEtherDeposit(r *runner.E2ERunner, args []string) {
1616
amount, ok := big.NewInt(0).SetString(args[0], 10)
1717
require.True(r, ok, "Invalid amount specified for TestEtherDeposit.")
1818

19-
hash := r.DepositEtherWithAmount(false, amount) // in wei
19+
hash := r.DepositEtherWithAmount(amount) // in wei
2020
// wait for the cctx to be mined
2121
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, hash.Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
2222
r.Logger.CCTX(*cctx, "deposit")

e2e/e2etests/test_migrate_chain_support.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestMigrateChainSupport(r *runner.E2ERunner, _ []string) {
136136
// deposit Ethers and ERC20 on ZetaChain
137137
etherAmount := big.NewInt(1e18)
138138
etherAmount = etherAmount.Mul(etherAmount, big.NewInt(10))
139-
txEtherDeposit := newRunner.DepositEtherWithAmount(false, etherAmount)
139+
txEtherDeposit := newRunner.DepositEtherWithAmount(etherAmount)
140140
newRunner.WaitForMinedCCTX(txEtherDeposit)
141141

142142
// perform withdrawals on the new chain

e2e/e2etests/test_stress_eth_deposit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestStressEtherDeposit(r *runner.E2ERunner, args []string) {
3131
// send the deposits
3232
for i := 0; i < numDeposits; i++ {
3333
i := i
34-
hash := r.DepositEtherWithAmount(false, depositAmount)
34+
hash := r.DepositEtherWithAmount(depositAmount)
3535
r.Logger.Print("index %d: starting deposit, tx hash: %s", i, hash.Hex())
3636

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

0 commit comments

Comments
 (0)