Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions code/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ mod week_2;
// entry point to run all topics covered
fn main() {
// call functions from session_3 modules
println!("Task 1:");
week_2::task_1::main();

println!("\nTask 2:");
week_2::task_2::main();

println!("\nTask 3:");
week_2::task_3::main();

println!("\nOther topics:");
week_2::function::run();
week_2::loops::run();
week_2::primitive_types::run();
Expand Down
6 changes: 5 additions & 1 deletion code/src/week_2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ pub mod function;
pub mod loops;
pub mod primitive_types;
pub mod slices;
pub mod variables;
pub mod variables;

pub mod task_1;
pub mod task_2;
pub mod task_3;
10 changes: 9 additions & 1 deletion code/src/week_2/task_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
fn btc_value_in_usd(btc: f64, rate: f64) -> f64 {
btc * rate
}

fn mine_blocks(limit: u8) {
for height in 1..=limit {
println!("Mining block #{}", height);
}
}

//main run main
fn main() {
pub fn main() {
let btc = 0.25; //a dream amount
let rate = 65_000.0; // example rate here
let value = btc_value_in_usd(btc, rate);
println!("{} BTC is worth ${} USD", btc, value);
mine_blocks(10);
}
18 changes: 13 additions & 5 deletions code/src/week_2/task_2.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
use core::time;

fn mine_blocks(limit: u8) {
let mut difficulty = 1;

for height in 1..=limit {
println!("Mining block #{}", height);

// Simulate difficulty
while difficulty < 3 {
let time_to_mine = difficulty * 1000;

while difficulty < 5 {
println!("Simulating difficulty level {}", difficulty);
difficulty += 1; // the number of starting zero's
// Simulate mining time
std::thread::sleep(std::time::Duration::from_millis(time_to_mine));
break;
}

if height % 5 == 0 {
println!("Checkpoint reached");
}

difficulty += 1;

}
}

fn main() {
mine_blocks(10);
pub fn main() {
mine_blocks(21);
}
26 changes: 8 additions & 18 deletions code/src/week_2/task_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,15 @@ fn get_rpc_url(network: &Network) -> &str {
}
}

fn print_network_details(network: &Network) {
match network {
Network::Mainnet => println!("This is the main Bitcoin network."),
Network::Testnet => println!("This is the test Bitcoin network."),
Network::Regtest => println!("This is the regtest Bitcoin network."),
}
}

fn main() {
let network = Network::Regtest;
pub fn main() {

print_network_details(&network);
println!("RPC URL: {}", get_rpc_url(&network));
let network = Network::Mainnet;
let mut network = Network::Regtest;
println!("Net: {}", get_rpc_url(&network));

print_network_details(&network);
println!("RPC URL: {}", get_rpc_url(&network));
let network = Network::Testnet;
network = Network::Mainnet;
println!("Net: {}", get_rpc_url(&network));

print_network_details(&network);
println!("RPC URL: {}", get_rpc_url(&network));
network = Network::Testnet;
println!("Net: {}", get_rpc_url(&network));

}