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

feat: add /peers API endpoint to torii #5235

Merged
merged 1 commit into from
Nov 19, 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
2 changes: 1 addition & 1 deletion crates/iroha/tests/status_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::task::spawn_blocking;
fn status_eq_excluding_uptime_and_queue(lhs: &Status, rhs: &Status) -> bool {
lhs.peers == rhs.peers
&& lhs.blocks == rhs.blocks
&& lhs.txs_accepted == rhs.txs_accepted
&& lhs.txs_approved == rhs.txs_approved
&& lhs.txs_rejected == rhs.txs_rejected
&& lhs.view_changes == rhs.view_changes
}
Expand Down
2 changes: 1 addition & 1 deletion crates/iroha_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub const TX_RETRIEVAL_INTERVAL: Duration = Duration::from_millis(100);
pub type IrohaNetwork = iroha_p2p::NetworkHandle<NetworkMessage>;

/// Ids of peers.
pub type PeersIds = UniqueVec<PeerId>;
pub type Peers = UniqueVec<PeerId>;

/// Type of `Sender<EventBox>` which should be used for channels of `Event` messages.
pub type EventsSender = broadcast::Sender<EventBox>;
Expand Down
12 changes: 9 additions & 3 deletions crates/iroha_core/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{num::NonZeroUsize, sync::Arc, time::SystemTime};

use eyre::{Result, WrapErr as _};
use iroha_data_model::peer::Peer;
use iroha_telemetry::metrics::Metrics;
use mv::storage::StorageReadOnly;
use parking_lot::Mutex;
Expand Down Expand Up @@ -76,20 +77,20 @@ impl MetricsReporter {
};
block_index += 1;
let block_txs_rejected = block.errors().count() as u64;
let block_txs_accepted = block.transactions().count() as u64 - block_txs_rejected;
let block_txs_approved = block.transactions().count() as u64 - block_txs_rejected;

