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(zetaclient)!: improve AppContext #2568

Merged
merged 28 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
daf4b6c
Implement chain registry
swift1337 Jul 25, 2024
f24d05a
Rewrite test-cases for AppContext
swift1337 Jul 26, 2024
0474abd
Drop `supplychecker`
swift1337 Jul 26, 2024
3d6e05f
Refactor app ctx Update worker
swift1337 Jul 29, 2024
fc39f4e
Refactor orchestrator
swift1337 Jul 29, 2024
782cef3
Refactor observer&signer; DROP postBlockHeaders
swift1337 Jul 29, 2024
031e7e0
Fix test cases [1]
swift1337 Jul 29, 2024
09478d6
Update changelog
swift1337 Jul 29, 2024
2d6e312
Allow Zeta Chain in appContext; address PR comments [1]
swift1337 Jul 30, 2024
487d7e1
Fix app context update
swift1337 Jul 30, 2024
5c63646
Check for `chain.IsZeta()`
swift1337 Jul 30, 2024
f2e3fac
Add AppContext.FilterChains
swift1337 Jul 30, 2024
e6fe5d7
Fix test cases [2]
swift1337 Jul 30, 2024
ca9826e
Fix test cases [3]
swift1337 Jul 30, 2024
ef8a500
Merge branch 'develop' into refactor/chain-params
swift1337 Jul 30, 2024
c642109
Address PR comments [1]
swift1337 Jul 30, 2024
38cb3cb
Address PR comments [2]
swift1337 Jul 30, 2024
65a3830
Add tests for `slices`
swift1337 Jul 30, 2024
ce22258
Merge branch 'develop' into refactor/chain-params
swift1337 Jul 30, 2024
e383125
Fix e2e tests [1]
swift1337 Jul 31, 2024
ac0ea1e
Fix e2e tests [2]
swift1337 Jul 31, 2024
c157e0d
Merge branch 'refactor/chain-params' of github.com:zeta-chain/node in…
swift1337 Jul 31, 2024
975c78e
Merge branch 'develop' into refactor/chain-params
swift1337 Aug 2, 2024
05b3d97
Resolve conflicts, converge codebase between PRs
swift1337 Aug 2, 2024
ac9fcf2
Add lodash; remove slices pkg
swift1337 Aug 2, 2024
aa57ba6
Address PR comments
swift1337 Aug 2, 2024
18b445d
Minor logging fix
swift1337 Aug 2, 2024
0f48ce3
Address PR comments
swift1337 Aug 2, 2024
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 @@ -72,6 +72,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
swift1337 marked this conversation as resolved.
Show resolved Hide resolved

### Tests

Expand Down
94 changes: 47 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 @@ -34,12 +34,16 @@ type debugArguments struct {
zetaChainID string
}

var homeDir = os.ExpandEnv("$HOME/.zetacored")
swift1337 marked this conversation as resolved.
Show resolved Hide resolved

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")
cmd := DebugCmd()

cmd.Flags().StringVar(&debugArgs.zetaCoreHome, "core-home", homeDir, "zetecore 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 +58,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")
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
}

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 +80,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())
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
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 +118,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 +170,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() {
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
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
17 changes: 8 additions & 9 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,16 @@ 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)
if len(btcChains) == 0 {
return errors.New("no BTC chains found")
}

