Skip to content

Commit

Permalink
fix: view change updates
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Jun 11, 2024
1 parent 5b80887 commit ac6badf
Show file tree
Hide file tree
Showing 20 changed files with 718 additions and 532 deletions.
34 changes: 29 additions & 5 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub struct Iroha {
/// A boolean value indicating whether or not the peers will receive data from the network.
/// Used in sumeragi testing.
#[cfg(debug_assertions)]
pub freeze_status: Arc<AtomicBool>,
pub freeze_status: FreezeStatus,
}

impl Drop for Iroha {
Expand Down Expand Up @@ -107,14 +107,37 @@ pub enum StartError {
StartTorii,
}

/// Handle for freezing and unfreezing the network
#[derive(Clone)]
#[cfg(debug_assertions)]
pub struct FreezeStatus(Arc<AtomicBool>, PeerId);

#[cfg(debug_assertions)]
impl FreezeStatus {
pub(crate) fn new(peer_id: PeerId) -> Self {
Self(Arc::new(AtomicBool::new(false)), peer_id)
}

/// Stop listening for messages
pub fn freeze(&self) {
iroha_logger::warn!(peer_id=%self.1, "NetworkRelay is frozen");
self.0.store(true, Ordering::SeqCst);
}
/// Start listening for messages
pub fn unfreeze(&self) {
iroha_logger::warn!(peer_id=%self.1, "NetworkRelay is unfrozen");
self.0.store(false, Ordering::SeqCst);
}
}

struct NetworkRelay {
sumeragi: SumeragiHandle,
block_sync: BlockSynchronizerHandle,
gossiper: TransactionGossiperHandle,
network: IrohaNetwork,
shutdown_notify: Arc<Notify>,
#[cfg(debug_assertions)]
freeze_status: Arc<AtomicBool>,
freeze_status: FreezeStatus,
}

impl NetworkRelay {
Expand Down Expand Up @@ -145,7 +168,7 @@ impl NetworkRelay {
use iroha_core::NetworkMessage::*;

#[cfg(debug_assertions)]
if self.freeze_status.load(Ordering::SeqCst) {
if self.freeze_status.0.load(Ordering::SeqCst) {
return;
}

Expand Down Expand Up @@ -335,7 +358,8 @@ impl Iroha {
.start();

#[cfg(debug_assertions)]
let freeze_status = Arc::new(AtomicBool::new(false));
let freeze_status = FreezeStatus::new(config.common.peer.clone());
Arc::new(AtomicBool::new(false));

let notify_shutdown = Arc::new(Notify::new());

Expand Down Expand Up @@ -510,7 +534,7 @@ impl Iroha {

#[allow(missing_docs)]
#[cfg(debug_assertions)]
pub fn freeze_status(&self) -> &Arc<AtomicBool> {
pub fn freeze_status(&self) -> &FreezeStatus {
&self.freeze_status
}

Expand Down
2 changes: 0 additions & 2 deletions cli/src/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ pub fn get_config_toml(
) -> toml::Table {
let (public_key, private_key) = peer_key_pair.into_parts();

iroha_logger::info!(%public_key, "sample configuration public key");

let mut raw = toml::Table::new();
iroha_config::base::toml::Writer::new(&mut raw)
.write("chain", chain_id)
Expand Down
2 changes: 1 addition & 1 deletion client/examples/million_accounts_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{thread, time::Duration};

use iroha::{
crypto::KeyPair,
data_model::{::isi::InstructionBox, prelude::*},
data_model::{isi::InstructionBox, prelude::*},
};
use iroha_genesis::{GenesisTransaction, GenesisTransactionBuilder};
use iroha_primitives::unique_vec;
Expand Down
2 changes: 1 addition & 1 deletion client/examples/register_1000_triggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tokio::runtime::Runtime;
fn generate_genesis(
num_triggers: u32,
chain_id: ChainId,
genesis_key_pair: &KeyPair,
genesis_key_pair: &iroha_crypto::KeyPair,
) -> Result<GenesisTransaction, Box<dyn std::error::Error>> {
let builder = GenesisTransactionBuilder::default();

Expand Down
11 changes: 5 additions & 6 deletions client/tests/integration/extra_functional/unstable_network.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::sync::atomic::Ordering;
use std::thread;

use iroha::{
Expand All @@ -14,7 +13,7 @@ use tokio::runtime::Runtime;
const MAX_TRANSACTIONS_IN_BLOCK: u32 = 5;

#[test]
fn unstable_network_4_peers_1_fault() {
fn unstable_network_5_peers_1_fault() {
let n_peers = 4;
let n_transactions = 20;
unstable_network(n_peers, 1, n_transactions, false, 10_805);
Expand All @@ -28,15 +27,15 @@ fn soft_fork() {
}

#[test]
fn unstable_network_7_peers_1_fault() {
fn unstable_network_8_peers_1_fault() {
let n_peers = 7;
let n_transactions = 20;
unstable_network(n_peers, 1, n_transactions, false, 10_850);
}

#[test]
#[ignore = "This test does not guarantee to have positive outcome given a fixed time."]
fn unstable_network_7_peers_2_faults() {
fn unstable_network_9_peers_2_faults() {
unstable_network(7, 2, 5, false, 10_890);
}

Expand Down Expand Up @@ -97,7 +96,7 @@ fn unstable_network(
for _i in 0..n_transactions {
// Make random peers faulty.
for f in freezers.choose_multiple(&mut rng, n_offline_peers as usize) {
f.store(true, Ordering::SeqCst);
f.freeze();
}

let quantity = Numeric::ONE;
Expand Down Expand Up @@ -127,7 +126,7 @@ fn unstable_network(

// Return all peers to normal function.
for f in &freezers {
f.store(false, Ordering::SeqCst);
f.unfreeze();
}
}
}
Empty file.
21 changes: 14 additions & 7 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ mod valid {
block: &SignedBlock,
topology: &Topology,
) -> Result<(), SignatureVerificationError> {
// TODO: ?
//let roles: &[Role] = if topology.view_change_index() >= 1 {
// &[Role::ValidatingPeer, Role::ObservingPeer]
//} else {
Expand Down Expand Up @@ -355,7 +356,7 @@ mod valid {
let proxy_tail_index = topology.proxy_tail_index();
let mut signatures = block.signatures().rev();

match signatures.next() {
let proxy_tail_signature = match signatures.next() {
Some(BlockSignature(signatory, signature))
if usize::try_from(*signatory)
.map_err(|_err| SignatureVerificationError::ProxyTailMissing)?
Expand All @@ -371,13 +372,15 @@ mod valid {
}

signature
.verify(topology.proxy_tail().public_key(), block.payload())
.map_err(|_err| SignatureVerificationError::ProxyTailMissing)?;
}
_ => {
return Err(SignatureVerificationError::ProxyTailMissing);
}
}
};

proxy_tail_signature
.verify(topology.proxy_tail().public_key(), block.payload())
.map_err(|_err| SignatureVerificationError::ProxyTailMissing)?;

Ok(())
}
Expand Down Expand Up @@ -435,7 +438,7 @@ mod valid {

if !block.header().is_genesis() {
if let Err(err) = Self::verify_leader_signature(&block, topology)
.map(|()| Self::verify_validator_signatures(&block, topology))
.and_then(|()| Self::verify_validator_signatures(&block, topology))
{
return WithEvents::new(Err((block, err.into())));
}
Expand Down Expand Up @@ -549,7 +552,7 @@ mod valid {
let prev_signatures = self.0.replace_signatures_unchecked(signatures);

if let Err(err) = Self::verify_leader_signature(self.as_ref(), topology)
.map(|()| Self::verify_validator_signatures(self.as_ref(), topology))
.and_then(|()| Self::verify_validator_signatures(self.as_ref(), topology))
{
self.0.replace_signatures_unchecked(prev_signatures);
WithEvents::new(Err(err))
Expand Down Expand Up @@ -641,9 +644,13 @@ mod valid {
#[cfg(test)]
mod tests {
use iroha_crypto::SignatureOf;
use iroha_primitives::unique_vec::UniqueVec;

use super::*;
use crate::sumeragi::network_topology::test_peers;
use crate::{
kura::Kura, query::store::LiveQueryStore, state::State,
sumeragi::network_topology::test_peers,
};

#[test]
fn signature_verification_ok() {
Expand Down
14 changes: 9 additions & 5 deletions core/src/block_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ pub mod message {
let start_height = match prev_hash {
Some(hash) => match block_sync.kura.get_block_height_by_hash(hash) {
None => {
error!(?prev_hash, "Block hash not found");
error!(
peer_id=%block_sync.peer_id,
block=%hash,
"Block hash not found"
);
return;
}
// It's get blocks *after*, so we add 1.
Expand Down Expand Up @@ -237,11 +241,11 @@ pub mod message {
}
}
Message::ShareBlocks(ShareBlocks { blocks, .. }) => {
use crate::sumeragi::message::BlockMessage;
use crate::sumeragi::message::BlockSyncUpdate;

for block in blocks.clone() {
block_sync
.sumeragi
.incoming_block_message(BlockMessage::BlockSyncUpdate(block.into()));
let msg = BlockSyncUpdate::from(&block);
block_sync.sumeragi.incoming_block_message(msg);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/smartcontracts/isi/triggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub mod isi {
}
}

let last_block_estimation = state_transaction.latest_block_ref().map(|block| {
let last_block_estimation = state_transaction.latest_block().map(|block| {
block.header().timestamp()
+ Duration::from_millis(block.header().consensus_estimation_ms)
});
Expand Down
17 changes: 5 additions & 12 deletions core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,11 +1011,11 @@ pub trait StateReadOnly {
fn query_handle(&self) -> &LiveQueryStoreHandle;
fn new_tx_amounts(&self) -> &Mutex<Vec<f64>>;

// Block-related methods

/// Get a reference to the latest block. Returns none if genesis is not committed.
///
/// If you only need hash of the latest block prefer using [`Self::latest_block_hash`]
#[inline]
fn latest_block_ref(&self) -> Option<Arc<SignedBlock>> {
fn latest_block(&self) -> Option<Arc<SignedBlock>> {
NonZeroUsize::new(self.height()).and_then(|height| self.kura().get_block_by_height(height))
}

Expand All @@ -1024,13 +1024,6 @@ pub trait StateReadOnly {
self.block_hashes().iter().nth_back(0).copied()
}

/// Return the view change index of the latest block
fn latest_block_view_change_index(&self) -> usize {
NonZeroUsize::new(self.height())
.and_then(|height| self.kura().get_block_by_height(height))
.map_or(0, |block| block.header().view_change_index as usize)
}

/// Return the hash of the block one before the latest block
fn prev_block_hash(&self) -> Option<HashOf<SignedBlock>> {
self.block_hashes().iter().nth_back(1).copied()
Expand Down Expand Up @@ -1106,7 +1099,7 @@ pub trait StateReadOnly {
}
}

/// Check if this [`SignedTransaction`] is already committed or rejected.
/// Check if [`SignedTransaction`] is already committed
#[inline]
fn has_transaction(&self, hash: HashOf<SignedTransaction>) -> bool {
self.transactions().get(&hash).is_some()
Expand Down Expand Up @@ -1273,7 +1266,7 @@ impl<'state> StateBlock<'state> {

/// Create time event using previous and current blocks
fn create_time_event(&self, block: &CommittedBlock) -> TimeEvent {
let prev_interval = self.latest_block_ref().map(|latest_block| {
let prev_interval = self.latest_block().map(|latest_block| {
let header = &latest_block.as_ref().header();

TimeInterval {
Expand Down
Loading

0 comments on commit ac6badf

Please sign in to comment.