Skip to content

Commit

Permalink
chore!: simplify peer id parsing (#5228)
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic authored Nov 12, 2024
1 parent e266b64 commit 2a30fc9
Show file tree
Hide file tree
Showing 20 changed files with 151 additions and 129 deletions.
2 changes: 1 addition & 1 deletion crates/iroha/tests/extra_functional/connected_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn register_new_peer() -> Result<()> {
network
.config()
// only one random peer
.write(["sumeragi", "trusted_peers"], [network.peer().peer()]),
.write(["trusted_peers"], [network.peer().peer()]),
None,
)
.await;
Expand Down
23 changes: 7 additions & 16 deletions crates/iroha_config/iroha_test_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ chain = "00000000-0000-0000-0000-000000000000"
public_key = "ed01201C61FAF8FE94E253B93114240394F79A607B7FA55F9E5A41EBEC74B88055768B"
private_key = "802620282ED9F3CF92811C3818DBC4AE594ED59DC1A2F78E4241E31924E101D6B1FB83"

trusted_peers = [
"ed01201C61FAF8FE94E253B93114240394F79A607B7FA55F9E5A41EBEC74B88055768B@127.0.0.1:1337",
"ed0120CC25624D62896D3A0BFD8940F928DC2ABF27CC57CEFEB442AA96D9081AAE58A1@127.0.0.1:1338",
"ed0120FACA9E8AA83225CB4D16D67F27DD4F93FC30FFA11ADC1F5C88FD5495ECC91020@127.0.0.1:1339",
"ed01208E351A70B6A603ED285D666B8D689B680865913BA03CE29FB7D13A166C4E7F1F@127.0.0.1:1340",
]

[network]
address = "127.0.0.1:1337"
public_address = "127.0.0.1:1337"
Expand All @@ -13,21 +20,5 @@ file = "./genesis.signed.scale"
[torii]
address = "127.0.0.1:8080"

[[sumeragi.trusted_peers]]
address = "127.0.0.1:1337"
public_key = "ed01201C61FAF8FE94E253B93114240394F79A607B7FA55F9E5A41EBEC74B88055768B"

[[sumeragi.trusted_peers]]
address = "127.0.0.1:1338"
public_key = "ed0120CC25624D62896D3A0BFD8940F928DC2ABF27CC57CEFEB442AA96D9081AAE58A1"

[[sumeragi.trusted_peers]]
address = "127.0.0.1:1339"
public_key = "ed0120FACA9E8AA83225CB4D16D67F27DD4F93FC30FFA11ADC1F5C88FD5495ECC91020"

[[sumeragi.trusted_peers]]
address = "127.0.0.1:1340"
public_key = "ed01208E351A70B6A603ED285D666B8D689B680865913BA03CE29FB7D13A166C4E7F1F"

[logger]
format = "pretty"
4 changes: 2 additions & 2 deletions crates/iroha_config/src/parameters/actual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct Common {
pub chain: ChainId,
pub key_pair: KeyPair,
pub peer: Peer,
pub trusted_peers: WithOrigin<TrustedPeers>,
}

/// Network options
Expand Down Expand Up @@ -119,10 +120,9 @@ impl Default for Queue {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Copy, Clone)]
#[allow(missing_docs)]
pub struct Sumeragi {
pub trusted_peers: WithOrigin<TrustedPeers>,
pub debug_force_soft_fork: bool,
}

Expand Down
50 changes: 27 additions & 23 deletions crates/iroha_config/src/parameters/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub struct Root {
public_key: WithOrigin<PublicKey>,
#[config(env = "PRIVATE_KEY")]
private_key: WithOrigin<PrivateKey>,
#[config(env = "TRUSTED_PEERS", default)]
trusted_peers: WithOrigin<TrustedPeers>,
#[config(nested)]
genesis: Genesis,
#[config(nested)]
Expand Down Expand Up @@ -104,36 +106,45 @@ impl Root {
.change_context(ParseError::BadKeyPair)
.ok_or_emit(&mut emitter);

let (network, block_sync, transaction_gossiper) = self.network.parse();
let Some((peer, trusted_peers)) = key_pair.as_ref().map(|key_pair| {
let peer = Peer::new(
network.address.value().clone(),
key_pair.public_key().clone(),
);

(
peer.clone(),
self.trusted_peers.map(|x| actual::TrustedPeers {
myself: peer,
others: x.0,
}),
)
}) else {
panic!("Key pair is missing");
};

let genesis = self.genesis.into();

let kura = self.kura.parse();

let (network, block_sync, transaction_gossiper) = self.network.parse();
let logger = self.logger;
let queue = self.queue;
let snapshot = self.snapshot;
let dev_telemetry = self.dev_telemetry;
let (torii, live_query_store) = self.torii.parse();
let telemetry = self.telemetry.map(actual::Telemetry::from);

let peer = key_pair.as_ref().map(|key_pair| {
Peer::new(
network.address.value().clone(),
key_pair.public_key().clone(),
)
});

let sumeragi = peer
.as_ref()
.map(|peer| self.sumeragi.parse_and_push_self(peer.clone()));
let sumeragi = self.sumeragi.parse();

emitter.into_result()?;

let key_pair = key_pair.unwrap();
let peer = actual::Common {
chain: self.chain.0,
key_pair,
peer: peer.unwrap(),
peer,
trusted_peers,
};

Ok(actual::Root {
Expand All @@ -142,7 +153,7 @@ impl Root {
genesis,
torii,
kura,
sumeragi: sumeragi.unwrap(),
sumeragi,
block_sync,
transaction_gossiper,
live_query_store,
Expand Down Expand Up @@ -211,16 +222,14 @@ impl Kura {
}
}

#[derive(Debug, Copy, Clone, ReadConfig)]
#[derive(Debug, Clone, Copy, ReadConfig)]
pub struct KuraDebug {
#[config(env = "KURA_DEBUG_OUTPUT_NEW_BLOCKS", default)]
output_new_blocks: bool,
}

#[derive(Debug, ReadConfig)]
#[derive(Debug, Clone, Copy, ReadConfig)]
pub struct Sumeragi {
#[config(env = "TRUSTED_PEERS", default)]
pub trusted_peers: WithOrigin<TrustedPeers>,
#[config(nested)]
pub debug: SumeragiDebug,
}
Expand All @@ -246,17 +255,12 @@ impl Default for TrustedPeers {
}

impl Sumeragi {
fn parse_and_push_self(self, self_id: Peer) -> actual::Sumeragi {
fn parse(self) -> actual::Sumeragi {
let Self {
trusted_peers,
debug: SumeragiDebug { force_soft_fork },
} = self;

actual::Sumeragi {
trusted_peers: trusted_peers.map(|x| actual::TrustedPeers {
myself: self_id,
others: x.0,
}),
debug_force_soft_fork: force_soft_fork,
}
}
Expand Down
42 changes: 21 additions & 21 deletions crates/iroha_config/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ fn minimal_config_snapshot() {
address: 127.0.0.1:1337,
id: ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB,
},
trusted_peers: WithOrigin {
value: TrustedPeers {
myself: Peer {
address: 127.0.0.1:1337,
id: ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB,
},
others: UniqueVec(
[
Peer {
address: 127.0.0.1:1338,
id: ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB,
},
],
),
},
origin: File {
id: ParameterId(trusted_peers),
path: "tests/fixtures/base_trusted_peers.toml",
},
},
},
network: Network {
address: WithOrigin {
Expand Down Expand Up @@ -135,26 +155,6 @@ fn minimal_config_snapshot() {
debug_output_new_blocks: false,
},
sumeragi: Sumeragi {
trusted_peers: WithOrigin {
value: TrustedPeers {
myself: Peer {
address: 127.0.0.1:1337,
id: ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB,
},
others: UniqueVec(
[
Peer {
address: 127.0.0.1:1338,
id: ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB,
},
],
),
},
origin: File {
id: ParameterId(sumeragi.trusted_peers),
path: "tests/fixtures/base_trusted_peers.toml",
},
},
debug_force_soft_fork: false,
},
block_sync: BlockSync {
Expand Down Expand Up @@ -210,7 +210,7 @@ fn self_is_presented_in_trusted_peers() {
load_config_from_fixtures("minimal_alone_with_genesis.toml").expect("valid config");

assert!(config
.sumeragi
.common
.trusted_peers
.value()
.clone()
Expand Down
6 changes: 3 additions & 3 deletions crates/iroha_config/tests/fixtures/base_trusted_peers.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[[sumeragi.trusted_peers]]
address = "127.0.0.1:1338"
public_key = "ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"
trusted_peers = [
"ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB@127.0.0.1:1338",
]
2 changes: 1 addition & 1 deletion crates/iroha_config/tests/fixtures/full.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ LOG_LEVEL=DEBUG
LOG_FORMAT=pretty
SNAPSHOT_MODE=read_write
SNAPSHOT_STORE_DIR=/snapshot/path/from/env
TRUSTED_PEERS=[{"address":"iroha2:1339","public_key":"ed0120312C1B7B5DE23D366ADCF23CD6DB92CE18B2AA283C7D9F5033B969C2DC2B92F4"}]
TRUSTED_PEERS=["ed0120312C1B7B5DE23D366ADCF23CD6DB92CE18B2AA283C7D9F5033B969C2DC2B92F4@iroha2:1339"]
8 changes: 4 additions & 4 deletions crates/iroha_config/tests/fixtures/full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ chain = "0"
public_key = "ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"
private_key = "8026208F4C15E5D664DA3F13778801D23D4E89B76E94C1B94B389544168B6CB894F84F"

trusted_peers = [
"ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB@localhost:8081",
]

[genesis]
public_key = "ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"
file = "genesis.signed.scale"
Expand Down Expand Up @@ -32,10 +36,6 @@ blocks_in_memory = 128
[kura.debug]
output_new_blocks = true

[[sumeragi.trusted_peers]]
address = "localhost:8081"
public_key = "ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"

[sumeragi.debug]
force_soft_fork = true

Expand Down
17 changes: 7 additions & 10 deletions crates/iroha_core/src/sumeragi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ impl SumeragiStartArgs {
#[allow(clippy::too_many_lines)]
pub fn start(self, shutdown_signal: ShutdownSignal) -> (SumeragiHandle, Child) {
let Self {
sumeragi_config,
config: SumeragiConfig {
debug_force_soft_fork,
},
common_config,
events_sender,
state,
Expand All @@ -151,7 +153,7 @@ impl SumeragiStartArgs {
genesis_network,
block_count: BlockCount(block_count),
#[cfg(feature = "telemetry")]
sumeragi_metrics:
metrics:
SumeragiMetrics {
view_changes,
dropped_messages,
Expand All @@ -176,7 +178,7 @@ impl SumeragiStartArgs {

topology = match state_view.height() {
0 => Topology::new(
sumeragi_config
common_config
.trusted_peers
.value()
.clone()
Expand Down Expand Up @@ -207,11 +209,6 @@ impl SumeragiStartArgs {

info!("Sumeragi has finished loading blocks and setting up the state");

#[cfg(debug_assertions)]
let debug_force_soft_fork = sumeragi_config.debug_force_soft_fork;
#[cfg(not(debug_assertions))]
let debug_force_soft_fork = false;

let peer = common_config.peer;
let sumeragi = main_loop::Sumeragi {
chain_id: common_config.chain,
Expand Down Expand Up @@ -295,7 +292,7 @@ impl VotingBlock<'_> {
/// Arguments for [`SumeragiHandle::start`] function
#[allow(missing_docs)]
pub struct SumeragiStartArgs {
pub sumeragi_config: SumeragiConfig,
pub config: SumeragiConfig,
pub common_config: CommonConfig,
pub events_sender: EventsSender,
pub state: Arc<State>,
Expand All @@ -306,7 +303,7 @@ pub struct SumeragiStartArgs {
pub genesis_network: GenesisWithPubKey,
pub block_count: BlockCount,
#[cfg(feature = "telemetry")]
pub sumeragi_metrics: SumeragiMetrics,
pub metrics: SumeragiMetrics,
}

/// Relevant sumeragi metrics
Expand Down
Loading

0 comments on commit 2a30fc9

Please sign in to comment.