Skip to content

Commit ae6d0af

Browse files
committed
feat: complete block tracking integration
- Add block_id to PutOptions for automatic block tracking - Add PutOptions::with_block() helper method - LocalStorage::put now automatically updates metadata with block info - Add execute_sync_with_block() and execute_get_weights_with_block() methods - Pass block_height and epoch to WASM instances via InstanceConfig - Add put_options_with_block() helper in validator - Update sync and weight execution to pass current block context
1 parent e08a6b0 commit ae6d0af

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

bins/validator-node/src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ where
9292
result
9393
}
9494

95+
/// Create PutOptions with the current block number for tracking
96+
fn put_options_with_block(state_manager: &StateManager) -> PutOptions {
97+
let block = state_manager.apply(|state| state.bittensor_block);
98+
PutOptions::with_block(block)
99+
}
100+
95101
// ==================== Shutdown Handler ====================
96102

97103
/// Handles graceful shutdown with state persistence
@@ -1519,12 +1525,13 @@ async fn main() -> Result<()> {
15191525
.collect()
15201526
};
15211527

1528+
let current_epoch = current_block / 360;
15221529
for challenge_id in challenges {
15231530
let challenge_id_str = challenge_id.to_string();
15241531
let module_path = format!("{}.wasm", challenge_id_str);
15251532

15261533
if let Some(ref executor) = wasm_executor {
1527-
match executor.execute_sync(&module_path) {
1534+
match executor.execute_sync_with_block(&module_path, current_block, current_epoch) {
15281535
Ok(sync_result) => {
15291536
info!(
15301537
challenge_id = %challenge_id,
@@ -3163,8 +3170,10 @@ async fn handle_block_event(
31633170
let challenges: Vec<String> = state_manager
31643171
.apply(|state| state.challenges.keys().map(|k| k.to_string()).collect());
31653172
let local_hotkey = keypair.hotkey();
3173+
let block_height = state_manager.apply(|state| state.bittensor_block);
3174+
let epoch = block_height / 360;
31663175
for cid in &challenges {
3167-
match executor.execute_get_weights(cid) {
3176+
match executor.execute_get_weights_with_block(cid, block_height, epoch) {
31683177
Ok(assignments) if !assignments.is_empty() => {
31693178
// Read emission_weight from chain state
31703179
let emission_weight = {

bins/validator-node/src/wasm_executor.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,16 @@ impl WasmChallengeExecutor {
915915
pub fn execute_get_weights(
916916
&self,
917917
module_path: &str,
918+
) -> Result<Vec<platform_challenge_sdk::WeightAssignment>> {
919+
self.execute_get_weights_with_block(module_path, 0, 0)
920+
}
921+
922+
/// Execute get_weights on a WASM challenge module with block context.
923+
pub fn execute_get_weights_with_block(
924+
&self,
925+
module_path: &str,
926+
block_height: u64,
927+
epoch: u64,
918928
) -> Result<Vec<platform_challenge_sdk::WeightAssignment>> {
919929
let start = Instant::now();
920930

@@ -932,6 +942,8 @@ impl WasmChallengeExecutor {
932942
},
933943
storage_backend: Arc::clone(&self.config.storage_backend),
934944
consensus_policy: ConsensusPolicy::read_only(),
945+
block_height,
946+
epoch,
935947
..Default::default()
936948
};
937949

@@ -990,6 +1002,17 @@ impl WasmChallengeExecutor {
9901002
pub fn execute_sync(
9911003
&self,
9921004
module_path: &str,
1005+
) -> Result<platform_challenge_sdk_wasm::WasmSyncResult> {
1006+
self.execute_sync_with_block(module_path, 0, 0)
1007+
}
1008+
1009+
/// Execute sync on a WASM challenge module with block context.
1010+
/// Returns WasmSyncResult with leaderboard hash and stats for consensus.
1011+
pub fn execute_sync_with_block(
1012+
&self,
1013+
module_path: &str,
1014+
block_height: u64,
1015+
epoch: u64,
9931016
) -> Result<platform_challenge_sdk_wasm::WasmSyncResult> {
9941017
let start = Instant::now();
9951018

@@ -1007,6 +1030,8 @@ impl WasmChallengeExecutor {
10071030
},
10081031
storage_backend: Arc::clone(&self.config.storage_backend),
10091032
consensus_policy: ConsensusPolicy::read_only(),
1033+
block_height,
1034+
epoch,
10101035
..Default::default()
10111036
};
10121037

crates/distributed-storage/src/local.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,14 +597,22 @@ impl DistributedStore for LocalStorage {
597597
stored_value.metadata.ttl_seconds = options.ttl_seconds;
598598
}
599599

600-
// Create entry with replication info (no block_id for regular put)
600+
// Update metadata with block tracking if provided
601+
if let Some(block_id) = options.block_id {
602+
if stored_value.metadata.created_block == 0 {
603+
stored_value.metadata.created_block = block_id;
604+
}
605+
stored_value.metadata.updated_block = block_id;
606+
}
607+
608+
// Create entry with replication info and optional block_id
601609
let entry = LocalEntry {
602610
value: stored_value.clone(),
603611
replication: ReplicationInfo {
604612
needs_replication: !options.local_only,
605613
..Default::default()
606614
},
607-
block_id: None,
615+
block_id: options.block_id,
608616
};
609617

610618
self.put_entry(&key, &entry)?;

crates/distributed-storage/src/store.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,18 @@ pub struct PutOptions {
238238
pub ttl_seconds: u64,
239239
/// Expected version for optimistic concurrency (None = ignore)
240240
pub expected_version: Option<u64>,
241+
/// Block number for tracking when this write occurred
242+
pub block_id: Option<u64>,
243+
}
244+
245+
impl PutOptions {
246+
/// Create options with a specific block ID for tracking
247+
pub fn with_block(block_id: u64) -> Self {
248+
Self {
249+
block_id: Some(block_id),
250+
..Default::default()
251+
}
252+
}
241253
}
242254

243255
/// Result of a list operation

0 commit comments

Comments
 (0)