self.metrics
.txs
.with_label_values(&["accepted"])
.inc_by(block_txs_accepted);
.inc_by(block_txs_approved);
self.metrics
.txs
.with_label_values(&["rejected"])
.inc_by(block_txs_rejected);
self.metrics
.txs
.with_label_values(&["total"])
.inc_by(block_txs_accepted + block_txs_rejected);
.inc_by(block_txs_approved + block_txs_rejected);
self.metrics.block_height.inc();
}
*lastest_block_height = block_index;
Expand Down Expand Up @@ -147,4 +148,9 @@ impl MetricsReporter {
pub fn metrics(&self) -> &Metrics {
&self.metrics
}

/// Last known online peers
pub fn online_peers(&self) -> Vec<Peer> {
self.network.online_peers(|x| x.iter().cloned().collect())
}
}
8 changes: 4 additions & 4 deletions crates/iroha_core/src/smartcontracts/isi/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ pub mod isi {
let peer_id = self.object;

let world = &mut state_transaction.world;
if let PushResult::Duplicate(duplicate) = world.trusted_peers_ids.push(peer_id.clone())
{
if let PushResult::Duplicate(duplicate) = world.peers.push(peer_id.clone()) {
return Err(RepetitionError {
instruction: InstructionType::Register,
id: IdBox::PeerId(duplicate),
Expand All @@ -63,11 +62,11 @@ pub mod isi {
) -> Result<(), Error> {
let peer_id = self.object;
let world = &mut state_transaction.world;
let Some(index) = world.trusted_peers_ids.iter().position(|id| id == &peer_id) else {
let Some(index) = world.peers.iter().position(|id| id == &peer_id) else {
return Err(FindError::Peer(peer_id).into());
};

world.trusted_peers_ids.remove(index);
world.peers.remove(index);

world.emit_events(Some(PeerEvent::Removed(peer_id)));

Expand Down Expand Up @@ -513,6 +512,7 @@ pub mod query {
Ok(state_ro
.world()
.peers()
.into_iter()
.filter(move |peer| filter.applies(peer))
.cloned())
}
Expand Down
66 changes: 27 additions & 39 deletions crates/iroha_core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use crate::{
},
wasm, Execute,
},
PeersIds,
Peers,
};

/// The global entity consisting of `domains`, `triggers` and etc.
Expand All @@ -65,8 +65,8 @@ use crate::{
pub struct World {
/// Iroha on-chain parameters.
pub(crate) parameters: Cell<Parameters>,
/// Identifications of discovered trusted peers.
pub(crate) trusted_peers_ids: Cell<PeersIds>,
/// Identifications of discovered peers.
pub(crate) peers: Cell<Peers>,
mversic marked this conversation as resolved.
Show resolved Hide resolved
/// Registered domains.
pub(crate) domains: Storage<DomainId, Domain>,
/// Registered accounts.
Expand All @@ -93,8 +93,8 @@ pub struct World {
pub struct WorldBlock<'world> {
/// Iroha on-chain parameters.
pub parameters: CellBlock<'world, Parameters>,
/// Identifications of discovered trusted peers.
pub(crate) trusted_peers_ids: CellBlock<'world, PeersIds>,
/// Identifications of discovered peers.
pub(crate) peers: CellBlock<'world, Peers>,
/// Registered domains.
pub(crate) domains: StorageBlock<'world, DomainId, Domain>,
/// Registered accounts.
Expand Down Expand Up @@ -123,8 +123,8 @@ pub struct WorldBlock<'world> {
pub struct WorldTransaction<'block, 'world> {
/// Iroha on-chain parameters.
pub(crate) parameters: CellTransaction<'block, 'world, Parameters>,
/// Identifications of discovered trusted peers.
pub(crate) trusted_peers_ids: CellTransaction<'block, 'world, PeersIds>,
/// Identifications of discovered peers.
pub(crate) peers: CellTransaction<'block, 'world, Peers>,
/// Registered domains.
pub(crate) domains: StorageTransaction<'block, 'world, DomainId, Domain>,
/// Registered accounts.
Expand Down Expand Up @@ -162,8 +162,8 @@ struct TransactionEventBuffer<'block> {
pub struct WorldView<'world> {
/// Iroha on-chain parameters.
pub(crate) parameters: CellView<'world, Parameters>,
/// Identifications of discovered trusted peers.
pub(crate) trusted_peers_ids: CellView<'world, PeersIds>,
/// Identifications of discovered peers.
pub(crate) peers: CellView<'world, Peers>,
/// Registered domains.
pub(crate) domains: StorageView<'world, DomainId, Domain>,
/// Registered accounts.
Expand Down Expand Up @@ -303,7 +303,7 @@ impl World {
Self::default()
}

/// Creates a [`World`] with these [`Domain`]s and trusted [`PeerId`]s.
/// Creates a [`World`] with these [`Domain`]s and [`Peer`]s.
pub fn with<D, A, Ad>(domains: D, accounts: A, asset_definitions: Ad) -> Self
where
D: IntoIterator<Item = Domain>,
Expand All @@ -313,7 +313,7 @@ impl World {
Self::with_assets(domains, accounts, asset_definitions, [])
}

/// Creates a [`World`] with these [`Domain`]s and trusted [`PeerId`]s.
/// Creates a [`World`] with these [`Domain`]s and [`Peer`]s.
pub fn with_assets<D, A, Ad, As>(
domains: D,
accounts: A,
Expand Down Expand Up @@ -352,7 +352,7 @@ impl World {
pub fn block(&self) -> WorldBlock {
WorldBlock {
parameters: self.parameters.block(),
trusted_peers_ids: self.trusted_peers_ids.block(),
peers: self.peers.block(),
domains: self.domains.block(),
accounts: self.accounts.block(),
asset_definitions: self.asset_definitions.block(),
Expand All @@ -371,7 +371,7 @@ impl World {
pub fn block_and_revert(&self) -> WorldBlock {
WorldBlock {
parameters: self.parameters.block_and_revert(),
trusted_peers_ids: self.trusted_peers_ids.block_and_revert(),
peers: self.peers.block_and_revert(),
domains: self.domains.block_and_revert(),
accounts: self.accounts.block_and_revert(),
asset_definitions: self.asset_definitions.block_and_revert(),
Expand All @@ -390,7 +390,7 @@ impl World {
pub fn view(&self) -> WorldView {
WorldView {
parameters: self.parameters.view(),
trusted_peers_ids: self.trusted_peers_ids.view(),
peers: self.peers.view(),
domains: self.domains.view(),
accounts: self.accounts.view(),
asset_definitions: self.asset_definitions.view(),
Expand All @@ -409,7 +409,7 @@ impl World {
#[allow(missing_docs)]
pub trait WorldReadOnly {
fn parameters(&self) -> &Parameters;
fn trusted_peers_ids(&self) -> &PeersIds;
fn peers(&self) -> &Peers;
fn domains(&self) -> &impl StorageReadOnly<DomainId, Domain>;
fn accounts(&self) -> &impl StorageReadOnly<AccountId, Account>;
fn asset_definitions(&self) -> &impl StorageReadOnly<AssetDefinitionId, AssetDefinition>;
Expand Down Expand Up @@ -635,17 +635,6 @@ pub trait WorldReadOnly {
fn asset_total_amount(&self, definition_id: &AssetDefinitionId) -> Result<Numeric, FindError> {
Ok(self.asset_definition(definition_id)?.total_quantity)
}

/// Get an immutable iterator over the [`PeerId`]s.
fn peers(&self) -> impl ExactSizeIterator<Item = &PeerId> {
self.trusted_peers_ids().iter()
}

/// Returns reference for trusted peer ids
#[inline]
fn peers_ids(&self) -> &PeersIds {
self.trusted_peers_ids()
}
}

macro_rules! impl_world_ro {
Expand All @@ -654,8 +643,8 @@ macro_rules! impl_world_ro {
fn parameters(&self) -> &Parameters {
&self.parameters
}
fn trusted_peers_ids(&self) -> &PeersIds {
&self.trusted_peers_ids
fn peers(&self) -> &Peers {
&self.peers
}
fn domains(&self) -> &impl StorageReadOnly<DomainId, Domain> {
&self.domains
Expand Down Expand Up @@ -700,7 +689,7 @@ impl<'world> WorldBlock<'world> {
pub fn trasaction(&mut self) -> WorldTransaction<'_, 'world> {
WorldTransaction {
parameters: self.parameters.transaction(),
trusted_peers_ids: self.trusted_peers_ids.transaction(),
peers: self.peers.transaction(),
domains: self.domains.transaction(),
accounts: self.accounts.transaction(),
asset_definitions: self.asset_definitions.transaction(),
Expand All @@ -723,7 +712,7 @@ impl<'world> WorldBlock<'world> {
// NOTE: intentionally destruct self not to forget commit some fields
let Self {
parameters,
trusted_peers_ids,
peers,
domains,
accounts,
asset_definitions,
Expand All @@ -747,7 +736,7 @@ impl<'world> WorldBlock<'world> {
asset_definitions.commit();
accounts.commit();
domains.commit();
trusted_peers_ids.commit();
peers.commit();
parameters.commit();
}
}
Expand All @@ -758,7 +747,7 @@ impl WorldTransaction<'_, '_> {
// NOTE: intentionally destruct self not to forget commit some fields
let Self {
parameters,
trusted_peers_ids,
peers,
domains,
accounts,
asset_definitions,
Expand All @@ -781,7 +770,7 @@ impl WorldTransaction<'_, '_> {
asset_definitions.apply();
accounts.apply();
domains.apply();
trusted_peers_ids.apply();
peers.apply();
parameters.apply();
events_buffer.events_created_in_transaction = 0;
}
Expand Down Expand Up @@ -1864,7 +1853,7 @@ pub(crate) mod deserialize {
M: MapAccess<'de>,
{
let mut parameters = None;
let mut trusted_peers_ids = None;
let mut peers = None;
let mut domains = None;
let mut accounts = None;
let mut asset_definitions = None;
Expand All @@ -1881,8 +1870,8 @@ pub(crate) mod deserialize {
"parameters" => {
parameters = Some(map.next_value()?);
}
"trusted_peers_ids" => {
trusted_peers_ids = Some(map.next_value()?);
"peers" => {
peers = Some(map.next_value()?);
}
"domains" => {
domains = Some(map.next_value()?);
Expand Down Expand Up @@ -1925,8 +1914,7 @@ pub(crate) mod deserialize {
Ok(World {
parameters: parameters
.ok_or_else(|| serde::de::Error::missing_field("parameters"))?,
trusted_peers_ids: trusted_peers_ids
.ok_or_else(|| serde::de::Error::missing_field("trusted_peers_ids"))?,
peers: peers.ok_or_else(|| serde::de::Error::missing_field("peers"))?,
domains: domains
.ok_or_else(|| serde::de::Error::missing_field("domains"))?,
accounts: accounts
Expand Down Expand Up @@ -1955,7 +1943,7 @@ pub(crate) mod deserialize {
"World",
&[
"parameters",
"trusted_peers_ids",
"peers",
"domains",
"roles",
"account_permissions",
Expand Down
6 changes: 3 additions & 3 deletions crates/iroha_core/src/sumeragi/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl Sumeragi {
}

// NOTE: By this time genesis block is executed and list of trusted peers is updated
self.topology = Topology::new(state_block.world.trusted_peers_ids.clone());
self.topology = Topology::new(state_block.world.peers.clone());
self.commit_block(block, state_block);
return Ok(());
}
Expand Down Expand Up @@ -318,7 +318,7 @@ impl Sumeragi {
);

// NOTE: By this time genesis block is executed and list of trusted peers is updated
self.topology = Topology::new(state_block.world.trusted_peers_ids.clone());
self.topology = Topology::new(state_block.world.peers.clone());

let genesis = genesis
.commit(&self.topology)
Expand All @@ -343,7 +343,7 @@ impl Sumeragi {
let prev_role = self.role();

self.topology
.block_committed(state_block.world.peers().cloned());
.block_committed(state_block.world.peers().clone());

let state_events =
state_block.apply_without_execution(&block, self.topology.as_ref().to_owned());
Expand Down
4 changes: 2 additions & 2 deletions crates/iroha_core/src/sumeragi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ impl SumeragiHandle {
.expect("INTERNAL BUG: Invalid block stored in Kura");

if block.as_ref().header().is_genesis() {
*topology = Topology::new(state_block.world.trusted_peers_ids.clone());
*topology = Topology::new(state_block.world.peers.clone());
}

topology.block_committed(state_block.world.peers().cloned());
topology.block_committed(state_block.world.peers().clone());

state_block
.apply_without_execution(&block, topology.as_ref().to_owned())
Expand Down
Loading
Loading