Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove recursion depth guard in SPV #58

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func ExampleNewClient() {
return
}
fmt.Printf("loaded client: %s", client.GetOptions().userAgent)
// Output:loaded client: go-paymail: v0.9.1
// Output:loaded client: go-paymail: v0.9.3
}

// BenchmarkNewClient benchmarks the method NewClient()
Expand Down
2 changes: 1 addition & 1 deletion definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
defaultSSLTimeout = 10 * time.Second // Default timeout in seconds
defaultUserAgent = "go-paymail: " + version // Default user agent
defaultNetwork = byte(Mainnet) // Default network
version = "v0.9.1" // Go-Paymail version
version = "v0.9.3" // Go-Paymail version
)

// Public defaults for paymail specs
Expand Down
14 changes: 3 additions & 11 deletions spv/bump_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/libsv/go-bt/v2"
)

const recursiveMaxDepth = 128 // arbitrarily chosen value

func ensureAncestorsArePresentInBump(tx *bt.Tx, dBeef *beef.DecodedBEEF) error {
ancestors, err := findMinedAncestors(tx, dBeef.Transactions)
if err != nil {
Expand All @@ -29,22 +27,16 @@ func findMinedAncestors(tx *bt.Tx, ancestors []*beef.TxData) (map[string]*beef.T
am := make(map[string]*beef.TxData)

for _, input := range tx.Inputs {
err := findMinedAncestorsForInput(input, ancestors, am, 0)

if err != nil {
if err := findMinedAncestorsForInput(input, ancestors, am); err != nil {
return nil, err
}
}

return am, nil
}

func findMinedAncestorsForInput(input *bt.Input, ancestors []*beef.TxData, ma map[string]*beef.TxData, depth uint) error {
if depth > recursiveMaxDepth { //primitive protection against Cyclic Graph (and therefore infinite loop)
return fmt.Errorf("invalid BUMP - cannot find mined parent for input %s on %d depth", input.String(), depth)
}
depth++

func findMinedAncestorsForInput(input *bt.Input, ancestors []*beef.TxData, ma map[string]*beef.TxData) error {
parent := findParentForInput(input, ancestors)
if parent == nil {
return fmt.Errorf("invalid BUMP - cannot find mined parent for input %s", input.String())
Expand All @@ -56,7 +48,7 @@ func findMinedAncestorsForInput(input *bt.Input, ancestors []*beef.TxData, ma ma
}

for _, in := range parent.Transaction.Inputs {
err := findMinedAncestorsForInput(in, ancestors, ma, depth)
err := findMinedAncestorsForInput(in, ancestors, ma) // we don't have to worry about infinite recursion - the graph will always be acyclic due to the nature of the transactions
if err != nil {
return err
}
Expand Down
Loading