From ab66e8d9ee9de71ff0a45193887dbad545e4e67c Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Tue, 9 Jan 2024 14:06:25 -0500 Subject: [PATCH] add transaction method to store --- explorer/explorer.go | 6 ++++++ persist/sqlite/query.go | 11 +++++++++++ persist/sqlite/txn.go | 1 + 3 files changed, 18 insertions(+) diff --git a/explorer/explorer.go b/explorer/explorer.go index 4865cd20..256f2e50 100644 --- a/explorer/explorer.go +++ b/explorer/explorer.go @@ -13,6 +13,7 @@ type Store interface { Tip() (types.ChainIndex, error) BlockByID(id types.BlockID) (types.Block, error) BlockByHeight(height uint64) (types.Block, error) + Transaction(id types.TransactionID) (types.Transaction, error) } // Explorer implements a Sia explorer. @@ -39,3 +40,8 @@ func (e *Explorer) BlockByID(id types.BlockID) (types.Block, error) { func (e *Explorer) BlockByHeight(height uint64) (types.Block, error) { return e.s.BlockByHeight(height) } + +// Transaction returns the transaction with the specified ID. +func (e *Explorer) Transaction(id types.TransactionID) (types.Transaction, error) { + return e.s.Transaction(id) +} diff --git a/persist/sqlite/query.go b/persist/sqlite/query.go index e2e716da..49617eda 100644 --- a/persist/sqlite/query.go +++ b/persist/sqlite/query.go @@ -144,3 +144,14 @@ func (s *Store) BlockByHeight(height uint64) (result types.Block, err error) { result, err = s.BlockByID(bid) return } + +// Transaction implements explorer.Store. +func (s *Store) Transaction(id types.TransactionID) (result types.Transaction, err error) { + var txnID int64 + if err = s.queryRow("SELECT id FROM transactions WHERE transaction_id = ?", encode(id)).Scan(&txnID); err != nil { + return + } + + result, err = s.transactionByID(txnID) + return +} diff --git a/persist/sqlite/txn.go b/persist/sqlite/txn.go index 7fcf6d2c..cde7cf35 100644 --- a/persist/sqlite/txn.go +++ b/persist/sqlite/txn.go @@ -58,6 +58,7 @@ func (s *Store) addTransactions(tx txn, bid types.BlockID, txns []types.Transact if err != nil { return err } + if _, err := tx.Exec("INSERT INTO block_transactions(block_id, transaction_id, block_order) VALUES (?, ?, ?)", encode(bid), txnID, i); err != nil { return err } else if err := s.addArbitraryData(tx, txnID, txn); err != nil {