Skip to content

Commit c971d8c

Browse files
committed
feat: take stab at building block info to hand off
1 parent a982790 commit c971d8c

File tree

5 files changed

+45
-17
lines changed

5 files changed

+45
-17
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/snapshot_bootstrapper/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async-compression = { version = "0.4.32", features = ["tokio", "gzip"] }
2424
reqwest = { version = "0.12", features = ["stream"] }
2525
futures-util = "0.3.31"
2626
tokio-util = "0.7.17"
27+
hex = "0.4.3"
2728

2829
[dev-dependencies]
2930
wiremock = "0.6.5"

modules/snapshot_bootstrapper/src/bootstrapper.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod publisher;
66
use crate::configuration::{ConfigError, NetworkConfig, SnapshotConfig, SnapshotFileMetadata};
77
use crate::downloader::{DownloadError, SnapshotDownloader};
88
use crate::publisher::SnapshotPublisher;
9+
use acropolis_common::genesis_values::GenesisValues;
910
use acropolis_common::snapshot::streaming_snapshot::StreamingSnapshotParser;
1011
use acropolis_common::{
1112
messages::{CardanoMessage, Message},
@@ -180,11 +181,15 @@ impl SnapshotBootstrapper {
180181
Self::parse_snapshot(&file_path, &mut publisher).await?;
181182
}
182183

183-
let metadata = publisher
184-
.metadata()
185-
.ok_or_else(|| anyhow::anyhow!("No metadata received from snapshots"))?;
184+
let last_snapshot =
185+
snapshots.last().ok_or_else(|| anyhow::anyhow!("No snapshots to process"))?;
186+
187+
let block_info = build_block_info_from_metadata(last_snapshot).map_err(|e| {
188+
BootstrapError::Parse(format!(
189+
"Failed to build block info from snapshot metadata: {e}"
190+
))
191+
})?;
186192

187-
let block_info = build_block_info_from_metadata(metadata);
188193
publisher.publish_completion(block_info).await?;
189194

190195
Ok(())
@@ -204,18 +209,32 @@ impl SnapshotBootstrapper {
204209
}
205210
}
206211

207-
fn build_block_info_from_metadata(
208-
metadata: &acropolis_common::snapshot::streaming_snapshot::SnapshotMetadata,
209-
) -> BlockInfo {
210-
BlockInfo {
212+
fn build_block_info_from_metadata(metadata: &SnapshotFileMetadata) -> Result<BlockInfo> {
213+
let (slot, block_hash_str) = metadata
214+
.parse_point()
215+
.ok_or_else(|| anyhow::anyhow!("Invalid point format: {}", metadata.point))?;
216+
217+
let hash = BlockHash::try_from(hex::decode(block_hash_str)?)
218+
.map_err(|e| anyhow::anyhow!("Invalid block hash hex: {:?}", e))?;
219+
220+
let genesis = GenesisValues::mainnet();
221+
let epoch_slot = slot - genesis.epoch_to_first_slot(slot);
222+
let timestamp = genesis.slot_to_timestamp(slot);
223+
224+
info!(
225+
"Block info built: slot={}, hash={}, epoch={}, slot_in_epoch={}, timestamp={}",
226+
slot, hash, metadata.epoch, epoch_slot, timestamp
227+
);
228+
229+
Ok(BlockInfo {
211230
status: BlockStatus::Immutable,
212-
slot: 0,
231+
slot,
213232
number: 0,
214-
hash: BlockHash::default(),
233+
hash,
215234
epoch: metadata.epoch,
216-
epoch_slot: 0,
235+
epoch_slot,
217236
new_epoch: false,
218-
timestamp: 0,
237+
timestamp,
219238
era: Era::Conway,
220-
}
239+
})
221240
}

modules/snapshot_bootstrapper/src/configuration.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ impl SnapshotFileMetadata {
108108
Ok(snapshots)
109109
}
110110

111+
pub fn parse_point(&self) -> Option<(u64, String)> {
112+
let parts: Vec<&str> = self.point.splitn(2, '.').collect();
113+
if parts.len() == 2 {
114+
let slot = parts[0].parse().ok()?;
115+
let hash = parts[1].to_string();
116+
Some((slot, hash))
117+
} else {
118+
None
119+
}
120+
}
121+
111122
pub fn file_path(&self, network_dir: &str) -> String {
112123
format!("{}/{}.cbor", network_dir, self.point)
113124
}

modules/snapshot_bootstrapper/src/publisher.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ impl SnapshotPublisher {
5858
)));
5959
self.context.publish(&self.completion_topic, message).await
6060
}
61-
62-
pub fn metadata(&self) -> Option<&SnapshotMetadata> {
63-
self.metadata.as_ref()
64-
}
6561
}
6662

6763
impl UtxoCallback for SnapshotPublisher {

0 commit comments

Comments
 (0)