Skip to content

Commit

Permalink
WIP: extend proof block header verifier to account for block height
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Jul 13, 2023
1 parent ae3e5e9 commit ff16d31
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
34 changes: 31 additions & 3 deletions itest/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,42 @@ func verifyProofBlob(t *testing.T, tapd *tapdHarness,
anchorTxBlockHeight := rpcAsset.ChainAnchor.BlockHeight
require.Greater(t, anchorTxBlockHeight, uint32(0))

headerVerifier := func(blockHeader wire.BlockHeader) error {
hash := blockHeader.BlockHash()
headerVerifier := func(header wire.BlockHeader, height uint32) error {
hash := header.BlockHash()
req := &chainrpc.GetBlockRequest{
BlockHash: hash.CloneBytes(),
}
_, err := tapd.cfg.LndNode.RPC.ChainKit.GetBlock(ctxb, req)
return err
if err != nil {
return err
}

//// Ensure that the block hash matches the hash of the block
//// found at the given height.
//blockHashReq := &chainrpc.GetBlockHashRequest{
// BlockHeight: int64(height),
//}
//blockHashResp, err := tapd.cfg.LndNode.RPC.ChainKit.GetBlockHash(
// ctxb, blockHashReq,
//)
//if err != nil {
// return err
//}
//
//var heightHash chainhash.Hash
//copy(heightHash[:], blockHashResp.BlockHash)
//
//expectedHash := hash
//if heightHash != expectedHash {
// return fmt.Errorf("block hash and block height "+
// "mismatch; (height: %x, hashAtHeight: %x, "+
// "expectedHash: %x)", height, heightHash,
// expectedHash)
//}

return nil
}

snapshot, err := f.Verify(ctxt, headerVerifier)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion proof/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ func (m *MockVerifier) Verify(_ context.Context, _ io.Reader,
// Header verification usually involves cross-referencing with chain data.
// Chain data is not available in unit tests. This function is useful for unit
// tests which are not primarily concerned with block header verification.
func MockHeaderVerifier(blockHeader wire.BlockHeader) error {
func MockHeaderVerifier(header wire.BlockHeader, height uint32) error {
return nil
}
4 changes: 2 additions & 2 deletions proof/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,10 @@ func TestProofBlockHeaderVerification(t *testing.T) {
// Header verifier compares given header to expected header. Verifier
// does not return error.
errHeaderVerifier := fmt.Errorf("invalid block header")
headerVerifier := func(blockHeader wire.BlockHeader) error {
headerVerifier := func(header wire.BlockHeader, height uint32) error {
// Compare given block header against base reference block
// header.
if blockHeader != originalBlockHeader {
if header != originalBlockHeader {
return errHeaderVerifier
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions proof/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (p *Proof) verifyMetaReveal() error {

// HeaderVerifier is a callback function which returns an error if the given
// block header is invalid (usually: not present on chain).
type HeaderVerifier func(blockHeader wire.BlockHeader) error
type HeaderVerifier func(blockHeader wire.BlockHeader, blockHeight uint32) error

// Verify verifies the proof by ensuring that:
//
Expand All @@ -313,7 +313,7 @@ func (p *Proof) Verify(ctx context.Context, prev *AssetSnapshot,
}

// Cross-check block header with a bitcoin node.
err := headerVerifier(p.BlockHeader)
err := headerVerifier(p.BlockHeader, p.BlockHeight)
if err != nil {
return nil, fmt.Errorf("failed to validate proof block "+
"header: %w", err)
Expand Down
28 changes: 24 additions & 4 deletions tapgarden/caretaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,12 +1025,32 @@ func GetTxFee(pkt *psbt.Packet) (int64, error) {
// GenHeaderVerifier generates a block header on-chain verification callback
// function given a chain bridge.
func GenHeaderVerifier(ctx context.Context,
chainBridge ChainBridge) func(header wire.BlockHeader) error {
chainBridge ChainBridge) func(wire.BlockHeader, uint32) error {

return func(blockHeader wire.BlockHeader) error {
return func(header wire.BlockHeader, height uint32) error {
_, err := chainBridge.GetBlock(
ctx, blockHeader.BlockHash(),
ctx, header.BlockHash(),
)
return err
if err != nil {
return err
}

// Ensure that the block hash matches the hash of the block
// found at the given height.
hash, err := chainBridge.GetBlockHash(
ctx, int64(height),
)
if err != nil {
return err
}

expectedHash := header.BlockHash()
if hash != expectedHash {
return fmt.Errorf("block hash and block height "+
"mismatch; (height: %x, hashAtHeight: %x, "+
"expectedHash: %x)", height, hash, expectedHash)
}

return nil
}
}

0 comments on commit ff16d31

Please sign in to comment.