This repository was archived by the owner on May 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Ledger workers #580
Merged
thibault-martinez
merged 36 commits into
iotaledger-archive:dev
from
thibault-martinez:ledger-workers
May 12, 2021
Merged
Ledger workers #580
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
c5b140f
Ledger workers
thibault-martinez 21225af
Fix compilation
thibault-martinez db2314f
Add missing docs
thibault-martinez 21f90b8
Fix clippy
thibault-martinez 9334c7a
Fix udeps
thibault-martinez 808e452
Fix comment
thibault-martinez 9d99230
Nits
thibault-martinez ac8eda0
Fix merkle hasher comment
thibault-martinez c9d60e4
Remove WhiteFlagMetadata::default
thibault-martinez 9e76690
Remove extraneous continue
thibault-martinez 96467bf
Remove some useless returns
thibault-martinez b8ebfa8
Add TODOs
thibault-martinez dcc8585
Change match into if..else
thibault-martinez 520c107
Rename variable
thibault-martinez c72d9e8
Improve WF traversal
thibault-martinez 1601816
Remove output clone
thibault-martinez abb0068
Collect instead of iter/insert
thibault-martinez 413ed2c
Remove early return
thibault-martinez 8ee6d47
Use async version of create_dir_all
thibault-martinez 0bcec68
Replace match with if/else
thibault-martinez 396734d
Use async versions of io::copy and File::create
thibault-martinez b1a25e4
Add BalanceDiffs::negated
thibault-martinez 0ad029f
Remove old message drop
thibault-martinez 5b71a68
Make clippy happy
thibault-martinez 1ff3bf7
Update changelog
thibault-martinez ddf88a4
Remove dashboard frontend
thibault-martinez 45a1415
Use PathBuf instead of String
thibault-martinez ed668c3
Remove early return
thibault-martinez dd6d74f
Remove fully qualified path
thibault-martinez 8af7f45
Slice matching
thibault-martinez 8fd183f
Remove some mutability/loops
thibault-martinez 7617d76
warn -> deny
thibault-martinez c4a4b8e
Address review comments
thibault-martinez 0a6d566
Update to bee-message 0.1.5
thibault-martinez 19aa9b1
Address all comments
thibault-martinez 8c95c45
Fix clippy and fmt
thibault-martinez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright 2020-2021 IOTA Stiftung | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use bee_message::MessageId; | ||
|
|
||
| use crypto::hashes::{Digest, Output}; | ||
|
|
||
| use std::marker::PhantomData; | ||
|
|
||
| /// Leaf domain separation prefix. | ||
| const LEAF_HASH_PREFIX: u8 = 0x00; | ||
| /// Node domain separation prefix. | ||
| const NODE_HASH_PREFIX: u8 = 0x01; | ||
|
|
||
| /// Computes the largest power of two less than or equal to `n`. | ||
| /// Undefined behaviour: 0 is not a valid value for `n`. | ||
| fn largest_power_of_two(n: u32) -> usize { | ||
| 1 << (32 - n.leading_zeros() - 1) | ||
| } | ||
|
|
||
| /// A Merkle hasher based on a digest function. | ||
| pub(crate) struct MerkleHasher<D> { | ||
| marker: PhantomData<D>, | ||
| } | ||
|
|
||
| impl<D: Default + Digest> MerkleHasher<D> { | ||
| /// Creates a new Merkle hasher. | ||
| pub(crate) fn new() -> Self { | ||
| Self { marker: PhantomData } | ||
| } | ||
|
|
||
| /// Returns the digest of the empty hash. | ||
| fn empty(&mut self) -> Output<D> { | ||
| D::digest(&[]) | ||
| } | ||
|
|
||
| /// Returns the digest of a Merkle leaf. | ||
| fn leaf(&mut self, message_id: MessageId) -> Output<D> { | ||
| let mut hasher = D::default(); | ||
|
|
||
| hasher.update([LEAF_HASH_PREFIX]); | ||
| hasher.update(message_id); | ||
| hasher.finalize() | ||
| } | ||
|
|
||
| /// Returns the digest of a Merkle node. | ||
| fn node(&mut self, message_ids: &[MessageId]) -> Output<D> { | ||
| let mut hasher = D::default(); | ||
| let (left, right) = message_ids.split_at(largest_power_of_two(message_ids.len() as u32 - 1)); | ||
|
|
||
| hasher.update([NODE_HASH_PREFIX]); | ||
| hasher.update(self.digest_inner(left)); | ||
| hasher.update(self.digest_inner(right)); | ||
| hasher.finalize() | ||
| } | ||
|
|
||
| /// Returns the digest of a list of hashes as an `Output<D>`. | ||
| fn digest_inner(&mut self, message_ids: &[MessageId]) -> Output<D> { | ||
| match message_ids { | ||
| [] => self.empty(), | ||
| [message_id] => self.leaf(*message_id), | ||
| _ => self.node(message_ids), | ||
| } | ||
| } | ||
|
|
||
| /// Returns the digest of a list of hashes as a `Vec<u8>`. | ||
| pub(crate) fn digest(&mut self, message_ids: &[MessageId]) -> Vec<u8> { | ||
| self.digest_inner(message_ids).to_vec() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
|
||
| use super::*; | ||
|
|
||
| use crypto::hashes::blake2b::Blake2b256; | ||
|
|
||
| use std::str::FromStr; | ||
|
|
||
| #[test] | ||
| fn tree() { | ||
| let hashes = [ | ||
| "52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649", | ||
| "81855ad8681d0d86d1e91e00167939cb6694d2c422acd208a0072939487f6999", | ||
| "eb9d18a44784045d87f3c67cf22746e995af5a25367951baa2ff6cd471c483f1", | ||
| "5fb90badb37c5821b6d95526a41a9504680b4e7c8b763a1b1d49d4955c848621", | ||
| "6325253fec738dd7a9e28bf921119c160f0702448615bbda08313f6a8eb668d2", | ||
| "0bf5059875921e668a5bdf2c7fc4844592d2572bcd0668d2d6c52f5054e2d083", | ||
| "6bf84c7174cb7476364cc3dbd968b0f7172ed85794bb358b0c3b525da1786f9f", | ||
| ] | ||
| .iter() | ||
| .map(|hash| MessageId::from_str(hash).unwrap()) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let hash = MerkleHasher::<Blake2b256>::new().digest(&hashes); | ||
|
|
||
| assert_eq!( | ||
| hex::encode(hash), | ||
| "bf67ce7ba23e8c0951b5abaec4f5524360d2c26d971ff226d3359fa70cdb0beb" | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.