diff --git a/Cargo.lock b/Cargo.lock index 47f6e80dde4..54b69c98dfe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3172,7 +3172,6 @@ dependencies = [ "eyre", "iroha_crypto", "iroha_data_model", - "iroha_schema", "once_cell", "serde", "serde_json", diff --git a/client/benches/tps/utils.rs b/client/benches/tps/utils.rs index ca63d75d89b..6a91fde39ba 100644 --- a/client/benches/tps/utils.rs +++ b/client/benches/tps/utils.rs @@ -4,11 +4,11 @@ use eyre::{Result, WrapErr}; use iroha_client::{ client::Client, data_model::{ + events::pipeline::{BlockEventFilter, BlockStatus}, parameter::{default::MAX_TRANSACTIONS_IN_BLOCK, ParametersBuilder}, prelude::*, }, }; -use iroha_data_model::events::pipeline::{BlockEventFilter, BlockStatus}; use nonzero_ext::nonzero; use serde::Deserialize; use test_network::*; diff --git a/client/examples/million_accounts_genesis.rs b/client/examples/million_accounts_genesis.rs index 5a6b1dc6692..126354bbabb 100644 --- a/client/examples/million_accounts_genesis.rs +++ b/client/examples/million_accounts_genesis.rs @@ -2,9 +2,10 @@ use std::{thread, time::Duration}; use iroha::samples::{construct_executor, get_config}; -use iroha_client::data_model::prelude::*; -use iroha_crypto::KeyPair; -use iroha_data_model::isi::InstructionBox; +use iroha_client::{ + crypto::KeyPair, + data_model::{isi::InstructionBox, prelude::*}, +}; use iroha_genesis::{GenesisNetwork, RawGenesisBlock, RawGenesisBlockBuilder}; use iroha_primitives::unique_vec; use test_network::{ diff --git a/client/examples/register_1000_triggers.rs b/client/examples/register_1000_triggers.rs index 6de1efd77bc..3dde24a5cb9 100644 --- a/client/examples/register_1000_triggers.rs +++ b/client/examples/register_1000_triggers.rs @@ -3,8 +3,10 @@ use std::str::FromStr; use iroha::samples::{construct_executor, get_config}; -use iroha_client::{client::Client, data_model::prelude::*}; -use iroha_data_model::trigger::TriggerId; +use iroha_client::{ + client::Client, + data_model::{prelude::*, trigger::TriggerId}, +}; use iroha_genesis::{GenesisNetwork, RawGenesisBlock, RawGenesisBlockBuilder}; use iroha_primitives::unique_vec; use test_network::{ diff --git a/client/src/client.rs b/client/src/client.rs index 2a4eed202d7..15be1ce3b5a 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -14,13 +14,6 @@ use eyre::{eyre, Result, WrapErr}; use futures_util::StreamExt; use http_default::{AsyncWebSocketStream, WebSocketStream}; pub use iroha_config::client_api::ConfigDTO; -use iroha_data_model::{ - events::pipeline::{ - BlockEventFilter, BlockStatus, PipelineEventBox, PipelineEventFilterBox, - TransactionEventFilter, TransactionStatus, - }, - query::QueryOutputBox, -}; use iroha_logger::prelude::*; use iroha_telemetry::metrics::Status; use iroha_torii_const::uri as torii_uri; @@ -35,9 +28,13 @@ use crate::{ crypto::{HashOf, KeyPair}, data_model::{ block::SignedBlock, + events::pipeline::{ + BlockEventFilter, BlockStatus, PipelineEventBox, PipelineEventFilterBox, + TransactionEventFilter, TransactionStatus, + }, isi::Instruction, prelude::*, - query::{predicate::PredicateBox, Pagination, Query, Sorting}, + query::{predicate::PredicateBox, Pagination, Query, QueryOutputBox, Sorting}, BatchedResponse, ChainId, ValidationFail, }, http::{Method as HttpMethod, RequestBuilder, Response, StatusCode}, @@ -70,17 +67,17 @@ pub type QueryResult = core::result::Result; /// Trait for signing transactions pub trait Sign { /// Sign transaction with provided key pair. - fn sign(self, key_pair: &crate::crypto::KeyPair) -> SignedTransaction; + fn sign(self, key_pair: &KeyPair) -> SignedTransaction; } impl Sign for TransactionBuilder { - fn sign(self, key_pair: &crate::crypto::KeyPair) -> SignedTransaction { + fn sign(self, key_pair: &KeyPair) -> SignedTransaction { self.sign(key_pair) } } impl Sign for SignedTransaction { - fn sign(self, key_pair: &crate::crypto::KeyPair) -> SignedTransaction { + fn sign(self, key_pair: &KeyPair) -> SignedTransaction { self.sign(key_pair) } } diff --git a/client/src/config.rs b/client/src/config.rs index 72bb909d8c7..d2c44603b25 100644 --- a/client/src/config.rs +++ b/client/src/config.rs @@ -9,14 +9,16 @@ use iroha_config::{ base, base::{FromEnv, StdEnv, UnwrapPartial}, }; -use iroha_crypto::KeyPair; -use iroha_data_model::{prelude::*, ChainId}; use iroha_primitives::small::SmallStr; use serde::{Deserialize, Serialize}; use serde_with::{DeserializeFromStr, SerializeDisplay}; use url::Url; -use crate::config::user::RootPartial; +use crate::{ + config::user::RootPartial, + crypto::KeyPair, + data_model::{prelude::*, ChainId}, +}; mod user; diff --git a/client/src/config/user.rs b/client/src/config/user.rs index 30a684e5bac..62869f563bf 100644 --- a/client/src/config/user.rs +++ b/client/src/config/user.rs @@ -7,13 +7,15 @@ use std::{fs::File, io::Read, path::Path, str::FromStr, time::Duration}; pub use boilerplate::*; use eyre::{eyre, Context, Report}; use iroha_config::base::{Emitter, ErrorsCollection}; -use iroha_crypto::{KeyPair, PrivateKey, PublicKey}; -use iroha_data_model::{account::AccountId, ChainId}; use merge::Merge; use serde_with::DeserializeFromStr; use url::Url; -use crate::config::BasicAuth; +use crate::{ + config::BasicAuth, + crypto::{KeyPair, PrivateKey, PublicKey}, + data_model::{account::AccountId, ChainId}, +}; impl RootPartial { /// Reads the partial layer from TOML diff --git a/client/src/config/user/boilerplate.rs b/client/src/config/user/boilerplate.rs index 500b13afecb..a1a71dd01a8 100644 --- a/client/src/config/user/boilerplate.rs +++ b/client/src/config/user/boilerplate.rs @@ -8,15 +8,17 @@ use iroha_config::base::{ Emitter, FromEnv, HumanDuration, Merge, ParseEnvResult, UnwrapPartial, UnwrapPartialResult, UserField, }; -use iroha_crypto::{PrivateKey, PublicKey}; -use iroha_data_model::{account::AccountId, ChainId}; use serde::Deserialize; -use crate::config::{ - base::{FromEnvResult, ReadEnv}, - user::{Account, OnlyHttpUrl, Root, Transaction}, - BasicAuth, DEFAULT_TRANSACTION_NONCE, DEFAULT_TRANSACTION_STATUS_TIMEOUT, - DEFAULT_TRANSACTION_TIME_TO_LIVE, +use crate::{ + config::{ + base::{FromEnvResult, ReadEnv}, + user::{Account, OnlyHttpUrl, Root, Transaction}, + BasicAuth, DEFAULT_TRANSACTION_NONCE, DEFAULT_TRANSACTION_STATUS_TIMEOUT, + DEFAULT_TRANSACTION_TIME_TO_LIVE, + }, + crypto::{PrivateKey, PublicKey}, + data_model::{account::AccountId, ChainId}, }; #[derive(Debug, Clone, Deserialize, Eq, PartialEq, Default, Merge)] diff --git a/client/src/query_builder.rs b/client/src/query_builder.rs index 4ccfe7c99db..5e4cefa6a97 100644 --- a/client/src/query_builder.rs +++ b/client/src/query_builder.rs @@ -1,10 +1,10 @@ use std::fmt::Debug; -use iroha_data_model::query::QueryOutputBox; - use crate::{ client::{Client, QueryOutput, QueryResult}, - data_model::query::{predicate::PredicateBox, sorting::Sorting, FetchSize, Pagination, Query}, + data_model::query::{ + predicate::PredicateBox, sorting::Sorting, FetchSize, Pagination, Query, QueryOutputBox, + }, }; pub struct QueryRequestBuilder<'a, R> { diff --git a/configs/swarm/executor.wasm b/configs/swarm/executor.wasm index 55348c7b4cb..5eb1f0ba765 100644 Binary files a/configs/swarm/executor.wasm and b/configs/swarm/executor.wasm differ diff --git a/core/benches/blocks/common.rs b/core/benches/blocks/common.rs index d88514f7c9f..96d89522876 100644 --- a/core/benches/blocks/common.rs +++ b/core/benches/blocks/common.rs @@ -41,7 +41,7 @@ pub fn create_block( Vec::new(), ) .chain(0, state) - .sign(key_pair) + .sign(key_pair.private_key()) .unpack(|_| {}) .commit(&topology) .unpack(|_| {}) diff --git a/core/benches/kura.rs b/core/benches/kura.rs index f9b53e25190..a1872eddadb 100644 --- a/core/benches/kura.rs +++ b/core/benches/kura.rs @@ -53,14 +53,14 @@ async fn measure_block_size_for_n_executors(n_executors: u32) { let topology = Topology::new(UniqueVec::new()); let mut block = { let mut state_block = state.block(); - BlockBuilder::new(vec![tx], topology, Vec::new()) + BlockBuilder::new(vec![tx], topology.clone(), Vec::new()) .chain(0, &mut state_block) - .sign(&KeyPair::random()) + .sign(KeyPair::random().private_key()) .unpack(|_| {}) }; for _ in 1..n_executors { - block = block.sign(&KeyPair::random()); + block = block.sign(&KeyPair::random(), &topology); } let mut block_store = BlockStore::new(dir.path(), LockStatus::Unlocked); block_store.create_files_if_they_do_not_exist().unwrap(); diff --git a/core/benches/validation.rs b/core/benches/validation.rs index d7e5459f090..14361af156e 100644 --- a/core/benches/validation.rs +++ b/core/benches/validation.rs @@ -186,7 +186,7 @@ fn sign_blocks(criterion: &mut Criterion) { b.iter_batched( || block.clone(), |block| { - let _: ValidBlock = block.sign(&key_pair).unpack(|_| {}); + let _: ValidBlock = block.sign(key_pair.private_key()).unpack(|_| {}); count += 1; }, BatchSize::SmallInput, diff --git a/core/src/block.rs b/core/src/block.rs index 68df8771241..edeb9b8be8b 100644 --- a/core/src/block.rs +++ b/core/src/block.rs @@ -7,7 +7,7 @@ use std::error::Error as _; use iroha_config::parameters::defaults::chain_wide::DEFAULT_CONSENSUS_ESTIMATION; -use iroha_crypto::{HashOf, KeyPair, MerkleTree, SignatureOf, SignaturesOf}; +use iroha_crypto::{HashOf, MerkleTree, SignatureOf}; use iroha_data_model::{ block::*, events::prelude::*, @@ -15,7 +15,6 @@ use iroha_data_model::{ transaction::{error::TransactionRejectionReason, prelude::*}, }; use iroha_genesis::GenesisTransaction; -use iroha_primitives::unique_vec::UniqueVec; use thiserror::Error; pub(crate) use self::event::WithEvents; @@ -66,9 +65,9 @@ pub enum BlockValidationError { /// Mismatch between the actual and expected topology. Expected: {expected:?}, actual: {actual:?} TopologyMismatch { /// Expected value - expected: UniqueVec, + expected: Vec, /// Actual value - actual: UniqueVec, + actual: Vec, }, /// Error during block signatures check SignatureVerification(#[from] SignatureVerificationError), @@ -218,7 +217,7 @@ mod pending { &transactions, ), transactions, - commit_topology: self.0.commit_topology.ordered_peers, + commit_topology: self.0.commit_topology.into_iter().collect(), event_recommendations: self.0.event_recommendations, })) } @@ -234,13 +233,13 @@ mod chained { impl BlockBuilder { /// Sign this block and get [`SignedBlock`]. - pub fn sign(self, key_pair: &KeyPair) -> WithEvents { - let signature = SignatureOf::new(key_pair, &self.0 .0); + pub fn sign(self, private_key: &PrivateKey) -> WithEvents { + let signature = BlockSignature(0, SignatureOf::new(private_key, &self.0 .0)); WithEvents::new(ValidBlock( SignedBlockV1 { + signatures: vec![signature], payload: self.0 .0, - signatures: SignaturesOf::from(signature), } .into(), )) @@ -307,16 +306,14 @@ mod valid { // NOTE: should be checked AFTER height and hash, both this issues lead to topology mismatch if !block.header().is_genesis() { - let actual_commit_topology = block.commit_topology(); - let expected_commit_topology = &topology.ordered_peers; + let actual_commit_topology = block.commit_topology().cloned().collect(); + let expected_commit_topology = topology.as_ref(); if actual_commit_topology != expected_commit_topology { - let actual_commit_topology = actual_commit_topology.clone(); - return WithEvents::new(Err(( block, BlockValidationError::TopologyMismatch { - expected: expected_commit_topology.clone(), + expected: expected_commit_topology.to_owned(), actual: actual_commit_topology, }, ))); @@ -413,7 +410,7 @@ mod valid { pub fn commit_with_signatures( mut self, topology: &Topology, - signatures: SignaturesOf, + signatures: Vec, expected_hash: HashOf, ) -> WithEvents> { if topology @@ -426,10 +423,13 @@ mod valid { ))); } - if !self.as_ref().signatures().is_subset(&signatures) { + if topology + .filter_signatures_by_roles(&[Role::ProxyTail], &signatures) + .is_empty() + { return WithEvents::new(Err(( self, - SignatureVerificationError::SignatureMissing.into(), + SignatureVerificationError::ProxyTailMissing.into(), ))); } @@ -474,8 +474,11 @@ mod valid { /// Add additional signatures for [`Self`]. #[must_use] - pub fn sign(self, key_pair: &KeyPair) -> ValidBlock { - ValidBlock(self.0.sign(key_pair)) + pub fn sign(self, key_pair: &KeyPair, topology: &Topology) -> ValidBlock { + let node_pos = topology + .position(key_pair.public_key()) + .expect("BUG: Node is not in topology"); + ValidBlock(self.0.sign(key_pair.private_key(), node_pos as u64)) } /// Add additional signature for [`Self`] @@ -485,7 +488,7 @@ mod valid { /// If given signature doesn't match block hash pub fn add_signature( &mut self, - signature: SignatureOf, + signature: BlockSignature, ) -> Result<(), iroha_crypto::error::Error> { self.0.add_signature(signature) } @@ -505,10 +508,10 @@ mod valid { .expect("Time should fit into u64"), }, transactions: Vec::new(), - commit_topology: UniqueVec::new(), + commit_topology: Vec::new(), event_recommendations: Vec::new(), })) - .sign(&KeyPair::random()) + .sign(iroha_crypto::KeyPair::random().private_key()) .unpack(|_| {}) } @@ -592,7 +595,9 @@ mod valid { let payload = payload(&block).clone(); key_pairs .iter() - .map(|key_pair| SignatureOf::new(key_pair, &payload)) + .map(|key_pair| { + BlockSignature(0, SignatureOf::new(key_pair.private_key(), &payload)) + }) .try_for_each(|signature| block.add_signature(signature)) .expect("Failed to add signatures"); @@ -612,7 +617,9 @@ mod valid { let payload = payload(&block).clone(); key_pairs .iter() - .map(|key_pair| SignatureOf::new(key_pair, &payload)) + .map(|key_pair| { + BlockSignature(0, SignatureOf::new(key_pair.private_key(), &payload)) + }) .try_for_each(|signature| block.add_signature(signature)) .expect("Failed to add signatures"); @@ -631,7 +638,8 @@ mod valid { let mut block = ValidBlock::new_dummy(); let payload = payload(&block).clone(); - let proxy_tail_signature = SignatureOf::new(&key_pairs[4], &payload); + let proxy_tail_signature = + BlockSignature(0, SignatureOf::new(key_pairs[4].private_key(), &payload)); block .add_signature(proxy_tail_signature) .expect("Failed to add signature"); @@ -661,7 +669,9 @@ mod valid { .iter() .enumerate() .filter(|(i, _)| *i != 4) // Skip proxy tail - .map(|(_, key_pair)| SignatureOf::new(key_pair, &payload)) + .map(|(_, key_pair)| { + BlockSignature(0, SignatureOf::new(key_pair.private_key(), &payload)) + }) .try_for_each(|signature| block.add_signature(signature)) .expect("Failed to add signatures"); @@ -801,6 +811,7 @@ mod tests { use iroha_crypto::SignatureVerificationFail; use iroha_data_model::prelude::*; use iroha_genesis::{GENESIS_ACCOUNT_ID, GENESIS_DOMAIN_ID}; + use iroha_primitives::unique_vec::UniqueVec; use super::*; use crate::{ @@ -856,7 +867,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let valid_block = BlockBuilder::new(transactions, topology, Vec::new()) .chain(0, &mut state_block) - .sign(&alice_keys) + .sign(alice_keys.private_key()) .unpack(|_| {}); // The first transaction should be confirmed @@ -931,7 +942,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let valid_block = BlockBuilder::new(transactions, topology, Vec::new()) .chain(0, &mut state_block) - .sign(&alice_keys) + .sign(alice_keys.private_key()) .unpack(|_| {}); // The first transaction should fail @@ -1001,7 +1012,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let valid_block = BlockBuilder::new(transactions, topology, Vec::new()) .chain(0, &mut state_block) - .sign(&alice_keys) + .sign(alice_keys.private_key()) .unpack(|_| {}); // The first transaction should be rejected @@ -1072,7 +1083,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let valid_block = BlockBuilder::new(transactions, topology.clone(), Vec::new()) .chain(0, &mut state_block) - .sign(&KeyPair::random()) + .sign(KeyPair::random().private_key()) .unpack(|_| {}); // Validate genesis block diff --git a/core/src/queue.rs b/core/src/queue.rs index 51e3ad38a5b..09b62b9e180 100644 --- a/core/src/queue.rs +++ b/core/src/queue.rs @@ -29,8 +29,7 @@ impl AcceptedTransaction { let transaction_signatories = self .as_ref() .signatures() - .iter() - .map(|signature| signature.public_key()) + .map(|signature| &signature.0) .cloned() .collect(); diff --git a/core/src/smartcontracts/isi/query.rs b/core/src/smartcontracts/isi/query.rs index daf7faae917..ea3ffd1e7e6 100644 --- a/core/src/smartcontracts/isi/query.rs +++ b/core/src/smartcontracts/isi/query.rs @@ -75,7 +75,7 @@ impl ValidQueryRequest { let account_has_public_key = state_ro .world() .map_account(query.authority(), |account| { - account.contains_signatory(query.signature().public_key()) + account.contains_signatory(&query.signature().0) }) .map_err(Error::from)?; if !account_has_public_key { @@ -315,7 +315,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let first_block = BlockBuilder::new(transactions.clone(), topology.clone(), Vec::new()) .chain(0, &mut state_block) - .sign(&ALICE_KEYS) + .sign(ALICE_KEYS.private_key()) .unpack(|_| {}) .commit(&topology) .unpack(|_| {}) @@ -327,7 +327,7 @@ mod tests { for _ in 1u64..blocks { let block = BlockBuilder::new(transactions.clone(), topology.clone(), Vec::new()) .chain(0, &mut state_block) - .sign(&ALICE_KEYS) + .sign(ALICE_KEYS.private_key()) .unpack(|_| {}) .commit(&topology) .unpack(|_| {}) @@ -469,7 +469,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let vcb = BlockBuilder::new(vec![va_tx.clone()], topology.clone(), Vec::new()) .chain(0, &mut state_block) - .sign(&ALICE_KEYS) + .sign(ALICE_KEYS.private_key()) .unpack(|_| {}) .commit(&topology) .unpack(|_| {}) diff --git a/core/src/sumeragi/main_loop.rs b/core/src/sumeragi/main_loop.rs index 49e1c1db8b6..523ebf170d3 100644 --- a/core/src/sumeragi/main_loop.rs +++ b/core/src/sumeragi/main_loop.rs @@ -1,7 +1,7 @@ //! The main event loop that powers sumeragi. use std::sync::mpsc; -use iroha_crypto::HashOf; +use iroha_crypto::{HashOf, KeyPair}; use iroha_data_model::{block::*, events::pipeline::PipelineEventBox, peer::PeerId}; use iroha_p2p::UpdateTopology; use tracing::{span, Level}; @@ -41,7 +41,7 @@ pub struct Sumeragi { /// is the proxy tail. pub debug_force_soft_fork: bool, /// The current network topology. - pub current_topology: Topology, + pub topology: Topology, /// In order to *be fast*, we must minimize communication with /// other subsystems where we can. This way the performance of /// sumeragi is more dependent on the code that is internal to the @@ -78,11 +78,11 @@ impl Sumeragi { self.network.post(post); } - #[allow(clippy::needless_pass_by_value, single_use_lifetimes)] // TODO: uncomment when anonymous lifetimes are stable - fn broadcast_packet_to<'peer_id>( + #[allow(clippy::needless_pass_by_value)] + fn broadcast_packet_to<'peer_id, I: IntoIterator + Send>( &self, msg: impl Into, - ids: impl IntoIterator + Send, + ids: I, ) { let msg = msg.into(); @@ -107,7 +107,7 @@ impl Sumeragi { /// Connect or disconnect peers according to the current network topology. fn connect_peers(&self, topology: &Topology) { - let peers = topology.ordered_peers.clone().into_iter().collect(); + let peers = topology.iter().cloned().collect(); self.network.update_topology(UpdateTopology(peers)); } @@ -155,16 +155,14 @@ impl Sumeragi { should_sleep = false; if let Err(error) = view_change_proof_chain.merge( msg.view_change_proofs, - &self.current_topology.ordered_peers, - self.current_topology.max_faults(), + &self.topology, state_view.latest_block_hash(), ) { trace!(%error, "Failed to add proofs into view change proof chain") } let current_view_change_index = view_change_proof_chain.verify_with_state( - &self.current_topology.ordered_peers, - self.current_topology.max_faults(), + &self.topology, state_view.latest_block_hash(), ) as u64; @@ -228,7 +226,7 @@ impl Sumeragi { let mut state_block = state.block(); let block = match ValidBlock::validate( block, - &self.current_topology, + &self.topology, &self.chain_id, genesis_public_key, &mut state_block, @@ -236,7 +234,7 @@ impl Sumeragi { .unpack(|e| self.send_event(e)) .and_then(|block| { block - .commit(&self.current_topology) + .commit(&self.topology) .unpack(|e| self.send_event(e)) .map_err(|(block, error)| (block.into(), error)) }) { @@ -247,7 +245,8 @@ impl Sumeragi { } }; - *state_block.world.trusted_peers_ids = block.as_ref().commit_topology().clone(); + *state_block.world.trusted_peers_ids = + block.as_ref().commit_topology().cloned().collect(); self.commit_block(block, state_block); return Err(EarlyReturn::GenesisBlockReceivedAndCommitted); } @@ -279,15 +278,15 @@ impl Sumeragi { .expect("Genesis invalid"); let mut state_block = state.block(); - let genesis = BlockBuilder::new(transactions, self.current_topology.clone(), vec![]) + let genesis = BlockBuilder::new(transactions, self.topology.clone(), vec![]) .chain(0, &mut state_block) - .sign(&self.key_pair) + .sign(self.key_pair.private_key()) .unpack(|e| self.send_event(e)); let genesis_msg = BlockCreated::from(genesis.clone()); let genesis = genesis - .commit(&self.current_topology) + .commit(&self.topology) .unpack(|e| self.send_event(e)) .expect("Genesis invalid"); @@ -297,7 +296,7 @@ impl Sumeragi { ); info!( - role = ?self.current_topology.role(&self.peer_id), + role = ?self.topology.role(&self.peer_id), block_hash = %genesis.as_ref().hash(), "Genesis block created", ); @@ -321,7 +320,7 @@ impl Sumeragi { ) { info!( addr=%self.peer_id.address, - role=%self.current_topology.role(&self.peer_id), + role=%self.topology.role(&self.peer_id), block_height=%block.as_ref().header().height, block_hash=%block.as_ref().hash(), "{}", Strategy::LOG_MESSAGE, @@ -344,8 +343,8 @@ impl Sumeragi { self.update_params(&state_block); self.cache_transaction(&state_block); - self.current_topology = new_topology; - self.connect_peers(&self.current_topology); + self.topology = new_topology; + self.connect_peers(&self.topology); // Commit new block making it's effect visible for the rest of application state_block.commit(); @@ -386,7 +385,7 @@ impl Sumeragi { ) -> Option> { let block_hash = block.hash(); let addr = &self.peer_id.address; - let role = self.current_topology.role(&self.peer_id); + let role = self.topology.role(&self.peer_id); trace!(%addr, %role, block=%block_hash, "Block received, voting..."); let mut state_block = state.block(); @@ -406,7 +405,7 @@ impl Sumeragi { } }; - let signed_block = block.sign(&self.key_pair); + let signed_block = block.sign(&self.key_pair, topology); Some(VotingBlock::new(signed_block, state_block)) } @@ -416,11 +415,8 @@ impl Sumeragi { view_change_proof_chain: &mut ProofChain, ) -> u64 { view_change_proof_chain.prune(state_view.latest_block_hash()); - view_change_proof_chain.verify_with_state( - &self.current_topology.ordered_peers, - self.current_topology.max_faults(), - state_view.latest_block_hash(), - ) as u64 + view_change_proof_chain.verify_with_state(&self.topology, state_view.latest_block_hash()) + as u64 } #[allow(clippy::too_many_lines)] @@ -431,10 +427,10 @@ impl Sumeragi { voting_block: &mut Option>, current_view_change_index: u64, genesis_public_key: &PublicKey, - voting_signatures: &mut Vec>, + voting_signatures: &mut Vec, ) { - let current_topology = &self.current_topology; - let role = current_topology.role(&self.peer_id); + let topology = &self.topology; + let role = topology.role(&self.peer_id); let addr = &self.peer_id.address; #[allow(clippy::suspicious_operation_groupings)] @@ -499,7 +495,7 @@ impl Sumeragi { BlockMessage::BlockCommitted(BlockCommitted { hash, signatures }), Role::Leader | Role::ValidatingPeer | Role::ProxyTail | Role::ObservingPeer, ) => { - let is_consensus_required = current_topology.is_consensus_required().is_some(); + let is_consensus_required = topology.is_consensus_required().is_some(); if role == Role::ProxyTail && is_consensus_required || role == Role::Leader && !is_consensus_required { @@ -507,7 +503,7 @@ impl Sumeragi { } else if let Some(voted_block) = voting_block.take() { match voted_block .block - .commit_with_signatures(current_topology, signatures, hash) + .commit_with_signatures(topology, signatures, hash) .unpack(|e| self.send_event(e)) { Ok(committed_block) => { @@ -534,26 +530,26 @@ impl Sumeragi { } } (BlockMessage::BlockCreated(block_created), Role::ValidatingPeer) => { - let current_topology = current_topology + let topology = topology .is_consensus_required() .expect("Peer has `ValidatingPeer` role, which mean that current topology require consensus"); // Release block writer before creating a new one let _ = voting_block.take(); if let Some(v_block) = - self.vote_for_block(state, ¤t_topology, genesis_public_key, block_created) + self.vote_for_block(state, &topology, genesis_public_key, block_created) { let block_hash = v_block.block.as_ref().hash(); let msg = BlockSigned::from(&v_block.block); - self.broadcast_packet_to(msg, [current_topology.proxy_tail()]); + self.broadcast_packet_to(msg, [topology.proxy_tail()]); info!(%addr, block=%block_hash, "Block validated, signed and forwarded"); *voting_block = Some(v_block); } } (BlockMessage::BlockCreated(BlockCreated { block }), Role::ObservingPeer) => { - let current_topology = current_topology.is_consensus_required().expect( + let current_topology = topology.is_consensus_required().expect( "Peer has `ObservingPeer` role, which mean that current topology require consensus" ); @@ -562,13 +558,13 @@ impl Sumeragi { let v_block = { let block_hash = block.hash_of_payload(); - let role = self.current_topology.role(&self.peer_id); + let role = self.topology.role(&self.peer_id); trace!(%addr, %role, %block_hash, "Block received, voting..."); let mut state_block = state.block(); match ValidBlock::validate( block, - ¤t_topology, + topology, &self.chain_id, genesis_public_key, &mut state_block, @@ -577,7 +573,7 @@ impl Sumeragi { { Ok(block) => { let block = if current_view_change_index >= 1 { - block.sign(&self.key_pair) + block.sign(&self.key_pair, topology) } else { block }; @@ -608,7 +604,7 @@ impl Sumeragi { // Release block writer before creating new one let _ = voting_block.take(); if let Some(mut new_block) = - self.vote_for_block(state, current_topology, genesis_public_key, block_created) + self.vote_for_block(state, topology, genesis_public_key, block_created) { // NOTE: Up until this point it was unknown which block is expected to be received, // therefore all the signatures (of any hash) were collected and will now be pruned @@ -624,8 +620,7 @@ impl Sumeragi { } else { &[Role::ValidatingPeer] }; - let valid_signatures = - current_topology.filter_signatures_by_roles(roles, &signatures); + let valid_signatures = topology.filter_signatures_by_roles(roles, &signatures); if let Some(voted_block) = voting_block.as_mut() { let voting_block_hash = voted_block.block.as_ref().hash_of_payload(); @@ -656,8 +651,8 @@ impl Sumeragi { round_start_time: &Instant, #[cfg_attr(not(debug_assertions), allow(unused_variables))] is_genesis_peer: bool, ) { - let current_topology = &self.current_topology; - let role = current_topology.role(&self.peer_id); + let topology = &self.topology; + let role = topology.role(&self.peer_id); let addr = &self.peer_id.address; match role { @@ -677,15 +672,15 @@ impl Sumeragi { let event_recommendations = Vec::new(); let new_block = BlockBuilder::new( transactions, - self.current_topology.clone(), + self.topology.clone(), event_recommendations, ) .chain(current_view_change_index, &mut state_block) - .sign(&self.key_pair) + .sign(self.key_pair.private_key()) .unpack(|e| self.send_event(e)); let created_in = create_block_start_time.elapsed(); - if current_topology.is_consensus_required().is_some() { + if topology.is_consensus_required().is_some() { info!(%addr, created_in_ms=%created_in.as_millis(), block=%new_block.as_ref().hash(), "Block created"); if created_in > self.pipeline_time() / 2 { @@ -696,10 +691,7 @@ impl Sumeragi { let msg = BlockCreated::from(new_block); self.broadcast_packet(msg); } else { - match new_block - .commit(current_topology) - .unpack(|e| self.send_event(e)) - { + match new_block.commit(topology).unpack(|e| self.send_event(e)) { Ok(committed_block) => { self.broadcast_packet(BlockCommitted::from(&committed_block)); self.commit_block(committed_block, state_block); @@ -717,7 +709,7 @@ impl Sumeragi { match voted_block .block - .commit(current_topology) + .commit(topology) .unpack(|e| self.send_event(e)) { Ok(committed_block) => { @@ -761,9 +753,9 @@ fn reset_state( old_latest_block_hash: &mut HashOf, latest_block: &SignedBlock, // below is the state that gets reset. - current_topology: &mut Topology, + topology: &mut Topology, voting_block: &mut Option, - voting_signatures: &mut Vec>, + voting_signatures: &mut Vec, round_start_time: &mut Instant, last_view_change_time: &mut Instant, view_change_time: &mut Duration, @@ -788,17 +780,17 @@ fn reset_state( if was_commit_or_view_change { *old_latest_block_hash = current_latest_block_hash; - *current_topology = Topology::recreate_topology( + *topology = Topology::recreate_topology( latest_block, current_view_change_index, - current_topology.ordered_peers.iter().cloned().collect(), + topology.iter().cloned().collect(), ); *voting_block = None; voting_signatures.clear(); *last_view_change_time = Instant::now(); *view_change_time = pipeline_time; - info!(addr=%peer_id.address, role=%current_topology.role(peer_id), %current_view_change_index, "View change updated"); + info!(addr=%peer_id.address, role=%topology.role(peer_id), %current_view_change_index, "View change updated"); } } @@ -823,7 +815,7 @@ pub(crate) fn run( state: Arc, ) { // Connect peers with initial topology - sumeragi.connect_peers(&sumeragi.current_topology); + sumeragi.connect_peers(&sumeragi.topology); let span = span!(tracing::Level::TRACE, "genesis").entered(); let is_genesis_peer = if state.view().height() == 0 @@ -849,7 +841,7 @@ pub(crate) fn run( info!( addr=%sumeragi.peer_id.address, - role_in_next_round=%sumeragi.current_topology.role(&sumeragi.peer_id), + role_in_next_round=%sumeragi.topology.role(&sumeragi.peer_id), "Sumeragi initialized", ); @@ -914,7 +906,7 @@ pub(crate) fn run( &state_view .latest_block_ref() .expect("state must have blocks"), - &mut sumeragi.current_topology, + &mut sumeragi.topology, &mut voting_block, &mut voting_signatures, &mut round_start_time, @@ -952,7 +944,7 @@ pub(crate) fn run( if (node_expects_block || current_view_change_index > 0) && last_view_change_time.elapsed() > view_change_time { - let role = sumeragi.current_topology.role(&sumeragi.peer_id); + let role = sumeragi.topology.role(&sumeragi.peer_id); if node_expects_block { if let Some(VotingBlock { block, .. }) = voting_block.as_ref() { @@ -964,16 +956,21 @@ pub(crate) fn run( warn!(peer_public_key=%sumeragi.peer_id.public_key, %role, "No block produced in due time, requesting view change..."); } - let suspect_proof = + let suspect_proof = { + let node_pos = sumeragi + .topology + .position(sumeragi.key_pair.public_key()) + .expect("BUG: Node is not part of consensus"); + ProofBuilder::new(state_view.latest_block_hash(), current_view_change_index) - .sign(&sumeragi.key_pair); + .sign(node_pos as u64, sumeragi.key_pair.private_key()) + }; view_change_proof_chain .insert_proof( - &sumeragi.current_topology.ordered_peers, - sumeragi.current_topology.max_faults(), - state_view.latest_block_hash(), suspect_proof, + &sumeragi.topology, + state_view.latest_block_hash(), ) .unwrap_or_else(|err| error!("{err}")); } @@ -995,7 +992,7 @@ pub(crate) fn run( &state_view .latest_block_ref() .expect("state must have blocks"), - &mut sumeragi.current_topology, + &mut sumeragi.topology, &mut voting_block, &mut voting_signatures, &mut round_start_time, @@ -1016,7 +1013,7 @@ pub(crate) fn run( fn add_signatures( block: &mut VotingBlock, - signatures: impl IntoIterator>, + signatures: impl IntoIterator, ) { for signature in signatures { if let Err(error) = block.block.add_signature(signature) { @@ -1216,7 +1213,7 @@ mod tests { fn create_data_for_test( chain_id: &ChainId, topology: &Topology, - leader_key_pair: &KeyPair, + leader_private_key: &PrivateKey, tx_signer_key_pair: &KeyPair, ) -> (State, Arc, SignedBlock) { // Predefined world state @@ -1226,7 +1223,7 @@ mod tests { let domain_id = "wonderland".parse().expect("Valid"); let mut domain = Domain::new(domain_id).build(&alice_id); assert!(domain.add_account(account).is_none()); - let world = World::with([domain], topology.ordered_peers.clone()); + let world = World::with([domain], topology.iter().cloned().collect()); let kura = Kura::blank_kura_for_testing(); let query_handle = LiveQueryStore::test().start(); let state = State::new(world, Arc::clone(&kura), query_handle); @@ -1250,7 +1247,7 @@ mod tests { // Creating a block of two identical transactions and validating it let block = BlockBuilder::new(vec![tx.clone(), tx], topology.clone(), Vec::new()) .chain(0, &mut state_block) - .sign(leader_key_pair) + .sign(leader_private_key) .unpack(|_| {}); let genesis = block @@ -1295,7 +1292,7 @@ mod tests { // Creating a block of two identical transactions and validating it BlockBuilder::new(vec![tx1, tx2], topology.clone(), Vec::new()) .chain(0, &mut state_block) - .sign(leader_key_pair) + .sign(leader_private_key) .unpack(|_| {}) }; @@ -1308,13 +1305,17 @@ mod tests { let chain_id = ChainId::from("0"); let tx_signer_key_pair = KeyPair::random(); - let leader_key_pair = KeyPair::random(); + let (leader_public_key, leader_private_key) = KeyPair::random().into_parts(); let topology = Topology::new(unique_vec![PeerId::new( "127.0.0.1:8080".parse().unwrap(), - leader_key_pair.public_key().clone(), + leader_public_key, )]); - let (state, _, mut block) = - create_data_for_test(&chain_id, &topology, &leader_key_pair, &tx_signer_key_pair); + let (state, _, mut block) = create_data_for_test( + &chain_id, + &topology, + &leader_private_key, + &tx_signer_key_pair, + ); // Malform block to make it invalid payload_mut(&mut block).commit_topology.clear(); @@ -1334,13 +1335,17 @@ mod tests { let chain_id = ChainId::from("0"); let tx_signer_key_pair = KeyPair::random(); - let leader_key_pair = KeyPair::random(); + let (leader_public_key, leader_private_key) = KeyPair::random().into_parts(); let topology = Topology::new(unique_vec![PeerId::new( "127.0.0.1:8080".parse().unwrap(), - leader_key_pair.public_key().clone(), + leader_public_key, )]); - let (state, kura, mut block) = - create_data_for_test(&chain_id, &topology, &leader_key_pair, &tx_signer_key_pair); + let (state, kura, mut block) = create_data_for_test( + &chain_id, + &topology, + &leader_private_key, + &tx_signer_key_pair, + ); let mut state_block = state.block(); let committed_block = ValidBlock::validate( @@ -1383,9 +1388,13 @@ mod tests { let tx_signer_key_pair = KeyPair::random(); let topology = Topology::new(UniqueVec::new()); - let leader_key_pair = KeyPair::random(); - let (state, _, mut block) = - create_data_for_test(&chain_id, &topology, &leader_key_pair, &tx_signer_key_pair); + let (_, leader_private_key) = KeyPair::random().into_parts(); + let (state, _, mut block) = create_data_for_test( + &chain_id, + &topology, + &leader_private_key, + &tx_signer_key_pair, + ); // Change block height payload_mut(&mut block).header.height = 42; @@ -1420,8 +1429,12 @@ mod tests { "127.0.0.1:8080".parse().unwrap(), leader_key_pair.public_key().clone(), )]); - let (state, _, block) = - create_data_for_test(&chain_id, &topology, &leader_key_pair, &tx_signer_key_pair); + let (state, _, block) = create_data_for_test( + &chain_id, + &topology, + leader_key_pair.private_key(), + &tx_signer_key_pair, + ); let result = handle_block_sync( &chain_id, tx_signer_key_pair.public_key(), @@ -1442,8 +1455,12 @@ mod tests { "127.0.0.1:8080".parse().unwrap(), leader_key_pair.public_key().clone(), )]); - let (state, kura, mut block) = - create_data_for_test(&chain_id, &topology, &leader_key_pair, &tx_signer_key_pair); + let (state, kura, mut block) = create_data_for_test( + &chain_id, + &topology, + leader_key_pair.private_key(), + &tx_signer_key_pair, + ); let mut state_block = state.block(); let committed_block = ValidBlock::validate( @@ -1487,8 +1504,12 @@ mod tests { "127.0.0.1:8080".parse().unwrap(), leader_key_pair.public_key().clone(), )]); - let (state, kura, mut block) = - create_data_for_test(&chain_id, &topology, &leader_key_pair, &tx_signer_key_pair); + let (state, kura, mut block) = create_data_for_test( + &chain_id, + &topology, + leader_key_pair.private_key(), + &tx_signer_key_pair, + ); // Increase block view change index payload_mut(&mut block).header.view_change_index = 42; @@ -1541,8 +1562,12 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let leader_key_pair = KeyPair::random(); - let (state, _, mut block) = - create_data_for_test(&chain_id, &topology, &leader_key_pair, &tx_signer_key_pair); + let (state, _, mut block) = create_data_for_test( + &chain_id, + &topology, + leader_key_pair.private_key(), + &tx_signer_key_pair, + ); // Change block height and view change index // Soft-fork on genesis block is not possible diff --git a/core/src/sumeragi/message.rs b/core/src/sumeragi/message.rs index c5d4fa27fa7..9417e95187f 100644 --- a/core/src/sumeragi/message.rs +++ b/core/src/sumeragi/message.rs @@ -1,6 +1,6 @@ //! Contains message structures for p2p communication during consensus. -use iroha_crypto::{HashOf, SignaturesOf}; -use iroha_data_model::block::{BlockPayload, SignedBlock}; +use iroha_crypto::HashOf; +use iroha_data_model::block::{BlockPayload, BlockSignature, SignedBlock}; use iroha_macro::*; use parity_scale_codec::{Decode, Encode}; @@ -59,17 +59,17 @@ pub struct BlockSigned { /// Hash of the block being signed. pub hash: HashOf, /// Set of signatures. - pub signatures: SignaturesOf, + pub signatures: Vec, } impl From<&ValidBlock> for BlockSigned { fn from(block: &ValidBlock) -> Self { let block_hash = block.as_ref().hash_of_payload(); - let block_signatures = block.as_ref().signatures().clone(); + let signatures = block.as_ref().signatures().cloned().collect(); Self { hash: block_hash, - signatures: block_signatures, + signatures, } } } @@ -81,17 +81,17 @@ pub struct BlockCommitted { /// Hash of the block being signed. pub hash: HashOf, /// Set of signatures. - pub signatures: SignaturesOf, + pub signatures: Vec, } impl From<&CommittedBlock> for BlockCommitted { fn from(block: &CommittedBlock) -> Self { let block_hash = block.as_ref().hash(); - let block_signatures = block.as_ref().signatures().clone(); + let signatures = block.as_ref().signatures().cloned().collect(); Self { hash: block_hash, - signatures: block_signatures, + signatures, } } } diff --git a/core/src/sumeragi/mod.rs b/core/src/sumeragi/mod.rs index 92c441f17dd..deddd18789c 100644 --- a/core/src/sumeragi/mod.rs +++ b/core/src/sumeragi/mod.rs @@ -9,7 +9,6 @@ use std::{ use eyre::Result; use iroha_config::parameters::actual::{Common as CommonConfig, Sumeragi as SumeragiConfig}; -use iroha_crypto::{KeyPair, SignatureOf}; use iroha_data_model::{block::SignedBlock, prelude::*}; use iroha_genesis::GenesisNetwork; use iroha_logger::prelude::*; @@ -72,14 +71,14 @@ impl SumeragiHandle { block: &SignedBlock, state_block: &mut StateBlock<'_>, events_sender: &EventsSender, - mut current_topology: Topology, + mut topology: Topology, ) -> Topology { // NOTE: topology need to be updated up to block's view_change_index - current_topology.rotate_all_n(block.header().view_change_index); + topology.rotate_all_n(block.header().view_change_index); let block = ValidBlock::validate( block.clone(), - ¤t_topology, + &topology, chain_id, genesis_public_key, state_block, @@ -88,14 +87,15 @@ impl SumeragiHandle { let _ = events_sender.send(e.into()); }) .expect("Kura: Invalid block") - .commit(¤t_topology) + .commit(&topology) .unpack(|e| { let _ = events_sender.send(e.into()); }) .expect("Kura: Invalid block"); if block.as_ref().header().is_genesis() { - *state_block.world.trusted_peers_ids = block.as_ref().commit_topology().clone(); + *state_block.world.trusted_peers_ids = + block.as_ref().commit_topology().cloned().collect(); } state_block @@ -139,7 +139,7 @@ impl SumeragiHandle { let (message_sender, message_receiver) = mpsc::sync_channel(100); let blocks_iter; - let mut current_topology; + let mut topology; { let state_view = state.view(); @@ -151,7 +151,7 @@ impl SumeragiHandle { ) }); - current_topology = match state_view.height() { + topology = match state_view.height() { 0 => { assert!(!sumeragi_config.trusted_peers.is_empty()); Topology::new(sumeragi_config.trusted_peers.clone()) @@ -172,13 +172,13 @@ impl SumeragiHandle { for block in blocks_iter { let mut state_block = state.block(); - current_topology = Self::replay_block( + topology = Self::replay_block( &common_config.chain_id, &genesis_network.public_key, &block, &mut state_block, &events_sender, - current_topology, + topology, ); state_block.commit(); } @@ -206,7 +206,7 @@ impl SumeragiHandle { control_message_receiver, message_receiver, debug_force_soft_fork, - current_topology, + topology, transaction_cache: Vec::new(), view_changes_metric: view_changes, }; diff --git a/core/src/sumeragi/network_topology.rs b/core/src/sumeragi/network_topology.rs index dfa22fd9cc5..9915447cb13 100644 --- a/core/src/sumeragi/network_topology.rs +++ b/core/src/sumeragi/network_topology.rs @@ -2,8 +2,10 @@ use derive_more::Display; use indexmap::IndexSet; -use iroha_crypto::{PublicKey, SignatureOf}; -use iroha_data_model::{block::SignedBlock, prelude::PeerId}; +use iroha_data_model::{ + block::{BlockSignature, SignedBlock}, + prelude::PeerId, +}; use iroha_logger::trace; use iroha_primitives::unique_vec::UniqueVec; @@ -19,10 +21,7 @@ use iroha_primitives::unique_vec::UniqueVec; /// /// Above is an illustration of how the various operations work for a f = 2 topology. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct Topology { - /// Current order of peers. The roles of peers are defined based on this order. - pub(crate) ordered_peers: UniqueVec, -} +pub struct Topology(UniqueVec); /// Topology with at least one peer #[derive(Debug, Clone, PartialEq, Eq, derive_more::Deref)] @@ -36,32 +35,59 @@ pub struct ConsensusTopology<'topology> { topology: &'topology Topology, } +impl AsRef<[PeerId]> for Topology { + fn as_ref(&self) -> &[PeerId] { + &self.0 + } +} + +impl IntoIterator for Topology { + type Item = PeerId; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + +impl FromIterator for Topology { + fn from_iter>(iter: T) -> Self { + Self(UniqueVec::from_iter(iter)) + } +} + impl Topology { /// Create a new topology. - pub fn new(peers: UniqueVec) -> Self { - Topology { - ordered_peers: peers, - } + pub fn new(peers: impl IntoIterator) -> Self { + Topology(peers.into_iter().collect()) + } + + pub(crate) fn iter(&self) -> impl ExactSizeIterator { + self.0.iter() + } + + pub(crate) fn position(&self, public_key: &iroha_crypto::PublicKey) -> Option { + self.iter().position(|peer| peer.public_key() == public_key) } /// True, if the topology contains at least one peer and thus requires consensus pub fn is_non_empty(&self) -> Option { - (!self.ordered_peers.is_empty()).then_some(NonEmptyTopology { topology: self }) + (!self.0.is_empty()).then_some(NonEmptyTopology { topology: self }) } /// Is consensus required, aka are there more than 1 peer. pub fn is_consensus_required(&self) -> Option { - (self.ordered_peers.len() > 1).then_some(ConsensusTopology { topology: self }) + (self.0.len() > 1).then_some(ConsensusTopology { topology: self }) } /// How many faulty peers can this topology tolerate. pub fn max_faults(&self) -> usize { - (self.ordered_peers.len().saturating_sub(1)) / 3 + (self.0.len().saturating_sub(1)) / 3 } /// The required amount of votes to commit a block with this topology. pub fn min_votes_for_commit(&self) -> usize { - let len = self.ordered_peers.len(); + let len = self.0.len(); if len > 3 { self.max_faults() * 2 + 1 } else { @@ -69,57 +95,51 @@ impl Topology { } } - /// Index of leader among `ordered_peers` + /// Index of leader #[allow(clippy::unused_self)] // In order to be consistent with `proxy_tail_index` method fn leader_index(&self) -> usize { 0 } - /// Index of leader among `ordered_peers` + /// Index of proxy tail fn proxy_tail_index(&self) -> usize { - // NOTE: proxy tail is the last element from the set A so that's why it's `min_votes_for_commit - 1` + // NOTE: proxy tail is the last element of set A self.min_votes_for_commit() - 1 } /// Filter signatures by roles in the topology. #[allow(clippy::comparison_chain)] - pub fn filter_signatures_by_roles<'a, T: 'a, I: IntoIterator>>( + pub fn filter_signatures_by_roles<'a, I: IntoIterator>( &self, roles: &[Role], signatures: I, - ) -> Vec> { - let mut public_keys = IndexSet::with_capacity(self.ordered_peers.len()); + ) -> Vec { + let mut filtered = Vec::new(); + for role in roles { match (role, self.is_non_empty(), self.is_consensus_required()) { - (Role::Leader, Some(topology), _) => { - public_keys.insert(topology.leader().public_key()); - } - (Role::ProxyTail, _, Some(topology)) => { - public_keys.insert(&topology.proxy_tail().public_key); - } + (Role::Leader, Some(topology), _) => filtered.push(topology.leader_index()), + (Role::ProxyTail, _, Some(topology)) => filtered.push(topology.proxy_tail_index()), (Role::ValidatingPeer, _, Some(topology)) => { - for peer in topology.validating_peers() { - public_keys.insert(peer.public_key()); - } + filtered.extend(topology.leader_index() + 1..topology.proxy_tail_index()); } (Role::ObservingPeer, _, Some(topology)) => { - for peer in topology.observing_peers() { - public_keys.insert(peer.public_key()); - } + filtered.extend(topology.proxy_tail_index() + 1..topology.0.len()); } _ => {} }; } + signatures .into_iter() - .filter(|signature| public_keys.contains(signature.public_key())) + .filter(|signature| filtered.contains(&(signature.0 as usize))) .cloned() .collect() } /// What role does this peer have in the topology. pub fn role(&self, peer_id: &PeerId) -> Role { - match self.ordered_peers.iter().position(|p| p == peer_id) { + match self.0.iter().position(|p| p == peer_id) { Some(index) if index == self.leader_index() => Role::Leader, Some(index) if index < self.proxy_tail_index() => Role::ValidatingPeer, Some(index) if index == self.proxy_tail_index() => Role::ProxyTail, @@ -134,13 +154,13 @@ impl Topology { /// Add or remove peers from the topology. pub fn update_peer_list(&mut self, new_peers: UniqueVec) { self.modify_peers_directly(|peers| peers.retain(|peer| new_peers.contains(peer))); - self.ordered_peers.extend(new_peers); + self.0.extend(new_peers); } /// Rotate peers n times where n is a number of failed attempt to create a block. pub fn rotate_all_n(&mut self, n: u64) { let len = self - .ordered_peers + .0 .len() .try_into() .expect("`usize` should fit into `u64`"); @@ -162,14 +182,21 @@ impl Topology { } /// Pull peers up in the topology to the top of the a set while preserving local order. - pub fn lift_up_peers(&mut self, to_lift_up: &[PublicKey]) { + pub fn lift_up_peers(&mut self, to_lift_up: &[u64]) { self.modify_peers_directly(|peers| { - peers.sort_by_cached_key(|peer| !to_lift_up.contains(&peer.public_key)); + let to_lift_up: IndexSet<_> = to_lift_up.iter().collect(); + + let mut node_pos = 0; + peers.sort_by_cached_key(|_| { + let res = !to_lift_up.contains(&node_pos); + node_pos += 1; + res + }); }); } /// Perform sequence of actions after block committed. - pub fn update_topology(&mut self, block_signees: &[PublicKey], new_peers: UniqueVec) { + pub fn update_topology(&mut self, block_signees: &[u64], new_peers: UniqueVec) { self.lift_up_peers(block_signees); self.rotate_set_a(); self.update_peer_list(new_peers); @@ -181,13 +208,9 @@ impl Topology { view_change_index: u64, new_peers: UniqueVec, ) -> Self { - let mut topology = Topology::new(block.commit_topology().clone()); - let block_signees = block - .signatures() - .into_iter() - .map(|s| s.public_key()) - .cloned() - .collect::>(); + let mut topology = Topology::new(block.commit_topology().cloned()); + + let block_signees = block.signatures().map(|s| s.0).collect::>(); topology.update_topology(&block_signees, new_peers); @@ -195,7 +218,7 @@ impl Topology { topology.rotate_all_n(view_change_index); { - // FIXME: This is a hack to prevent consensus from running amock due to + // HACK: This is a hack to prevent consensus from running amock due to // a bug in the implementation by reverting to predictable ordering let view_change_limit: usize = view_change_index @@ -205,60 +228,59 @@ impl Topology { if view_change_limit > 1 { iroha_logger::error!("Restarting consensus(internal bug). Report to developers"); - let mut peers: Vec<_> = topology.ordered_peers.iter().cloned().collect(); + let mut peers: Vec<_> = topology.0.iter().cloned().collect(); peers.sort(); let peers_count = peers.len(); peers.rotate_right(view_change_limit % peers_count); - topology = Topology::new(peers.into_iter().collect()); + topology = Topology::new(peers.into_iter()); } } topology } - /// Modify [`ordered_peers`](Self::ordered_peers) directly as [`Vec`]. fn modify_peers_directly(&mut self, f: impl FnOnce(&mut Vec)) { - let unique_peers = std::mem::take(&mut self.ordered_peers); - + let unique_peers = std::mem::take(&mut self.0); let mut peers_vec = Vec::from(unique_peers); + f(&mut peers_vec); - self.ordered_peers = UniqueVec::from_iter(peers_vec); + self.0 = UniqueVec::from_iter(peers_vec); } } impl<'topology> NonEmptyTopology<'topology> { /// Get leader's [`PeerId`]. pub fn leader(&self) -> &'topology PeerId { - &self.topology.ordered_peers[self.topology.leader_index()] + &self.topology.0[self.topology.leader_index()] } } impl<'topology> ConsensusTopology<'topology> { /// Get proxy tail's peer id. pub fn proxy_tail(&self) -> &'topology PeerId { - &self.topology.ordered_peers[self.topology.proxy_tail_index()] + &self.topology.0[self.topology.proxy_tail_index()] } /// Get leader's [`PeerId`] pub fn leader(&self) -> &'topology PeerId { - &self.topology.ordered_peers[self.topology.leader_index()] + &self.topology.0[self.topology.leader_index()] } /// Get validating [`PeerId`]s. pub fn validating_peers(&self) -> &'topology [PeerId] { - &self.ordered_peers[self.leader_index() + 1..self.proxy_tail_index()] + &self.0[self.leader_index() + 1..self.proxy_tail_index()] } /// Get observing [`PeerId`]s. pub fn observing_peers(&self) -> &'topology [PeerId] { - &self.ordered_peers[self.proxy_tail_index() + 1..] + &self.0[self.proxy_tail_index() + 1..] } /// Get voting [`PeerId`]s. pub fn voting_peers(&self) -> &'topology [PeerId] { - &self.ordered_peers[self.leader_index()..=self.proxy_tail_index()] + &self.0[self.leader_index()..=self.proxy_tail_index()] } } @@ -280,7 +302,10 @@ pub enum Role { #[cfg(test)] macro_rules! test_peers { ($($id:literal),+$(,)?) => {{ - let mut iter = ::core::iter::repeat_with(|| KeyPair::random()); + let mut iter = ::core::iter::repeat_with( + || iroha_crypto::KeyPair::random() + ); + test_peers![$($id),*: iter] }}; ($($id:literal),+$(,)?: $key_pair_iter:expr) => { @@ -298,6 +323,8 @@ mod tests { use iroha_crypto::KeyPair; use iroha_primitives::unique_vec; + use crate::block::ValidBlock; + use super::*; fn topology() -> Topology { @@ -306,11 +333,7 @@ mod tests { } fn extract_ports(topology: &Topology) -> Vec { - topology - .ordered_peers - .iter() - .map(|peer| peer.address.port()) - .collect() + topology.0.iter().map(|peer| peer.address.port()).collect() } #[test] @@ -324,12 +347,7 @@ mod tests { fn lift_up_peers() { let mut topology = topology(); // Will lift up 1, 2, 4, 6 - let to_lift_up = &[ - topology.ordered_peers[1].public_key().clone(), - topology.ordered_peers[2].public_key().clone(), - topology.ordered_peers[4].public_key().clone(), - topology.ordered_peers[6].public_key().clone(), - ]; + let to_lift_up = &[1, 2, 4, 6]; topology.lift_up_peers(to_lift_up); assert_eq!(extract_ports(&topology), vec![1, 2, 4, 6, 0, 3, 5]) } @@ -340,9 +358,9 @@ mod tests { // New peers will be 0, 2, 5, 7 let new_peers = { let mut peers = unique_vec![ - topology.ordered_peers[5].clone(), - topology.ordered_peers[0].clone(), - topology.ordered_peers[2].clone(), + topology.0[5].clone(), + topology.0[0].clone(), + topology.0[2].clone(), ]; peers.extend(test_peers![7]); peers @@ -360,37 +378,31 @@ mod tests { let peers = test_peers![0, 1, 2, 3, 4, 5, 6: key_pairs_iter]; let topology = Topology::new(peers.clone()); - let dummy = "value to sign"; - let signatures = key_pairs - .iter() - .map(|key_pair| SignatureOf::new(key_pair, &dummy)) - .collect::>>(); + let dummy_block = ValidBlock::new_dummy(); + let dummy_signature = &dummy_block.as_ref().signatures().next().unwrap().1; + let dummy_signatures = (0..key_pairs.len()) + .map(|i| BlockSignature(i as u64, dummy_signature.clone())) + .collect::>(); let leader_signatures = - topology.filter_signatures_by_roles(&[Role::Leader], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::Leader], dummy_signatures.iter()); assert_eq!(leader_signatures.len(), 1); - assert_eq!(leader_signatures[0].public_key(), peers[0].public_key()); + assert_eq!(leader_signatures[0].0, 0); let proxy_tail_signatures = - topology.filter_signatures_by_roles(&[Role::ProxyTail], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ProxyTail], dummy_signatures.iter()); assert_eq!(proxy_tail_signatures.len(), 1); - assert_eq!(proxy_tail_signatures[0].public_key(), peers[4].public_key()); + assert_eq!(proxy_tail_signatures[0].0, 4); let validating_peers_signatures = - topology.filter_signatures_by_roles(&[Role::ValidatingPeer], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ValidatingPeer], dummy_signatures.iter()); assert_eq!(validating_peers_signatures.len(), 3); - assert!(validating_peers_signatures - .iter() - .map(|s| s.public_key()) - .eq(peers[1..4].iter().map(PeerId::public_key))); + assert!(validating_peers_signatures.iter().map(|s| s.0).eq(1..4)); let observing_peers_signatures = - topology.filter_signatures_by_roles(&[Role::ObservingPeer], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ObservingPeer], dummy_signatures.iter()); assert_eq!(observing_peers_signatures.len(), 2); - assert!(observing_peers_signatures - .iter() - .map(|s| s.public_key()) - .eq(peers[5..].iter().map(PeerId::public_key))); + assert!(observing_peers_signatures.iter().map(|s| s.0).eq(5..7)); } #[test] @@ -401,26 +413,26 @@ mod tests { let peers = UniqueVec::new(); let topology = Topology::new(peers); - let dummy = "value to sign"; - let signatures = key_pairs - .iter() - .map(|key_pair| SignatureOf::new(key_pair, &dummy)) - .collect::>>(); + let dummy_block = ValidBlock::new_dummy(); + let dummy_signature = &dummy_block.as_ref().signatures().next().unwrap().1; + let dummy_signatures = (0..key_pairs.len()) + .map(|i| BlockSignature(i as u64, dummy_signature.clone())) + .collect::>(); let leader_signatures = - topology.filter_signatures_by_roles(&[Role::Leader], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::Leader], dummy_signatures.iter()); assert!(leader_signatures.is_empty()); let proxy_tail_signatures = - topology.filter_signatures_by_roles(&[Role::ProxyTail], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ProxyTail], dummy_signatures.iter()); assert!(proxy_tail_signatures.is_empty()); let validating_peers_signatures = - topology.filter_signatures_by_roles(&[Role::ValidatingPeer], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ValidatingPeer], dummy_signatures.iter()); assert!(validating_peers_signatures.is_empty()); let observing_peers_signatures = - topology.filter_signatures_by_roles(&[Role::ObservingPeer], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ObservingPeer], dummy_signatures.iter()); assert!(observing_peers_signatures.is_empty()); } @@ -433,27 +445,27 @@ mod tests { let peers = test_peers![0: key_pairs_iter]; let topology = Topology::new(peers.clone()); - let dummy = "value to sign"; - let signatures = key_pairs - .iter() - .map(|key_pair| SignatureOf::new(key_pair, &dummy)) - .collect::>>(); + let dummy_block = ValidBlock::new_dummy(); + let dummy_signature = &dummy_block.as_ref().signatures().next().unwrap().1; + let dummy_signatures = (0..key_pairs.len()) + .map(|i| BlockSignature(i as u64, dummy_signature.clone())) + .collect::>(); let leader_signatures = - topology.filter_signatures_by_roles(&[Role::Leader], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::Leader], dummy_signatures.iter()); assert_eq!(leader_signatures.len(), 1); - assert_eq!(leader_signatures[0].public_key(), peers[0].public_key()); + assert_eq!(leader_signatures[0].0, 0); let proxy_tail_signatures = - topology.filter_signatures_by_roles(&[Role::ProxyTail], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ProxyTail], dummy_signatures.iter()); assert!(proxy_tail_signatures.is_empty()); let validating_peers_signatures = - topology.filter_signatures_by_roles(&[Role::ValidatingPeer], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ValidatingPeer], dummy_signatures.iter()); assert!(validating_peers_signatures.is_empty()); let observing_peers_signatures = - topology.filter_signatures_by_roles(&[Role::ObservingPeer], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ObservingPeer], dummy_signatures.iter()); assert!(observing_peers_signatures.is_empty()); } @@ -466,28 +478,28 @@ mod tests { let peers = test_peers![0, 1: key_pairs_iter]; let topology = Topology::new(peers.clone()); - let dummy = "value to sign"; - let signatures = key_pairs - .iter() - .map(|key_pair| SignatureOf::new(key_pair, &dummy)) - .collect::>>(); + let dummy_block = ValidBlock::new_dummy(); + let dummy_signature = &dummy_block.as_ref().signatures().next().unwrap().1; + let dummy_signatures = (0..key_pairs.len()) + .map(|i| BlockSignature(i as u64, dummy_signature.clone())) + .collect::>(); let leader_signatures = - topology.filter_signatures_by_roles(&[Role::Leader], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::Leader], dummy_signatures.iter()); assert_eq!(leader_signatures.len(), 1); - assert_eq!(leader_signatures[0].public_key(), peers[0].public_key()); + assert_eq!(leader_signatures[0].0, 0); let proxy_tail_signatures = - topology.filter_signatures_by_roles(&[Role::ProxyTail], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ProxyTail], dummy_signatures.iter()); assert_eq!(proxy_tail_signatures.len(), 1); - assert_eq!(proxy_tail_signatures[0].public_key(), peers[1].public_key()); + assert_eq!(proxy_tail_signatures[0].0, 1); let validating_peers_signatures = - topology.filter_signatures_by_roles(&[Role::ValidatingPeer], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ValidatingPeer], dummy_signatures.iter()); assert!(validating_peers_signatures.is_empty()); let observing_peers_signatures = - topology.filter_signatures_by_roles(&[Role::ObservingPeer], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ObservingPeer], dummy_signatures.iter()); assert!(observing_peers_signatures.is_empty()); } @@ -500,32 +512,29 @@ mod tests { let peers = test_peers![0, 1, 2: key_pairs_iter]; let topology = Topology::new(peers.clone()); - let dummy = "value to sign"; - let signatures = key_pairs - .iter() - .map(|key_pair| SignatureOf::new(key_pair, &dummy)) - .collect::>>(); + let dummy_block = ValidBlock::new_dummy(); + let dummy_signature = &dummy_block.as_ref().signatures().next().unwrap().1; + let dummy_signatures = (0..key_pairs.len()) + .map(|i| BlockSignature(i as u64, dummy_signature.clone())) + .collect::>(); let leader_signatures = - topology.filter_signatures_by_roles(&[Role::Leader], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::Leader], dummy_signatures.iter()); assert_eq!(leader_signatures.len(), 1); - assert_eq!(leader_signatures[0].public_key(), peers[0].public_key()); + assert_eq!(leader_signatures[0].0, 0); let proxy_tail_signatures = - topology.filter_signatures_by_roles(&[Role::ProxyTail], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ProxyTail], dummy_signatures.iter()); assert_eq!(proxy_tail_signatures.len(), 1); - assert_eq!(proxy_tail_signatures[0].public_key(), peers[2].public_key()); + assert_eq!(proxy_tail_signatures[0].0, 2); let validating_peers_signatures = - topology.filter_signatures_by_roles(&[Role::ValidatingPeer], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ValidatingPeer], dummy_signatures.iter()); assert_eq!(validating_peers_signatures.len(), 1); - assert_eq!( - validating_peers_signatures[0].public_key(), - peers[1].public_key() - ); + assert_eq!(validating_peers_signatures[0].0, 1); let observing_peers_signatures = - topology.filter_signatures_by_roles(&[Role::ObservingPeer], signatures.iter()); + topology.filter_signatures_by_roles(&[Role::ObservingPeer], dummy_signatures.iter()); assert!(observing_peers_signatures.is_empty()); } diff --git a/core/src/sumeragi/view_change.rs b/core/src/sumeragi/view_change.rs index 9a24f0ece33..8b48c7928cc 100644 --- a/core/src/sumeragi/view_change.rs +++ b/core/src/sumeragi/view_change.rs @@ -3,12 +3,15 @@ use derive_more::{Deref, DerefMut}; use eyre::Result; -use indexmap::IndexSet; -use iroha_crypto::{HashOf, KeyPair, SignatureOf, SignaturesOf}; -use iroha_data_model::{block::SignedBlock, prelude::PeerId}; +use iroha_crypto::{HashOf, PrivateKey, SignatureOf}; +use iroha_data_model::block::SignedBlock; use parity_scale_codec::{Decode, Encode}; use thiserror::Error; +use super::network_topology::Topology; + +type ViewChangeProofSignature = (u64, SignatureOf); + /// Error emerge during insertion of `Proof` into `ProofChain` #[derive(Error, displaydoc::Display, Debug, Clone, Copy)] #[allow(missing_docs)] @@ -30,7 +33,7 @@ struct ProofPayload { /// The proof of a view change. It needs to be signed by f+1 peers for proof to be valid and view change to happen. #[derive(Debug, Clone, Decode, Encode)] pub struct SignedProof { - signatures: SignaturesOf, + signatures: Vec, /// Collection of signatures from the different peers. payload: ProofPayload, } @@ -54,40 +57,40 @@ impl ProofBuilder { } /// Sign this message with the peer's public and private key. - pub fn sign(mut self, key_pair: &KeyPair) -> SignedProof { - let signature = SignatureOf::new(key_pair, &self.0.payload); - self.0.signatures.insert(signature); + pub fn sign(mut self, node_pos: u64, private_key: &PrivateKey) -> SignedProof { + let signature = SignatureOf::new(private_key, &self.0.payload); + self.0.signatures.push((node_pos, signature)); self.0 } } impl SignedProof { /// Verify the signatures of `other` and add them to this proof. - fn merge_signatures(&mut self, other: SignaturesOf) { - for signature in other { - if signature.verify(&self.payload).is_ok() { - self.signatures.insert(signature); + fn merge_signatures(&mut self, other: Vec, topology: &Topology) { + for (node_pos, signature) in other { + let public_key = topology.as_ref()[node_pos as usize].public_key(); + + if signature.verify(public_key, &self.payload).is_ok() { + self.signatures.push((node_pos, signature)); } } } /// Verify if the proof is valid, given the peers in `topology`. - fn verify(&self, peers: &[PeerId], max_faults: usize) -> bool { - let peer_public_keys: IndexSet<_> = peers.iter().map(PeerId::public_key).collect(); - + fn verify(&self, topology: &Topology) -> bool { let valid_count = self .signatures .iter() - .filter(|signature| { - signature.verify(&self.payload).is_ok() - && peer_public_keys.contains(signature.public_key()) + .filter(|&(node_pos, signature)| { + let public_key = topology.as_ref()[*node_pos as usize].public_key(); + signature.verify(public_key, &self.payload).is_ok() }) .count(); // See Whitepaper for the information on this limit. #[allow(clippy::int_plus_one)] { - valid_count >= max_faults + 1 + valid_count >= topology.max_faults() + 1 } } } @@ -100,8 +103,7 @@ impl ProofChain { /// Verify the view change proof chain. pub fn verify_with_state( &self, - peers: &[PeerId], - max_faults: usize, + topology: &Topology, latest_block_hash: Option>, ) -> usize { self.iter() @@ -109,7 +111,7 @@ impl ProofChain { .take_while(|(i, proof)| { proof.payload.latest_block_hash == latest_block_hash && proof.payload.view_change_index == (*i as u64) - && proof.verify(peers, max_faults) + && proof.verify(topology) }) .count() } @@ -134,23 +136,21 @@ impl ProofChain { /// - If proof view change number differs from view change number pub fn insert_proof( &mut self, - peers: &[PeerId], - max_faults: usize, - latest_block_hash: Option>, new_proof: SignedProof, + topology: &Topology, + latest_block_hash: Option>, ) -> Result<(), Error> { if new_proof.payload.latest_block_hash != latest_block_hash { return Err(Error::BlockHashMismatch); } - let next_unfinished_view_change = - self.verify_with_state(peers, max_faults, latest_block_hash); + let next_unfinished_view_change = self.verify_with_state(topology, latest_block_hash); if new_proof.payload.view_change_index != (next_unfinished_view_change as u64) { return Err(Error::ViewChangeNotFound); // We only care about the current view change that may or may not happen. } let is_proof_chain_incomplete = next_unfinished_view_change < self.len(); if is_proof_chain_incomplete { - self[next_unfinished_view_change].merge_signatures(new_proof.signatures); + self[next_unfinished_view_change].merge_signatures(new_proof.signatures, topology); } else { self.push(new_proof); } @@ -165,8 +165,7 @@ impl ProofChain { pub fn merge( &mut self, mut other: Self, - peers: &[PeerId], - max_faults: usize, + topology: &Topology, latest_block_hash: Option>, ) -> Result<(), Error> { // Prune to exclude invalid proofs @@ -175,8 +174,7 @@ impl ProofChain { return Err(Error::BlockHashMismatch); } - let next_unfinished_view_change = - self.verify_with_state(peers, max_faults, latest_block_hash); + let next_unfinished_view_change = self.verify_with_state(topology, latest_block_hash); let is_proof_chain_incomplete = next_unfinished_view_change < self.len(); let other_contain_additional_proofs = next_unfinished_view_change < other.len(); @@ -184,7 +182,7 @@ impl ProofChain { // Case 1: proof chain is incomplete and other have corresponding proof. (true, true) => { let new_proof = other.swap_remove(next_unfinished_view_change); - self[next_unfinished_view_change].merge_signatures(new_proof.signatures); + self[next_unfinished_view_change].merge_signatures(new_proof.signatures, topology); } // Case 2: proof chain is complete, but other have additional proof. (false, true) => { diff --git a/core/src/tx.rs b/core/src/tx.rs index 070ff3e7d03..a0aa786589b 100644 --- a/core/src/tx.rs +++ b/core/src/tx.rs @@ -14,7 +14,9 @@ pub use iroha_data_model::prelude::*; use iroha_data_model::{ isi::error::Mismatch, query::error::FindError, - transaction::{error::TransactionLimitError, TransactionLimits, TransactionPayload}, + transaction::{ + error::TransactionLimitError, TransactionLimits, TransactionPayload, TransactionSignature, + }, }; use iroha_genesis::GenesisTransaction; use iroha_logger::{debug, error}; @@ -63,8 +65,8 @@ impl AcceptedTransaction { })); } - for signature in tx.0.signatures() { - if signature.public_key() != genesis_public_key { + for TransactionSignature(public_key, signature) in tx.0.signatures() { + if public_key != genesis_public_key { return Err(SignatureVerificationFail { signature: signature.clone().into(), reason: "Signature doesn't correspond to genesis public key".to_string(), diff --git a/crypto/src/signature/mod.rs b/crypto/src/signature/mod.rs index 29c22cc6844..b4b807e80fa 100644 --- a/crypto/src/signature/mod.rs +++ b/crypto/src/signature/mod.rs @@ -11,13 +11,8 @@ pub(crate) mod ed25519; pub(crate) mod secp256k1; #[cfg(not(feature = "std"))] -use alloc::{ - boxed::Box, collections::btree_set, format, string::String, string::ToString as _, vec, - vec::Vec, -}; +use alloc::{boxed::Box, format, string::String, vec, vec::Vec}; use core::{borrow::Borrow as _, marker::PhantomData}; -#[cfg(feature = "std")] -use std::collections::btree_set; use arrayref::array_ref; use derive_more::{Deref, DerefMut}; @@ -30,7 +25,7 @@ use serde::{Deserialize, Serialize}; use sha2::Digest as _; use zeroize::Zeroize as _; -use crate::{error::ParseError, ffi, hex_decode, Error, HashOf, KeyPair, PublicKey}; +use crate::{error::ParseError, ffi, hex_decode, Error, HashOf, PrivateKey, PublicKey}; /// Construct cryptographic RNG from seed. fn rng_from_seed(mut seed: Vec) -> impl CryptoRngCore { @@ -44,31 +39,19 @@ ffi::ffi_item! { #[serde_with::serde_as] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, getset::Getters)] #[cfg_attr(not(feature="ffi_import"), derive(derive_more::DebugCustom, Hash, Decode, Encode, Deserialize, Serialize, IntoSchema))] - #[cfg_attr(not(feature="ffi_import"), debug( - fmt = "{{ pub_key: {public_key}, payload: {} }}", - "hex::encode_upper(payload)" - ))] + #[cfg_attr(not(feature="ffi_import"), debug(fmt = "{{ {} }}", "hex::encode_upper(payload)"))] pub struct Signature { - /// Public key that is used for verification. Payload is verified by algorithm - /// that corresponds with the public key's digest function. - #[getset(get = "pub")] - public_key: PublicKey, - /// Signature payload #[serde_as(as = "serde_with::hex::Hex")] - payload: ConstVec, + payload: ConstVec } } impl Signature { - /// Access the signature's payload - pub fn payload(&self) -> &[u8] { - self.payload.as_ref() - } - /// Creates new signature by signing payload via [`KeyPair::private_key`]. - pub fn new(key_pair: &KeyPair, payload: &[u8]) -> Self { + pub fn new(private_key: &PrivateKey, payload: &[u8]) -> Self { use crate::secrecy::ExposeSecret; - let signature = match key_pair.private_key.0.expose_secret() { + + let signature = match private_key.0.expose_secret() { crate::PrivateKeyInner::Ed25519(sk) => ed25519::Ed25519Sha512::sign(payload, sk), crate::PrivateKeyInner::Secp256k1(sk) => { secp256k1::EcdsaSecp256k1Sha256::sign(payload, sk) @@ -76,8 +59,8 @@ impl Signature { crate::PrivateKeyInner::BlsSmall(sk) => bls::BlsSmall::sign(payload, sk), crate::PrivateKeyInner::BlsNormal(sk) => bls::BlsNormal::sign(payload, sk), }; + Self { - public_key: key_pair.public_key.clone(), payload: ConstVec::new(signature), } } @@ -88,9 +71,8 @@ impl Signature { /// /// This method exists to allow reproducing the signature in a more efficient way than through /// deserialization. - pub fn from_bytes(public_key: PublicKey, payload: &[u8]) -> Self { + pub fn from_bytes(payload: &[u8]) -> Self { Self { - public_key, payload: ConstVec::new(payload), } } @@ -99,28 +81,28 @@ impl Signature { /// /// # Errors /// If passed string is not a valid hex. - pub fn from_hex(public_key: PublicKey, payload: impl AsRef) -> Result { + pub fn from_hex(payload: impl AsRef) -> Result { let payload: Vec = hex_decode(payload.as_ref())?; - Ok(Self::from_bytes(public_key, &payload)) + Ok(Self::from_bytes(&payload)) } /// Verify `payload` using signed data and [`KeyPair::public_key`]. /// /// # Errors /// Fails if the message doesn't pass verification - pub fn verify(&self, payload: &[u8]) -> Result<(), Error> { - match self.public_key.0.borrow() { + pub fn verify(&self, public_key: &PublicKey, payload: &[u8]) -> Result<(), Error> { + match public_key.0.borrow() { crate::PublicKeyInner::Ed25519(pk) => { - ed25519::Ed25519Sha512::verify(payload, self.payload(), pk) + ed25519::Ed25519Sha512::verify(payload, &self.payload, pk) } crate::PublicKeyInner::Secp256k1(pk) => { - secp256k1::EcdsaSecp256k1Sha256::verify(payload, self.payload(), pk) + secp256k1::EcdsaSecp256k1Sha256::verify(payload, &self.payload, pk) } crate::PublicKeyInner::BlsSmall(pk) => { - bls::BlsSmall::verify(payload, self.payload(), pk) + bls::BlsSmall::verify(payload, &self.payload, pk) } crate::PublicKeyInner::BlsNormal(pk) => { - bls::BlsNormal::verify(payload, self.payload(), pk) + bls::BlsNormal::verify(payload, &self.payload, pk) } }?; @@ -128,19 +110,6 @@ impl Signature { } } -// TODO: Enable in ffi_import -#[cfg(not(feature = "ffi_import"))] -impl From for (PublicKey, Vec) { - fn from( - Signature { - public_key, - payload: signature, - }: Signature, - ) -> Self { - (public_key, signature.into_vec()) - } -} - // TODO: Enable in ffi_import #[cfg(not(feature = "ffi_import"))] impl From> for Signature { @@ -234,8 +203,8 @@ impl SignatureOf { /// # Errors /// Fails if signing fails #[inline] - fn from_hash(key_pair: &KeyPair, hash: HashOf) -> Self { - Self(Signature::new(key_pair, hash.as_ref()), PhantomData) + fn from_hash(private_key: &PrivateKey, hash: HashOf) -> Self { + Self(Signature::new(private_key, hash.as_ref()), PhantomData) } /// Verify signature for this hash @@ -243,8 +212,8 @@ impl SignatureOf { /// # Errors /// /// Fails if the given hash didn't pass verification - fn verify_hash(&self, hash: HashOf) -> Result<(), Error> { - self.0.verify(hash.as_ref()) + fn verify_hash(&self, public_key: &PublicKey, hash: HashOf) -> Result<(), Error> { + self.0.verify(public_key, hash.as_ref()) } } @@ -256,269 +225,16 @@ impl SignatureOf { /// # Errors /// Fails if signing fails #[inline] - pub fn new(key_pair: &KeyPair, value: &T) -> Self { - Self::from_hash(key_pair, HashOf::new(value)) + pub fn new(private_key: &PrivateKey, value: &T) -> Self { + Self::from_hash(private_key, HashOf::new(value)) } /// Verifies signature for this item /// /// # Errors /// Fails if verification fails - pub fn verify(&self, value: &T) -> Result<(), Error> { - self.verify_hash(HashOf::new(value)) - } -} - -/// Wrapper around [`SignatureOf`] used to reimplement [`Eq`], [`Ord`], [`Hash`] -/// to compare signatures only by their [`PublicKey`]. -#[derive(Deref, DerefMut, Decode, Encode, Deserialize, Serialize, IntoSchema)] -#[serde(transparent, bound(deserialize = ""))] -#[schema(transparent)] -#[repr(transparent)] -#[cfg(not(feature = "ffi_import"))] -pub struct SignatureWrapperOf( - #[deref] - #[deref_mut] - SignatureOf, -); - -#[cfg(not(feature = "ffi_import"))] -impl SignatureWrapperOf { - #[inline] - fn inner(self) -> SignatureOf { - self.0 - } -} - -#[cfg(not(feature = "ffi_import"))] -impl core::fmt::Debug for SignatureWrapperOf { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - self.0.fmt(f) - } -} - -#[cfg(not(feature = "ffi_import"))] -impl Clone for SignatureWrapperOf { - fn clone(&self) -> Self { - Self(self.0.clone()) - } -} - -#[allow(clippy::unconditional_recursion)] // False-positive -#[cfg(not(feature = "ffi_import"))] -impl PartialEq for SignatureWrapperOf { - fn eq(&self, other: &Self) -> bool { - self.0.public_key().eq(other.0.public_key()) - } -} -#[cfg(not(feature = "ffi_import"))] -impl Eq for SignatureWrapperOf {} - -#[cfg(not(feature = "ffi_import"))] -impl PartialOrd for SignatureWrapperOf { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} -#[cfg(not(feature = "ffi_import"))] -impl Ord for SignatureWrapperOf { - fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.0.public_key().cmp(other.0.public_key()) - } -} - -#[cfg(not(feature = "ffi_import"))] -impl core::hash::Hash for SignatureWrapperOf { - // Implement `Hash` manually to be consistent with `Ord` - fn hash(&self, state: &mut H) { - self.0.public_key().hash(state); - } -} - -/// Container for multiple signatures, each corresponding to a different public key. -/// -/// If the public key of the added signature is already in the set, -/// the associated signature will be replaced with the new one. -/// -/// GUARANTEE 1: Each signature corresponds to a different public key -#[allow(clippy::derived_hash_with_manual_eq)] -#[derive(Hash, Decode, Encode, Deserialize, Serialize, IntoSchema)] -#[serde(transparent)] -// Transmute guard -#[repr(transparent)] -#[cfg(not(feature = "ffi_import"))] -pub struct SignaturesOf { - signatures: btree_set::BTreeSet>, -} - -#[cfg(not(feature = "ffi_import"))] -impl core::fmt::Debug for SignaturesOf { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct(core::any::type_name::()) - .field("signatures", &self.signatures) - .finish() - } -} - -#[cfg(not(feature = "ffi_import"))] -impl Clone for SignaturesOf { - fn clone(&self) -> Self { - let signatures = self.signatures.clone(); - Self { signatures } - } -} - -#[allow(clippy::unconditional_recursion)] // False-positive -#[cfg(not(feature = "ffi_import"))] -impl PartialEq for SignaturesOf { - fn eq(&self, other: &Self) -> bool { - self.signatures.eq(&other.signatures) - } -} - -#[cfg(not(feature = "ffi_import"))] -impl Eq for SignaturesOf {} - -#[cfg(not(feature = "ffi_import"))] -impl PartialOrd for SignaturesOf { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -#[cfg(not(feature = "ffi_import"))] -impl Ord for SignaturesOf { - fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.signatures.cmp(&other.signatures) - } -} - -#[cfg(not(feature = "ffi_import"))] -impl IntoIterator for SignaturesOf { - type Item = SignatureOf; - type IntoIter = core::iter::Map< - btree_set::IntoIter>, - fn(SignatureWrapperOf) -> SignatureOf, - >; - fn into_iter(self) -> Self::IntoIter { - self.signatures.into_iter().map(SignatureWrapperOf::inner) - } -} - -#[cfg(not(feature = "ffi_import"))] -impl<'itm, T> IntoIterator for &'itm SignaturesOf { - type Item = &'itm SignatureOf; - type IntoIter = core::iter::Map< - btree_set::Iter<'itm, SignatureWrapperOf>, - fn(&'itm SignatureWrapperOf) -> &'itm SignatureOf, - >; - fn into_iter(self) -> Self::IntoIter { - self.signatures.iter().map(core::ops::Deref::deref) - } -} - -#[cfg(not(feature = "ffi_import"))] -impl Extend> for SignaturesOf { - fn extend(&mut self, iter: T) - where - T: IntoIterator>, - { - for signature in iter { - self.insert(signature); - } - } -} - -#[cfg(not(feature = "ffi_import"))] -impl From> for btree_set::BTreeSet> { - fn from(source: SignaturesOf) -> Self { - source.into_iter().collect() - } -} - -#[cfg(not(feature = "ffi_import"))] -impl From>> for SignaturesOf { - fn from(source: btree_set::BTreeSet>) -> Self { - source.into_iter().collect() - } -} - -#[cfg(not(feature = "ffi_import"))] -impl From> for SignaturesOf { - fn from(signature: SignatureOf) -> Self { - Self { - signatures: [SignatureWrapperOf(signature)].into(), - } - } -} - -#[cfg(not(feature = "ffi_import"))] -impl FromIterator> for SignaturesOf { - fn from_iter>>(signatures: T) -> Self { - Self { - signatures: signatures.into_iter().map(SignatureWrapperOf).collect(), - } - } -} - -#[cfg(not(feature = "ffi_import"))] -impl SignaturesOf { - /// Adds a signature. If the signature with this key was present, replaces it. - pub fn insert(&mut self, signature: SignatureOf) { - self.signatures.insert(SignatureWrapperOf(signature)); - } - - /// Return all signatures. - #[inline] - pub fn iter(&self) -> impl ExactSizeIterator> { - self.into_iter() - } - - /// Number of signatures. - #[inline] - #[allow(clippy::len_without_is_empty)] - pub fn len(&self) -> usize { - self.signatures.len() - } - - /// Verify signatures for this hash - /// - /// # Errors - /// Fails if verificatoin of any signature fails - pub fn verify_hash(&self, hash: HashOf) -> Result<(), SignatureVerificationFail> { - self.iter().try_for_each(|signature| { - signature - .verify_hash(hash) - .map_err(|error| SignatureVerificationFail { - signature: Box::new(signature.clone()), - reason: error.to_string(), - }) - }) - } - - /// Returns true if the set is a subset of another, i.e., other contains at least all the elements in self. - pub fn is_subset(&self, other: &Self) -> bool { - self.signatures.is_subset(&other.signatures) - } -} - -#[cfg(not(feature = "ffi_import"))] -impl SignaturesOf { - /// Create new signatures container - /// - /// # Errors - /// Forwards [`SignatureOf::new`] errors - #[inline] - pub fn new(key_pair: &KeyPair, value: &T) -> Self { - SignatureOf::new(key_pair, value).into() - } - - /// Verifies all signatures - /// - /// # Errors - /// Fails if validation of any signature fails - pub fn verify(&self, item: &T) -> Result<(), SignatureVerificationFail> { - self.verify_hash(HashOf::new(item)) + pub fn verify(&self, public_key: &PublicKey, value: &T) -> Result<(), Error> { + self.verify_hash(public_key, HashOf::new(value)) } } @@ -544,12 +260,7 @@ impl core::fmt::Debug for SignatureVerificationFail { #[cfg(not(feature = "ffi_import"))] impl core::fmt::Display for SignatureVerificationFail { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!( - f, - "Failed to verify signatures because of signature {}: {}", - self.signature.public_key(), - self.reason, - ) + write!(f, "Failed to verify signatures: {}", self.reason,) } } @@ -559,21 +270,18 @@ impl std::error::Error for SignatureVerificationFail {} #[cfg(test)] mod tests { - use core::str::FromStr; - use serde_json::json; use super::*; - use crate::Algorithm; + use crate::{Algorithm, KeyPair}; #[test] #[cfg(feature = "rand")] fn create_signature_ed25519() { let key_pair = KeyPair::random_with_algorithm(crate::Algorithm::Ed25519); let message = b"Test message to sign."; - let signature = Signature::new(&key_pair, message); - assert_eq!(*signature.public_key(), *key_pair.public_key()); - signature.verify(message).unwrap(); + let signature = Signature::new(key_pair.private_key(), message); + signature.verify(key_pair.public_key(), message).unwrap(); } #[test] @@ -581,9 +289,8 @@ mod tests { fn create_signature_secp256k1() { let key_pair = KeyPair::random_with_algorithm(Algorithm::Secp256k1); let message = b"Test message to sign."; - let signature = Signature::new(&key_pair, message); - assert_eq!(*signature.public_key(), *key_pair.public_key()); - signature.verify(message).unwrap(); + let signature = Signature::new(key_pair.private_key(), message); + signature.verify(key_pair.public_key(), message).unwrap(); } #[test] @@ -591,9 +298,8 @@ mod tests { fn create_signature_bls_normal() { let key_pair = KeyPair::random_with_algorithm(Algorithm::BlsNormal); let message = b"Test message to sign."; - let signature = Signature::new(&key_pair, message); - assert_eq!(*signature.public_key(), *key_pair.public_key()); - signature.verify(message).unwrap(); + let signature = Signature::new(key_pair.private_key(), message); + signature.verify(key_pair.public_key(), message).unwrap(); } #[test] @@ -601,55 +307,8 @@ mod tests { fn create_signature_bls_small() { let key_pair = KeyPair::random_with_algorithm(Algorithm::BlsSmall); let message = b"Test message to sign."; - let signature = Signature::new(&key_pair, message); - assert_eq!(*signature.public_key(), *key_pair.public_key()); - signature.verify(message).unwrap(); - } - - #[test] - #[cfg(all(feature = "rand", not(feature = "ffi_import")))] - fn signatures_of_deduplication_by_public_key() { - let key_pair = KeyPair::random(); - let signatures = [ - SignatureOf::new(&key_pair, &1), - SignatureOf::new(&key_pair, &2), - SignatureOf::new(&key_pair, &3), - ] - .into_iter() - .collect::>(); - // Signatures with the same public key was deduplicated - assert_eq!(signatures.len(), 1); - } - - #[test] - #[cfg(not(feature = "ffi_import"))] - fn signature_wrapper_btree_and_hash_sets_consistent_results() { - use std::collections::{BTreeSet, HashSet}; - - let keys = 5; - let signatures_per_key = 10; - let signatures = core::iter::repeat_with(KeyPair::random) - .take(keys) - .flat_map(|key| { - core::iter::repeat_with(move || key.clone()) - .zip(0..) - .map(|(key, i)| SignatureOf::new(&key, &i)) - .take(signatures_per_key) - }) - .map(SignatureWrapperOf) - .collect::>(); - let hash_set: HashSet<_> = signatures.clone().into_iter().collect(); - let btree_set: BTreeSet<_> = signatures.into_iter().collect(); - - // Check that `hash_set` is subset of `btree_set` - for signature in &hash_set { - assert!(btree_set.contains(signature)); - } - // Check that `btree_set` is subset `hash_set` - for signature in &btree_set { - assert!(hash_set.contains(signature)); - } - // From the above we can conclude that `SignatureWrapperOf` have consistent behavior for `HashSet` and `BTreeSet` + let signature = Signature::new(key_pair.private_key(), message); + signature.verify(key_pair.public_key(), message).unwrap(); } #[test] @@ -666,12 +325,9 @@ mod tests { #[test] fn signature_from_hex_simply_reproduces_the_data() { - let public_key = "e701210312273E8810581E58948D3FB8F9E8AD53AAA21492EBB8703915BBB565A21B7FCC"; let payload = "3a7991af1abb77f3fd27cc148404a6ae4439d095a63591b77c788d53f708a02a1509a611ad6d97b01d871e58ed00c8fd7c3917b6ca61a8c2833a19e000aac2e4"; - let value = Signature::from_hex(PublicKey::from_str(public_key).unwrap(), payload).unwrap(); - - assert_eq!(value.public_key().to_string(), public_key); - assert_eq!(value.payload(), hex::decode(payload).unwrap()); + let value = Signature::from_hex(payload).unwrap(); + assert_eq!(value.payload.as_ref(), &hex::decode(payload).unwrap()); } } diff --git a/data_model/src/block.rs b/data_model/src/block.rs index b0f861757b9..f11a8dbf1b5 100644 --- a/data_model/src/block.rs +++ b/data_model/src/block.rs @@ -10,11 +10,10 @@ use core::{fmt::Display, time::Duration}; use derive_more::Display; #[cfg(all(feature = "std", feature = "transparent_api"))] -use iroha_crypto::KeyPair; -use iroha_crypto::{HashOf, MerkleTree, SignaturesOf}; +use iroha_crypto::PrivateKey; +use iroha_crypto::{HashOf, MerkleTree, SignatureOf}; use iroha_data_model_derive::model; use iroha_macro::FromVariant; -use iroha_primitives::unique_vec::UniqueVec; use iroha_schema::IntoSchema; use iroha_version::{declare_versioned, version_with_scale}; use parity_scale_codec::{Decode, Encode}; @@ -92,13 +91,29 @@ mod model { /// Block header pub header: BlockHeader, /// Topology of the network at the time of block commit. - pub commit_topology: UniqueVec, + pub commit_topology: Vec, /// array of transactions, which successfully passed validation and consensus step. pub transactions: Vec, /// Event recommendations. pub event_recommendations: Vec, } + /// Signature of a block + #[derive( + Debug, + Clone, + PartialEq, + Eq, + PartialOrd, + Ord, + Decode, + Encode, + Deserialize, + Serialize, + IntoSchema, + )] + pub struct BlockSignature(pub u64, pub SignatureOf); + /// Signed block #[version_with_scale(version = 1, versioned_alias = "SignedBlock")] #[derive( @@ -109,8 +124,9 @@ mod model { #[ffi_type] pub struct SignedBlockV1 { /// Signatures of peers which approved this block. - pub signatures: SignaturesOf, + pub signatures: Vec, /// Block payload + #[serde(flatten)] pub payload: BlockPayload, } } @@ -159,16 +175,16 @@ impl SignedBlock { /// Topology of the network at the time of block commit. #[inline] #[cfg(feature = "transparent_api")] - pub fn commit_topology(&self) -> &UniqueVec { + pub fn commit_topology(&self) -> impl ExactSizeIterator { let SignedBlock::V1(block) = self; - &block.payload.commit_topology + block.payload.commit_topology.iter() } /// Signatures of peers which approved this block. #[inline] - pub fn signatures(&self) -> &SignaturesOf { + pub fn signatures(&self) -> impl ExactSizeIterator { let SignedBlock::V1(block) = self; - &block.signatures + block.signatures.iter() } /// Calculate block hash @@ -189,10 +205,10 @@ impl SignedBlock { /// Add additional signatures to this block #[must_use] #[cfg(feature = "transparent_api")] - pub fn sign(mut self, key_pair: &KeyPair) -> Self { + pub fn sign(mut self, private_key: &PrivateKey, node_pos: u64) -> Self { let SignedBlock::V1(block) = &mut self; - let signature = iroha_crypto::SignatureOf::new(key_pair, &block.payload); - block.signatures.insert(signature); + let signature = SignatureOf::new(private_key, &block.payload); + block.signatures.push(BlockSignature(node_pos, signature)); self } @@ -204,30 +220,22 @@ impl SignedBlock { #[cfg(feature = "transparent_api")] pub fn add_signature( &mut self, - signature: iroha_crypto::SignatureOf, + signature: BlockSignature, ) -> Result<(), iroha_crypto::error::Error> { let SignedBlock::V1(block) = self; - signature.verify(&block.payload)?; - - let SignedBlock::V1(block) = self; - block.signatures.insert(signature); + block.signatures.push(signature); Ok(()) } /// Add additional signatures to this block #[cfg(feature = "transparent_api")] - pub fn replace_signatures( - &mut self, - signatures: iroha_crypto::SignaturesOf, - ) -> bool { + pub fn replace_signatures(&mut self, signatures: Vec) -> bool { #[cfg(not(feature = "std"))] use alloc::collections::BTreeSet; #[cfg(feature = "std")] - use std::collections::BTreeSet; - let SignedBlock::V1(block) = self; - block.signatures = BTreeSet::new().into(); + block.signatures = Vec::new(); for signature in signatures { if self.add_signature(signature).is_err() { @@ -246,13 +254,13 @@ mod candidate { #[derive(Decode, Deserialize)] struct SignedBlockCandidate { - signatures: SignaturesOf, + signatures: Vec, + #[serde(flatten)] payload: BlockPayload, } impl SignedBlockCandidate { fn validate(self) -> Result { - self.validate_signatures()?; self.validate_header()?; if self.payload.transactions.is_empty() { @@ -283,12 +291,6 @@ mod candidate { Ok(()) } - - fn validate_signatures(&self) -> Result<(), &'static str> { - self.signatures - .verify(&self.payload) - .map_err(|_| "Transaction contains invalid signatures") - } } impl Decode for SignedBlockV1 { diff --git a/data_model/src/query/mod.rs b/data_model/src/query/mod.rs index 9837f00905d..d5b99bead19 100644 --- a/data_model/src/query/mod.rs +++ b/data_model/src/query/mod.rs @@ -1184,12 +1184,28 @@ pub mod http { pub filter: PredicateBox, } + /// Signature of query + #[derive( + Debug, + Clone, + PartialEq, + Eq, + PartialOrd, + Ord, + Decode, + Encode, + Deserialize, + Serialize, + IntoSchema, + )] + pub struct QuerySignature(pub PublicKey, pub SignatureOf); + /// I/O ready structure to send queries. #[derive(Debug, Clone, Encode, Serialize, IntoSchema)] #[version_with_scale(version = 1, versioned_alias = "SignedQuery")] pub struct SignedQueryV1 { /// Signature of the client who sends this query. - pub signature: SignatureOf, + pub signature: QuerySignature, /// Payload pub payload: QueryPayload, } @@ -1225,13 +1241,15 @@ pub mod http { #[derive(Decode, Deserialize)] struct SignedQueryCandidate { - signature: SignatureOf, + signature: QuerySignature, payload: QueryPayload, } impl SignedQueryCandidate { fn validate(self) -> Result { - if self.signature.verify(&self.payload).is_err() { + let QuerySignature(public_key, signature) = &self.signature; + + if signature.verify(public_key, &self.payload).is_err() { return Err("Query signature not valid"); } @@ -1267,7 +1285,7 @@ pub mod http { #[cfg(feature = "transparent_api")] impl SignedQuery { /// Return query signature - pub fn signature(&self) -> &SignatureOf { + pub fn signature(&self) -> &QuerySignature { let SignedQuery::V1(query) = self; &query.signature } @@ -1314,8 +1332,10 @@ pub mod http { #[inline] #[must_use] pub fn sign(self, key_pair: &iroha_crypto::KeyPair) -> SignedQuery { + let signature = SignatureOf::new(key_pair.private_key(), &self.payload); + SignedQueryV1 { - signature: SignatureOf::new(key_pair, &self.payload), + signature: QuerySignature(key_pair.public_key().clone(), signature), payload: self.payload, } .into() diff --git a/data_model/src/transaction.rs b/data_model/src/transaction.rs index 0a541f70d04..41439df5833 100644 --- a/data_model/src/transaction.rs +++ b/data_model/src/transaction.rs @@ -9,7 +9,7 @@ use core::{ }; use derive_more::{DebugCustom, Display}; -use iroha_crypto::SignaturesOf; +use iroha_crypto::{KeyPair, PublicKey, SignatureOf}; use iroha_data_model_derive::model; use iroha_macro::FromVariant; use iroha_schema::IntoSchema; @@ -30,6 +30,7 @@ mod model { use getset::{CopyGetters, Getters}; use super::*; + use crate::account::AccountId; /// Either ISI or Wasm binary #[derive( @@ -140,6 +141,22 @@ mod model { pub max_wasm_size_bytes: u64, } + /// Signature of transaction + #[derive( + Debug, + Clone, + PartialEq, + Eq, + PartialOrd, + Ord, + Decode, + Encode, + Deserialize, + Serialize, + IntoSchema, + )] + pub struct TransactionSignature(pub PublicKey, pub SignatureOf); + /// Transaction that contains at least one signature /// /// `Iroha` and its clients use [`Self`] to send transactions over the network. @@ -155,8 +172,9 @@ mod model { #[ffi_type] pub struct SignedTransactionV1 { /// [`iroha_crypto::SignatureOf`]<[`TransactionPayload`]>. - pub(super) signatures: SignaturesOf, + pub(super) signatures: Vec, /// [`Transaction`] payload. + #[serde(flatten)] pub(super) payload: TransactionPayload, } @@ -292,9 +310,9 @@ impl SignedTransaction { /// Return transaction signatures #[inline] - pub fn signatures(&self) -> &SignaturesOf { + pub fn signatures(&self) -> impl ExactSizeIterator { let SignedTransaction::V1(tx) = self; - &tx.signatures + tx.signatures.iter() } /// Calculate transaction [`Hash`](`iroha_crypto::HashOf`). @@ -305,10 +323,13 @@ impl SignedTransaction { /// Sign transaction with provided key pair. #[must_use] - pub fn sign(self, key_pair: &iroha_crypto::KeyPair) -> SignedTransaction { + pub fn sign(self, key_pair: &KeyPair) -> SignedTransaction { let SignedTransaction::V1(mut tx) = self; - let signature = iroha_crypto::SignatureOf::new(key_pair, &tx.payload); - tx.signatures.insert(signature); + let signature = SignatureOf::new(key_pair.private_key(), &tx.payload); + tx.signatures.push(TransactionSignature( + key_pair.public_key().clone(), + signature, + )); SignedTransactionV1 { payload: tx.payload, @@ -346,33 +367,40 @@ mod candidate { #[derive(Decode, Deserialize)] struct SignedTransactionCandidate { - signatures: SignaturesOf, + signatures: Vec, + #[serde(flatten)] payload: TransactionPayload, } impl SignedTransactionCandidate { fn validate(self) -> Result { + self.validate_instructions()?; self.validate_signatures()?; - self.validate_instructions() + + Ok(SignedTransactionV1 { + signatures: self.signatures, + payload: self.payload, + }) } - fn validate_instructions(self) -> Result { + fn validate_instructions(&self) -> Result<(), &'static str> { if let Executable::Instructions(instructions) = &self.payload.instructions { if instructions.is_empty() { return Err("Transaction is empty"); } } - Ok(SignedTransactionV1 { - payload: self.payload, - signatures: self.signatures, - }) + Ok(()) } fn validate_signatures(&self) -> Result<(), &'static str> { - self.signatures - .verify(&self.payload) - .map_err(|_| "Transaction contains invalid signatures") + for TransactionSignature(public_key, signature) in &self.signatures { + signature + .verify(public_key, &self.payload) + .map_err(|_| "Transaction contains invalid signatures")?; + } + + Ok(()) } } @@ -740,8 +768,12 @@ mod http { /// Sign transaction with provided key pair. #[must_use] - pub fn sign(self, key_pair: &iroha_crypto::KeyPair) -> SignedTransaction { - let signatures = SignaturesOf::new(key_pair, &self.payload); + pub fn sign(self, key_pair: &KeyPair) -> SignedTransaction { + let signature = SignatureOf::new(key_pair.private_key(), &self.payload); + let signatures = vec![TransactionSignature( + key_pair.public_key().clone(), + signature, + )]; SignedTransactionV1 { payload: self.payload, diff --git a/docs/source/references/schema.json b/docs/source/references/schema.json index e9196b814df..b0bd13ab381 100644 --- a/docs/source/references/schema.json +++ b/docs/source/references/schema.json @@ -679,6 +679,12 @@ } ] }, + "BlockSignature": { + "Tuple": [ + "u64", + "SignatureOf" + ] + }, "BlockStatus": { "Enum": [ { @@ -3048,6 +3054,12 @@ } ] }, + "QuerySignature": { + "Tuple": [ + "PublicKey", + "SignatureOf" + ] + }, "Register": { "Struct": [ { @@ -3567,10 +3579,6 @@ }, "Signature": { "Struct": [ - { - "name": "public_key", - "type": "PublicKey" - }, { "name": "payload", "type": "Vec" @@ -3594,22 +3602,6 @@ "SignatureOf": "Signature", "SignatureOf": "Signature", "SignatureOf": "Signature", - "SignaturesOf": { - "Struct": [ - { - "name": "signatures", - "type": "SortedVec>" - } - ] - }, - "SignaturesOf": { - "Struct": [ - { - "name": "signatures", - "type": "SortedVec>" - } - ] - }, "SignedBlock": { "Enum": [ { @@ -3623,7 +3615,7 @@ "Struct": [ { "name": "signatures", - "type": "SignaturesOf" + "type": "Vec" }, { "name": "payload", @@ -3644,7 +3636,7 @@ "Struct": [ { "name": "signature", - "type": "SignatureOf" + "type": "QuerySignature" }, { "name": "payload", @@ -3665,7 +3657,7 @@ "Struct": [ { "name": "signatures", - "type": "SignaturesOf" + "type": "Vec" }, { "name": "payload", @@ -3776,12 +3768,6 @@ "SortedVec": { "Vec": "PublicKey" }, - "SortedVec>": { - "Vec": "SignatureOf" - }, - "SortedVec>": { - "Vec": "SignatureOf" - }, "String": "String", "StringPredicate": { "Enum": [ @@ -3957,6 +3943,12 @@ } ] }, + "TransactionSignature": { + "Tuple": [ + "PublicKey", + "SignatureOf" + ] + }, "TransactionStatus": { "Enum": [ { @@ -4408,6 +4400,9 @@ } ] }, + "Vec": { + "Vec": "BlockSignature" + }, "Vec": { "Vec": "EventBox" }, @@ -4435,6 +4430,9 @@ "Vec": { "Vec": "QueryOutputBox" }, + "Vec": { + "Vec": "TransactionSignature" + }, "Vec": { "Vec": "TransactionValue" }, diff --git a/genesis/Cargo.toml b/genesis/Cargo.toml index 75b8186cc7e..e30bd125b2b 100644 --- a/genesis/Cargo.toml +++ b/genesis/Cargo.toml @@ -13,7 +13,6 @@ workspace = true [dependencies] iroha_crypto = { workspace = true } iroha_data_model = { workspace = true, features = ["http"] } -iroha_schema = { workspace = true } derive_more = { workspace = true, features = ["deref"] } serde = { workspace = true, features = ["derive"] } diff --git a/p2p/src/peer.rs b/p2p/src/peer.rs index bfa8af93b80..801a688dbb6 100644 --- a/p2p/src/peer.rs +++ b/p2p/src/peer.rs @@ -258,7 +258,7 @@ mod run { } } }; - // Reset idle and ping timeout as peer received message from another peer + // Reset idle and ping timeout as peer received message from another peer idle_interval.reset(); ping_interval.reset(); } @@ -428,7 +428,7 @@ mod run { mod state { //! Module for peer stages. - use iroha_crypto::{KeyGenOption, KeyPair, Signature}; + use iroha_crypto::{KeyGenOption, KeyPair, PublicKey, Signature}; use iroha_primitives::addr::SocketAddr; use super::{cryptographer::Cryptographer, *}; @@ -572,8 +572,8 @@ mod state { let write_half = &mut connection.write; let payload = create_payload::(&kx_local_pk, &kx_remote_pk); - let signature = Signature::new(&key_pair, &payload); - let data = signature.encode(); + let signature = Signature::new(key_pair.private_key(), &payload); + let data = (key_pair.public_key(), signature).encode(); let data = &cryptographer.encrypt(data.as_slice())?; @@ -621,13 +621,12 @@ mod state { let data = cryptographer.decrypt(data.as_slice())?; - let signature: Signature = DecodeAll::decode_all(&mut data.as_slice())?; + let (remote_pub_key, signature): (PublicKey, Signature) = + DecodeAll::decode_all(&mut data.as_slice())?; // Swap order of keys since we are verifying for other peer order remote/local keys is reversed let payload = create_payload::(&kx_remote_pk, &kx_local_pk); - signature.verify(&payload)?; - - let (remote_pub_key, _) = signature.into(); + signature.verify(&remote_pub_key, &payload)?; let peer_id = PeerId::new(peer_addr, remote_pub_key); diff --git a/schema/gen/src/lib.rs b/schema/gen/src/lib.rs index e3c56afd90c..81274c989db 100644 --- a/schema/gen/src/lib.rs +++ b/schema/gen/src/lib.rs @@ -92,8 +92,6 @@ types!( BTreeMap, BTreeSet, BTreeSet, - BTreeSet>, - BTreeSet>, BatchedResponse, BatchedResponseV1, BlockEvent, @@ -321,10 +319,6 @@ types!( SignatureOf, SignatureOf, SignatureOf, - SignatureWrapperOf, - SignatureWrapperOf, - SignaturesOf, - SignaturesOf, SignedBlock, SignedBlockV1, SignedQuery,