diff --git a/rest/src/main.rs b/rest/src/main.rs index 97a407b1..613c9305 100644 --- a/rest/src/main.rs +++ b/rest/src/main.rs @@ -997,6 +997,50 @@ async fn slashing_params() -> Value { }) } +fn parse_signing_info( + validator: ValidatorQueryInfo, + all_keys: Vec<(Address, [u8; 32])>, + last_signed_blocks: Vec<(Address, Option)>, + latest_block: u64, +) -> Value { + let cons_key = all_keys + .iter() + .find(|entry| (**entry).0 == validator.address.into()) + .map(|entry| (*entry).1) + .unwrap(); + + let mut hasher = sha2::Sha256::new(); + hasher.update(cons_key); + let hash = hasher.finalize().to_vec()[..20].to_vec(); + + let address = bech32::encode( + "nomicvalcons", + hash.to_vec().to_base32(), + bech32::Variant::Bech32 + ).unwrap(); + + let last_signed_block: u64 = last_signed_blocks + .iter() + .find(|entry| (**entry).0 == validator.address.into()) + .map(|entry| (*entry).1) + .unwrap() + .unwrap_or(latest_block); + + let skipped_blocks: u64 = latest_block - last_signed_block; + + json!({ + "address": address, + "start_height": "0", // TODO: fix, + "index_offset": "0", // TODO: fix, + "jailed_until": Utc.timestamp_opt(validator.jailed_until.unwrap_or(0), 0) + .unwrap() + .format("%Y-%m-%dT%H:%M:%SZ") + .to_string(), + "tombstoned": validator.tombstoned, + "missed_blocks_counter": skipped_blocks.to_string(), + }) +} + #[get("/cosmos/slashing/v1beta1/signing_infos")] async fn signing_infos() -> Value { let client = tm::HttpClient::new(app_host()).unwrap(); @@ -1022,43 +1066,12 @@ async fn signing_infos() -> Value { let mut signing_infos = vec![]; for validator in all_validators { - let cons_key = all_keys - .iter() - .find(|entry| (**entry).0 == validator.address.into()) - .map(|entry| (*entry).1) - .unwrap(); - - let mut hasher = sha2::Sha256::new(); - hasher.update(cons_key); - let hash = hasher.finalize().to_vec()[..20].to_vec(); - - let address = bech32::encode( - "nomicvalcons", - hash.to_vec().to_base32(), - bech32::Variant::Bech32 - ).unwrap(); - - let last_signed_block: u64 = last_signed_blocks - .iter() - .find(|entry| (**entry).0 == validator.address.into()) - .map(|entry| (*entry).1) - .unwrap() - .unwrap_or(latest_block); - - let skipped_blocks: u64 = latest_block - last_signed_block; - - let signing_info = json!({ - "address": address, - "start_height": "0", // TODO: fix, - "index_offset": "0", // TODO: fix, - "jailed_until": Utc.timestamp_opt(validator.jailed_until.unwrap_or(0), 0) - .unwrap() - .format("%Y-%m-%dT%H:%M:%SZ") - .to_string(), - "tombstoned": validator.tombstoned, - "missed_blocks_counter": skipped_blocks.to_string(), - }); - signing_infos.push(signing_info); + signing_infos.push(parse_signing_info( + validator, + all_keys.clone(), + last_signed_blocks.clone(), + latest_block, + )); } json!({ @@ -1070,6 +1083,49 @@ async fn signing_infos() -> Value { }) } +#[get("/cosmos/slashing/v1beta1/signing_infos/")] +async fn signing_info(cons_addr: &str) -> Value { + let client = tm::HttpClient::new(app_host()).unwrap(); + + let all_validators: Vec = app_client() + .query(|app: InnerApp| app.staking.all_validators()) + .await + .unwrap(); + + let all_keys: Vec<_> = app_client() + .query(|app: InnerApp| app.staking.consensus_keys()) + .await + .unwrap(); + + let last_signed_blocks = app_client() + .query(|app: InnerApp| app.staking.last_signed_blocks()) + .await + .unwrap(); + + let latest_block_response = client.latest_block().await.unwrap(); + let latest_block: u64 = latest_block_response.block.header.height.value(); + + let mut signing_infos = vec![]; + + for validator in all_validators { + signing_infos.push(parse_signing_info( + validator, + all_keys.clone(), + last_signed_blocks.clone(), + latest_block, + )); + } + + let signing_info = signing_infos + .iter() + .find(|value| (**value).get("address").unwrap() == cons_addr) + .unwrap(); + + json!({ + "val_signing_info": signing_info + }) +} + fn parse_block(res: tendermint_rpc::endpoint::block::Response) -> Value { let last_commit = res.block.last_commit.unwrap(); let signatures: Vec<_> = last_commit @@ -1463,6 +1519,7 @@ fn rocket() -> _ { staking_params, slashing_params, signing_infos, + signing_info, latest_block, block, latest_validator_set,