Skip to content

Commit

Permalink
feat: inbound tx tracker (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD authored Oct 19, 2023
1 parent 9c0eed2 commit 161406e
Show file tree
Hide file tree
Showing 69 changed files with 5,364 additions and 1,049 deletions.
3 changes: 2 additions & 1 deletion app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
)

const releaseVersion = "v10.1.0"
Expand All @@ -16,7 +17,7 @@ func SetupHandlers(app *App) {
for m, mb := range app.mm.Modules {
vm[m] = mb.ConsensusVersion()
}

vm[observertypes.ModuleName] = vm[observertypes.ModuleName] - 1
return app.mm.RunMigrations(ctx, app.configurator, vm)
})

Expand Down
189 changes: 189 additions & 0 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package main

import (
"context"
"fmt"
"io"
"strconv"
"strings"
"sync"

"github.com/btcsuite/btcd/rpcclient"
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/testutil/sample"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient"
"github.com/zeta-chain/zetacore/zetaclient/config"
)

var debugArgs = debugArguments{}

type debugArguments struct {
zetaCoreHome string
zetaNode string
zetaChainID string
}

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")
}

func DebugCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-ballot-from-intx [txHash] [chainID]",
Short: "provide txHash and chainID to get the ballot status for the txHash",
RunE: func(cmd *cobra.Command, args []string) error {
cobra.ExactArgs(2)
cfg, err := config.Load(debugArgs.zetaCoreHome)
if err != nil {
return err
}
chainID, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}
txHash := args[0]
var ballotIdentifier string
chainLogger := zerolog.New(io.Discard).Level(zerolog.Disabled)
bridge, err := zetaclient.NewZetaCoreBridge(&zetaclient.Keys{OperatorAddress: sdk.MustAccAddressFromBech32(sample.AccAddress())}, debugArgs.zetaNode, "", debugArgs.zetaChainID)
if err != nil {
return err
}
coreParams, err := bridge.GetCoreParams()
if err != nil {
return err
}
tssEthAddress, err := bridge.GetEthTssAddress()
if err != nil {
return err
}

chain := common.GetChainFromChainID(chainID)
if chain == nil {
return fmt.Errorf("invalid chain id")
}

if common.IsEVMChain(chain.ChainId) {

ob := zetaclient.EVMChainClient{
Mu: &sync.Mutex{},
}
ob.WithZetaClient(bridge)
ob.WithLogger(chainLogger)
client := &ethclient.Client{}
coinType := common.CoinType_Cmd
for chain, evmConfig := range cfg.GetAllEVMConfigs() {
if chainID == chain {
client, err = ethclient.Dial(evmConfig.Endpoint)
if err != nil {
return err
}
ob.WithEvmClient(client)
ob.WithChain(*common.GetChainFromChainID(chainID))
}
}
hash := ethcommon.HexToHash(txHash)
tx, isPending, err := client.TransactionByHash(context.Background(), hash)
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")
}

for _, chainCoreParams := range coreParams {
if chainCoreParams.ChainId == chainID {
ob.WithParams(observertypes.CoreParams{
ChainId: chainID,
ConnectorContractAddress: chainCoreParams.ConnectorContractAddress,
ZetaTokenContractAddress: chainCoreParams.ZetaTokenContractAddress,
Erc20CustodyContractAddress: chainCoreParams.Erc20CustodyContractAddress,
})
cfg.EVMChainConfigs[chainID].ZetaTokenContractAddress = chainCoreParams.ZetaTokenContractAddress
ob.SetConfig(cfg)
if strings.EqualFold(tx.To().Hex(), chainCoreParams.ConnectorContractAddress) {
coinType = common.CoinType_Zeta
} else if strings.EqualFold(tx.To().Hex(), chainCoreParams.Erc20CustodyContractAddress) {
coinType = common.CoinType_ERC20
} else if strings.EqualFold(tx.To().Hex(), tssEthAddress) {
coinType = common.CoinType_Gas
}

}
}

switch coinType {
case common.CoinType_Zeta:
ballotIdentifier, err = ob.CheckReceiptForCoinTypeZeta(txHash, false)
if err != nil {
return err
}

case common.CoinType_ERC20:
ballotIdentifier, err = ob.CheckReceiptForCoinTypeERC20(txHash, false)
if err != nil {
return err
}

case common.CoinType_Gas:
ballotIdentifier, err = ob.CheckReceiptForCoinTypeGas(txHash, false)
if err != nil {
return err
}
default:
fmt.Println("CoinType not detected")
}
fmt.Println("CoinType : ", coinType)
} else if common.IsBitcoinChain(chain.ChainId) {
obBtc := zetaclient.BitcoinChainClient{
Mu: &sync.Mutex{},
}
obBtc.WithZetaClient(bridge)
obBtc.WithLogger(chainLogger)
obBtc.WithChain(*common.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
}
obBtc.WithBtcClient(btcClient)
ballotIdentifier, err = obBtc.CheckReceiptForBtcTxHash(txHash, false)
if err != nil {
return err
}

}
fmt.Println("BallotIdentifier : ", ballotIdentifier)

ballot, err := bridge.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 nil
},
}

return cmd
}
5 changes: 5 additions & 0 deletions common/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ func (chain Chain) IsKlaytnChain() bool {
return chain.ChainId == 1001
}

// SupportMerkleProof returns true if the chain supports block header-based verification
func (chain Chain) SupportMerkleProof() bool {
return IsEVMChain(chain.ChainId) || IsBitcoinChain(chain.ChainId)
}

func IsBitcoinChain(chainID int64) bool {
return chainID == 18444 || // regtest
chainID == 18332 || //testnet
Expand Down
17 changes: 15 additions & 2 deletions common/coin.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package common

const (
ZETADenom = "azeta"
import (
"fmt"
"strconv"
)

func GetCoinType(coin string) (CoinType, error) {
coinInt, err := strconv.ParseInt(coin, 10, 32)
if err != nil {
return CoinType_Cmd, err
}
if coinInt < 0 || coinInt > 3 {
return CoinType_Cmd, fmt.Errorf("invalid coin type %d", coinInt)
}
// #nosec G701 always in range
return CoinType(coinInt), nil
}
4 changes: 4 additions & 0 deletions common/default_chains_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func BtcChainID() int64 {
return BtcMainnetChain().ChainId
}

func BtcDustOffset() int64 {
return 0
}

func PolygonChain() Chain {
return Chain{
ChainName: ChainName_polygon_mainnet,
Expand Down
4 changes: 4 additions & 0 deletions common/default_chains_mock_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func BtcChainID() int64 {
return BtcMainnetChain().ChainId
}

func BtcDustOffset() int64 {
return 2000
}

func PolygonChain() Chain {
return Chain{
ChainName: ChainName_polygon_mainnet,
Expand Down
4 changes: 4 additions & 0 deletions common/default_chains_privnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func BtcChainID() int64 {
return BtcRegtestChain().ChainId
}

func BtcDustOffset() int64 {
return 2000
}

func DefaultChainsList() []*Chain {
chains := []Chain{
BtcRegtestChain(),
Expand Down
4 changes: 4 additions & 0 deletions common/default_chains_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func BtcChainID() int64 {
return BtcTestNetChain().ChainId
}

func BtcDustOffset() int64 {
return 2000
}

func MumbaiChain() Chain {
return Chain{
ChainName: ChainName_mumbai_testnet,
Expand Down
Loading

0 comments on commit 161406e

Please sign in to comment.