Skip to content

Commit b2195f0

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 8aa9d1e commit b2195f0

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

tapdb/supply_commit.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,50 @@ 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+
outpointBytes, err := encodeOutpoint(
1443+
txIn.PreviousOutPoint,
1444+
)
1445+
if err != nil {
1446+
return fmt.Errorf("failed to encode "+
1447+
"outpoint %v: %w",
1448+
txIn.PreviousOutPoint, err)
1449+
}
1450+
1451+
log.Infof("Attempting to mark outpoint as "+
1452+
"spent: %v (hash=%x, index=%d)",
1453+
txIn.PreviousOutPoint,
1454+
txIn.PreviousOutPoint.Hash[:],
1455+
txIn.PreviousOutPoint.Index)
1456+
1457+
// Mark this specific pre-commitment as spent.
1458+
err = db.MarkPreCommitmentSpentByOutpoint(ctx,
1459+
sqlc.MarkPreCommitmentSpentByOutpointParams{
1460+
SpentByCommitID: sqlInt64(
1461+
newCommitmentID,
1462+
),
1463+
Outpoint: outpointBytes,
1464+
},
1465+
)
1466+
if err != nil {
1467+
// It's OK if this outpoint doesn't exist in our
1468+
// table - it might be an old commitment output
1469+
// or a wallet input for fees. We only care
1470+
// about marking actual pre-commitments as
1471+
// spent.
1472+
log.Debugf("Could not mark outpoint %v as "+
1473+
"spent (may not be a "+
1474+
"pre-commitment): %v",
1475+
txIn.PreviousOutPoint, err)
1476+
} else {
1477+
log.Infof("Successfully marked outpoint "+
1478+
"as spent: %v", txIn.PreviousOutPoint)
1479+
}
1480+
}
1481+
14381482
// To finish up our book keeping, we'll now finalize the state
14391483
// transition on disk.
14401484
err = db.FinalizeSupplyCommitTransition(ctx, transitionID)

0 commit comments

Comments
 (0)