Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signing_infos and signing_info endpoint #290

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" }
106 changes: 101 additions & 5 deletions rest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use ibc_proto::ibc::core::client::v1::IdentifiedClientState;
use ibc_proto::ibc::core::connection::v1::ConnectionEnd as RawConnectionEnd;
use ibc_proto::ibc::lightclients::tendermint::v1::ClientState as RawTmClientState;

use bech32::ToBase32;
use sha2::Digest;
use tendermint_proto::types::CommitSig as RawCommitSig;
use tendermint_rpc as tm;
use tm::Client as _;
Expand Down Expand Up @@ -995,6 +997,99 @@ async fn slashing_params() -> Value {
})
}

async fn get_signing_infos() -> Vec<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;

signing_infos.push(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
}

#[get("/cosmos/slashing/v1beta1/signing_infos")]
async fn signing_infos() -> Value {
let signing_infos: Vec<_> = get_signing_infos().await;

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

#[get("/cosmos/slashing/v1beta1/signing_infos/<cons_addr>")]
async fn signing_info(cons_addr: &str) -> Value {
let signing_infos: Vec<_> = get_signing_infos().await;

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
Expand Down Expand Up @@ -1070,7 +1165,9 @@ async fn block(height: u32) -> Value {
}

fn parse_validator_set(res: tendermint_rpc::endpoint::validators::Response) -> Value {
let validators: Vec<_> = res.validators.iter()
let validators: Vec<_> = res
.validators
.iter()
.map(|validator| -> Value {
json!({
"address": validator.address,
Expand Down Expand Up @@ -1098,10 +1195,7 @@ fn parse_validator_set(res: tendermint_rpc::endpoint::validators::Response) -> V
async fn latest_validator_set() -> Value {
let client = tm::HttpClient::new(app_host()).unwrap();

let block = client
.latest_block()
.await
.unwrap();
let block = client.latest_block().await.unwrap();

let res = client
.validators(block.block.header.height, tendermint_rpc::Paging::All)
Expand Down Expand Up @@ -1392,6 +1486,8 @@ fn rocket() -> _ {
validator,
staking_params,
slashing_params,
signing_infos,
signing_info,
latest_block,
block,
latest_validator_set,
Expand Down
Loading