Skip to content

Commit

Permalink
tracker: record burn script along with loss details
Browse files Browse the repository at this point in the history
  • Loading branch information
RaghavSood committed Jun 7, 2024
1 parent 6823002 commit 6cdc82d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion storage/sqlite/loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func scanLosses(rows *sql.Rows) ([]types.Loss, error) {
var losses []types.Loss
for rows.Next() {
var loss types.Loss
err := rows.Scan(&loss.ID, &loss.TxID, &loss.BlockHash, &loss.BlockHeight, &loss.Vout, &loss.Amount, &loss.CreatedAt)
err := rows.Scan(&loss.ID, &loss.TxID, &loss.BlockHash, &loss.BlockHeight, &loss.Vout, &loss.Amount, &loss.CreatedAt, &loss.BurnScript)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE losses ADD COLUMN burn_script TEXT DEFAULT '';
CREATE INDEX losses_burn_script_idx ON losses (burn_script);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP INDEX losses_burn_script_idx;
ALTER TABLE losses DROP COLUMN burn_script;
-- +goose StatementEnd
4 changes: 2 additions & 2 deletions storage/sqlite/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (d *SqliteBackend) RecordBlockIndexResults(block types.Block, txoutset type

// Insert the losses records into the losses table
for _, loss := range losses {
_, err = tx.Exec("INSERT INTO losses (tx_id, block_hash, block_height, vout, amount) VALUES (?, ?, ?, ?, ?) ON CONFLICT DO NOTHING", loss.TxID, loss.BlockHash, loss.BlockHeight, loss.Vout, loss.Amount)
_, err = tx.Exec("INSERT INTO losses (tx_id, block_hash, block_height, vout, amount, burn_script) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT (tx_id, vout) DO UPDATE SET block_hash=excluded.block_hash, block_height=excluded.block_height, vout=excluded.vout, amount=excluded.amount, burn_script=excluded.burn_script", loss.TxID, loss.BlockHash, loss.BlockHeight, loss.Vout, loss.Amount, loss.BurnScript)
if err != nil {
tx.Rollback()
return fmt.Errorf("failed to insert loss record: %v", err)
Expand Down Expand Up @@ -74,7 +74,7 @@ func (d *SqliteBackend) RecordTransactionIndexResults(losses []types.Loss, trans

// Insert the losses records into the losses table
for _, loss := range losses {
_, err = tx.Exec("INSERT INTO losses (tx_id, block_hash, block_height, vout, amount) VALUES (?, ?, ?, ?, ?) ON CONFLICT (tx_id, vout) DO NOTHING", loss.TxID, loss.BlockHash, loss.BlockHeight, loss.Vout, loss.Amount)
_, err = tx.Exec("INSERT INTO losses (tx_id, block_hash, block_height, vout, amount, burn_script) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT (tx_id, vout) DO UPDATE SET block_hash=excluded.block_hash, block_height=excluded.block_height, vout=excluded.vout, amount=excluded.amount, burn_script=excluded.burn_script", loss.TxID, loss.BlockHash, loss.BlockHeight, loss.Vout, loss.Amount, loss.BurnScript)
if err != nil {
tx.Rollback()
return fmt.Errorf("failed to insert loss record: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ func (t *Tracker) scanTransactions(blockhash string, blockHeight int64, transact
BlockHeight: blockHeight,
Vout: vout.N,
Amount: types.FromBTCFloat64(vout.Value),
BurnScript: vout.ScriptPubKey.Hex,
})

atLeastOneBurn = true
Expand All @@ -359,6 +360,7 @@ func (t *Tracker) scanTransactions(blockhash string, blockHeight int64, transact
BlockHeight: blockHeight,
Vout: vout.N,
Amount: types.FromBTCFloat64(vout.Value),
BurnScript: vout.ScriptPubKey.Hex,
})

atLeastOneBurn = true
Expand Down
1 change: 1 addition & 0 deletions types/loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type Loss struct {
Vout int
Amount *BigInt
CreatedAt time.Time
BurnScript string
}

0 comments on commit 6cdc82d

Please sign in to comment.