Skip to content

Commit b563ae1

Browse files
committed
[fix] #2133: Rewrite topology to be closer the whitepaper.
Signed-off-by: Sam H. Smith <[email protected]>
1 parent 0678429 commit b563ae1

File tree

11 files changed

+248
-612
lines changed

11 files changed

+248
-612
lines changed

core/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ hex = "0.4.3"
7575
byte-unit = "4.0.18"
7676
once_cell = "1.16.0"
7777

78-
[[bench]]
79-
name = "sumeragi"
80-
harness = false
81-
8278
[[bench]]
8379
name = "validation"
8480
harness = false

core/benches/sumeragi.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

core/benches/validation.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{collections::BTreeSet, str::FromStr as _, sync::Arc};
55
use criterion::{criterion_group, criterion_main, Criterion};
66
use iroha_core::{
77
prelude::*,
8+
sumeragi::network_topology::Topology,
89
tx::{AcceptedTransaction, TransactionValidator},
910
wsv::World,
1011
};
@@ -149,10 +150,12 @@ fn chain_blocks(criterion: &mut Criterion) {
149150
let _ = criterion.bench_function("chain_block", |b| {
150151
b.iter(|| {
151152
success_count += 1;
152-
let new_block =
153-
block
154-
.clone()
155-
.chain(success_count, Some(previous_block_hash.transmute()), 0);
153+
let new_block = block.clone().chain(
154+
success_count,
155+
Some(previous_block_hash.transmute()),
156+
0,
157+
Topology::new(Vec::new()),
158+
);
156159
previous_block_hash = new_block.hash();
157160
});
158161
});

core/src/block.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl PendingBlock {
7979
height: u64,
8080
previous_block_hash: Option<HashOf<VersionedCommittedBlock>>,
8181
view_change_index: u64,
82+
committed_with_topology: Topology,
8283
) -> ChainedBlock {
8384
ChainedBlock {
8485
transactions: self.transactions,
@@ -91,13 +92,13 @@ impl PendingBlock {
9192
previous_block_hash,
9293
transactions_hash: None,
9394
rejected_transactions_hash: None,
94-
genesis_topology: None,
95+
committed_with_topology,
9596
},
9697
}
9798
}
9899

99100
/// Create a new blockchain with current block as a first block.
100-
pub fn chain_first_with_genesis_topology(self, genesis_topology: Topology) -> ChainedBlock {
101+
pub fn chain_first_with_topology(self, genesis_topology: Topology) -> ChainedBlock {
101102
ChainedBlock {
102103
transactions: self.transactions,
103104
event_recommendations: self.event_recommendations,
@@ -109,7 +110,7 @@ impl PendingBlock {
109110
previous_block_hash: None,
110111
transactions_hash: None,
111112
rejected_transactions_hash: None,
112-
genesis_topology: Some(genesis_topology),
113+
committed_with_topology: genesis_topology,
113114
},
114115
}
115116
}
@@ -127,7 +128,7 @@ impl PendingBlock {
127128
previous_block_hash: None,
128129
transactions_hash: None,
129130
rejected_transactions_hash: None,
130-
genesis_topology: None,
131+
committed_with_topology: Topology::new(Vec::new()),
131132
},
132133
}
133134
}
@@ -162,8 +163,8 @@ pub struct BlockHeader {
162163
pub transactions_hash: Option<HashOf<MerkleTree<VersionedSignedTransaction>>>,
163164
/// Hash of merkle tree root of the tree of rejected transactions' hashes.
164165
pub rejected_transactions_hash: Option<HashOf<MerkleTree<VersionedSignedTransaction>>>,
165-
/// Genesis topology
166-
pub genesis_topology: Option<Topology>,
166+
/// Network topology when the block was committed.
167+
pub committed_with_topology: Topology,
167168
}
168169

169170
impl BlockHeader {
@@ -388,7 +389,7 @@ impl SignedBlock {
388389
previous_block_hash: None,
389390
transactions_hash: None,
390391
rejected_transactions_hash: None,
391-
genesis_topology: None,
392+
committed_with_topology: Topology::new(Vec::new()),
392393
},
393394
rejected_transactions: Vec::new(),
394395
transactions: Vec::new(),

core/src/genesis.rs

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
clippy::arithmetic_side_effects
99
)]
1010

11-
use std::{collections::HashSet, fmt::Debug, fs::File, io::BufReader, ops::Deref, path::Path};
11+
use std::{fmt::Debug, fs::File, io::BufReader, ops::Deref, path::Path};
1212

