Skip to content

Commit

Permalink
fix(inx): reorder sync process to always insert milestone last (#907)
Browse files Browse the repository at this point in the history
Reorder sync process to always insert milestone last
  • Loading branch information
Alexandcoats authored Nov 21, 2022
1 parent c2f8d30 commit 4b97af7
Showing 1 changed file with 25 additions and 36 deletions.
61 changes: 25 additions & 36 deletions src/bin/inx-chronicle/stardust_inx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ use chronicle::{
inx::{BlockWithMetadataMessage, Inx, InxError, LedgerUpdateMessage, MarkerMessage},
types::{
ledger::{BlockMetadata, LedgerInclusionState, LedgerOutput, LedgerSpent, MilestoneIndexTimestamp},
stardust::{
block::{Block, BlockId, Payload},
milestone::MilestoneTimestamp,
},
stardust::block::{Block, BlockId, Payload},
tangle::MilestoneIndex,
},
};
Expand Down Expand Up @@ -325,9 +322,24 @@ impl InxWorker {
self.handle_protocol_params(inx, milestone_index).await?;
self.handle_node_configuration(inx, milestone_index).await?;

// This acts as a checkpoint for the syncing and has to be done last, after everything else completed.
#[allow(unused)]
let milestone_timestamp = self.handle_milestone(inx, milestone_index).await?;
let milestone = inx.read_milestone(milestone_index.0.into()).await?;

let milestone_index: MilestoneIndex = milestone.milestone_info.milestone_index;

let milestone_timestamp = milestone.milestone_info.milestone_timestamp.into();

let milestone_id = milestone
.milestone_info
.milestone_id
.ok_or(InxWorkerError::MissingMilestoneInfo(milestone_index))?;

let payload =
if let iota_types::block::payload::Payload::Milestone(payload) = milestone.milestone.inner_unverified()? {
chronicle::types::stardust::block::payload::MilestonePayload::from(payload)
} else {
// The raw data is guaranteed to contain a milestone payload.
unreachable!();
};

#[cfg(all(feature = "analytics", feature = "metrics"))]
let analytics_start_time = std::time::Instant::now();
Expand Down Expand Up @@ -372,6 +384,12 @@ impl InxWorker {
}
}

// This acts as a checkpoint for the syncing and has to be done last, after everything else completed.
self.db
.collection::<MilestoneCollection>()
.insert_milestone(milestone_id, milestone_index, milestone_timestamp, payload)
.await?;

Ok(())
}

Expand Down Expand Up @@ -403,35 +421,6 @@ impl InxWorker {
Ok(())
}

#[instrument(skip_all, err, level = "trace")]
async fn handle_milestone(&mut self, inx: &mut Inx, milestone_index: MilestoneIndex) -> Result<MilestoneTimestamp> {
let milestone = inx.read_milestone(milestone_index.0.into()).await?;

let milestone_index: MilestoneIndex = milestone.milestone_info.milestone_index;

let milestone_timestamp = milestone.milestone_info.milestone_timestamp.into();

let milestone_id = milestone
.milestone_info
.milestone_id
.ok_or(InxWorkerError::MissingMilestoneInfo(milestone_index))?;

let payload =
if let iota_types::block::payload::Payload::Milestone(payload) = milestone.milestone.inner_unverified()? {
chronicle::types::stardust::block::payload::MilestonePayload::from(payload)
} else {
// The raw data is guaranteed to contain a milestone payload.
unreachable!();
};

self.db
.collection::<MilestoneCollection>()
.insert_milestone(milestone_id, milestone_index, milestone_timestamp, payload)
.await?;

Ok(milestone_timestamp)
}

#[instrument(skip(self, inx), err, level = "trace")]
async fn handle_cone_stream(&mut self, inx: &mut Inx, milestone_index: MilestoneIndex) -> Result<()> {
let cone_stream = inx.read_milestone_cone(milestone_index.0.into()).await?;
Expand Down

0 comments on commit 4b97af7

Please sign in to comment.