Skip to content

Commit 8af377b

Browse files
committed
tapdb: make sure we actually mark pre commitments as spent
In this commit, we fix a bug where we wouldn't actually mark pre commitmetns as spent. We'll do so now, which makes sure that when we fetch the pre commits for a later batch, we aren't trying to include one that might be double spent.
1 parent 6157831 commit 8af377b

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tapdb/supply_commit.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,51 @@ func (s *SupplyCommitMachine) ApplyStateTransition(
14351435
"confirmation: %w", err)
14361436
}
14371437

1438+
// Mark the specific pre-commitments that were spent in this
1439+
// transaction as spent by the new commitment. We identify them
1440+
// by looking at the transaction inputs.
1441+
for _, txIn := range newCommitment.Txn.TxIn {
1442+
// Encode the outpoint to match the format stored in the DB.
1443+
outpointBytes, err := encodeOutpoint(
1444+
txIn.PreviousOutPoint,
1445+
)
1446+
if err != nil {
1447+
return fmt.Errorf("failed to encode "+
1448+
"outpoint %v: %w",
1449+
txIn.PreviousOutPoint, err)
1450+
}
1451+
1452+
log.Infof("Attempting to mark outpoint as "+
1453+
"spent: %v (hash=%x, index=%d)",
1454+
txIn.PreviousOutPoint,
1455+
txIn.PreviousOutPoint.Hash[:],
1456+
txIn.PreviousOutPoint.Index)
1457+
1458+
// Mark this specific pre-commitment as spent.
1459+
err = db.MarkPreCommitmentSpentByOutpoint(ctx,
1460+
sqlc.MarkPreCommitmentSpentByOutpointParams{
1461+
SpentByCommitID: sqlInt64(
1462+
newCommitmentID,
1463+
),
1464+
Outpoint: outpointBytes,
1465+
},
1466+
)
1467+
if err != nil {
1468+
// It's OK if this outpoint doesn't exist in our
1469+
// table - it might be an old commitment output
1470+
// or a wallet input for fees. We only care
1471+
// about marking actual pre-commitments as
1472+
// spent.
1473+
log.Debugf("Could not mark outpoint %v as "+
1474+
"spent (may not be a "+
1475+
"pre-commitment): %v",
1476+
txIn.PreviousOutPoint, err)
1477+
} else {
1478+
log.Infof("Successfully marked outpoint "+
1479+
"as spent: %v", txIn.PreviousOutPoint)
1480+
}
1481+
}
1482+
14381483
// To finish up our book keeping, we'll now finalize the state
14391484
// transition on disk.
14401485
err = db.FinalizeSupplyCommitTransition(ctx, transitionID)

0 commit comments

Comments
 (0)