Skip to content

Commit

Permalink
resolve comments 1
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Oct 9, 2023
1 parent 1269476 commit fc4198e
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 117 deletions.
1 change: 1 addition & 0 deletions common/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (chain Chain) IsKlaytnChain() bool {
return chain.ChainId == 1001
}

// IsProvable List of chains which support block header-based verification on zetchain
func (chain Chain) IsProvable() bool {
return chain.ChainId == 1 ||
chain.ChainId == 5 ||
Expand Down
8 changes: 4 additions & 4 deletions common/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package common

import "strconv"

func GetCoinType(coin string) CoinType {
coinInt, err := strconv.ParseInt(coin, 10, 64)
func GetCoinType(coin string) (CoinType, error) {
coinInt, err := strconv.ParseInt(coin, 10, 32)
if err != nil {
panic(err)
return CoinType_Cmd, err
}
return CoinType(coinInt)
return CoinType(coinInt), nil
}
12 changes: 8 additions & 4 deletions x/crosschain/client/cli/cli_in_tx_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ import (

func CmdAddToInTxTracker() *cobra.Command {
cmd := &cobra.Command{
Use: "add-to-in-tx-tracker [chain-id] [tx-hash] [coin-type]",
Short: "Add a out-tx-tracker \n Use 0:Zeta,1:Gas,2:ERC20",
Args: cobra.ExactArgs(3),
Use: "add-to-in-tx-tracker [chain-id] [tx-hash] [coin-type]",
Short: `Add a out-tx-tracker
Use 0:Zeta,1:Gas,2:ERC20`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argChain, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
}
argTxHash := args[1]
argsCoinType := common.GetCoinType(args[2])
argsCoinType, err := common.GetCoinType(args[2])
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
Expand Down
106 changes: 0 additions & 106 deletions x/crosschain/keeper/in_tx_tracker.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package keeper

import (
"context"
"fmt"

errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
eth "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
observerTypes "github.com/zeta-chain/zetacore/x/observer/types"
)

func getInTrackerKey(chainID int64, txHash string) string {
Expand Down Expand Up @@ -83,103 +77,3 @@ func (k Keeper) GetAllInTxTrackerForChainPaginated(ctx sdk.Context, chainID int6
})
return
}

func (k msgServer) AddToInTxTracker(goCtx context.Context, msg *types.MsgAddToInTxTracker) (*types.MsgAddToInTxTrackerResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
chain := k.zetaObserverKeeper.GetParams(ctx).GetChainFromChainID(msg.ChainId)
if chain == nil {
return nil, observerTypes.ErrSupportedChains
}

adminPolicyAccount := k.zetaObserverKeeper.GetParams(ctx).GetAdminPolicyAccount(observerTypes.Policy_Type_group1)
isAdmin := msg.Creator == adminPolicyAccount

isObserver, err := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, chain)
if err != nil {
ctx.Logger().Error("Error while checking if the account is an observer", err)
}
isProven := false
if msg.Proof != nil {
isProven, err = k.VerifyInTxTrackerProof(ctx, msg.Proof, msg.BlockHash, msg.TxIndex, msg.ChainId, msg.CoinType)
if err != nil {
return nil, err
}
}

// Sender needs to be either the admin policy account or an observer
if !(isAdmin || isObserver || isProven) {
return nil, errorsmod.Wrap(observerTypes.ErrNotAuthorized, fmt.Sprintf("Creator %s", msg.Creator))
}

k.Keeper.SetInTxTracker(ctx, types.InTxTracker{
ChainId: msg.ChainId,
TxHash: msg.TxHash,
CoinType: msg.CoinType,
})
return &types.MsgAddToInTxTrackerResponse{}, nil
}

func (k Keeper) VerifyInTxTrackerProof(ctx sdk.Context, proof *common.Proof, hash string, txIndex int64, chainID int64, coinType common.CoinType) (bool, error) {

senderChain := common.GetChainFromChainID(chainID)
if senderChain == nil {
return false, types.ErrUnsupportedChain
}

if !senderChain.IsProvable() {
return false, types.ErrCannotVerifyProof.Wrapf("chain %d does not support block header verification", chainID)
}

blockHash := eth.HexToHash(hash)
res, found := k.zetaObserverKeeper.GetBlockHeader(ctx, blockHash.Bytes())
if !found {
return false, errorsmod.Wrap(observerTypes.ErrBlockHeaderNotFound, fmt.Sprintf("block header not found %s", blockHash))
}

// verify and process the proof
val, err := proof.Verify(res.Header, int(txIndex))
if err != nil && !common.IsErrorInvalidProof(err) {
return false, err
}
var txx ethtypes.Transaction
err = txx.UnmarshalBinary(val)
if err != nil {
return false, err
}

coreParams, found := k.zetaObserverKeeper.GetCoreParamsByChainID(ctx, senderChain.ChainId)
if !found {
return false, types.ErrUnsupportedChain.Wrapf("core params not found for chain %d", senderChain.ChainId)
}
tssRes, err := k.GetTssAddress(ctx, &types.QueryGetTssAddressRequest{})
if err != nil {
return false, err
}
tssAddr := eth.HexToAddress(tssRes.Eth)
if tssAddr == (eth.Address{}) {
return false, fmt.Errorf("tss address not found")
}
if common.IsEVMChain(chainID) {
switch coinType {
case common.CoinType_Zeta:
if txx.To().Hex() != coreParams.ConnectorContractAddress {
return false, types.ErrCannotVerifyProof.Wrapf("receiver is not connector contract for coin type %s", coinType)
}
return true, nil
case common.CoinType_ERC20:
if txx.To().Hex() != coreParams.Erc20CustodyContractAddress {
return false, types.ErrCannotVerifyProof.Wrapf("receiver is not erc20Custory contract for coin type %s", coinType)
}
return true, nil
case common.CoinType_Gas:
if txx.To().Hex() != tssAddr.Hex() {
return false, types.ErrCannotVerifyProof.Wrapf("receiver is not tssAddress contract for coin type %s", coinType)
}
return true, nil
}
}
if common.IsBitcoinChain(chainID) {
return false, types.ErrCannotVerifyProof.Wrapf("cannot verify proof for bitcoin chain %d", chainID)
}
return false, fmt.Errorf("proof failed")
}
116 changes: 116 additions & 0 deletions x/crosschain/keeper/msg_add_to_intx_tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package keeper

import (
"context"
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
eth "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
observerTypes "github.com/zeta-chain/zetacore/x/observer/types"
)

func (k msgServer) AddToInTxTracker(goCtx context.Context, msg *types.MsgAddToInTxTracker) (*types.MsgAddToInTxTrackerResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
chain := k.zetaObserverKeeper.GetParams(ctx).GetChainFromChainID(msg.ChainId)
if chain == nil {
return nil, observerTypes.ErrSupportedChains
}

adminPolicyAccount := k.zetaObserverKeeper.GetParams(ctx).GetAdminPolicyAccount(observerTypes.Policy_Type_group1)
isAdmin := msg.Creator == adminPolicyAccount

isObserver, err := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, chain)
if err != nil {
ctx.Logger().Error("Error while checking if the account is an observer", err)
}
isProven := false
if !(isAdmin || isObserver) && msg.Proof != nil {
isProven, err = k.VerifyInTxTrackerProof(ctx, msg.Proof, msg.BlockHash, msg.TxIndex, msg.ChainId, msg.CoinType)
if err != nil {
return nil, types.ErrCannotVerifyProof.Wrapf(err.Error())
}
}

// Sender needs to be either the admin policy account or an observer
if !(isAdmin || isObserver || isProven) {
return nil, errorsmod.Wrap(observerTypes.ErrNotAuthorized, fmt.Sprintf("Creator %s", msg.Creator))
}

k.Keeper.SetInTxTracker(ctx, types.InTxTracker{
ChainId: msg.ChainId,
TxHash: msg.TxHash,
CoinType: msg.CoinType,
})
return &types.MsgAddToInTxTrackerResponse{}, nil
}

// https://github.com/zeta-chain/node/issues/1254
func (k Keeper) VerifyInTxTrackerProof(ctx sdk.Context, proof *common.Proof, hash string, txIndex int64, chainID int64, coinType common.CoinType) (bool, error) {
if common.IsBitcoinChain(chainID) {
return false, fmt.Errorf("cannot verify proof for bitcoin chain %d", chainID)
}

senderChain := common.GetChainFromChainID(chainID)
if senderChain == nil {
return false, types.ErrUnsupportedChain
}

if !senderChain.IsProvable() {
return false, fmt.Errorf("chain %d does not support block header verification", chainID)
}

blockHash := eth.HexToHash(hash)
res, found := k.zetaObserverKeeper.GetBlockHeader(ctx, blockHash.Bytes())
if !found {
return false, fmt.Errorf("block header not found %s", blockHash)
}

// verify and process the proof
val, err := proof.Verify(res.Header, int(txIndex))
if err != nil && !common.IsErrorInvalidProof(err) {
return false, err
}
var txx ethtypes.Transaction
err = txx.UnmarshalBinary(val)
if err != nil {
return false, err
}

coreParams, found := k.zetaObserverKeeper.GetCoreParamsByChainID(ctx, senderChain.ChainId)
if !found {
return false, types.ErrUnsupportedChain.Wrapf("core params not found for chain %d", senderChain.ChainId)
}
tssRes, err := k.GetTssAddress(ctx, &types.QueryGetTssAddressRequest{})
if err != nil {
return false, err
}
tssAddr := eth.HexToAddress(tssRes.Eth)
if tssAddr == (eth.Address{}) {
return false, fmt.Errorf("tss address not found")
}
if common.IsEVMChain(chainID) {
switch coinType {
case common.CoinType_Zeta:
if txx.To().Hex() != coreParams.ConnectorContractAddress {
return false, fmt.Errorf("receiver is not connector contract for coin type %s", coinType)
}
return true, nil
case common.CoinType_ERC20:
if txx.To().Hex() != coreParams.Erc20CustodyContractAddress {
return false, fmt.Errorf("receiver is not erc20Custory contract for coin type %s", coinType)
}
return true, nil
case common.CoinType_Gas:
if txx.To().Hex() != tssAddr.Hex() {
return false, fmt.Errorf("receiver is not tssAddress contract for coin type %s", coinType)
}
return true, nil
}
}

return false, fmt.Errorf("proof failed")
}
11 changes: 10 additions & 1 deletion x/crosschain/types/message_add_to_in_tx_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ func (msg *MsgAddToInTxTracker) ValidateBasic() error {
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
if common.GetChainFromChainID(msg.ChainId) == nil {
chain := common.GetChainFromChainID(msg.ChainId)
if chain == nil {
return errorsmod.Wrapf(ErrInvalidChainID, "chain id (%d)", msg.ChainId)
}
if msg.Proof != nil && !chain.IsProvable() {
return errorsmod.Wrapf(ErrCannotVerifyProof, "chain id %d does not support proof-based trackers", msg.ChainId)
}
_, err = common.GetCoinType(msg.CoinType.String())
if err != nil {
return err
}

return nil
}
4 changes: 2 additions & 2 deletions zetaclient/inbound_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
"golang.org/x/net/context"
)

// ExternalChainWatcherForNewInboundTrackerSuggestions At each tick, gets a list of Inbound tracker suggestions from zeta-core and tries to check if the in-tx was confirmed.
// If it was, it tries to broadcast the confirmation vote. If this zeta client has previously broadcast the vote, the tx would be rejected
func (ob *EVMChainClient) ExternalChainWatcherForNewInboundTrackerSuggestions() {
// At each tick, query the Connector contract
ticker := NewDynamicTicker(fmt.Sprintf("EVM_ExternalChainWatcher_InboundTrackerSuggestions_%d", ob.chain.ChainId), ob.GetCoreParams().InTxTicker)
defer ticker.Stop()
ob.logger.ExternalChainWatcher.Info().Msg("ExternalChainWatcher for inboundTrackerSuggestions started")
Expand Down Expand Up @@ -88,7 +89,6 @@ func (ob *BitcoinChainClient) CheckReceiptForBtcTxHash(txHash string, vote bool)
if err != nil {
return "", err
}
fmt.Println(block.Height)
event, err := GetBtcEvent(*tx, tss, uint64(block.Height), &ob.logger.WatchInTx)
if err != nil {
return "", err
Expand Down

0 comments on commit fc4198e

Please sign in to comment.