Skip to content

Commit 6093732

Browse files
committed
style: cargo fmt
1 parent f434215 commit 6093732

File tree

6 files changed

+109
-71
lines changed

6 files changed

+109
-71
lines changed

bins/platform-sudo/src/main.rs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustyline::DefaultEditor;
1010
use serde::{Deserialize, Serialize};
1111
use sp_core::{sr25519, Pair};
1212
use std::path::PathBuf;
13-
use tracing::{info, warn, error};
13+
use tracing::{error, info, warn};
1414

1515
#[derive(Parser)]
1616
#[command(name = "platform-sudo")]
@@ -104,15 +104,16 @@ impl SudoCli {
104104
fn new(rpc_url: String, sudo_key: Option<String>) -> Result<Self> {
105105
let keypair = if let Some(key) = sudo_key {
106106
// Try to parse as hex or file
107-
let key_bytes = if key.starts_with("0x") {
108-
hex::decode(&key[2..]).context("Invalid hex key")?
107+
let key_bytes = if let Some(stripped) = key.strip_prefix("0x") {
108+
hex::decode(stripped).context("Invalid hex key")?
109109
} else if std::path::Path::new(&key).exists() {
110110
std::fs::read(&key).context("Failed to read key file")?
111111
} else {
112112
hex::decode(&key).context("Invalid hex key")?
113113
};
114-
115-
let seed: [u8; 32] = key_bytes.try_into()
114+
115+
let seed: [u8; 32] = key_bytes
116+
.try_into()
116117
.map_err(|_| anyhow::anyhow!("Key must be 32 bytes"))?;
117118
Some(sr25519::Pair::from_seed(&seed))
118119
} else {
@@ -127,21 +128,29 @@ impl SudoCli {
127128
}
128129

129130
fn sign(&self, data: &[u8]) -> Result<Vec<u8>> {
130-
let keypair = self.keypair.as_ref()
131+
let keypair = self
132+
.keypair
133+
.as_ref()
131134
.ok_or_else(|| anyhow::anyhow!("Sudo key not configured"))?;
132135
Ok(keypair.sign(data).0.to_vec())
133136
}
134137

135138
fn hotkey(&self) -> Result<Hotkey> {
136-
let keypair = self.keypair.as_ref()
139+
let keypair = self
140+
.keypair
141+
.as_ref()
137142
.ok_or_else(|| anyhow::anyhow!("Sudo key not configured"))?;
138143
Ok(Hotkey(keypair.public().0))
139144
}
140145

141-
async fn upload_wasm(&self, file: &PathBuf, challenge_id: &str, name: Option<String>) -> Result<()> {
142-
let wasm_bytes = std::fs::read(file)
143-
.context("Failed to read WASM file")?;
144-
146+
async fn upload_wasm(
147+
&self,
148+
file: &PathBuf,
149+
challenge_id: &str,
150+
name: Option<String>,
151+
) -> Result<()> {
152+
let wasm_bytes = std::fs::read(file).context("Failed to read WASM file")?;
153+
145154
info!(file = %file.display(), size = wasm_bytes.len(), "Uploading WASM module");
146155

147156
let challenge_id = if challenge_id == "new" {
@@ -151,7 +160,7 @@ impl SudoCli {
151160
};
152161

153162
let timestamp = chrono::Utc::now().timestamp_millis();
154-
163+
155164
// Create the update message
156165
let update = ChallengeUpdateMessage {
157166
challenge_id,
@@ -176,7 +185,8 @@ impl SudoCli {
176185
timestamp,
177186
};
178187

179-
let response = self.client
188+
let response = self
189+
.client
180190
.post(format!("{}/sudo/challenge", self.rpc_url))
181191
.json(&request)
182192
.send()
@@ -202,7 +212,7 @@ impl SudoCli {
202212
async fn set_challenge_status(&self, challenge_id: &str, active: bool) -> Result<()> {
203213
let action = if active { "activate" } else { "deactivate" };
204214
let timestamp = chrono::Utc::now().timestamp_millis();
205-
215+
206216
let challenge_id = ChallengeId::from_string(challenge_id);
207217

208218
let update = ChallengeUpdateMessage {
@@ -226,7 +236,8 @@ impl SudoCli {
226236
timestamp,
227237
};
228238

229-
let response = self.client
239+
let response = self
240+
.client
230241
.post(format!("{}/sudo/challenge", self.rpc_url))
231242
.json(&request)
232243
.send()
@@ -250,7 +261,8 @@ impl SudoCli {
250261
}
251262

252263
async fn list_challenges(&self) -> Result<()> {
253-
let response = self.client
264+
let response = self
265+
.client
254266
.get(format!("{}/challenges", self.rpc_url))
255267
.send()
256268
.await
@@ -273,7 +285,8 @@ impl SudoCli {
273285
}
274286

275287
async fn status(&self) -> Result<()> {
276-
let response = self.client
288+
let response = self
289+
.client
277290
.get(format!("{}/health", self.rpc_url))
278291
.send()
279292
.await
@@ -294,7 +307,7 @@ impl SudoCli {
294307

295308
async fn interactive(&self) -> Result<()> {
296309
let mut rl = DefaultEditor::new()?;
297-
310+
298311
println!("\nPlatform Sudo CLI - Interactive Mode");
299312
println!("Type 'help' for available commands, 'exit' to quit\n");
300313

@@ -303,8 +316,8 @@ impl SudoCli {
303316
match readline {
304317
Ok(line) => {
305318
let _ = rl.add_history_entry(&line);
306-
let parts: Vec<&str> = line.trim().split_whitespace().collect();
307-
319+
let parts: Vec<&str> = line.split_whitespace().collect();
320+
308321
if parts.is_empty() {
309322
continue;
310323
}
@@ -314,9 +327,15 @@ impl SudoCli {
314327
println!("\nCommands:");
315328
println!(" upload <file> <challenge_id> [name] - Upload WASM module");
316329
println!(" activate <challenge_id> - Activate challenge");
317-
println!(" deactivate <challenge_id> - Deactivate challenge");
318-
println!(" list - List all challenges");
319-
println!(" status - Show validator status");
330+
println!(
331+
" deactivate <challenge_id> - Deactivate challenge"
332+
);
333+
println!(
334+
" list - List all challenges"
335+
);
336+
println!(
337+
" status - Show validator status"
338+
);
320339
println!(" exit | quit - Exit CLI\n");
321340
}
322341
"upload" if parts.len() >= 3 => {
@@ -356,8 +375,8 @@ impl SudoCli {
356375
}
357376
}
358377
}
359-
Err(rustyline::error::ReadlineError::Interrupted) |
360-
Err(rustyline::error::ReadlineError::Eof) => {
378+
Err(rustyline::error::ReadlineError::Interrupted)
379+
| Err(rustyline::error::ReadlineError::Eof) => {
361380
println!("\nGoodbye!");
362381
break;
363382
}

bins/validator-node/src/main.rs

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,14 +1096,17 @@ async fn handle_network_event(
10961096
data_bytes = update.data.len(),
10971097
"Received authorized challenge update from sudo key"
10981098
);
1099-
1099+
11001100
// Handle different update types
11011101
let challenge_id_str = update.challenge_id.to_string();
11021102
match update.update_type.as_str() {
11031103
"wasm_upload" => {
11041104
// Store WASM in distributed storage
11051105
let wasm_key = StorageKey::new("wasm", &challenge_id_str);
1106-
match storage.put(wasm_key, update.data.clone(), PutOptions::default()).await {
1106+
match storage
1107+
.put(wasm_key, update.data.clone(), PutOptions::default())
1108+
.await
1109+
{
11071110
Ok(metadata) => {
11081111
info!(
11091112
challenge_id = %update.challenge_id,
@@ -1114,19 +1117,21 @@ async fn handle_network_event(
11141117
// Register challenge in state if not exists
11151118
state_manager.apply(|state| {
11161119
if state.get_challenge(&update.challenge_id).is_none() {
1117-
let challenge_config = platform_p2p_consensus::ChallengeConfig {
1118-
id: update.challenge_id,
1119-
name: challenge_id_str.clone(),
1120-
weight: 100, // Default weight
1121-
is_active: true,
1122-
creator: update.updater.clone(),
1123-
created_at: chrono::Utc::now().timestamp_millis(),
1124-
wasm_hash: metadata.value_hash,
1125-
};
1120+
let challenge_config =
1121+
platform_p2p_consensus::ChallengeConfig {
1122+
id: update.challenge_id,
1123+
name: challenge_id_str.clone(),
1124+
weight: 100, // Default weight
1125+
is_active: true,
1126+
creator: update.updater.clone(),
1127+
created_at: chrono::Utc::now()
1128+
.timestamp_millis(),
1129+
wasm_hash: metadata.value_hash,
1130+
};
11261131
state.add_challenge(challenge_config);
11271132
}
11281133
});
1129-
1134+
11301135
// Load and log WASM routes
11311136
if let Some(ref executor) = wasm_executor_ref {
11321137
match executor.execute_get_routes(
@@ -1195,7 +1200,7 @@ async fn handle_network_event(
11951200
);
11961201
}
11971202
}
1198-
1203+
11991204
// Invalidate WASM cache
12001205
if let Some(ref executor) = wasm_executor_ref {
12011206
executor.invalidate_cache(&challenge_id_str);
@@ -1217,10 +1222,10 @@ async fn handle_network_event(
12171222
value_len = proposal.value.len(),
12181223
"Received storage proposal"
12191224
);
1220-
1225+
12211226
// Verify proposer is a known validator
12221227
let proposer_valid = validator_set.is_validator(&proposal.proposer);
1223-
1228+
12241229
if !proposer_valid {
12251230
warn!(
12261231
proposer = %proposal.proposer.to_hex(),
@@ -1238,30 +1243,33 @@ async fn handle_network_event(
12381243
votes: std::collections::HashMap::new(),
12391244
finalized: false,
12401245
};
1241-
1246+
12421247
state_manager.apply(|state| {
12431248
state.add_storage_proposal(storage_proposal);
12441249
});
1245-
1250+
12461251
// Auto-vote approve (validator trusts other validators)
12471252
// In production, could verify via WASM validate_storage_write
12481253
let my_hotkey = keypair.hotkey();
12491254
let timestamp = chrono::Utc::now().timestamp_millis();
1250-
1255+
12511256
// Sign the vote
12521257
let vote_data = bincode::serialize(&(&proposal.proposal_id, true, timestamp))
12531258
.unwrap_or_default();
12541259
let signature = keypair.sign_bytes(&vote_data).unwrap_or_default();
1255-
1260+
12561261
let vote_msg = P2PMessage::StorageVote(StorageVoteMessage {
12571262
proposal_id: proposal.proposal_id,
12581263
voter: my_hotkey,
12591264
approve: true,
12601265
timestamp,
12611266
signature,
12621267
});
1263-
1264-
if let Err(e) = p2p_cmd_tx.send(platform_p2p_consensus::P2PCommand::Broadcast(vote_msg)).await {
1268+
1269+
if let Err(e) = p2p_cmd_tx
1270+
.send(platform_p2p_consensus::P2PCommand::Broadcast(vote_msg))
1271+
.await
1272+
{
12651273
warn!(error = %e, "Failed to broadcast storage vote");
12661274
}
12671275
}
@@ -1273,29 +1281,35 @@ async fn handle_network_event(
12731281
approve = vote.approve,
12741282
"Received storage vote"
12751283
);
1276-
1284+
12771285
// Verify voter is a known validator
12781286
if !validator_set.is_validator(&vote.voter) {
12791287
warn!(voter = %vote.voter.to_hex(), "Vote from unknown validator");
12801288
} else {
12811289
// Add vote to proposal
12821290
let consensus_result = state_manager.apply(|state| {
1283-
state.vote_storage_proposal(&vote.proposal_id, vote.voter.clone(), vote.approve)
1291+
state.vote_storage_proposal(
1292+
&vote.proposal_id,
1293+
vote.voter.clone(),
1294+
vote.approve,
1295+
)
12841296
});
1285-
1297+
12861298
// If consensus reached and approved, write to distributed storage
12871299
if let Some(true) = consensus_result {
1288-
let proposal_opt = state_manager.apply(|state| {
1289-
state.remove_storage_proposal(&vote.proposal_id)
1290-
});
1291-
1300+
let proposal_opt = state_manager
1301+
.apply(|state| state.remove_storage_proposal(&vote.proposal_id));
1302+
12921303
if let Some(proposal) = proposal_opt {
12931304
let storage_key = StorageKey::new(
12941305
&format!("challenge:{}", proposal.challenge_id),
12951306
&proposal.key,
12961307
);
1297-
1298-
match storage.put(storage_key, proposal.value.clone(), PutOptions::default()).await {
1308+
1309+
match storage
1310+
.put(storage_key, proposal.value.clone(), PutOptions::default())
1311+
.await
1312+
{
12991313
Ok(_) => {
13001314
info!(
13011315
proposal_id = %hex::encode(&proposal.proposal_id[..8]),

bins/validator-node/src/wasm_executor.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ impl WasmChallengeExecutor {
928928
.await
929929
})
930930
});
931-
931+
932932
match result {
933933
Ok(Some(stored)) => {
934934
info!(
@@ -939,7 +939,10 @@ impl WasmChallengeExecutor {
939939
Some(stored.data)
940940
}
941941
Ok(None) => {
942-
debug!(module = module_path, "WASM module not found in distributed storage");
942+
debug!(
943+
module = module_path,
944+
"WASM module not found in distributed storage"
945+
);
943946
None
944947
}
945948
Err(e) => {
@@ -956,8 +959,9 @@ impl WasmChallengeExecutor {
956959
Some(bytes) => bytes,
957960
None => {
958961
let full_path = self.config.module_dir.join(module_path);
959-
std::fs::read(&full_path)
960-
.with_context(|| format!("Failed to read WASM module from {}", full_path.display()))?
962+
std::fs::read(&full_path).with_context(|| {
963+
format!("Failed to read WASM module from {}", full_path.display())
964+
})?
961965
}
962966
};
963967

crates/p2p-consensus/src/state.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl ChainState {
459459
approve: bool,
460460
) -> Option<bool> {
461461
let total_validators = self.validators.len();
462-
462+
463463
if let Some(proposal) = self.pending_storage_proposals.get_mut(proposal_id) {
464464
if proposal.finalized {
465465
return None;
@@ -1304,6 +1304,7 @@ mod tests {
13041304
is_active: true,
13051305
creator: Hotkey([0u8; 32]),
13061306
created_at: chrono::Utc::now().timestamp_millis(),
1307+
wasm_hash: [0u8; 32],
13071308
};
13081309

13091310
let id = config.id;

0 commit comments

Comments
 (0)