Skip to content

Commit c3fa7a0

Browse files
author
Jenita
committed
feat: track witness set for transaction
Signed-off-by: Jenita <jkawan@blinklabs.io>
1 parent 6d6530d commit c3fa7a0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

database/plugin/metadata/sqlite/transaction.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ func (d *MetadataStoreSqlite) SetTransaction(
8989
if result.Error != nil {
9090
return fmt.Errorf("create transaction: %w", result.Error)
9191
}
92+
// If ID is still zero (conflict path with SQLite), fetch it by hash
93+
if tmpTx.ID == 0 {
94+
existingTx, err := d.GetTransactionByHash(txHash, txn)
95+
if err != nil {
96+
return fmt.Errorf("failed to fetch transaction ID after upsert: %w", err)
97+
}
98+
if existingTx == nil {
99+
return fmt.Errorf("transaction not found after upsert: %x", txHash)
100+
}
101+
tmpTx.ID = existingTx.ID
102+
}
92103
// Add Inputs to Transaction
93104
for _, input := range tx.Inputs() {
94105
inTxId := input.Id().Bytes()
@@ -278,6 +289,21 @@ func (d *MetadataStoreSqlite) SetTransaction(
278289
}
279290
}
280291
// Extract and save witness set data
292+
// Delete existing witness records to ensure idempotency on retry
293+
if tmpTx.ID != 0 {
294+
if result := txn.Where("transaction_id = ?", tmpTx.ID).Delete(&models.KeyWitness{}); result.Error != nil {
295+
return fmt.Errorf("delete existing key witnesses: %w", result.Error)
296+
}
297+
if result := txn.Where("transaction_id = ?", tmpTx.ID).Delete(&models.Script{}); result.Error != nil {
298+
return fmt.Errorf("delete existing scripts: %w", result.Error)
299+
}
300+
if result := txn.Where("transaction_id = ?", tmpTx.ID).Delete(&models.Redeemer{}); result.Error != nil {
301+
return fmt.Errorf("delete existing redeemers: %w", result.Error)
302+
}
303+
if result := txn.Where("transaction_id = ?", tmpTx.ID).Delete(&models.PlutusData{}); result.Error != nil {
304+
return fmt.Errorf("delete existing plutus data: %w", result.Error)
305+
}
306+
}
281307
if tx.Witnesses() != nil {
282308
ws := tx.Witnesses()
283309

0 commit comments

Comments
 (0)