Skip to content

Commit

Permalink
add tests for out_tx_Tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Oct 19, 2023
1 parent 2df1e78 commit e31659d
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 251 deletions.
5 changes: 2 additions & 3 deletions testutil/sample/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sample

import (
"context"
"fmt"
"math/big"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
Expand Down Expand Up @@ -31,7 +30,7 @@ func PubKeySet() *common.PubKeySet {
return &pubKeySet
}

func Proof() (tx_index int64, block *ethtypes.Block, header ethtypes.Header, headerRLP []byte, proof *common.Proof, err error) {
func Proof() (tx_index int64, block *ethtypes.Block, header ethtypes.Header, headerRLP []byte, proof *common.Proof, tx *ethtypes.Transaction, err error) {
tx_index = int64(9)
RPC_URL := "https://rpc.ankr.com/eth_goerli"
client, err := ethclient.Dial(RPC_URL)
Expand All @@ -40,7 +39,6 @@ func Proof() (tx_index int64, block *ethtypes.Block, header ethtypes.Header, hea
}
bn := int64(9889649)
block, err = client.BlockByNumber(context.Background(), big.NewInt(bn))
fmt.Println(block)
headerRLP, _ = rlp.EncodeToBytes(block.Header())
err = rlp.DecodeBytes(headerRLP, &header)
if err != nil {
Expand All @@ -55,5 +53,6 @@ func Proof() (tx_index int64, block *ethtypes.Block, header ethtypes.Header, hea
return
}
proof = common.NewEthereumProof(p)
tx = block.Transactions()[tx_index]
return
}
3 changes: 3 additions & 0 deletions x/crosschain/keeper/msg_server_add_to_intx_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func (k Keeper) AddToInTxTracker(goCtx context.Context, msg *types.MsgAddToInTxT

if common.IsEVMChain(msg.ChainId) {
err = k.VerifyEVMInTxBody(ctx, msg, txBytes)
if err != nil {
return nil, types.ErrCannotVerifyProof.Wrapf(err.Error())
}
} else {
return nil, types.ErrCannotVerifyProof.Wrapf(fmt.Sprintf("cannot verify inTx body for chain %d", msg.ChainId))
}
Expand Down
86 changes: 66 additions & 20 deletions x/crosschain/keeper/msg_server_add_to_intx_tracker_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build TESTNET
// +build TESTNET

package keeper_test

import (
Expand All @@ -17,56 +20,88 @@ import (
func TestMsgServer_AddToInTxTracker(t *testing.T) {
t.Run("Add proof based tracker with correct proof", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
txHash := "string"
chainID := int64(5)
txIndex, block, header, headerRLP, proof, err := sample.Proof()
txIndex, block, header, headerRLP, proof, tx, err := sample.Proof()
require.NoError(t, err)
SetupVerificationParams(zk, ctx, txIndex, chainID, header, headerRLP, block)
_, err = k.AddToInTxTracker(ctx, &types.MsgAddToInTxTracker{
Creator: sample.AccAddress(),
ChainId: chainID,
TxHash: txHash,
TxHash: tx.Hash().Hex(),
CoinType: common.CoinType_Zeta,
Proof: proof,
BlockHash: block.Hash().Hex(),
TxIndex: txIndex,
})
require.NoError(t, err)
_, found := k.GetInTxTracker(ctx, chainID, txHash)
_, found := k.GetInTxTracker(ctx, chainID, tx.Hash().Hex())
require.True(t, found)
})

t.Run("Fail to add proof based tracker with wrong tx hash", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
chainID := int64(5)
txIndex, block, header, headerRLP, proof, tx, err := sample.Proof()
require.NoError(t, err)
SetupVerificationParams(zk, ctx, txIndex, chainID, header, headerRLP, block)
_, err = k.AddToInTxTracker(ctx, &types.MsgAddToInTxTracker{
Creator: sample.AccAddress(),
ChainId: chainID,
TxHash: "fake_hash",
CoinType: common.CoinType_Zeta,
Proof: proof,
BlockHash: block.Hash().Hex(),
TxIndex: txIndex,
})
require.Error(t, err)
_, found := k.GetInTxTracker(ctx, chainID, tx.Hash().Hex())
require.False(t, found)
})

t.Run("Fail to add proof based tracker with wrong chain id", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
chainID := int64(5)
txIndex, block, header, headerRLP, proof, tx, err := sample.Proof()
require.NoError(t, err)
SetupVerificationParams(zk, ctx, txIndex, chainID, header, headerRLP, block)
_, err = k.AddToInTxTracker(ctx, &types.MsgAddToInTxTracker{
Creator: sample.AccAddress(),
ChainId: 97,
TxHash: tx.Hash().Hex(),
CoinType: common.CoinType_Zeta,
Proof: proof,
BlockHash: block.Hash().Hex(),
TxIndex: txIndex,
})
require.Error(t, err)
_, found := k.GetInTxTracker(ctx, chainID, tx.Hash().Hex())
require.False(t, found)
})

t.Run("Fail to add proof based tracker with wrong proof", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
txHash := "string"
chainID := int64(1)
txIndex, block, header, headerRLP, _, err := sample.Proof()
chainID := int64(5)
txIndex, block, header, headerRLP, _, tx, err := sample.Proof()
require.NoError(t, err)
SetupVerificationParams(zk, ctx, txIndex, chainID, header, headerRLP, block)

_, err = k.AddToInTxTracker(ctx, &types.MsgAddToInTxTracker{
Creator: sample.AccAddress(),
ChainId: chainID,
TxHash: txHash,
TxHash: tx.Hash().Hex(),
CoinType: common.CoinType_Zeta,
Proof: common.NewEthereumProof(ethereum.NewProof()),
BlockHash: block.Hash().Hex(),
TxIndex: txIndex,
})
require.Error(t, err)
_, found := k.GetInTxTracker(ctx, chainID, txHash)
_, found := k.GetInTxTracker(ctx, chainID, tx.Hash().Hex())
require.False(t, found)
})
t.Run("normal user submit without proof", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
tx_hash := "string"
chainID := int64(1)
chainID := int64(5)
_, err := k.AddToInTxTracker(ctx, &types.MsgAddToInTxTracker{
Creator: sample.AccAddress(),
ChainId: chainID,
Expand All @@ -80,12 +115,12 @@ func TestMsgServer_AddToInTxTracker(t *testing.T) {
_, found := k.GetInTxTracker(ctx, chainID, tx_hash)
require.False(t, found)
})
t.Run("admin add tx tracker with admin", func(t *testing.T) {
t.Run("admin add tx tracker", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
tx_hash := "string"
chainID := int64(1)
chainID := int64(5)
_, err := k.AddToInTxTracker(ctx, &types.MsgAddToInTxTracker{
Creator: admin,
ChainId: chainID,
Expand All @@ -104,7 +139,7 @@ func TestMsgServer_AddToInTxTracker(t *testing.T) {
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
tx_hash := "string"
chainID := int64(1)
chainID := int64(5)
_, err := k.AddToInTxTracker(ctx, &types.MsgAddToInTxTracker{
Creator: admin,
ChainId: chainID,
Expand All @@ -123,6 +158,17 @@ func TestMsgServer_AddToInTxTracker(t *testing.T) {
}

func SetupVerificationParams(zk keepertest.ZetaKeepers, ctx sdk.Context, tx_index int64, chainID int64, header ethtypes.Header, headerRLP []byte, block *ethtypes.Block) {
params := zk.ObserverKeeper.GetParams(ctx)
params.ObserverParams = append(params.ObserverParams, &observerTypes.ObserverParams{
Chain: &common.Chain{
ChainId: chainID,
ChainName: common.ChainName_goerli_testnet,
},
BallotThreshold: sdk.OneDec(),
MinObserverDelegation: sdk.OneDec(),
IsSupported: true,
})
zk.ObserverKeeper.SetParams(ctx, params)
zk.ObserverKeeper.SetBlockHeader(ctx, common.BlockHeader{
Height: block.Number().Int64(),
Hash: block.Hash().Bytes(),
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/msg_server_add_to_outtx_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

// AddToOutTxTracker adds a new record to the outbound transaction tracker.
// only the admin policy account and the observer validators are authorized to broadcast this message without proof.
func (k msgServer) AddToOutTxTracker(goCtx context.Context, msg *types.MsgAddToOutTxTracker) (*types.MsgAddToOutTxTrackerResponse, error) {
func (k Keeper) AddToOutTxTracker(goCtx context.Context, msg *types.MsgAddToOutTxTracker) (*types.MsgAddToOutTxTrackerResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
chain := k.zetaObserverKeeper.GetParams(ctx).GetChainFromChainID(msg.ChainId)
if chain == nil {
Expand Down
Loading

0 comments on commit e31659d

Please sign in to comment.