Skip to content

Commit 8b0662c

Browse files
Merge pull request #61 from AploCoin/rewrite
Rewrite
2 parents 2994bed + df1a6b4 commit 8b0662c

23 files changed

+2688
-3369
lines changed

.github/workflows/rust.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Rust
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches: [ "main", "dev", "rewrite" ]
66
pull_request:
7-
branches: [ "main", "dev" ]
7+
branches: [ "main", "dev", "rewrite" ]
88

99
env:
1010
CARGO_TERM_COLOR: always
@@ -21,4 +21,4 @@ jobs:
2121
- name: Check format code
2222
run: cargo fmt -- --check
2323
- name: Clippy
24-
run: cargo clippy -- -D warnings
24+
run: cargo clippy -- -D warnings

Cargo.toml

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
base64 = "0.13"
10-
byteorder = "1.2.7"
11-
colored = ">=2"
12-
env_logger = "0.9.0"
13-
error-stack = "0.3.1"
9+
base64 = "0.22.1"
10+
byteorder = "1.5.0"
11+
colored = "2.1.0"
12+
env_logger = "0.11.3"
13+
error-stack = "0.4.1"
1414
hex = "0.4.3"
1515
lazy_static = "1.4.0"
16-
log = "0.4.17"
17-
num-bigint = "0.4"
18-
num-traits = "0.2"
19-
rsa = "0.5"
20-
secp256k1 = { version = "0.22.1", features = ["rand-std","bitcoin_hashes"] }
21-
sha2 = "0.9.5"
16+
log = "0.4.21"
17+
num-bigint = "0.4.4"
18+
num-traits = "0.2.19"
19+
rsa = "0.9.6"
20+
secp256k1 = { version = "0.29.0", features = ["rand-std"] }
21+
sha2 = "0.10.8"
2222
sled = "0.34.7"
23-
thiserror = "1.0"
24-
tokio = { version = "1", features = ["full"] }
25-
zstd = "0.9"
23+
thiserror = "1.0.59"
24+
tokio = { version = "1.37.0", features = ["full"] }
25+
zstd = "0.13.1"
26+
primitive-types = "0.12.2"
27+
async-trait = "0.1.80"
28+
parking_lot = "0.12.2"
2629

2730
[dev-dependencies]
2831
rand = "0.8.5"
2932

30-
[profile.test]
33+
[profile.test]
3134
opt-level = 3

examples/mine.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use blockchaintree::static_values::BLOCKS_PER_EPOCH;
2+
use blockchaintree::tools;
3+
use blockchaintree::{blockchaintree::BlockChainTree, static_values};
4+
use primitive_types::U256;
5+
use std::time::{SystemTime, UNIX_EPOCH};
6+
7+
fn main() {
8+
let rt = tokio::runtime::Runtime::new().unwrap();
9+
10+
let mut tree = BlockChainTree::new().unwrap();
11+
12+
let main_chain = tree.get_main_chain();
13+
14+
let wallet = [1u8; 33];
15+
16+
loop {
17+
println!("Current height: {}", main_chain.get_height());
18+
println!(
19+
"Current miner balance: {}",
20+
tree.get_amount(&wallet).unwrap()
21+
);
22+
println!(
23+
"Current root balance: {}",
24+
tree.get_amount(&static_values::ROOT_PUBLIC_ADDRESS)
25+
.unwrap()
26+
);
27+
let mut nonce = U256::zero();
28+
let last_block = main_chain.get_last_block().unwrap().unwrap();
29+
let prev_hash = last_block.hash().unwrap();
30+
let difficulty = last_block.get_info().difficulty;
31+
println!(
32+
"Current difficulty: {}",
33+
tools::count_leading_zeros(&difficulty)
34+
);
35+
while nonce < U256::MAX {
36+
let mut pow = [0u8; 32];
37+
nonce.to_big_endian(&mut pow);
38+
if tools::check_pow(&prev_hash, &difficulty, &pow) {
39+
let timestamp = SystemTime::now()
40+
.duration_since(UNIX_EPOCH)
41+
.unwrap()
42+
.as_secs();
43+
44+
println!("Found nonce! {}", nonce);
45+
46+
let transactions: &[[u8; 32]] =
47+
if ((last_block.get_info().height + 1) % BLOCKS_PER_EPOCH).is_zero() {
48+
println!("Cycle ended!");
49+
&[]
50+
} else {
51+
&[[25u8; 32]]
52+
};
53+
54+
let block = rt
55+
.block_on(tree.emmit_new_main_block(&pow, &wallet, transactions, timestamp))
56+
.unwrap();
57+
58+
tree.send_amount(
59+
&static_values::ROOT_PUBLIC_ADDRESS,
60+
&wallet,
61+
*static_values::MAIN_CHAIN_PAYMENT,
62+
)
63+
.unwrap();
64+
65+
println!("Added new block! {:?}\n", block.hash().unwrap());
66+
67+
rt.block_on(tree.flush()).unwrap();
68+
break;
69+
}
70+
nonce += U256::one();
71+
}
72+
}
73+
}

