Skip to content

Commit bf7d1dd

Browse files
committed
Make transaction repository handle the store in chunk
Since it's a sqlite limitation, it's the repository job to handle that.
1 parent eb5c95f commit bf7d1dd

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

mithril-aggregator/src/database/provider/cardano_transaction.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,13 @@ impl TransactionStore for CardanoTransactionRepository {
331331
})
332332
}
333333

334-
async fn store_transactions(&self, transactions: &[CardanoTransaction]) -> StdResult<()> {
335-
self.create_transactions(transactions.to_vec())
336-
.await
337-
.with_context(|| "CardanoTransactionRepository can not store transactions")?;
338-
334+
async fn store_transactions(&self, transactions: Vec<CardanoTransaction>) -> StdResult<()> {
335+
// Chunk transactions to avoid an error when we exceed sqlite binding limitations
336+
for transactions_in_chunk in transactions.chunks(100) {
337+
self.create_transactions(transactions_in_chunk.to_vec())
338+
.await
339+
.with_context(|| "CardanoTransactionRepository can not store transactions")?;
340+
}
339341
Ok(())
340342
}
341343
}
@@ -556,7 +558,7 @@ mod tests {
556558
CardanoTransaction::new("tx-hash-456", 11, 51, "block-hash-456", 100),
557559
];
558560
repository
559-
.store_transactions(&cardano_transactions)
561+
.create_transactions(cardano_transactions)
560562
.await
561563
.unwrap();
562564

@@ -626,7 +628,7 @@ mod tests {
626628
CardanoTransaction::new("tx-hash-456".to_string(), 11, 51, "block-hash-456", 100),
627629
];
628630
repository
629-
.store_transactions(&cardano_transactions)
631+
.create_transactions(cardano_transactions.clone())
630632
.await
631633
.unwrap();
632634

@@ -657,7 +659,7 @@ mod tests {
657659
99,
658660
)];
659661
repository
660-
.store_transactions(&cardano_transactions)
662+
.create_transactions(cardano_transactions)
661663
.await
662664
.unwrap();
663665

@@ -694,7 +696,7 @@ mod tests {
694696
CardanoTransaction::new("tx-hash-456".to_string(), 11, 51, "block-hash-456", 100),
695697
];
696698
repository
697-
.store_transactions(&cardano_transactions)
699+
.create_transactions(cardano_transactions)
698700
.await
699701
.unwrap();
700702

mithril-aggregator/src/services/cardano_transactions_importer.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub trait TransactionStore: Send + Sync {
2424
) -> StdResult<Vec<CardanoTransaction>>;
2525

2626
/// Store list of transactions
27-
async fn store_transactions(&self, transactions: &[CardanoTransaction]) -> StdResult<()>;
27+
async fn store_transactions(&self, transactions: Vec<CardanoTransaction>) -> StdResult<()>;
2828
}
2929

3030
/// Import and store [CardanoTransaction].
@@ -89,12 +89,9 @@ impl CardanoTransactionsImporter {
8989
from.unwrap_or(0)
9090
);
9191

92-
let transaction_chunk_size = 100;
93-
for transactions_in_chunk in parsed_transactions.chunks(transaction_chunk_size) {
94-
self.transaction_store
95-
.store_transactions(transactions_in_chunk)
96-
.await?;
97-
}
92+
self.transaction_store
93+
.store_transactions(parsed_transactions)
94+
.await?;
9895
Ok(())
9996
}
10097
}

mithril-signer/src/cardano_transactions_importer.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub trait TransactionStore: Send + Sync {
2424
) -> StdResult<Vec<CardanoTransaction>>;
2525

2626
/// Store list of transactions
27-
async fn store_transactions(&self, transactions: &[CardanoTransaction]) -> StdResult<()>;
27+
async fn store_transactions(&self, transactions: Vec<CardanoTransaction>) -> StdResult<()>;
2828
}
2929

3030
/// Import and store [CardanoTransaction].
@@ -89,12 +89,9 @@ impl CardanoTransactionsImporter {
8989
from.unwrap_or(0)
9090
);
9191

92-
let transaction_chunk_size = 100;
93-
for transactions_in_chunk in parsed_transactions.chunks(transaction_chunk_size) {
94-
self.transaction_store
95-
.store_transactions(transactions_in_chunk)
96-
.await?;
97-
}
92+
self.transaction_store
93+
.store_transactions(parsed_transactions)
94+
.await?;
9895
Ok(())
9996
}
10097
}

mithril-signer/src/database/provider/cardano_transaction.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,13 @@ impl TransactionStore for CardanoTransactionRepository {
330330
})
331331
}
332332

333-
async fn store_transactions(&self, transactions: &[CardanoTransaction]) -> StdResult<()> {
334-
self.create_transactions(transactions.to_vec())
335-
.await
336-
.with_context(|| "CardanoTransactionRepository can not store transactions")?;
337-
333+
async fn store_transactions(&self, transactions: Vec<CardanoTransaction>) -> StdResult<()> {
334+
// Chunk transactions to avoid an error when we exceed sqlite binding limitations
335+
for transactions_in_chunk in transactions.chunks(100) {
336+
self.create_transactions(transactions_in_chunk.to_vec())
337+
.await
338+
.with_context(|| "CardanoTransactionRepository can not store transactions")?;
339+
}
338340
Ok(())
339341
}
340342
}
@@ -539,7 +541,7 @@ mod tests {
539541
CardanoTransaction::new("tx-hash-456", 11, 51, "block-hash-456", 100),
540542
];
541543
repository
542-
.store_transactions(&cardano_transactions)
544+
.create_transactions(cardano_transactions)
543545
.await
544546
.unwrap();
545547

@@ -609,7 +611,7 @@ mod tests {
609611
CardanoTransaction::new("tx-hash-456".to_string(), 11, 51, "block-hash-456", 100),
610612
];
611613
repository
612-
.store_transactions(&cardano_transactions)
614+
.create_transactions(cardano_transactions.clone())
613615
.await
614616
.unwrap();
615617

@@ -640,7 +642,7 @@ mod tests {
640642
99,
641643
)];
642644
repository
643-
.store_transactions(&cardano_transactions)
645+
.create_transactions(cardano_transactions)
644646
.await
645647
.unwrap();
646648

@@ -677,7 +679,7 @@ mod tests {
677679
CardanoTransaction::new("tx-hash-456".to_string(), 11, 51, "block-hash-456", 100),
678680
];
679681
repository
680-
.store_transactions(&cardano_transactions)
682+
.create_transactions(cardano_transactions)
681683
.await
682684
.unwrap();
683685

0 commit comments

Comments
 (0)