1313
use derive_more::Deref;
1414
use eyre::{bail, eyre, Result, WrapErr};
@@ -24,16 +24,11 @@ use iroha_schema::prelude::*;
2424
use serde::{Deserialize, Serialize};
2525
use tokio::{time, time::Duration};
2626

27-
use crate::{
28-
sumeragi::network_topology::{GenesisBuilder as GenesisTopologyBuilder, Topology},
29-
tx::VersionedAcceptedTransaction,
30-
IrohaNetwork,
31-
};
27+
use crate::{sumeragi::network_topology::Topology, tx::VersionedAcceptedTransaction, IrohaNetwork};
3228

3329
// TODO: 8 is just the optimal value for tests. This number should be
3430
// revised as soon as we have real data, to fix #1855.
3531
type Online = SmallVec<[PeerId; 8]>;
36-
type Offline = SmallVec<[PeerId; 8]>;
3732

3833
/// Time to live for genesis transactions.
3934
const GENESIS_TRANSACTIONS_TTL_MS: u64 = 100_000;
@@ -88,31 +83,13 @@ async fn try_get_online_topology(
8883
network_topology: &Topology,
8984
network: Addr<IrohaNetwork>,
9085
) -> Result<Topology> {
91-
let (online_peers, offline_peers) =
92-
check_peers_status(this_peer_id, network_topology, network).await;
86+
let online_peers = check_peers_status(this_peer_id, network_topology, network).await;
9387
let set_a_len = network_topology.min_votes_for_commit();
9488
if online_peers.len() < set_a_len {
95-
return Err(eyre!("Not enough online peers for consensus."));
89+
eyre::bail!("Not enough online peers for consensus.");
9690
}
97-
let genesis_topology = if network_topology.sorted_peers().len() == 1 {
98-
network_topology.clone()
99-
} else {
100-
let set_a: HashSet<_> = online_peers[..set_a_len].iter().cloned().collect();
101-
let set_b: HashSet<_> = online_peers[set_a_len..]
102-
.iter()
103-
.cloned()
104-
.chain(offline_peers.into_iter())
105-
.collect();
106-
#[allow(clippy::expect_used)]
107-
GenesisTopologyBuilder::new()
108-
.with_leader(this_peer_id.clone())
109-
.with_set_a(set_a)
110-
.with_set_b(set_b)
111-
.build()
112-
.expect("Preconditions should be already checked.")
113-
};
11491
iroha_logger::info!("Waiting for active peers finished.");
115-
Ok(genesis_topology)
92+
Ok(network_topology.clone())
11693
}
11794

11895
/// Checks which [`Peer`]s are online and which are offline
@@ -121,7 +98,7 @@ async fn check_peers_status(
12198
this_peer_id: &PeerId,
12299
network_topology: &Topology,
123100
network: Addr<IrohaNetwork>,
124-
) -> (Online, Offline) {
101+
) -> Online {
125102
#[allow(clippy::expect_used)]
126103
let peers = network
127104
.send(iroha_p2p::network::GetConnectedPeers)
@@ -130,13 +107,13 @@ async fn check_peers_status(
130107
.peers;
131108
iroha_logger::info!(peer_count = peers.len(), "Peers status");
132109

133-
let (online, offline): (SmallVec<_>, SmallVec<_>) = network_topology
134-
.sorted_peers()
110+
let (online, _offline): (SmallVec<_>, SmallVec<_>) = network_topology
111+
.sorted_peers
135112
.iter()
136113
.cloned()
137114
.partition(|id| peers.contains(&id.public_key) || this_peer_id.public_key == id.public_key);
138115

139-
(online, offline)
116+
online
140117
}
141118

142119
#[async_trait::async_trait]

core/src/smartcontracts/isi/query.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,12 @@ mod tests {
255255

256256
for height in 1u64..blocks {
257257
let block: VersionedCommittedBlock = PendingBlock::new(transactions.clone(), vec![])
258-
.chain(height, Some(curr_hash), 0)
258+
.chain(
259+
height,
260+
Some(curr_hash),
261+
0,
262+
crate::sumeragi::network_topology::Topology::new(vec![]),
263+
)
259264
.validate(&TransactionValidator::new(limits), &wsv)
260265
.sign(ALICE_KEYS.clone())
261266
.expect("Failed to sign blocks.")

0 commit comments

Comments
 (0)