Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition for finalized blocks #1389

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/analytics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ impl<'a, I: InputSource> Slot<'a, I> {
db,
};

let mut block_stream = self.accepted_block_stream().await?.boxed();
let mut block_stream = self.finalized_block_stream().await?.boxed();

let mut counter = 0;

while let Some(data) = block_stream.try_next().await? {
if let Some((payload, metadata)) = data
Expand All @@ -332,8 +334,11 @@ impl<'a, I: InputSource> Slot<'a, I> {
}
}
self.handle_block(analytics, &data.block, &ctx).await?;
counter += 1;
}

println!("analytics blocks: {counter}");

influxdb
.insert_measurement((analytics as &mut dyn DynAnalytics).take_measurement(&ctx).await?)
.await?;
Expand Down
19 changes: 14 additions & 5 deletions src/bin/inx-chronicle/inx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ impl InxWorker {
};

debug!(
"The node has a pruning epoch index of `{}` and a latest confirmed slot index of `{}`.",
"The node has a pruning epoch index of `{}` and a latest finalized slot index of `{}`.",
node_status.pruning_epoch,
node_status.latest_commitment.commitment_id.slot_index()
node_status.latest_finalized_commitment.commitment_id.slot_index()
);

let mut node_configuration = inx.get_node_configuration().await?;
Expand Down Expand Up @@ -303,7 +303,7 @@ impl InxWorker {
tracing::Span::current().record("created", slot.ledger_updates().created_outputs().len());
tracing::Span::current().record("consumed", slot.ledger_updates().consumed_outputs().len());

self.handle_accepted_blocks(&slot).await?;
self.handle_finalized_blocks(&slot).await?;

#[cfg(feature = "influx")]
self.update_influx(
Expand All @@ -326,8 +326,17 @@ impl InxWorker {
}

#[instrument(skip_all, err, level = "trace")]
async fn handle_accepted_blocks<'a>(&mut self, slot: &Slot<'a, Inx>) -> Result<()> {
let blocks_stream = slot.accepted_block_stream().await?;
async fn handle_finalized_blocks<'a>(&mut self, slot: &Slot<'a, Inx>) -> Result<()> {
// let mut block_stream = slot.finalized_block_stream().await?.boxed();

// let mut counter = 0;
// while let Some(data) = block_stream.try_next().await? {
// counter += 1;
// }

// println!("inx blocks: {counter}");

let blocks_stream = slot.finalized_block_stream().await?;

let mut tasks = blocks_stream
.try_chunks(INSERT_BATCH_SIZE)
Expand Down
14 changes: 13 additions & 1 deletion src/tangle/slot_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,25 @@ impl<'a, I: InputSource> Slot<'a, I> {
pub fn commitment(&self) -> &Raw<SlotCommitment> {
&self.commitment.commitment
}

/// Check whether the slot has been finalized.
pub async fn is_finalized(&self) -> bool {
// TODO: unwrap
self.commitment.commitment_id.slot_index() >= self.source.latest_finalized_slot_index().await.unwrap()
}
}

impl<'a, I: InputSource> Slot<'a, I> {
/// Returns the accepted blocks of a slot.
pub async fn accepted_block_stream(
pub async fn finalized_block_stream(
&self,
) -> Result<impl Stream<Item = Result<BlockWithTransactionMetadata, I::Error>> + '_, I::Error> {
while !self.is_finalized().await {
println!("not finalized: {}", self.index());
tokio::time::sleep(core::time::Duration::from_millis(100)).await;
}
println!("finalized: {}", self.index());

Ok(self
.source
.accepted_blocks(self.index())
Expand Down
10 changes: 10 additions & 0 deletions src/tangle/sources/inx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,14 @@ impl InputSource for Inx {

Ok(LedgerUpdateStore::init(consumed, created))
}

async fn latest_finalized_slot_index(&self) -> Result<SlotIndex, Self::Error> {
let mut inx = self.clone();
Ok(inx
.get_node_status()
.await?
.latest_finalized_commitment
.commitment_id
.slot_index())
}
}
4 changes: 4 additions & 0 deletions src/tangle/sources/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ impl InputSource for BTreeMap<SlotIndex, InMemoryData> {
.ledger_updates
.clone())
}

async fn latest_finalized_slot_index(&self) -> Result<SlotIndex, Self::Error> {
todo!()
}
}
5 changes: 4 additions & 1 deletion src/tangle/sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub trait InputSource: Send + Sync {
range: impl RangeBounds<SlotIndex> + Send,
) -> Result<BoxStream<Result<Commitment, Self::Error>>, Self::Error>;

/// A stream of accepted blocks for a given slot index.
/// A stream of (at least) accepted blocks for a given slot index.
async fn accepted_blocks(
&self,
index: SlotIndex,
Expand All @@ -41,4 +41,7 @@ pub trait InputSource: Send + Sync {

/// Retrieves the updates to the ledger for a given range of slots.
async fn ledger_updates(&self, index: SlotIndex) -> Result<LedgerUpdateStore, Self::Error>;

/// Retrieves the latest finalized slot index.
async fn latest_finalized_slot_index(&self) -> Result<SlotIndex, Self::Error>;
}
4 changes: 4 additions & 0 deletions src/tangle/sources/mongodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@ impl InputSource for MongoDb {

Ok(LedgerUpdateStore::init(consumed, created))
}

async fn latest_finalized_slot_index(&self) -> Result<SlotIndex, Self::Error> {
todo!()
}
}
Loading