Skip to content

Commit

Permalink
Fix importer failing if it did not have any blockrangeroot to save
Browse files Browse the repository at this point in the history
It raised an retryable error that the state machine tried immediatly to
retry leading to a loop.

+ Refactor the import_block_range method to flatten it's code, making it
  more readble.
+ Add a log that tell which BlockRangeRoots the importer will try to
  compute.
  • Loading branch information
Alenar committed Apr 22, 2024
1 parent eae5899 commit 9ca6619
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ impl TransactionStore for CardanoTransactionRepository {
&self,
block_ranges: Vec<(BlockRange, MKTreeNode)>,
) -> StdResult<()> {
self.create_block_ranges(block_ranges).await?;
if !block_ranges.is_empty() {
self.create_block_ranges(block_ranges).await?;
}
Ok(())
}
}
Expand Down
63 changes: 32 additions & 31 deletions mithril-aggregator/src/services/cardano_transactions_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,43 +123,44 @@ impl CardanoTransactionsImporter {
}

async fn import_block_ranges(&self) -> StdResult<()> {
match self
let block_ranges = match self
.transaction_store
.get_block_interval_without_block_range_root()
.await?
.map(|range| BlockRange::all_ranges_in(BlockRange::start(range.start)..range.end))
{
None => {
// Nothing to do
Ok(())
}
Some(range) => {
let block_ranges =
BlockRange::all_ranges_in(BlockRange::start(range.start)..range.end);

if block_ranges.is_empty() {
return Ok(());
}

let mut block_ranges_with_merkle_root: Vec<(BlockRange, MKTreeNode)> = vec![];
for block_range in block_ranges {
let transactions = self
.transaction_store
.get_transactions_between(block_range.start..block_range.end)
.await?;

if transactions.is_empty() {
continue;
}

let merkle_root = MKTree::new(&transactions)?.compute_root()?;
block_ranges_with_merkle_root.push((block_range, merkle_root));
}

self.transaction_store
.store_block_ranges(block_ranges_with_merkle_root)
.await
// Everything is already computed
None => return Ok(()),
// Not enough block to form at least one block range
Some(ranges) if ranges.is_empty() => return Ok(()),
Some(ranges) => ranges,
};

debug!(
self.logger,
"TransactionsImporter - computing Block Range Roots";
"start_block" => block_ranges.first().map(|br| br.start).unwrap_or(0),
"end_block" => block_ranges.last().map(|br| br.end).unwrap_or(0),
);

let mut block_ranges_with_merkle_root: Vec<(BlockRange, MKTreeNode)> = vec![];
for block_range in block_ranges {
let transactions = self
.transaction_store
.get_transactions_between(block_range.start..block_range.end)
.await?;

if transactions.is_empty() {
continue;
}

let merkle_root = MKTree::new(&transactions)?.compute_root()?;
block_ranges_with_merkle_root.push((block_range, merkle_root));
}

self.transaction_store
.store_block_ranges(block_ranges_with_merkle_root)
.await
}
}

Expand Down
63 changes: 32 additions & 31 deletions mithril-signer/src/cardano_transactions_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,43 +123,44 @@ impl CardanoTransactionsImporter {
}

async fn import_block_ranges(&self) -> StdResult<()> {
match self
let block_ranges = match self
.transaction_store
.get_block_interval_without_block_range_root()
.await?
.map(|range| BlockRange::all_ranges_in(BlockRange::start(range.start)..range.end))
{
None => {
// Nothing to do
Ok(())
}
Some(range) => {
let block_ranges =
BlockRange::all_ranges_in(BlockRange::start(range.start)..range.end);

if block_ranges.is_empty() {
return Ok(());
}

let mut block_ranges_with_merkle_root: Vec<(BlockRange, MKTreeNode)> = vec![];
for block_range in block_ranges {
let transactions = self
.transaction_store
.get_transactions_between(block_range.start..block_range.end)
.await?;

if transactions.is_empty() {
continue;
}

let merkle_root = MKTree::new(&transactions)?.compute_root()?;
block_ranges_with_merkle_root.push((block_range, merkle_root));
}

self.transaction_store
.store_block_ranges(block_ranges_with_merkle_root)
.await
// Everything is already computed
None => return Ok(()),
// Not enough block to form at least one block range
Some(ranges) if ranges.is_empty() => return Ok(()),
Some(ranges) => ranges,
};

debug!(
self.logger,
"TransactionsImporter - computing Block Range Roots";
"start_block" => block_ranges.first().map(|br| br.start).unwrap_or(0),
"end_block" => block_ranges.last().map(|br| br.end).unwrap_or(0),
);

let mut block_ranges_with_merkle_root: Vec<(BlockRange, MKTreeNode)> = vec![];
for block_range in block_ranges {
let transactions = self
.transaction_store
.get_transactions_between(block_range.start..block_range.end)
.await?;

if transactions.is_empty() {
continue;
}

let merkle_root = MKTree::new(&transactions)?.compute_root()?;
block_ranges_with_merkle_root.push((block_range, merkle_root));
}

self.transaction_store
.store_block_ranges(block_ranges_with_merkle_root)
.await
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ impl TransactionStore for CardanoTransactionRepository {
&self,
block_ranges: Vec<(BlockRange, MKTreeNode)>,
) -> StdResult<()> {
self.create_block_ranges(block_ranges).await?;
if !block_ranges.is_empty() {
self.create_block_ranges(block_ranges).await?;
}
Ok(())
}
}
Expand Down

0 comments on commit 9ca6619

Please sign in to comment.