-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
233 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
"github.com/zeta-chain/zetacore/pkg/proofs" | ||
keepertest "github.com/zeta-chain/zetacore/testutil/keeper" | ||
"github.com/zeta-chain/zetacore/testutil/sample" | ||
"github.com/zeta-chain/zetacore/x/lightclient/types" | ||
) | ||
|
||
// TODO: Add test for Bitcoin proof verification | ||
// https://github.com/zeta-chain/node/issues/1994 | ||
|
||
func TestKeeper_Prove(t *testing.T) { | ||
t.Run("should error if req is nil", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.LightclientKeeper(t) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
|
||
_, err := k.Prove(wctx, nil) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("should error if block hash is invalid", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.LightclientKeeper(t) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
|
||
proof, _, _, txIndex, _, hash := generateProof(t) | ||
|
||
_, err := k.Prove(wctx, &types.QueryProveRequest{ | ||
ChainId: 1000, | ||
TxHash: hash.Hex(), | ||
Proof: proof, | ||
BlockHash: "invalid", | ||
TxIndex: txIndex, | ||
}) | ||
require.ErrorContains(t, err, "cannot convert hash to bytes for chain") | ||
}) | ||
|
||
t.Run("should error if block header not found", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.LightclientKeeper(t) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
|
||
proof, _, blockHash, txIndex, chainID, hash := generateProof(t) | ||
|
||
_, err := k.Prove(wctx, &types.QueryProveRequest{ | ||
ChainId: chainID, | ||
TxHash: hash.Hex(), | ||
Proof: proof, | ||
BlockHash: blockHash, | ||
TxIndex: txIndex, | ||
}) | ||
require.ErrorContains(t, err, "block header not found") | ||
}) | ||
|
||
t.Run("should returns response with proven false if invalid proof", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.LightclientKeeper(t) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
|
||
proof, blockHeader, blockHash, txIndex, chainID, hash := generateProof(t) | ||
|
||
k.SetBlockHeader(ctx, blockHeader) | ||
|
||
res, err := k.Prove(wctx, &types.QueryProveRequest{ | ||
ChainId: chainID, | ||
TxHash: hash.Hex(), | ||
Proof: proof, | ||
BlockHash: blockHash, | ||
TxIndex: txIndex + 1, // change txIndex to make it invalid | ||
}) | ||
require.NoError(t, err) | ||
require.False(t, res.Valid) | ||
}) | ||
|
||
t.Run("should returns response with proven true if valid proof", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.LightclientKeeper(t) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
|
||
proof, blockHeader, blockHash, txIndex, chainID, hash := generateProof(t) | ||
|
||
k.SetBlockHeader(ctx, blockHeader) | ||
|
||
res, err := k.Prove(wctx, &types.QueryProveRequest{ | ||
ChainId: chainID, | ||
TxHash: hash.Hex(), | ||
Proof: proof, | ||
BlockHash: blockHash, | ||
TxIndex: txIndex, | ||
}) | ||
require.NoError(t, err) | ||
require.True(t, res.Valid) | ||
}) | ||
|
||
t.Run("should error if error during proof verification", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.LightclientKeeper(t) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
|
||
proof, blockHeader, blockHash, txIndex, chainID, hash := generateProof(t) | ||
|
||
// corrupt the block header | ||
blockHeader.Header = proofs.HeaderData{} | ||
|
||
k.SetBlockHeader(ctx, blockHeader) | ||
|
||
_, err := k.Prove(wctx, &types.QueryProveRequest{ | ||
ChainId: chainID, | ||
TxHash: hash.Hex(), | ||
Proof: proof, | ||
BlockHash: blockHash, | ||
TxIndex: txIndex, | ||
}) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("should error if tx hash mismatch", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.LightclientKeeper(t) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
|
||
proof, blockHeader, blockHash, txIndex, chainID, _ := generateProof(t) | ||
|
||
k.SetBlockHeader(ctx, blockHeader) | ||
|
||
_, err := k.Prove(wctx, &types.QueryProveRequest{ | ||
ChainId: chainID, | ||
TxHash: sample.Hash().Hex(), // change tx hash to make it invalid | ||
Proof: proof, | ||
BlockHash: blockHash, | ||
TxIndex: txIndex, | ||
}) | ||
require.ErrorContains(t, err, "tx hash mismatch") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters