Skip to content

Commit

Permalink
[feature] #1782: make iroha_crypto no_std compatible
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <marin.versic101@gmail.com>
  • Loading branch information
mversic authored Jan 11, 2022
1 parent 4975ef3 commit 84d01ac
Show file tree
Hide file tree
Showing 15 changed files with 655 additions and 461 deletions.
12 changes: 3 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl VersionedValidBlock {
wsv: &WorldStateView<W>,
is_instruction_allowed: &IsInstructionAllowedBoxed<W>,
is_query_allowed: &IsQueryAllowedBoxed<W>,
) -> VersionedValidBlock {
) -> Self {
self.into_v1()
.revalidate(wsv, is_instruction_allowed, is_query_allowed)
.into()
Expand All @@ -392,14 +392,12 @@ impl VersionedValidBlock {
/// Sign this block and get [`VersionedValidBlock`](`Self`).
/// # Errors
/// Look at [`ValidBlock`](`ValidBlock`) for more info
pub fn sign(self, key_pair: KeyPair) -> Result<VersionedValidBlock> {
pub fn sign(self, key_pair: KeyPair) -> Result<Self> {
self.into_v1().sign(key_pair).map(Into::into)
}

/// Signatures that are verified with the `hash` of this block as `payload`.
pub fn verified_signatures(
&'_ self,
) -> impl Iterator<Item = &'_ SignatureOf<VersionedValidBlock>> + '_ {
pub fn verified_signatures(&self) -> impl Iterator<Item = &SignatureOf<Self>> {
self.as_v1()
.verified_signatures()
.map(SignatureOf::transmute_ref)
Expand Down Expand Up @@ -473,13 +471,17 @@ impl ValidBlock {
/// Commit block to the store.
//TODO: pass block store and block sender as parameters?
pub fn commit(self) -> CommittedBlock {
#[allow(clippy::expect_used)]
let signatures: SignaturesOf<_> = self
.signatures
.try_into()
.expect("Expected at least one signature");

CommittedBlock {
header: self.header,
rejected_transactions: self.rejected_transactions,
transactions: self.transactions,
signatures: SignaturesOf::from_iter_unchecked(
self.signatures.into_iter().map(SignatureOf::transmute),
),
signatures: signatures.transmute(),
}
}

Expand All @@ -489,8 +491,8 @@ impl ValidBlock {
wsv: &WorldStateView<W>,
is_instruction_allowed: &IsInstructionAllowedBoxed<W>,
is_query_allowed: &IsQueryAllowedBoxed<W>,
) -> ValidBlock {
ValidBlock {
) -> Self {
Self {
signatures: self.signatures,
..ChainedBlock {
header: self.header,
Expand All @@ -515,15 +517,15 @@ impl ValidBlock {
///
/// # Errors
/// Fails if generating signature fails
pub fn sign(mut self, key_pair: KeyPair) -> Result<ValidBlock> {
pub fn sign(mut self, key_pair: KeyPair) -> Result<Self> {
self.signatures.insert(
SignatureOf::from_hash(key_pair, &self.hash()).wrap_err("Failed to sign block")?,
);
Ok(self)
}

/// Signatures that are verified with the `hash` of this block as `payload`.
pub fn verified_signatures(&'_ self) -> impl Iterator<Item = &'_ SignatureOf<ValidBlock>> + '_ {
pub fn verified_signatures(&self) -> impl Iterator<Item = &SignatureOf<Self>> {
let hash = self.hash();
self.signatures
.iter()
Expand Down Expand Up @@ -553,7 +555,7 @@ impl ValidBlock {
#[cfg(test)]
#[allow(clippy::restriction)]
pub fn new_dummy() -> Self {
ValidBlock {
Self {
header: BlockHeader {
timestamp: 0,
height: 1,
Expand Down Expand Up @@ -655,7 +657,7 @@ impl VersionedCommittedBlock {
}

/// Signatures that are verified with the `hash` of this block as `payload`.
pub fn verified_signatures(&'_ self) -> impl Iterator<Item = &'_ SignatureOf<Self>> + '_ {
pub fn verified_signatures(&self) -> impl Iterator<Item = &SignatureOf<Self>> {
self.as_v1()
.verified_signatures()
.map(SignatureOf::transmute_ref)
Expand Down Expand Up @@ -685,7 +687,7 @@ impl CommittedBlock {
}

/// Signatures that are verified with the `hash` of this block as `payload`.
pub fn verified_signatures(&'_ self) -> impl Iterator<Item = &'_ SignatureOf<Self>> + '_ {
pub fn verified_signatures(&self) -> impl Iterator<Item = &SignatureOf<Self>> {
self.signatures.verified_by_hash(self.hash())
}
}
Expand All @@ -699,12 +701,11 @@ impl From<CommittedBlock> for ValidBlock {
signatures,
}: CommittedBlock,
) -> Self {
let signatures = signatures.into_iter().map(SignatureOf::transmute).collect();
Self {
header,
rejected_transactions,
transactions,
signatures,
signatures: signatures.transmute().into(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Queue {
.get_mut()
.as_mut_v1()
.signatures
.merge(tx.into_v1().signatures);
.extend(tx.as_v1().signatures.clone());
return Ok(());
}
Entry::Vacant(entry) => entry,
Expand Down
29 changes: 17 additions & 12 deletions core/src/sumeragi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,8 @@ impl<G: GenesisNetworkTrait, K: KuraTrait, W: WorldTrait, F: FaultInjection>
Entry::Occupied(mut occupied) => {
let proof_votes = occupied.get_mut();
let count = proof_votes.signatures().len();
proof_votes.merge_signatures(&proof);
proof_votes.merge_signatures(proof.signatures().clone());

if proof_votes.signatures().len() > count {
(true, proof_votes.clone())
} else {
Expand All @@ -794,7 +795,7 @@ impl<G: GenesisNetworkTrait, K: KuraTrait, W: WorldTrait, F: FaultInjection>
let (count_increased, merged_proof) = self.merge_view_change_votes(proof.clone()).await;
if count_increased {
self.broadcast_msg(ViewChangeSuggested::new(
proof.clone(),
proof,
self.view_change_proofs().clone(),
))
.await;
Expand Down Expand Up @@ -857,17 +858,20 @@ impl<G: GenesisNetworkTrait, K: KuraTrait, W: WorldTrait, F: FaultInjection>
trace!(?event);
drop(self.events_sender.send(event));
}
let signed_block = block.sign(self.key_pair.clone())?;
if !network_topology.is_consensus_required() {
self.commit_block(block).await;
self.commit_block(signed_block).await;
return Ok(());
}

let voting_block = VotingBlock::new(block.clone());
self.voting_block = Some(voting_block.clone());
self.broadcast_msg(BlockCreated::from(block.sign(self.key_pair.clone())?))
let voting_block = VotingBlock::new(signed_block.clone());
let voting_block_hash = voting_block.block.hash();

self.voting_block = Some(voting_block);
self.broadcast_msg(BlockCreated::from(signed_block.clone()))
.await;
self.start_commit_countdown(
voting_block.clone(),
voting_block_hash,
*self.latest_block_hash(),
self.latest_view_change_hash(),
ctx,
Expand All @@ -878,16 +882,15 @@ impl<G: GenesisNetworkTrait, K: KuraTrait, W: WorldTrait, F: FaultInjection>

/// Starts countdown for a period in which the `voting_block` should be committed.
#[iroha_futures::telemetry_future]
#[log(skip(self, voting_block))]
#[log(skip(self, voting_block_hash))]
#[allow(clippy::expect_used)]
pub async fn start_commit_countdown(
&self,
voting_block: VotingBlock,
voting_block_hash: HashOf<VersionedValidBlock>,
latest_block: HashOf<VersionedCommittedBlock>,
latest_view_change: HashOf<Proof>,
ctx: &mut Context<Self>,
) {
let voting_block_hash = voting_block.block.hash();
let proof = view_change::Proof::commit_timeout(
voting_block_hash,
latest_view_change,
Expand Down Expand Up @@ -1387,10 +1390,12 @@ pub mod message {
//TODO: send to set b so they can observe
}
let voting_block = VotingBlock::new(self.block.clone());
sumeragi.voting_block = Some(voting_block.clone());
let voting_block_hash = voting_block.block.hash();
sumeragi.voting_block = Some(voting_block);

sumeragi
.start_commit_countdown(
voting_block,
voting_block_hash,
*sumeragi.latest_block_hash(),
sumeragi.latest_view_change_hash(),
ctx,
Expand Down
9 changes: 4 additions & 5 deletions core/src/sumeragi/view_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,14 @@ impl Proof {
/// Can fail during creation of signature
pub fn sign(mut self, key_pair: KeyPair) -> Result<Proof> {
let signature = SignatureOf::new(key_pair, &self.payload)?.transmute();
self.signatures.add(signature);
self.signatures.insert(signature);
Ok(self)
}

/// Adds verified signatures of `other` to self.
pub fn merge_signatures(&mut self, other: &Proof) {
self.signatures.merge(SignaturesOf::from_iter_unchecked(
other.signatures.verified_by_hash(self.hash()).cloned(),
));
pub fn merge_signatures(&mut self, other: SignaturesOf<Proof>) {
self.signatures
.extend(other.into_verified_by_hash(self.hash()))
}

/// Verify if the proof is valid, given the peers in `topology`.
Expand Down
7 changes: 6 additions & 1 deletion core/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ impl AcceptedTransaction {
transaction
.check_instruction_len(max_instruction_number)
.wrap_err("Failed to accept transaction")?;
let signatures = SignaturesOf::from_iter(&transaction.payload, transaction.signatures)
let signatures: SignaturesOf<_> = transaction
.signatures
.try_into()
.map_err(eyre::Error::from)?;
signatures
.verify(&transaction.payload)
.wrap_err("Failed to verify transaction signatures")?;

Ok(Self {
Expand Down
18 changes: 10 additions & 8 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
iroha_schema = { path = "../schema"}
[features]
default = ["std"]
std = ["ursa"]

derive_more = "0.99.16"
ursa = "=0.3.7"
eyre = "0.6.5"
hex = "0.4.0"
[dependencies]
iroha_schema = { path = "../schema" }

parity-scale-codec = { version = "2.3.1", features = ["derive"] }
serde = { version = "1.0.130", features = ["derive"] }
derive_more = { version = "0.99.16", default-features = false, features = ["deref", "deref_mut", "display"] }
parity-scale-codec = { version = "2.3.1" , default-features = false, features = ["derive", "full"] }
serde = { version = "1.0.130", default-features = false, features = ["derive"] }
hex = { version = "0.4.0", default-features = false, features = ["alloc", "serde"] }
ursa = { version = "=0.3.7", optional = true }

[dev-dependencies]
hex-literal = "0.3.4"
Expand Down
Loading

0 comments on commit 84d01ac

Please sign in to comment.