Skip to content

Commit

Permalink
[bridge] Add print registration info in cli (#17908)
Browse files Browse the repository at this point in the history
## Description 

Add a subcommand to display current registration info.

## Test plan 

```
sui-bridge-cli print-sui-bridge-info --sui-rpc-url https://rpc.testnet.sui.io:443
```
---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
  • Loading branch information
longbowlu authored May 29, 2024
1 parent 9f4fabc commit 9c5c7a0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/sui-bridge-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ pub enum BridgeCommand {
#[clap(long = "eth-rpc-url")]
eth_rpc_url: String,
},
/// Print current registration info
#[clap(name = "print-bridge-registration-info")]
PrintBridgeRegistrationInfo {
#[clap(long = "sui-rpc-url")]
sui_rpc_url: String,
},
/// Client to facilitate and execute Bridge actions
#[clap(name = "client")]
Client {
Expand Down
79 changes: 79 additions & 0 deletions crates/sui-bridge-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use clap::*;
use ethers::providers::Middleware;
use shared_crypto::intent::Intent;
use shared_crypto::intent::IntentMessage;
use std::collections::HashMap;
use std::str::from_utf8;
use std::sync::Arc;
use sui_bridge::client::bridge_authority_aggregator::BridgeAuthorityAggregator;
use sui_bridge::crypto::BridgeAuthorityPublicKey;
use sui_bridge::eth_transaction_builder::build_eth_transaction;
use sui_bridge::sui_client::SuiClient;
use sui_bridge::sui_transaction_builder::build_sui_transaction;
Expand All @@ -21,8 +24,13 @@ use sui_bridge_cli::{
};
use sui_config::Config;
use sui_sdk::SuiClient as SuiSdkClient;
use sui_sdk::SuiClientBuilder;
use sui_types::bridge::BridgeChainId;
use sui_types::bridge::MoveTypeCommitteeMemberRegistration;
use sui_types::committee::TOTAL_VOTING_POWER;
use sui_types::crypto::AuthorityPublicKeyBytes;
use sui_types::crypto::Signature;
use sui_types::crypto::ToFromBytes;
use sui_types::transaction::Transaction;

#[tokio::main]
Expand Down Expand Up @@ -187,6 +195,77 @@ async fn main() -> anyhow::Result<()> {
return Ok(());
}

BridgeCommand::PrintBridgeRegistrationInfo { sui_rpc_url } => {
let sui_bridge_client = SuiClient::<SuiSdkClient>::new(&sui_rpc_url).await?;
let bridge_summary = sui_bridge_client
.get_bridge_summary()
.await
.map_err(|e| anyhow::anyhow!("Failed to get bridge summary: {:?}", e))?;
let move_type_bridge_committee = bridge_summary.committee;
let sui_client = SuiClientBuilder::default().build(sui_rpc_url).await?;
let stakes = sui_client
.governance_api()
.get_committee_info(None)
.await?
.validators
.into_iter()
.collect::<HashMap<_, _>>();
let names = sui_client
.governance_api()
.get_latest_sui_system_state()
.await?
.active_validators
.into_iter()
.map(|summary| {
let protocol_key =
AuthorityPublicKeyBytes::from_bytes(&summary.protocol_pubkey_bytes)
.unwrap();
(summary.sui_address, (protocol_key, summary.name))
})
.collect::<HashMap<_, _>>();
let mut authorities = vec![];
for (_, member) in move_type_bridge_committee.member_registration {
let MoveTypeCommitteeMemberRegistration {
sui_address,
bridge_pubkey_bytes,
http_rest_url,
} = member;
let Ok(pubkey) = BridgeAuthorityPublicKey::from_bytes(&bridge_pubkey_bytes) else {
println!(
"Invalid bridge pubkey for validator {}: {:?}",
sui_address, bridge_pubkey_bytes
);
continue;
};
let Ok(base_url) = from_utf8(&http_rest_url) else {
println!(
"Invalid bridge http url for validator: {}: {:?}",
sui_address, http_rest_url
);
continue;
};
let base_url = base_url.to_string();

let (protocol_key, name) = names.get(&sui_address).unwrap();
let stake = stakes.get(protocol_key).unwrap();
authorities.push((name, sui_address, pubkey, base_url, stake));
}
let total_stake = authorities
.iter()
.map(|(_, _, _, _, stake)| **stake)
.sum::<u64>();
println!(
"Total registered stake: {}%",
total_stake as f32 / TOTAL_VOTING_POWER as f32 * 100.0
);
for (name, sui_address, pubkey, base_url, stake) in authorities {
println!(
"Name: {}, Sui Address: {}, Bridge Authority Pubkey: {}, Bridge Node URL: {}, Stake: {}",
name, sui_address, pubkey, base_url, stake
);
}
}

BridgeCommand::Client { config_path, cmd } => {
let config = BridgeCliConfig::load(config_path).expect("Couldn't load BridgeCliConfig");
let config = LoadedBridgeCliConfig::load(config).await?;
Expand Down

0 comments on commit 9c5c7a0

Please sign in to comment.