tss, err := mc.NewTSS(
ctx,
zetacoreClient,
tssHistoricalList,
bitcoinChainID,
btcChains[0].ID(),
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
hotkeyPass,
server,
)
Expand Down Expand Up @@ -266,7 +265,7 @@ func start(_ *cobra.Command, _ []string) error {
}
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 {
if len(appContext.ListChainIDs()) == 0 {
startLogger.Error().Msgf("No chains enabled in updated config %s ", cfg.String())
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
56 changes: 56 additions & 0 deletions pkg/slices/slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package slices
swift1337 marked this conversation as resolved.
Show resolved Hide resolved

import (
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
)

// Map applies a function to each item in a slice and returns a new slice with the results.
func Map[T, V any](items []T, f func(T) V) []V {
result := make([]V, len(items))

for i, item := range items {
result[i] = f(item)
}

return result
}

// ElementsMatch returns true if two slices have the same elements in the same order.
// Note that this function SORTS the slices before comparing them.
func ElementsMatch[T constraints.Ordered](a, b []T) bool {
if len(a) != len(b) {
return false
}

slices.Sort(a)
slices.Sort(b)

for i, v := range a {
if v != b[i] {
return false
}
}

return true
}
swift1337 marked this conversation as resolved.
Show resolved Hide resolved

// Diff returns the elements in `a` that are not in `b`
func Diff[T comparable](a, b []T) []T {
var (
cache = map[T]struct{}{}
result []T
)

for _, v := range b {
cache[v] = struct{}{}
}

for _, v := range a {
if _, ok := cache[v]; !ok {
result = append(result, v)
}
}

return result
}
17 changes: 2 additions & 15 deletions x/observer/types/chain_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ethchains "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"

"github.com/zeta-chain/zetacore/pkg/chains"
solanacontract "github.com/zeta-chain/zetacore/pkg/contract/solana"
Expand Down Expand Up @@ -57,19 +56,8 @@ func ValidateChainParams(params *ChainParams) error {
return fmt.Errorf("chain params cannot be nil")
}

// TODO: ZetaChain chain params should be completely removed
// Once removed, this check is no longer necessary as all chasin params would need the same checks
// https://github.com/zeta-chain/node/issues/2419
_, err := chains.ZetaChainFromChainID(params.ChainId)
if err == nil {
// zeta chain skips the rest of the checks for now
return nil
}

// ignore error from ZetaChainFromChainID if reason is chain is not zeta chain
// return error otherwise
if !errors.Is(err, chains.ErrNotZetaChain) {
return err
if chains.IsZetaChain(params.ChainId, nil) {
return fmt.Errorf("zeta chain cannot have observer chain parameters")
}

if params.ConfirmationCount == 0 {
Expand Down Expand Up @@ -164,7 +152,6 @@ func GetDefaultChainParams() ChainParamsList {
GetDefaultBtcTestnetChainParams(),
GetDefaultBtcRegtestChainParams(),
GetDefaultGoerliLocalnetChainParams(),
GetDefaultZetaPrivnetChainParams(),
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
},
}
}
Expand Down
17 changes: 2 additions & 15 deletions zetaclient/chains/bitcoin/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (ob *Observer) WatchInbound(ctx context.Context) error {
for {
select {
case <-ticker.C():
if !app.IsInboundObservationEnabled(ob.GetChainParams()) {
if !app.IsInboundObservationEnabled() {
sampledLogger.Info().
Msgf("WatchInbound: inbound observation is disabled for chain %d", ob.Chain().ChainId)
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
continue
Expand All @@ -69,11 +69,6 @@ func (ob *Observer) WatchInbound(ctx context.Context) error {
// ObserveInbound observes the Bitcoin chain for inbounds and post votes to zetacore
// TODO(revamp): simplify this function into smaller functions
func (ob *Observer) ObserveInbound(ctx context.Context) error {
app, err := zctx.FromContext(ctx)
if err != nil {
return err
}

zetaCoreClient := ob.ZetacoreClient()

// get and update latest block height
Expand Down Expand Up @@ -123,14 +118,6 @@ func (ob *Observer) ObserveInbound(ctx context.Context) error {
// https://github.com/zeta-chain/node/issues/1847
// TODO: move this logic in its own routine
// https://github.com/zeta-chain/node/issues/2204
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
blockHeaderVerification, found := app.GetBlockHeaderEnabledChains(ob.Chain().ChainId)
if found && blockHeaderVerification.Enabled {
// #nosec G115 always in range
err = ob.postBlockHeader(ctx, int64(blockNumber))
if err != nil {
ob.logger.Inbound.Warn().Err(err).Msgf("observeInboundBTC: error posting block header %d", blockNumber)
}
}
if len(res.Block.Tx) > 1 {
// get depositor fee
depositorFee := bitcoin.CalcDepositorFee(res.Block, ob.Chain().ChainId, ob.netParams, ob.logger.Inbound)
Expand Down Expand Up @@ -206,7 +193,7 @@ func (ob *Observer) WatchInboundTracker(ctx context.Context) error {
for {
select {
case <-ticker.C():
if !app.IsInboundObservationEnabled(ob.GetChainParams()) {
if !app.IsInboundObservationEnabled() {
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
continue
}
err := ob.ProcessInboundTrackers(ctx)
Expand Down
Loading
Loading