@@ -20,7 +20,6 @@ import (
2020
2121 "github.com/blinklabs-io/dingo/database/models"
2222 "github.com/blinklabs-io/gouroboros/ledger"
23- lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
2423 "gorm.io/gorm"
2524)
2625
@@ -102,6 +101,9 @@ func (d *MetadataStoreSqlite) GetUtxosByAddress(
102101 var ret []models.Utxo
103102 // Build sub-query for address
104103 var addrQuery * gorm.DB
104+ if txn == nil {
105+ txn = d .DB ()
106+ }
105107 if addr .PaymentKeyHash () != ledger .NewBlake2b224 (nil ) {
106108 addrQuery = txn .Where ("payment_key = ?" , addr .PaymentKeyHash ().Bytes ())
107109 }
@@ -115,6 +117,9 @@ func (d *MetadataStoreSqlite) GetUtxosByAddress(
115117 addrQuery = txn .Where ("staking_key = ?" , addr .StakeKeyHash ().Bytes ())
116118 }
117119 }
120+ if addrQuery == nil {
121+ return ret , nil
122+ }
118123 result := txn .
119124 Where ("deleted_slot = 0" ).
120125 Where (addrQuery ).
@@ -193,43 +198,6 @@ func (d *MetadataStoreSqlite) DeleteUtxosAfterSlot(
193198 return nil
194199}
195200
196- // SetUtxo saves a UTxO
197- func (d * MetadataStoreSqlite ) SetUtxo (
198- txId []byte , // hash
199- idx uint32 , // idx
200- slot uint64 , // slot
201- payment []byte , // payment
202- stake []byte , // stake
203- amount uint64 , // amount
204- assets * lcommon.MultiAsset [lcommon.MultiAssetTypeOutput ], // assets (multi-asset)
205- txn * gorm.DB ,
206- ) error {
207- tmpUtxo := models.Utxo {
208- TxId : txId ,
209- OutputIdx : idx ,
210- AddedSlot : slot ,
211- PaymentKey : payment ,
212- StakingKey : stake ,
213- }
214-
215- // Handle assets if present
216- if assets != nil {
217- tmpUtxo .Assets = models .ConvertMultiAssetToModels (assets )
218- }
219-
220- var result * gorm.DB
221- if txn != nil {
222- result = txn .Create (& tmpUtxo )
223- } else {
224- result = d .DB ().Create (& tmpUtxo )
225- }
226-
227- if result .Error != nil {
228- return result .Error
229- }
230- return nil
231- }
232-
233201// AddUtxos saves a batch of UTxOs
234202func (d * MetadataStoreSqlite ) AddUtxos (
235203 utxos []models.UtxoSlot ,
@@ -239,19 +207,16 @@ func (d *MetadataStoreSqlite) AddUtxos(
239207 for _ , utxo := range utxos {
240208 items = append (
241209 items ,
242- utxoLedgerToModel (utxo .Utxo , utxo .Slot ),
210+ models . UtxoLedgerToModel (utxo .Utxo , utxo .Slot ),
243211 )
244212 }
245- if txn != nil {
246- result := txn .Create (items )
247- if result .Error != nil {
248- return result .Error
249- }
250- } else {
251- result := d .DB ().Create (items )
252- if result .Error != nil {
253- return result .Error
254- }
213+ if txn == nil {
214+ txn = d .DB ()
215+ }
216+ result := txn .Session (& gorm.Session {FullSaveAssociations : true }).
217+ CreateInBatches (items , 1000 )
218+ if result .Error != nil {
219+ return result .Error
255220 }
256221 return nil
257222}
@@ -302,28 +267,3 @@ func (d *MetadataStoreSqlite) SetUtxosNotDeletedAfterSlot(
302267 }
303268 return nil
304269}
305-
306- func utxoLedgerToModel (
307- utxo ledger.Utxo ,
308- slot uint64 ,
309- ) models.Utxo {
310- outAddr := utxo .Output .Address ()
311- ret := models.Utxo {
312- TxId : utxo .Id .Id ().Bytes (),
313- OutputIdx : utxo .Id .Index (),
314- AddedSlot : slot ,
315- PaymentKey : outAddr .PaymentKeyHash ().Bytes (),
316- StakingKey : outAddr .StakeKeyHash ().Bytes (),
317- Cbor : utxo .Output .Cbor (),
318- }
319-
320- if multiAssetOutput , ok := utxo .Output .(interface {
321- MultiAsset () * lcommon.MultiAsset [lcommon.MultiAssetTypeOutput ]
322- }); ok {
323- if multiAsset := multiAssetOutput .MultiAsset (); multiAsset != nil {
324- ret .Assets = models .ConvertMultiAssetToModels (multiAsset )
325- }
326- }
327-
328- return ret
329- }
0 commit comments