@@ -44,21 +44,11 @@ func addMinerPayouts(tx *txn, bid types.BlockID, scos []types.SiacoinOutput) err
4444 return nil
4545}
4646
47- func getTransactionID (tx * txn , txnID types.TransactionID ) (result int64 , err error ) {
48- err = tx .QueryRow (`SELECT id FROM transactions WHERE transaction_id = ?` , encode (txnID )).Scan (& result )
49- return
50- }
51-
52- func addMinerFees (tx * txn , txn types.Transaction ) error {
47+ func addMinerFees (tx * txn , dbID int64 , txn types.Transaction ) error {
5348 if len (txn .MinerFees ) == 0 {
5449 return nil
5550 }
5651
57- dbID , err := getTransactionID (tx , txn .ID ())
58- if err != nil {
59- return fmt .Errorf ("addMinerFees: failed to get transaction ID: %w" , err )
60- }
61-
6252 stmt , err := tx .Prepare (`INSERT INTO transaction_miner_fees(transaction_id, transaction_order, fee) VALUES (?, ?, ?)` )
6353 if err != nil {
6454 return fmt .Errorf ("addMinerFees: failed to prepare statement: %w" , err )
@@ -73,16 +63,11 @@ func addMinerFees(tx *txn, txn types.Transaction) error {
7363 return nil
7464}
7565
76- func addArbitraryData (tx * txn , txn types.Transaction ) error {
66+ func addArbitraryData (tx * txn , dbID int64 , txn types.Transaction ) error {
7767 if len (txn .ArbitraryData ) == 0 {
7868 return nil
7969 }
8070
81- dbID , err := getTransactionID (tx , txn .ID ())
82- if err != nil {
83- return fmt .Errorf ("addArbitraryData: failed to get transaction ID: %w" , err )
84- }
85-
8671 stmt , err := tx .Prepare (`INSERT INTO transaction_arbitrary_data(transaction_id, transaction_order, data) VALUES (?, ?, ?)` )
8772 if err != nil {
8873 return fmt .Errorf ("addArbitraryData: failed to prepare statement: %w" , err )
@@ -97,16 +82,11 @@ func addArbitraryData(tx *txn, txn types.Transaction) error {
9782 return nil
9883}
9984
100- func addSignatures (tx * txn , txn types.Transaction ) error {
85+ func addSignatures (tx * txn , dbID int64 , txn types.Transaction ) error {
10186 if len (txn .Signatures ) == 0 {
10287 return nil
10388 }
10489
105- dbID , err := getTransactionID (tx , txn .ID ())
106- if err != nil {
107- return fmt .Errorf ("addSignatures: failed to get transaction ID: %w" , err )
108- }
109-
11090 stmt , err := tx .Prepare (`INSERT INTO transaction_signatures(transaction_id, transaction_order, parent_id, public_key_index, timelock, covered_fields, signature) VALUES (?, ?, ?, ?, ?, ?, ?)` )
11191 if err != nil {
11292 return fmt .Errorf ("addSignatures: failed to prepare statement: %w" , err )
@@ -121,16 +101,11 @@ func addSignatures(tx *txn, txn types.Transaction) error {
121101 return nil
122102}
123103
124- func addSiacoinInputs (tx * txn , txn types.Transaction ) error {
104+ func addSiacoinInputs (tx * txn , dbID int64 , txn types.Transaction ) error {
125105 if len (txn .SiacoinInputs ) == 0 {
126106 return nil
127107 }
128108
129- dbID , err := getTransactionID (tx , txn .ID ())
130- if err != nil {
131- return fmt .Errorf ("addSiacoinInputs: failed to get transaction ID: %w" , err )
132- }
133-
134109 stmt , err := tx .Prepare (`INSERT INTO transaction_siacoin_inputs(transaction_id, transaction_order, unlock_conditions, parent_id) VALUES (?, ?, ?, (SELECT id FROM siacoin_elements WHERE output_id = ?))` )
135110 if err != nil {
136111 return fmt .Errorf ("addSiacoinInputs: failed to prepare statement: %w" , err )
@@ -145,16 +120,11 @@ func addSiacoinInputs(tx *txn, txn types.Transaction) error {
145120 return nil
146121}
147122
148- func addSiacoinOutputs (tx * txn , txn types.Transaction ) error {
123+ func addSiacoinOutputs (tx * txn , dbID int64 , txn types.Transaction ) error {
149124 if len (txn .SiacoinOutputs ) == 0 {
150125 return nil
151126 }
152127
153- dbID , err := getTransactionID (tx , txn .ID ())
154- if err != nil {
155- return fmt .Errorf ("addSiacoinOutputs: failed to get transaction ID: %w" , err )
156- }
157-
158128 stmt , err := tx .Prepare (`INSERT INTO transaction_siacoin_outputs(transaction_id, transaction_order, output_id) VALUES (?, ?, (SELECT id FROM siacoin_elements WHERE output_id = ?))` )
159129 if err != nil {
160130 return fmt .Errorf ("addSiacoinOutputs: failed to prepare statement: %w" , err )
@@ -169,16 +139,11 @@ func addSiacoinOutputs(tx *txn, txn types.Transaction) error {
169139 return nil
170140}
171141
172- func addSiafundInputs (tx * txn , txn types.Transaction ) error {
142+ func addSiafundInputs (tx * txn , dbID int64 , txn types.Transaction ) error {
173143 if len (txn .SiafundInputs ) == 0 {
174144 return nil
175145 }
176146
177- dbID , err := getTransactionID (tx , txn .ID ())
178- if err != nil {
179- return fmt .Errorf ("addSiafundInputs: failed to get transaction ID: %w" , err )
180- }
181-
182147 stmt , err := tx .Prepare (`INSERT INTO transaction_siafund_inputs(transaction_id, transaction_order, unlock_conditions, claim_address, parent_id) VALUES (?, ?, ?, ?, (SELECT id FROM siafund_elements WHERE output_id = ?))` )
183148 if err != nil {
184149 return fmt .Errorf ("addSiafundInputs: failed to prepare statement: %w" , err )
@@ -194,16 +159,11 @@ func addSiafundInputs(tx *txn, txn types.Transaction) error {
194159 return nil
195160}
196161
197- func addSiafundOutputs (tx * txn , txn types.Transaction ) error {
162+ func addSiafundOutputs (tx * txn , dbID int64 , txn types.Transaction ) error {
198163 if len (txn .SiafundOutputs ) == 0 {
199164 return nil
200165 }
201166
202- dbID , err := getTransactionID (tx , txn .ID ())
203- if err != nil {
204- return fmt .Errorf ("addSiafundOutputs: failed to get transaction ID: %w" , err )
205- }
206-
207167 stmt , err := tx .Prepare (`INSERT INTO transaction_siafund_outputs(transaction_id, transaction_order, output_id) VALUES (?, ?, (SELECT id FROM siafund_elements WHERE output_id = ?))` )
208168 if err != nil {
209169 return fmt .Errorf ("addSiafundOutputs: failed to prepare statement: %w" , err )
@@ -218,16 +178,11 @@ func addSiafundOutputs(tx *txn, txn types.Transaction) error {
218178 return nil
219179}
220180
221- func addFileContracts (tx * txn , txn types.Transaction ) error {
181+ func addFileContracts (tx * txn , dbID int64 , txn types.Transaction ) error {
222182 if len (txn .FileContracts ) == 0 {
223183 return nil
224184 }
225185
226- dbID , err := getTransactionID (tx , txn .ID ())
227- if err != nil {
228- return fmt .Errorf ("addFileContracts: failed to get transaction ID: %w" , err )
229- }
230-
231186 stmt , err := tx .Prepare (`INSERT INTO transaction_file_contracts(transaction_id, transaction_order, contract_id) VALUES (?, ?, (SELECT id FROM file_contract_elements WHERE contract_id = ? AND revision_number = ?))` )
232187 if err != nil {
233188 return fmt .Errorf ("addFileContracts: failed to prepare statement: %w" , err )
@@ -242,16 +197,11 @@ func addFileContracts(tx *txn, txn types.Transaction) error {
242197 return nil
243198}
244199
245- func addFileContractRevisions (tx * txn , txn types.Transaction ) error {
200+ func addFileContractRevisions (tx * txn , dbID int64 , txn types.Transaction ) error {
246201 if len (txn .FileContractRevisions ) == 0 {
247202 return nil
248203 }
249204
250- dbID , err := getTransactionID (tx , txn .ID ())
251- if err != nil {
252- return fmt .Errorf ("addFileContractRevisions: failed to get transaction ID: %w" , err )
253- }
254-
255205 stmt , err := tx .Prepare (`INSERT INTO transaction_file_contract_revisions(transaction_id, transaction_order, parent_id, unlock_conditions, contract_id) VALUES (?, ?, ?, ?, (SELECT id FROM file_contract_elements WHERE contract_id = ? AND revision_number = ?))` )
256206 if err != nil {
257207 return fmt .Errorf ("addFileContractRevisions: failed to prepare statement: %w" , err )
@@ -268,16 +218,11 @@ func addFileContractRevisions(tx *txn, txn types.Transaction) error {
268218 return nil
269219}
270220
271- func addStorageProofs (tx * txn , txn types.Transaction ) error {
221+ func addStorageProofs (tx * txn , dbID int64 , txn types.Transaction ) error {
272222 if len (txn .StorageProofs ) == 0 {
273223 return nil
274224 }
275225
276- dbID , err := getTransactionID (tx , txn .ID ())
277- if err != nil {
278- return fmt .Errorf ("addStorageProofs: failed to get transaction ID: %w" , err )
279- }
280-
281226 stmt , err := tx .Prepare (`INSERT INTO transaction_storage_proofs(transaction_id, transaction_order, parent_id, leaf, proof) VALUES (?, ?, ?, ?, ?)` )
282227 if err != nil {
283228 return fmt .Errorf ("addStorageProofs: failed to prepare statement: %w" , err )
@@ -355,6 +300,11 @@ func addTransactions(tx *txn, bid types.BlockID, txns []types.Transaction) (map[
355300}
356301
357302func addTransactionFields (tx * txn , txns []types.Transaction , txnExist map [types.TransactionID ]bool ) error {
303+ stmt , err := tx .Prepare (`SELECT id FROM transactions WHERE transaction_id = ?` )
304+ if err != nil {
305+ return fmt .Errorf ("failed to prepare transaction ID statement: %w" , err )
306+ }
307+
358308 for _ , txn := range txns {
359309 txnID := txn .ID ()
360310 exist , ok := txnExist [txnID ]
@@ -370,25 +320,30 @@ func addTransactionFields(tx *txn, txns []types.Transaction, txnExist map[types.
370320 // multiple of the same transaction in a block
371321 txnExist [txnID ] = true
372322
373- if err := addMinerFees (tx , txn ); err != nil {
323+ var dbID int64
324+ if err := stmt .QueryRow (encode (txnID )).Scan (& dbID ); err != nil {
325+ return fmt .Errorf ("failed to scan for transaction ID: %w" , err )
326+ }
327+
328+ if err := addMinerFees (tx , dbID , txn ); err != nil {
374329 return fmt .Errorf ("failed to add miner fees: %w" , err )
375- } else if err := addArbitraryData (tx , txn ); err != nil {
330+ } else if err := addArbitraryData (tx , dbID , txn ); err != nil {
376331 return fmt .Errorf ("failed to add arbitrary data: %w" , err )
377- } else if err := addSignatures (tx , txn ); err != nil {
332+ } else if err := addSignatures (tx , dbID , txn ); err != nil {
378333 return fmt .Errorf ("failed to add signatures: %w" , err )
379- } else if err := addSiacoinInputs (tx , txn ); err != nil {
334+ } else if err := addSiacoinInputs (tx , dbID , txn ); err != nil {
380335 return fmt .Errorf ("failed to add siacoin inputs: %w" , err )
381- } else if err := addSiacoinOutputs (tx , txn ); err != nil {
336+ } else if err := addSiacoinOutputs (tx , dbID , txn ); err != nil {
382337 return fmt .Errorf ("failed to add siacoin outputs: %w" , err )
383- } else if err := addSiafundInputs (tx , txn ); err != nil {
338+ } else if err := addSiafundInputs (tx , dbID , txn ); err != nil {
384339 return fmt .Errorf ("failed to add siafund inputs: %w" , err )
385- } else if err := addSiafundOutputs (tx , txn ); err != nil {
340+ } else if err := addSiafundOutputs (tx , dbID , txn ); err != nil {
386341 return fmt .Errorf ("failed to add siafund outputs: %w" , err )
387- } else if err := addFileContracts (tx , txn ); err != nil {
342+ } else if err := addFileContracts (tx , dbID , txn ); err != nil {
388343 return fmt .Errorf ("failed to add file contract: %w" , err )
389- } else if err := addFileContractRevisions (tx , txn ); err != nil {
344+ } else if err := addFileContractRevisions (tx , dbID , txn ); err != nil {
390345 return fmt .Errorf ("failed to add file contract revisions: %w" , err )
391- } else if err := addStorageProofs (tx , txn ); err != nil {
346+ } else if err := addStorageProofs (tx , dbID , txn ); err != nil {
392347 return fmt .Errorf ("failed to add storage proofs: %w" , err )
393348 }
394349 }
0 commit comments