examples/mine_derivative.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use blockchaintree::block::Block as _;
2+
use blockchaintree::tools;
3+
use blockchaintree::{blockchaintree::BlockChainTree, static_values};
4+
use primitive_types::U256;
5+
use std::time::{SystemTime, UNIX_EPOCH};
6+
7+
fn main() {
8+
let rt = tokio::runtime::Runtime::new().unwrap();
9+
10+
let mut tree = BlockChainTree::new().unwrap();
11+
12+
let wallet = [1u8; 33];
13+
14+
let chain = tree.get_derivative_chain(&wallet).unwrap();
15+
16+
loop {
17+
println!("Current height: {}", chain.get_height());
18+
println!(
19+
"Current miner gas amount: {}",
20+
tree.get_gas(&wallet).unwrap()
21+
);
22+
let mut nonce = U256::zero();
23+
let (prev_hash, difficulty, _prev_timestamp, _height) =
24+
if let Some(block) = chain.get_last_block().unwrap() {
25+
(
26+
block.hash().unwrap(),
27+
block.get_info().difficulty,
28+
block.get_info().timestamp,
29+
block.get_info().height,
30+
)
31+
} else {
32+
let block = tree
33+
.get_main_chain()
34+
.find_by_hash(&chain.genesis_hash)
35+
.unwrap()
36+
.unwrap();
37+
(
38+
block.hash().unwrap(),
39+
static_values::BEGINNING_DIFFICULTY,
40+
block.get_info().timestamp,
41+
U256::zero(),
42+
)
43+
};
44+
println!(
45+
"Current difficulty: {}",
46+
tools::count_leading_zeros(&difficulty)
47+
);
48+
while nonce < U256::MAX {
49+
let mut pow = [0u8; 32];
50+
nonce.to_big_endian(&mut pow);
51+
if tools::check_pow(&prev_hash, &difficulty, &pow) {
52+
let timestamp = SystemTime::now()
53+
.duration_since(UNIX_EPOCH)
54+
.unwrap()
55+
.as_secs();
56+
57+
println!("Found nonce! {}", nonce);
58+
59+
let block = rt
60+
.block_on(tree.emmit_new_derivative_block(&pow, &wallet, timestamp))
61+
.unwrap();
62+
63+
tree.add_gas(&wallet, *static_values::MAIN_CHAIN_PAYMENT)
64+
.unwrap();
65+
66+
println!("Added new block! {:?}\n", block.hash().unwrap());
67+
68+
rt.block_on(chain.flush()).unwrap();
69+
break;
70+
}
71+
nonce += U256::one();
72+
}
73+
}
74+
}

gen_test_data.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import hashlib
2+
import os
3+
4+
5+
def leading_zeros(num):
6+
if num == 0:
7+
return 8
8+
9+
leading_zeros = 0
10+
while num & 0b10000000 == 0:
11+
leading_zeros += 1
12+
num = num << 1
13+
num = num & 0b11111111
14+
return leading_zeros
15+
16+
17+
def total_leading_zeros(hash):
18+
to_return = 0
19+
for byte in hash:
20+
l_zeros = leading_zeros(byte)
21+
to_return += l_zeros
22+
if l_zeros < 8:
23+
break
24+
25+
return to_return
26+
27+
def to_hex_list(bt: bytes) -> list:
28+
r = []
29+
for b in bt:
30+
r.append(f'0x{hex(b)[2:].upper()}')
31+
return r
32+
33+
def gen(hash, difficulty):
34+
difficulty = total_leading_zeros(difficulty)
35+
for i in range(1000):
36+
pow = b'' + os.urandom(10)
37+
hasher = hashlib.sha256()
38+
hasher.update(hash)
39+
hasher.update(pow)
40+
41+
generated_hash = hasher.digest()
42+
ghash_leadin_zeros = total_leading_zeros(generated_hash)
43+
44+
if ghash_leadin_zeros >= difficulty:
45+
print(', '.join(to_hex_list(pow)), True)
46+
else:
47+
print(', '.join(to_hex_list(pow)), False)
48+
49+
50+
gen(hashlib.sha256(b'text').digest(),
51+
b'\x0F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF')

0 commit comments

Comments
 (0)