Skip to content

Commit 894642a

Browse files
committed
pass only necessary fields to transaction field functions and reuse result of txn.ID() where possible
1 parent c726909 commit 894642a

File tree

2 files changed

+55
-60
lines changed

2 files changed

+55
-60
lines changed

persist/sqlite/consensus.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ func addMinerPayouts(tx *txn, bid types.BlockID, scos []types.SiacoinOutput) err
4444
return nil
4545
}
4646

47-
func addMinerFees(tx *txn, dbID int64, txn types.Transaction) error {
48-
if len(txn.MinerFees) == 0 {
47+
func addMinerFees(tx *txn, dbID int64, minerFees []types.Currency) error {
48+
if len(minerFees) == 0 {
4949
return nil
5050
}
5151

@@ -55,16 +55,16 @@ func addMinerFees(tx *txn, dbID int64, txn types.Transaction) error {
5555
}
5656
defer stmt.Close()
5757

58-
for i, fee := range txn.MinerFees {
58+
for i, fee := range minerFees {
5959
if _, err := stmt.Exec(dbID, i, encode(fee)); err != nil {
6060
return fmt.Errorf("addMinerFees: failed to execute statement: %w", err)
6161
}
6262
}
6363
return nil
6464
}
6565

66-
func addArbitraryData(tx *txn, dbID int64, txn types.Transaction) error {
67-
if len(txn.ArbitraryData) == 0 {
66+
func addArbitraryData(tx *txn, dbID int64, arbitraryData [][]byte) error {
67+
if len(arbitraryData) == 0 {
6868
return nil
6969
}
7070

@@ -74,16 +74,16 @@ func addArbitraryData(tx *txn, dbID int64, txn types.Transaction) error {
7474
}
7575
defer stmt.Close()
7676

77-
for i, arbitraryData := range txn.ArbitraryData {
78-
if _, err := stmt.Exec(dbID, i, arbitraryData); err != nil {
77+
for i, arb := range arbitraryData {
78+
if _, err := stmt.Exec(dbID, i, arb); err != nil {
7979
return fmt.Errorf("addArbitraryData: failed to execute statement: %w", err)
8080
}
8181
}
8282
return nil
8383
}
8484

85-
func addSignatures(tx *txn, dbID int64, txn types.Transaction) error {
86-
if len(txn.Signatures) == 0 {
85+
func addSignatures(tx *txn, dbID int64, signatures []types.TransactionSignature) error {
86+
if len(signatures) == 0 {
8787
return nil
8888
}
8989

@@ -93,16 +93,16 @@ func addSignatures(tx *txn, dbID int64, txn types.Transaction) error {
9393
}
9494
defer stmt.Close()
9595

96-
for i, sig := range txn.Signatures {
96+
for i, sig := range signatures {
9797
if _, err := stmt.Exec(dbID, i, encode(sig.ParentID), sig.PublicKeyIndex, encode(sig.Timelock), encode(sig.CoveredFields), sig.Signature); err != nil {
9898
return fmt.Errorf("addSignatures: failed to execute statement: %w", err)
9999
}
100100
}
101101
return nil
102102
}
103103

104-
func addSiacoinInputs(tx *txn, dbID int64, txn types.Transaction) error {
105-
if len(txn.SiacoinInputs) == 0 {
104+
func addSiacoinInputs(tx *txn, dbID int64, siacoinInputs []types.SiacoinInput) error {
105+
if len(siacoinInputs) == 0 {
106106
return nil
107107
}
108108

@@ -112,7 +112,7 @@ func addSiacoinInputs(tx *txn, dbID int64, txn types.Transaction) error {
112112
}
113113
defer stmt.Close()
114114

115-
for i, sci := range txn.SiacoinInputs {
115+
for i, sci := range siacoinInputs {
116116
if _, err := stmt.Exec(dbID, i, encode(sci.UnlockConditions), encode(sci.ParentID)); err != nil {
117117
return fmt.Errorf("addSiacoinInputs: failed to execute statement: %w", err)
118118
}
@@ -139,8 +139,8 @@ func addSiacoinOutputs(tx *txn, dbID int64, txn types.Transaction) error {
139139
return nil
140140
}
141141

142-
func addSiafundInputs(tx *txn, dbID int64, txn types.Transaction) error {
143-
if len(txn.SiafundInputs) == 0 {
142+
func addSiafundInputs(tx *txn, dbID int64, siafundInputs []types.SiafundInput) error {
143+
if len(siafundInputs) == 0 {
144144
return nil
145145
}
146146

@@ -150,7 +150,7 @@ func addSiafundInputs(tx *txn, dbID int64, txn types.Transaction) error {
150150
}
151151
defer stmt.Close()
152152

153-
for i, sfi := range txn.SiafundInputs {
153+
for i, sfi := range siafundInputs {
154154
if _, err := stmt.Exec(dbID, i, encode(sfi.UnlockConditions), encode(sfi.ClaimAddress), encode(sfi.ParentID)); err != nil {
155155
return fmt.Errorf("addSiafundInputs: failed to execute statement: %w", err)
156156
}
@@ -197,8 +197,8 @@ func addFileContracts(tx *txn, dbID int64, txn types.Transaction) error {
197197
return nil
198198
}
199199

200-
func addFileContractRevisions(tx *txn, dbID int64, txn types.Transaction) error {
201-
if len(txn.FileContractRevisions) == 0 {
200+
func addFileContractRevisions(tx *txn, dbID int64, fileContractRevisions []types.FileContractRevision) error {
201+
if len(fileContractRevisions) == 0 {
202202
return nil
203203
}
204204

@@ -208,8 +208,7 @@ func addFileContractRevisions(tx *txn, dbID int64, txn types.Transaction) error
208208
}
209209
defer stmt.Close()
210210

211-
for i := range txn.FileContractRevisions {
212-
fcr := &txn.FileContractRevisions[i]
211+
for i, fcr := range fileContractRevisions {
213212
if _, err := stmt.Exec(dbID, i, encode(fcr.ParentID), encode(fcr.UnlockConditions), encode(fcr.ParentID), encode(fcr.FileContract.RevisionNumber)); err != nil {
214213
return fmt.Errorf("addFileContractRevisions: failed to execute statement: %w", err)
215214
}
@@ -218,8 +217,8 @@ func addFileContractRevisions(tx *txn, dbID int64, txn types.Transaction) error
218217
return nil
219218
}
220219

221-
func addStorageProofs(tx *txn, dbID int64, txn types.Transaction) error {
222-
if len(txn.StorageProofs) == 0 {
220+
func addStorageProofs(tx *txn, dbID int64, storageProofs []types.StorageProof) error {
221+
if len(storageProofs) == 0 {
223222
return nil
224223
}
225224

@@ -229,7 +228,7 @@ func addStorageProofs(tx *txn, dbID int64, txn types.Transaction) error {
229228
}
230229
defer stmt.Close()
231230

232-
for i, proof := range txn.StorageProofs {
231+
for i, proof := range storageProofs {
233232
if _, err := stmt.Exec(dbID, i, encode(proof.ParentID), proof.Leaf[:], encode(proof.Proof)); err != nil {
234233
return fmt.Errorf("addStorageProofs: failed to execute statement: %w", err)
235234
}
@@ -325,25 +324,25 @@ func addTransactionFields(tx *txn, txns []types.Transaction, txnExist map[types.
325324
return fmt.Errorf("failed to scan for transaction ID: %w", err)
326325
}
327326

328-
if err := addMinerFees(tx, dbID, txn); err != nil {
327+
if err := addMinerFees(tx, dbID, txn.MinerFees); err != nil {
329328
return fmt.Errorf("failed to add miner fees: %w", err)
330-
} else if err := addArbitraryData(tx, dbID, txn); err != nil {
329+
} else if err := addArbitraryData(tx, dbID, txn.ArbitraryData); err != nil {
331330
return fmt.Errorf("failed to add arbitrary data: %w", err)
332-
} else if err := addSignatures(tx, dbID, txn); err != nil {
331+
} else if err := addSignatures(tx, dbID, txn.Signatures); err != nil {
333332
return fmt.Errorf("failed to add signatures: %w", err)
334-
} else if err := addSiacoinInputs(tx, dbID, txn); err != nil {
333+
} else if err := addSiacoinInputs(tx, dbID, txn.SiacoinInputs); err != nil {
335334
return fmt.Errorf("failed to add siacoin inputs: %w", err)
336335
} else if err := addSiacoinOutputs(tx, dbID, txn); err != nil {
337336
return fmt.Errorf("failed to add siacoin outputs: %w", err)
338-
} else if err := addSiafundInputs(tx, dbID, txn); err != nil {
337+
} else if err := addSiafundInputs(tx, dbID, txn.SiafundInputs); err != nil {
339338
return fmt.Errorf("failed to add siafund inputs: %w", err)
340339
} else if err := addSiafundOutputs(tx, dbID, txn); err != nil {
341340
return fmt.Errorf("failed to add siafund outputs: %w", err)
342341
} else if err := addFileContracts(tx, dbID, txn); err != nil {
343342
return fmt.Errorf("failed to add file contract: %w", err)
344-
} else if err := addFileContractRevisions(tx, dbID, txn); err != nil {
343+
} else if err := addFileContractRevisions(tx, dbID, txn.FileContractRevisions); err != nil {
345344
return fmt.Errorf("failed to add file contract revisions: %w", err)
346-
} else if err := addStorageProofs(tx, dbID, txn); err != nil {
345+
} else if err := addStorageProofs(tx, dbID, txn.StorageProofs); err != nil {
347346
return fmt.Errorf("failed to add storage proofs: %w", err)
348347
}
349348
}

persist/sqlite/v2consensus.go

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ func updateV2FileContractIndices(tx *txn, revert bool, index types.ChainIndex, f
294294
return nil
295295
}
296296

297-
func addV2SiacoinInputs(tx *txn, dbID int64, txn types.V2Transaction) error {
298-
if len(txn.SiacoinInputs) == 0 {
297+
func addV2SiacoinInputs(tx *txn, dbID int64, siacoinInputs []types.V2SiacoinInput) error {
298+
if len(siacoinInputs) == 0 {
299299
return nil
300300
}
301301

@@ -305,20 +305,19 @@ func addV2SiacoinInputs(tx *txn, dbID int64, txn types.V2Transaction) error {
305305
}
306306
defer stmt.Close()
307307

308-
for i, sci := range txn.SiacoinInputs {
308+
for i, sci := range siacoinInputs {
309309
if _, err := stmt.Exec(dbID, i, encode(sci.SatisfiedPolicy), encode(types.SiacoinOutputID(sci.Parent.ID))); err != nil {
310310
return fmt.Errorf("addV2SiacoinInputs: failed to execute statement: %w", err)
311311
}
312312
}
313313
return nil
314314
}
315315

316-
func addV2SiacoinOutputs(tx *txn, dbID int64, txn types.V2Transaction) error {
316+
func addV2SiacoinOutputs(tx *txn, dbID int64, txnID types.TransactionID, txn types.V2Transaction) error {
317317
if len(txn.SiacoinOutputs) == 0 {
318318
return nil
319319
}
320320

321-
txnID := txn.ID()
322321
stmt, err := tx.Prepare(`INSERT INTO v2_transaction_siacoin_outputs(transaction_id, transaction_order, output_id) VALUES (?, ?, (SELECT id FROM siacoin_elements WHERE output_id = ?))`)
323322
if err != nil {
324323
return fmt.Errorf("addV2SiacoinOutputs: failed to prepare statement: %w", err)
@@ -333,8 +332,8 @@ func addV2SiacoinOutputs(tx *txn, dbID int64, txn types.V2Transaction) error {
333332
return nil
334333
}
335334

336-
func addV2SiafundInputs(tx *txn, dbID int64, txn types.V2Transaction) error {
337-
if len(txn.SiafundInputs) == 0 {
335+
func addV2SiafundInputs(tx *txn, dbID int64, siafundInputs []types.V2SiafundInput) error {
336+
if len(siafundInputs) == 0 {
338337
return nil
339338
}
340339

@@ -344,21 +343,19 @@ func addV2SiafundInputs(tx *txn, dbID int64, txn types.V2Transaction) error {
344343
}
345344
defer stmt.Close()
346345

347-
for i, sfi := range txn.SiafundInputs {
346+
for i, sfi := range siafundInputs {
348347
if _, err := stmt.Exec(dbID, i, encode(sfi.ClaimAddress), encode(sfi.SatisfiedPolicy), encode(types.SiafundOutputID(sfi.Parent.ID))); err != nil {
349348
return fmt.Errorf("addV2SiafundInputs: failed to execute statement: %w", err)
350349
}
351350
}
352351
return nil
353352
}
354353

355-
func addV2SiafundOutputs(tx *txn, dbID int64, txn types.V2Transaction) error {
354+
func addV2SiafundOutputs(tx *txn, dbID int64, txnID types.TransactionID, txn types.V2Transaction) error {
356355
if len(txn.SiafundOutputs) == 0 {
357356
return nil
358357
}
359358

360-
txnID := txn.ID()
361-
362359
stmt, err := tx.Prepare(`INSERT INTO v2_transaction_siafund_outputs(transaction_id, transaction_order, output_id) VALUES (?, ?, (SELECT id FROM siafund_elements WHERE output_id = ?))`)
363360
if err != nil {
364361
return fmt.Errorf("addV2SiafundOutputs: failed to prepare statement: %w", err)
@@ -373,12 +370,11 @@ func addV2SiafundOutputs(tx *txn, dbID int64, txn types.V2Transaction) error {
373370
return nil
374371
}
375372

376-
func addV2FileContracts(tx *txn, dbID int64, txn types.V2Transaction) error {
373+
func addV2FileContracts(tx *txn, dbID int64, txnID types.TransactionID, txn types.V2Transaction) error {
377374
if len(txn.FileContracts) == 0 {
378375
return nil
379376
}
380377

381-
txnID := txn.ID()
382378
stmt, err := tx.Prepare(`INSERT INTO v2_transaction_file_contracts(transaction_id, transaction_order, contract_id) VALUES (?, ?, (SELECT id FROM v2_file_contract_elements WHERE contract_id = ? AND revision_number = ?))`)
383379
if err != nil {
384380
return fmt.Errorf("addV2FileContracts: failed to prepare statement: %w", err)
@@ -393,8 +389,8 @@ func addV2FileContracts(tx *txn, dbID int64, txn types.V2Transaction) error {
393389
return nil
394390
}
395391

396-
func addV2FileContractRevisions(tx *txn, dbID int64, txn types.V2Transaction) error {
397-
if len(txn.FileContractRevisions) == 0 {
392+
func addV2FileContractRevisions(tx *txn, dbID int64, fileContractRevisions []types.V2FileContractRevision) error {
393+
if len(fileContractRevisions) == 0 {
398394
return nil
399395
}
400396

@@ -404,16 +400,16 @@ func addV2FileContractRevisions(tx *txn, dbID int64, txn types.V2Transaction) er
404400
}
405401
defer stmt.Close()
406402

407-
for i, fcr := range txn.FileContractRevisions {
403+
for i, fcr := range fileContractRevisions {
408404
if _, err := stmt.Exec(dbID, i, encode(types.FileContractID(fcr.Parent.ID)), encode(fcr.Parent.V2FileContract.RevisionNumber), encode(types.FileContractID(fcr.Parent.ID)), encode(fcr.Revision.RevisionNumber)); err != nil {
409405
return fmt.Errorf("addV2FileContractRevisions: failed to execute statement: %w", err)
410406
}
411407
}
412408
return nil
413409
}
414410

415-
func addV2FileContractResolutions(tx *txn, dbID int64, txn types.V2Transaction) error {
416-
if len(txn.FileContractResolutions) == 0 {
411+
func addV2FileContractResolutions(tx *txn, dbID int64, fileContractResolutions []types.V2FileContractResolution) error {
412+
if len(fileContractResolutions) == 0 {
417413
return nil
418414
}
419415

@@ -435,7 +431,7 @@ func addV2FileContractResolutions(tx *txn, dbID int64, txn types.V2Transaction)
435431
}
436432
defer expirationStmt.Close()
437433

438-
for i, fcr := range txn.FileContractResolutions {
434+
for i, fcr := range fileContractResolutions {
439435
resolutionType := explorer.V2ResolutionType(fcr.Resolution)
440436
switch v := fcr.Resolution.(type) {
441437
case *types.V2FileContractRenewal:
@@ -455,8 +451,8 @@ func addV2FileContractResolutions(tx *txn, dbID int64, txn types.V2Transaction)
455451
return nil
456452
}
457453

458-
func addV2Attestations(tx *txn, dbID int64, txn types.V2Transaction) error {
459-
if len(txn.Attestations) == 0 {
454+
func addV2Attestations(tx *txn, dbID int64, attestations []types.Attestation) error {
455+
if len(attestations) == 0 {
460456
return nil
461457
}
462458

@@ -466,7 +462,7 @@ func addV2Attestations(tx *txn, dbID int64, txn types.V2Transaction) error {
466462
}
467463
defer stmt.Close()
468464

469-
for i, attestation := range txn.Attestations {
465+
for i, attestation := range attestations {
470466
if _, err := stmt.Exec(dbID, i, encode(attestation.PublicKey), attestation.Key, attestation.Value, encode(attestation.Signature)); err != nil {
471467
return fmt.Errorf("addV2Attestations: failed to execute statement: %w", err)
472468
}
@@ -500,21 +496,21 @@ func addV2TransactionFields(tx *txn, txns []types.V2Transaction, txnSeen map[typ
500496
return fmt.Errorf("failed to scan for transaction ID: %w", err)
501497
}
502498

503-
if err := addV2Attestations(tx, dbID, txn); err != nil {
499+
if err := addV2Attestations(tx, dbID, txn.Attestations); err != nil {
504500
return fmt.Errorf("failed to add attestations: %w", err)
505-
} else if err := addV2SiacoinInputs(tx, dbID, txn); err != nil {
501+
} else if err := addV2SiacoinInputs(tx, dbID, txn.SiacoinInputs); err != nil {
506502
return fmt.Errorf("failed to add siacoin inputs: %w", err)
507-
} else if err := addV2SiacoinOutputs(tx, dbID, txn); err != nil {
503+
} else if err := addV2SiacoinOutputs(tx, dbID, txnID, txn); err != nil {
508504
return fmt.Errorf("failed to add siacoin outputs: %w", err)
509-
} else if err := addV2SiafundInputs(tx, dbID, txn); err != nil {
505+
} else if err := addV2SiafundInputs(tx, dbID, txn.SiafundInputs); err != nil {
510506
return fmt.Errorf("failed to add siafund inputs: %w", err)
511-
} else if err := addV2SiafundOutputs(tx, dbID, txn); err != nil {
507+
} else if err := addV2SiafundOutputs(tx, dbID, txnID, txn); err != nil {
512508
return fmt.Errorf("failed to add siafund outputs: %w", err)
513-
} else if err := addV2FileContracts(tx, dbID, txn); err != nil {
509+
} else if err := addV2FileContracts(tx, dbID, txnID, txn); err != nil {
514510
return fmt.Errorf("failed to add file contracts: %w", err)
515-
} else if err := addV2FileContractRevisions(tx, dbID, txn); err != nil {
511+
} else if err := addV2FileContractRevisions(tx, dbID, txn.FileContractRevisions); err != nil {
516512
return fmt.Errorf("failed to add file contract revisions: %w", err)
517-
} else if err := addV2FileContractResolutions(tx, dbID, txn); err != nil {
513+
} else if err := addV2FileContractResolutions(tx, dbID, txn.FileContractResolutions); err != nil {
518514
return fmt.Errorf("failed to add file contract resolutions: %w", err)
519515
}
520516
}

0 commit comments

Comments
 (0)