Skip to content

Commit d2ab286

Browse files
authored
feat: add get fork for block resource (#493)
Signed-off-by: bkioshn <[email protected]>
1 parent d410083 commit d2ab286

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

hermes/bin/src/runtime_extensions/hermes/cardano/host.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,30 @@ impl HostBlock for HermesRuntimeContext {
333333
Ok(block.raw().clone())
334334
}
335335

336+
/// Fork count is a counter that is incremented every time there is a roll-back in
337+
/// live-chain. It is used to help followers determine how far to roll-back to
338+
/// resynchronize without storing full block history. The fork count starts at 1 for
339+
/// live blocks and increments if the live chain tip is purged due to a detected
340+
/// fork, but it does not track the exact number of forks reported by peers.
341+
///
342+
/// - 0 - for all immutable data
343+
/// - 1 - for any data read from the blockchain during a *backfill* on initial sync
344+
/// - 2+ - for each subsequent rollback detected while reading live blocks.
345+
///
346+
/// Note: This fork terminology is different from fork in blockchain.
347+
///
348+
/// ** Returns **
349+
///
350+
/// - `u64` : The fork count.
351+
fn get_fork(
352+
&mut self,
353+
self_: wasmtime::component::Resource<Block>,
354+
) -> wasmtime::Result<u64> {
355+
let mut app_state = STATE.block.get_app_state(self.app_name())?;
356+
let block = app_state.get_object(&self_)?;
357+
Ok(block.fork().into())
358+
}
359+
336360
fn drop(
337361
&mut self,
338362
rep: wasmtime::component::Resource<Block>,

hermes/bin/src/runtime_extensions/hermes/cardano/network.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub(crate) fn spawn_subscribe(
7070
"Failed to spawn chain follower"
7171
);
7272
return;
73-
}; // // Hold onto the clone inside the thread to keep Arc alive
73+
};
7474

7575
rt.block_on(subscribe(
7676
cmd_rx,

wasm/examples/rust/cardano/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ impl hermes::exports::hermes::cardano::event_on_block::Guest for TestComponent {
1515
let is_immutable = block.is_immutable();
1616
let is_rollback = block.is_rollback();
1717
let network = subscription_id.get_network();
18+
let fork = block.get_fork();
1819
if let Ok(txn) = block.get_txn(0) {
1920
txn_hash = txn.get_txn_hash();
2021
}
@@ -26,7 +27,7 @@ impl hermes::exports::hermes::cardano::event_on_block::Guest for TestComponent {
2627
None,
2728
None,
2829
None,
29-
format!("✈️ - on_cardano_block event trigger - subscription ID: {subscription_id:?}, network: {network:?}, slot: {slot:?}, is rollback: {is_rollback:?}, is immutable: {is_immutable}, txn hash: {txn_hash:?}").as_str(),
30+
format!("✈️ - on_cardano_block event trigger - subscription ID: {subscription_id:?}, network: {network:?}, slot: {slot:?}, is rollback: {is_rollback:?}, is immutable: {is_immutable}, txn hash: {txn_hash:?}, fork: {fork:?}").as_str(),
3031
None,
3132
);
3233
}

wasm/wasi/wit/deps/hermes-cardano/api.wit

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,23 @@ interface api {
198198
///
199199
/// - `cbor` : The CBOR format of the block.
200200
raw: func() -> cbor;
201+
202+
/// Fork count is a counter that is incremented every time there is a roll-back in
203+
/// live-chain. It is used to help followers determine how far to roll-back to
204+
/// resynchronize without storing full block history. The fork count starts at 1 for
205+
/// live blocks and increments if the live chain tip is purged due to a detected
206+
/// fork, but it does not track the exact number of forks reported by peers.
207+
///
208+
/// - 0 - for all immutable data
209+
/// - 1 - for any data read from the blockchain during a *backfill* on initial sync
210+
/// - 2+ - for each subsequent rollback detected while reading live blocks.
211+
///
212+
/// Note: This fork terminology is different from fork in blockchain.
213+
///
214+
/// ** Returns **
215+
///
216+
/// - `u64` : The fork count.
217+
get-fork: func() -> u64;
201218
}
202219

203220
/// Cardano transaction

0 commit comments

Comments
 (0)