Skip to content

Commit c6e7b9d

Browse files
authored
Merge pull request #197 from b-open-io/update/chaintracker-context
Add context parameter to ChainTracker interface
2 parents caec21f + 1d924b5 commit c6e7b9d

File tree

14 files changed

+64
-33
lines changed

14 files changed

+64
-33
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. The format
44

55
## Table of Contents
66

7+
- [1.2.4 - 2025-06-30](#124---2025-06-30)
78
- [1.2.3 - 2025-06-30](#123---2025-06-30)
89
- [1.2.2 - 2025-06-27](#122---2025-06-27)
910
- [1.2.1 - 2025-06-12](#121---2025-06-12)
@@ -38,6 +39,15 @@ All notable changes to this project will be documented in this file. The format
3839
- [1.1.0 - 2024-08-19](#110---2024-08-19)
3940
- [1.0.0 - 2024-06-06](#100---2024-06-06)
4041

42+
## [1.2.4] - 2025-06-30
43+
44+
### Changed
45+
- Add context parameter to ChainTracker.IsValidRootForHeight for cancellation/timeout support
46+
- Update all ChainTracker implementations to take context (WhatsOnChain, HeadersClient, GullibleHeadersClient)
47+
- Update MerklePath.Verify and VerifyHex to take context
48+
- Update BEEF.Verify to take context
49+
- Update spv.Verify and VerifyScripts to take context
50+
4151
## [1.2.3] - 2025-06-30
4252

4353
### Added

docs/examples/validate_spv/validate_spv.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/base64"
56
"fmt"
67

@@ -32,7 +33,8 @@ func main() {
3233
return
3334
}
3435

35-
valid, err := client.IsValidRootForHeight(root, tx.MerklePath.BlockHeight)
36+
ctx := context.Background()
37+
valid, err := client.IsValidRootForHeight(ctx, root, tx.MerklePath.BlockHeight)
3638
if err != nil {
3739
fmt.Println("Error validating root for height:", err)
3840
return

docs/examples/verify_beef/verify_beef.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"context"
5+
46
"github.com/bsv-blockchain/go-sdk/spv"
57
"github.com/bsv-blockchain/go-sdk/transaction"
68
)
@@ -15,6 +17,7 @@ func main() {
1517
panic(err)
1618
}
1719
// This ensures the BEEF structure is legitimate
18-
verified, _ := tx.MerklePath.Verify(tx.TxID(), &spv.GullibleHeadersClient{})
20+
ctx := context.Background()
21+
verified, _ := tx.MerklePath.Verify(ctx, tx.TxID(), &spv.GullibleHeadersClient{})
1922
println(verified)
2023
}

docs/examples/verify_transaction/verify_transaction.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"context"
5+
46
"github.com/bsv-blockchain/go-sdk/spv"
57
"github.com/bsv-blockchain/go-sdk/transaction"
68
)
@@ -16,6 +18,7 @@ func main() {
1618
}
1719
// This ensures the BEEF structure is legitimate, the scripts are valid, and the merkle path is correct
1820
// Also optionally verifies fees
19-
verified, _ := spv.Verify(tx, &spv.GullibleHeadersClient{}, nil)
21+
ctx := context.Background()
22+
verified, _ := spv.Verify(ctx, tx, &spv.GullibleHeadersClient{}, nil)
2023
println(verified)
2124
}

spv/scripts_only.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
type GullibleHeadersClient struct{}
1010

11-
func (g *GullibleHeadersClient) IsValidRootForHeight(merkleRoot *chainhash.Hash, height uint32) (bool, error) {
11+
func (g *GullibleHeadersClient) IsValidRootForHeight(ctx context.Context, merkleRoot *chainhash.Hash, height uint32) (bool, error) {
1212
// DO NOT USE IN A REAL PROJECT due to security risks of accepting any merkle root as valid without verification
1313
return true, nil
1414
}

spv/verify.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package spv
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/bsv-blockchain/go-sdk/script/interpreter"
78
"github.com/bsv-blockchain/go-sdk/transaction"
89
"github.com/bsv-blockchain/go-sdk/transaction/chaintracker"
910
)
1011

11-
func Verify(t *transaction.Transaction,
12+
func Verify(ctx context.Context, t *transaction.Transaction,
1213
chainTracker chaintracker.ChainTracker,
1314
feeModel transaction.FeeModel) (bool, error) {
1415
verifiedTxids := make(map[string]struct{})
@@ -28,7 +29,7 @@ func Verify(t *transaction.Transaction,
2829
}
2930

3031
if tx.MerklePath != nil {
31-
if isValid, err := tx.MerklePath.Verify(txid, chainTracker); err != nil {
32+
if isValid, err := tx.MerklePath.Verify(ctx, txid, chainTracker); err != nil {
3233
return false, err
3334
} else if isValid {
3435
verifiedTxids[txidStr] = struct{}{}
@@ -80,6 +81,6 @@ func Verify(t *transaction.Transaction,
8081
return true, nil
8182
}
8283

83-
func VerifyScripts(t *transaction.Transaction) (bool, error) {
84-
return Verify(t, &GullibleHeadersClient{}, nil)
84+
func VerifyScripts(ctx context.Context, t *transaction.Transaction) (bool, error) {
85+
return Verify(ctx, t, &GullibleHeadersClient{}, nil)
8586
}

spv/verify_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const BEEF = "AQC+7wH+kQYNAAcCVAIKXThHm90iVbs15AIfFQEYl5xesbHCXMkYy9SqoR1vNVUAAZ
1717
func TestSPVVerify(t *testing.T) {
1818
tx, err := transaction.NewTransactionFromBEEFHex(BRC62Hex)
1919
require.NoError(t, err)
20-
verified, err := Verify(tx, &GullibleHeadersClient{}, nil)
20+
ctx := t.Context()
21+
verified, err := Verify(ctx, tx, &GullibleHeadersClient{}, nil)
2122
require.NoError(t, err)
2223
require.True(t, verified)
2324
}
@@ -27,7 +28,8 @@ func TestSPVVerifyScripts(t *testing.T) {
2728
require.NoError(t, err)
2829
tx, err := transaction.NewTransactionFromBEEF(buf)
2930
require.NoError(t, err)
30-
verified, err := VerifyScripts(tx)
31+
ctx := t.Context()
32+
verified, err := VerifyScripts(ctx, tx)
3133
require.NoError(t, err)
3234
require.True(t, verified)
3335
}
@@ -40,7 +42,8 @@ func TestSPVVerifyWithSufficientFee(t *testing.T) {
4042
Satoshis: 10,
4143
}
4244

43-
verified, err := Verify(tx, &GullibleHeadersClient{}, feeModel)
45+
ctx := t.Context()
46+
verified, err := Verify(ctx, tx, &GullibleHeadersClient{}, feeModel)
4447
require.NoError(t, err)
4548
require.True(t, verified)
4649
}
@@ -53,7 +56,8 @@ func TestSPVVerifyWithInsufficientFee(t *testing.T) {
5356
Satoshis: 1,
5457
}
5558

56-
verified, err := Verify(tx, &GullibleHeadersClient{}, feeModel)
59+
ctx := t.Context()
60+
verified, err := Verify(ctx, tx, &GullibleHeadersClient{}, feeModel)
5761
require.Error(t, err)
5862
require.Contains(t, err.Error(), "fee is too low")
5963
require.False(t, verified)

transaction/beef.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package transaction
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/binary"
67
"encoding/hex"
78
"fmt"
@@ -780,7 +781,7 @@ func (b *Beef) IsValid(allowTxidOnly bool) bool {
780781
return r.valid
781782
}
782783

783-
func (b *Beef) Verify(chainTracker chaintracker.ChainTracker, allowTxidOnly bool) (bool, error) {
784+
func (b *Beef) Verify(ctx context.Context, chainTracker chaintracker.ChainTracker, allowTxidOnly bool) (bool, error) {
784785
r := b.verifyValid(allowTxidOnly)
785786
if !r.valid {
786787
return false, nil
@@ -790,7 +791,7 @@ func (b *Beef) Verify(chainTracker chaintracker.ChainTracker, allowTxidOnly bool
790791
if err != nil {
791792
return false, err
792793
}
793-
ok, err := chainTracker.IsValidRootForHeight(h, height)
794+
ok, err := chainTracker.IsValidRootForHeight(ctx, h, height)
794795
if err != nil || !ok {
795796
return false, err
796797
}

transaction/chaintracker/chaintracker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ import (
77
)
88

99
type ChainTracker interface {
10-
IsValidRootForHeight(root *chainhash.Hash, height uint32) (bool, error)
10+
IsValidRootForHeight(ctx context.Context, root *chainhash.Hash, height uint32) (bool, error)
1111
CurrentHeight(ctx context.Context) (uint32, error)
1212
}

transaction/chaintracker/headers_client/headers_client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Client struct {
3434
ApiKey string
3535
}
3636

37-
func (c Client) IsValidRootForHeight(root *chainhash.Hash, height uint32) (bool, error) {
37+
func (c Client) IsValidRootForHeight(ctx context.Context, root *chainhash.Hash, height uint32) (bool, error) {
3838
type requestBody struct {
3939
MerkleRoot string `json:"merkleRoot"`
4040
BlockHeight uint32 `json:"blockHeight"`
@@ -46,7 +46,7 @@ func (c Client) IsValidRootForHeight(root *chainhash.Hash, height uint32) (bool,
4646
return false, fmt.Errorf("error marshaling JSON: %v", err)
4747
}
4848

49-
req, err := http.NewRequest("POST", c.Url+"/api/v1/chain/merkleroot/verify", bytes.NewBuffer(jsonPayload))
49+
req, err := http.NewRequestWithContext(ctx, "POST", c.Url+"/api/v1/chain/merkleroot/verify", bytes.NewBuffer(jsonPayload))
5050
if err != nil {
5151
return false, fmt.Errorf("error creating request: %v", err)
5252
}

0 commit comments

Comments
 (0)