Skip to content

Commit

Permalink
proof: extend unit tests to account for block height
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Jul 19, 2023
1 parent 28c9a75 commit 03be04b
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions proof/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"math/rand"
"os"
Expand Down Expand Up @@ -79,6 +78,7 @@ func assertEqualProof(t *testing.T, expected, actual *Proof) {

require.Equal(t, expected.PrevOut, actual.PrevOut)
require.Equal(t, expected.BlockHeader, actual.BlockHeader)
require.Equal(t, expected.BlockHeight, actual.BlockHeight)
require.Equal(t, expected.AnchorTx, actual.AnchorTx)
require.Equal(t, expected.TxMerkleProof, actual.TxMerkleProof)
require.Equal(t, expected.Asset, actual.Asset)
Expand Down Expand Up @@ -315,16 +315,22 @@ func genRandomGenesisWithProof(t testing.TB, assetType asset.Type,
[]*btcutil.Tx{btcutil.NewTx(genesisTx)}, false,
)
merkleRoot := merkleTree[len(merkleTree)-1]

// We'll use the genesis hash of the mainnet chain as the parent block.
blockHeader := wire.NewBlockHeader(
0, chaincfg.MainNetParams.GenesisHash, merkleRoot, 0, 0,
)

// We'll set the block height to 1, as the genesis block is at height 0.
blockHeight := uint32(1)

txMerkleProof, err := NewTxMerkleProof([]*wire.MsgTx{genesisTx}, 0)
require.NoError(t, err)

return Proof{
PrevOut: genesisTx.TxIn[0].PreviousOutPoint,
BlockHeader: *blockHeader,
BlockHeight: blockHeight,
AnchorTx: *genesisTx,
TxMerkleProof: *txMerkleProof,
Asset: *genesisAsset,
Expand Down Expand Up @@ -480,17 +486,20 @@ func TestProofBlockHeaderVerification(t *testing.T) {
t, asset.Collectible, nil, nil, true, nil, nil,
)

// Create a base reference for the block header. We will later modify
// the proof block header.
originalBlockHeader := proof.BlockHeader
// Create a base reference for the block header and block height. We
// will later modify these proof fields.
var (
originalBlockHeader = proof.BlockHeader
originalBlockHeight = proof.BlockHeight
)

// Header verifier compares given header to expected header. Verifier
// does not return error.
errHeaderVerifier := fmt.Errorf("invalid block header")
headerVerifier := func(header wire.BlockHeader, height uint32) error {
// Compare given block header against base reference block
// header.
if header != originalBlockHeader {
if header != originalBlockHeader || height != originalBlockHeight {
return errHeaderVerifier
}
return nil
Expand All @@ -509,8 +518,18 @@ func TestProofBlockHeaderVerification(t *testing.T) {
_, actualErr := proof.Verify(
context.Background(), nil, headerVerifier,
)
actualErrInner := errors.Unwrap(actualErr)
require.Equal(t, actualErrInner, errHeaderVerifier)
require.ErrorIs(t, actualErr, errHeaderVerifier)

// Reset proof block header.
proof.BlockHeader.Nonce = originalBlockHeader.Nonce

// Modify proof block height, then check that the verification function
// propagates the correct error.
proof.BlockHeight += 1
_, actualErr = proof.Verify(
context.Background(), nil, headerVerifier,
)
require.ErrorIs(t, actualErr, errHeaderVerifier)
}

// TestProofFileVerification ensures that the proof file encoding and decoding
Expand Down

0 comments on commit 03be04b

Please sign in to comment.