Skip to content

Commit 8eccb4d

Browse files
committed
Implement basic PoW module
1 parent 294c861 commit 8eccb4d

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

Cargo.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
arrayref = "0.3.7"
910
async-std = { version = "1.12.0", features = ["attributes"] }
1011
async-trait = "0.1.68"
12+
bytes = "1.4.0"
1113
futures = "0.3.28"
14+
hmac-sha512 = "1.1.4"
1215
libp2p = { version = "0.51.3", features = ["async-std", "dns", "macros", "noise", "ping", "tcp", "websocket", "yamux"] }
16+
log = "0.4.17"
1317
multiaddr = "0.17.1"

src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::error::Error;
22

3+
mod pow;
4+
35
use futures::StreamExt;
46
use libp2p::core::upgrade::Version;
57
use libp2p::swarm::keep_alive::Behaviour;

src/pow.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use bytes::Bytes;
2+
use hmac_sha512::Hash;
3+
use log::info;
4+
5+
// Function to do PoW for sending messages into network
6+
fn do_pow(target: u64, initial_hash: Bytes) -> (u64, u64) {
7+
info!("PoW has started");
8+
9+
let mut nonce: u64 = 0;
10+
let mut trial_value = u64::MAX;
11+
while trial_value > target {
12+
nonce += 1;
13+
trial_value = u64::from_be_bytes(
14+
Hash::hash(Hash::hash(
15+
[&nonce.to_be_bytes()[..], &initial_hash[..]].concat(),
16+
))[0..8]
17+
.try_into()
18+
.unwrap(),
19+
);
20+
}
21+
22+
info!("PoW has ended");
23+
return (trial_value, nonce);
24+
}

0 commit comments

Comments
 (0)