Patika.dev Community Gaming Solana & Rust Practicum
Hands on Task: Merkle Tree Root Calculator on Rust
Firstly, we should understand what is Merkle Tree and how it works well. In order to do this, please research about merkle tree and watch the video carefully.
Our main purpose is that take inputs from txt files, hash them and calculate the root. Example txt file looks like:
3
patika
izmir
denizli
adana
mersin
deniz
gemi
ucak
The first line is n which is
Instructions
- Fork or clone this repository
- Add dependencies to Cargo.toml which you would like to use
- Read data from txt file and use it as is required so, store the n as u32 and store splitted String inputs to a Vector of Strings
- Hash all inputs with SHA256 from sha-2 crate. It should be hex encoded then for the sake of this assignment it can be hashed like this:
fn hash_single_input(a: &str) -> String {
let mut hasher = Sha256::new();
let input = a;
hasher.update(input);
let hash = hasher.finalize();
let hex = hex::encode(&hash);
return hex.to_string();
}
Note: But also writing macro rule for hashing would be great!
Also you can check your output from here
Moreover, if you want to hash two hash it you can concat two strings and hash it with the function above.
And also you can check your output from here adding inputs to another without any spaces. For example 'izmir' and 'denizli':
izmirdenizli
- Then, from naked inputs you are expected to calculate root of merkle tree. Return the root as a String output.
- You can use helper function templates or just remove them.
- You can run tests like below:
cargo run test
- Pass all tests :)