Skip to content

Commit

Permalink
proof
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Apr 17, 2024
1 parent ab9b1b0 commit 60e57f3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
44 changes: 44 additions & 0 deletions testutil/sample/lightclient.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package sample

import (
"testing"

ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/proofs"
"github.com/zeta-chain/zetacore/pkg/proofs/ethereum"
"github.com/zeta-chain/zetacore/pkg/testdata"
lightclienttypes "github.com/zeta-chain/zetacore/x/lightclient/types"
)

Expand Down Expand Up @@ -30,3 +39,38 @@ func VerificationFlags() lightclienttypes.VerificationFlags {
BtcTypeChainEnabled: true,
}
}

// Proof generates a proof and block header
// returns the proof, block header, block hash, tx index, chain id, and tx hash
func Proof(t *testing.T) (*proofs.Proof, proofs.BlockHeader, string, int64, int64, ethcommon.Hash) {
header, err := testdata.ReadEthHeader()
require.NoError(t, err)
b, err := rlp.EncodeToBytes(&header)
require.NoError(t, err)

var txs ethtypes.Transactions
for i := 0; i < testdata.TxsCount; i++ {
tx, err := testdata.ReadEthTx(i)
require.NoError(t, err)
txs = append(txs, &tx)
}
txsTree := ethereum.NewTrie(txs)

// choose 2 as the index of the tx to prove
txIndex := 2
proof, err := txsTree.GenerateProof(txIndex)
require.NoError(t, err)

chainID := chains.SepoliaChain().ChainId
ethProof := proofs.NewEthereumProof(proof)
ethHeader := proofs.NewEthereumHeader(b)
blockHeader := proofs.BlockHeader{
Height: header.Number.Int64(),
Hash: header.Hash().Bytes(),
ParentHash: header.ParentHash.Bytes(),
ChainId: chainID,
Header: ethHeader,
}
txHash := txs[txIndex].Hash()
return ethProof, blockHeader, header.Hash().Hex(), int64(txIndex), chainID, txHash
}
12 changes: 6 additions & 6 deletions x/lightclient/keeper/grpc_query_prove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestKeeper_Prove(t *testing.T) {
k, ctx, _, _ := keepertest.LightclientKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

proof, _, _, txIndex, _, hash := generateProof(t)
proof, _, _, txIndex, _, hash := sample.Proof(t)

_, err := k.Prove(wctx, &types.QueryProveRequest{
ChainId: 1000,
Expand All @@ -43,7 +43,7 @@ func TestKeeper_Prove(t *testing.T) {
k, ctx, _, _ := keepertest.LightclientKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

proof, _, blockHash, txIndex, chainID, hash := generateProof(t)
proof, _, blockHash, txIndex, chainID, hash := sample.Proof(t)

_, err := k.Prove(wctx, &types.QueryProveRequest{
ChainId: chainID,
Expand All @@ -59,7 +59,7 @@ func TestKeeper_Prove(t *testing.T) {
k, ctx, _, _ := keepertest.LightclientKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

proof, blockHeader, blockHash, txIndex, chainID, hash := generateProof(t)
proof, blockHeader, blockHash, txIndex, chainID, hash := sample.Proof(t)

k.SetBlockHeader(ctx, blockHeader)

Expand All @@ -78,7 +78,7 @@ func TestKeeper_Prove(t *testing.T) {
k, ctx, _, _ := keepertest.LightclientKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

proof, blockHeader, blockHash, txIndex, chainID, hash := generateProof(t)
proof, blockHeader, blockHash, txIndex, chainID, hash := sample.Proof(t)

k.SetBlockHeader(ctx, blockHeader)

Expand All @@ -97,7 +97,7 @@ func TestKeeper_Prove(t *testing.T) {
k, ctx, _, _ := keepertest.LightclientKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

proof, blockHeader, blockHash, txIndex, chainID, hash := generateProof(t)
proof, blockHeader, blockHash, txIndex, chainID, hash := sample.Proof(t)

// corrupt the block header
blockHeader.Header = proofs.HeaderData{}
Expand All @@ -118,7 +118,7 @@ func TestKeeper_Prove(t *testing.T) {
k, ctx, _, _ := keepertest.LightclientKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

proof, blockHeader, blockHash, txIndex, chainID, _ := generateProof(t)
proof, blockHeader, blockHash, txIndex, chainID, _ := sample.Proof(t)

k.SetBlockHeader(ctx, blockHeader)

Expand Down
45 changes: 2 additions & 43 deletions x/lightclient/keeper/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,14 @@ package keeper_test
import (
"testing"

ethcommon "github.com/ethereum/go-ethereum/common"

ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/proofs"
"github.com/zeta-chain/zetacore/pkg/proofs/ethereum"
"github.com/zeta-chain/zetacore/pkg/testdata"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/x/lightclient/types"
)

// generateProof generates a proof and block header
// returns the proof, block header, block hash, tx index, chain id, and tx hash
func generateProof(t *testing.T) (*proofs.Proof, proofs.BlockHeader, string, int64, int64, ethcommon.Hash) {
header, err := testdata.ReadEthHeader()
require.NoError(t, err)
b, err := rlp.EncodeToBytes(&header)
require.NoError(t, err)

var txs ethtypes.Transactions
for i := 0; i < testdata.TxsCount; i++ {
tx, err := testdata.ReadEthTx(i)
require.NoError(t, err)
txs = append(txs, &tx)
}
txsTree := ethereum.NewTrie(txs)

// choose 2 as the index of the tx to prove
txIndex := 2
proof, err := txsTree.GenerateProof(txIndex)
require.NoError(t, err)

chainID := chains.SepoliaChain().ChainId
ethProof := proofs.NewEthereumProof(proof)
ethHeader := proofs.NewEthereumHeader(b)
blockHeader := proofs.BlockHeader{
Height: header.Number.Int64(),
Hash: header.Hash().Bytes(),
ParentHash: header.ParentHash.Bytes(),
ChainId: chainID,
Header: ethHeader,
}
txHash := txs[txIndex].Hash()
return ethProof, blockHeader, header.Hash().Hex(), int64(txIndex), chainID, txHash
}

func TestKeeper_VerifyProof(t *testing.T) {
t.Run("should error if verification flags not found", func(t *testing.T) {
k, ctx, _, _ := keepertest.LightclientKeeper(t)
Expand Down Expand Up @@ -123,7 +82,7 @@ func TestKeeper_VerifyProof(t *testing.T) {
t.Run("should fail if proof can't be verified", func(t *testing.T) {
k, ctx, _, _ := keepertest.LightclientKeeper(t)

proof, blockHeader, blockHash, txIndex, chainID, _ := generateProof(t)
proof, blockHeader, blockHash, txIndex, chainID, _ := sample.Proof(t)

k.SetVerificationFlags(ctx, types.VerificationFlags{
EthTypeChainEnabled: true,
Expand All @@ -140,7 +99,7 @@ func TestKeeper_VerifyProof(t *testing.T) {
t.Run("can verify a proof", func(t *testing.T) {
k, ctx, _, _ := keepertest.LightclientKeeper(t)

proof, blockHeader, blockHash, txIndex, chainID, _ := generateProof(t)
proof, blockHeader, blockHash, txIndex, chainID, _ := sample.Proof(t)

k.SetVerificationFlags(ctx, types.VerificationFlags{
EthTypeChainEnabled: true,
Expand Down

0 comments on commit 60e57f3

Please sign in to comment.