Skip to content

Commit

Permalink
Add a plain list of services to web-client peer info structs
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou authored and jsdanielh committed Dec 6, 2024
1 parent a3831f2 commit c801676
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
19 changes: 18 additions & 1 deletion web-client/example/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ window.Nimiq = Nimiq;
init().then(async () => {
const config = new Nimiq.ClientConfiguration();
config.logLevel('debug');
// config.network('testalbatross');
// config.seedNodes([
// "/dns4/seed1.pos.nimiq-testnet.com/tcp/8443/wss",
// "/dns4/seed2.pos.nimiq-testnet.com/tcp/8443/wss",
// "/dns4/seed3.pos.nimiq-testnet.com/tcp/8443/wss",
// "/dns4/seed4.pos.nimiq-testnet.com/tcp/8443/wss",
// ]);

const client = await Nimiq.Client.create(config.build());
window.client = client; // Prevent garbage collection and for playing around
Expand Down Expand Up @@ -46,7 +53,17 @@ init().then(async () => {

document.querySelector('#address-book').addEventListener("click", async () => {
let contacts = await client.getAddressBook();
console.table(contacts);
console.log(contacts
.sort((a, b) => {
// Sort seeds first
const aIsSeed = a.address.includes('seed');
const bIsSeed = b.address.includes('seed');
if (aIsSeed && !bIsSeed) return -1;
if (!aIsSeed && bIsSeed) return 1;

// Sort alphabetically
return a.address.localeCompare(b.address);
}));
});

document.querySelector('#query-staking-contract').addEventListener("click", async () => {
Expand Down
41 changes: 41 additions & 0 deletions web-client/src/client/peer_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,49 @@ use wasm_bindgen::prelude::*;
#[derive(serde::Serialize, Tsify)]
#[serde(rename_all = "camelCase")]
pub struct PlainPeerInfo {
/// A libp2p peer ID
pub peer_id: String,
/// Address of the peer in `Multiaddr` format
address: String,
/// Node type of the peer
#[tsify(type = "'full' | 'history' | 'light'")]
#[serde(rename = "type")]
node_type: String,
/// List of services the peer is providing
services: Vec<PlainService>,
}

/// Available peer service flags
#[derive(serde::Serialize, Tsify)]
#[serde(rename_all = "kebab-case")]
pub enum PlainService {
FullBlocks,
History,
AccountsProof,
AccountsChunk,
Mempool,
TransactionIndex,
Validator,
PreGenesisTransactions,

// Catch-all to not have to panic when new services are added
Unknown,
}

impl From<Services> for PlainService {
fn from(services: Services) -> Self {
match services {
Services::FULL_BLOCKS => Self::FullBlocks,
Services::HISTORY => Self::History,
Services::ACCOUNTS_PROOF => Self::AccountsProof,
Services::ACCOUNTS_CHUNKS => Self::AccountsChunk,
Services::MEMPOOL => Self::Mempool,
Services::TRANSACTION_INDEX => Self::TransactionIndex,
Services::VALIDATOR => Self::Validator,
Services::PRE_GENESIS_TRANSACTIONS => Self::PreGenesisTransactions,
_ => Self::Unknown,
}
}
}

impl PlainPeerInfo {
Expand All @@ -35,6 +71,11 @@ impl PlainPeerInfo {
peer_id,
address: peer_info.get_address().to_string(),
node_type: node_type.to_string(),
services: peer_info
.get_services()
.into_iter()
.map(|s| s.into())
.collect(),
}
}
}
Expand Down

0 comments on commit c801676

Please sign in to comment.