Skip to content

Commit

Permalink
Merge pull request #42 from libsv/fix/fee-verification
Browse files Browse the repository at this point in the history
Fix: Fee Verification for SPV
  • Loading branch information
roppa committed Apr 5, 2022
2 parents b482a46 + 25fb06f commit 463d2d2
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions spv/ancestors_binary.go
Expand Up @@ -149,6 +149,18 @@ func VerifyAncestors(ctx context.Context, ancestry *Ancestry, mpv MerkleProofVer
ancestors[paymentTxID] = &Ancestor{
Tx: ancestry.PaymentTx,
}
if opts.fees {
if opts.feeQuote == nil {
return ErrNoFeeQuoteSupplied
}
ok, err := ancestry.PaymentTx.IsFeePaidEnough(opts.feeQuote)
if err != nil {
return err
}
if !ok {
return ErrFeePaidNotEnough
}
}
for _, ancestor := range ancestors {
inputsToCheck := make(map[[32]byte]*extendedInput)
if len(ancestor.Tx.Inputs) == 0 {
Expand Down Expand Up @@ -206,27 +218,6 @@ func VerifyAncestors(ctx context.Context, ancestry *Ancestry, mpv MerkleProofVer
}
}
}
if opts.fees {
if opts.feeQuote == nil {
return ErrNoFeeQuoteSupplied
}
// no need to check fees for transactions we have proofs for
if ancestor.Proof == nil {
// add satoshi amounts to all inputs which correspond to outputs we have
for inputID, extendedInput := range inputsToCheck {
if ancestry.Ancestors[inputID] == nil {
return ErrCannotCalculateFeePaid
}
sats := ancestry.Ancestors[inputID].Tx.Outputs[extendedInput.input.PreviousTxOutIndex].Satoshis
ancestor.Tx.Inputs[extendedInput.vin].PreviousTxSatoshis = sats
}
// check the fees
ok, err := ancestor.Tx.IsFeePaidEnough(opts.feeQuote)
if err != nil || !ok {
return ErrFeePaidNotEnough
}
}
}
}
return nil
}
Expand Down

0 comments on commit 463d2d2

Please sign in to comment.