Skip to content

Commit

Permalink
Add signing_infos endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Mar 21, 2024
1 parent 2d44997 commit 2640b77
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ default-run = "nomic"

[dependencies]
bitcoin = { version = "0.29.2", features = ["serde", "rand"] }
orga = { git = "https://github.com/nomic-io/orga.git", rev = "8e180ff2a839de81152d8f65be98d18264fbfa14", features = [
orga = { git = "https://github.com/nomic-io/orga.git", rev = "c879e1fbf9df4133ca9df062f177aea6c32ea2c5", features = [
"merk-verify",
] }
thiserror = "1.0.30"
Expand Down
6 changes: 4 additions & 2 deletions rest/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ serde_json = "1.0.78"
lazy_static = "1.4.0"
tokio = "1.19.2"
chrono = { version = "0.4.31", features = ["serde"] }
sha2 = "0.10.6"
bech32 = { version = "0.9.1" }
76 changes: 76 additions & 0 deletions rest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use ibc_proto::ibc::lightclients::tendermint::v1::ClientState as RawTmClientStat
use tendermint_proto::types::CommitSig as RawCommitSig;
use tendermint_rpc as tm;
use tm::Client as _;
use bech32::ToBase32;
use sha2::Digest;

lazy_static::lazy_static! {
static ref QUERY_CACHE: Arc<RwLock<HashMap<String, (u64, String)>>> = Arc::new(RwLock::new(HashMap::new()));
Expand Down Expand Up @@ -995,6 +997,79 @@ async fn slashing_params() -> Value {
})
}

#[get("/cosmos/slashing/v1beta1/signing_infos")]
async fn signing_infos() -> Value {
let client = tm::HttpClient::new(app_host()).unwrap();

let all_validators: Vec<ValidatorQueryInfo> = 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 {
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);
}

json!({
"info": signing_infos,
"pagination": {
"next_key": null,
"total": signing_infos.len().to_string(),
}
})
}

fn parse_block(res: tendermint_rpc::endpoint::block::Response) -> Value {
let last_commit = res.block.last_commit.unwrap();
let signatures: Vec<_> = last_commit
Expand Down Expand Up @@ -1387,6 +1462,7 @@ fn rocket() -> _ {
validator,
staking_params,
slashing_params,
signing_infos,
latest_block,
block,
latest_validator_set,
Expand Down

0 comments on commit 2640b77

Please sign in to comment.