Skip to content

Commit

Permalink
Merge pull request #61 from AploCoin/rewrite
Browse files Browse the repository at this point in the history
Rewrite
  • Loading branch information
YeahNotSewerSide authored May 4, 2024
2 parents 2994bed + df1a6b4 commit 8b0662c
Show file tree
Hide file tree
Showing 23 changed files with 2,688 additions and 3,369 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Rust

on:
push:
branches: [ "main" ]
branches: [ "main", "dev", "rewrite" ]
pull_request:
branches: [ "main", "dev" ]
branches: [ "main", "dev", "rewrite" ]

env:
CARGO_TERM_COLOR: always
Expand All @@ -21,4 +21,4 @@ jobs:
- name: Check format code
run: cargo fmt -- --check
- name: Clippy
run: cargo clippy -- -D warnings
run: cargo clippy -- -D warnings
33 changes: 18 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
base64 = "0.13"
byteorder = "1.2.7"
colored = ">=2"
env_logger = "0.9.0"
error-stack = "0.3.1"
base64 = "0.22.1"
byteorder = "1.5.0"
colored = "2.1.0"
env_logger = "0.11.3"
error-stack = "0.4.1"
hex = "0.4.3"
lazy_static = "1.4.0"
log = "0.4.17"
num-bigint = "0.4"
num-traits = "0.2"
rsa = "0.5"
secp256k1 = { version = "0.22.1", features = ["rand-std","bitcoin_hashes"] }
sha2 = "0.9.5"
log = "0.4.21"
num-bigint = "0.4.4"
num-traits = "0.2.19"
rsa = "0.9.6"
secp256k1 = { version = "0.29.0", features = ["rand-std"] }
sha2 = "0.10.8"
sled = "0.34.7"
thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
zstd = "0.9"
thiserror = "1.0.59"
tokio = { version = "1.37.0", features = ["full"] }
zstd = "0.13.1"
primitive-types = "0.12.2"
async-trait = "0.1.80"
parking_lot = "0.12.2"

[dev-dependencies]
rand = "0.8.5"

[profile.test]
[profile.test]
opt-level = 3
73 changes: 73 additions & 0 deletions examples/mine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use blockchaintree::static_values::BLOCKS_PER_EPOCH;
use blockchaintree::tools;
use blockchaintree::{blockchaintree::BlockChainTree, static_values};
use primitive_types::U256;
use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
let rt = tokio::runtime::Runtime::new().unwrap();

let mut tree = BlockChainTree::new().unwrap();

let main_chain = tree.get_main_chain();

let wallet = [1u8; 33];

loop {
println!("Current height: {}", main_chain.get_height());
println!(
"Current miner balance: {}",
tree.get_amount(&wallet).unwrap()
);
println!(
"Current root balance: {}",
tree.get_amount(&static_values::ROOT_PUBLIC_ADDRESS)
.unwrap()
);
let mut nonce = U256::zero();
let last_block = main_chain.get_last_block().unwrap().unwrap();
let prev_hash = last_block.hash().unwrap();
let difficulty = last_block.get_info().difficulty;
println!(
"Current difficulty: {}",
tools::count_leading_zeros(&difficulty)
);
while nonce < U256::MAX {
let mut pow = [0u8; 32];
nonce.to_big_endian(&mut pow);
if tools::check_pow(&prev_hash, &difficulty, &pow) {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();

println!("Found nonce! {}", nonce);

let transactions: &[[u8; 32]] =
if ((last_block.get_info().height + 1) % BLOCKS_PER_EPOCH).is_zero() {
println!("Cycle ended!");
&[]
} else {
&[[25u8; 32]]
};

let block = rt
.block_on(tree.emmit_new_main_block(&pow, &wallet, transactions, timestamp))
.unwrap();

tree.send_amount(
&static_values::ROOT_PUBLIC_ADDRESS,
&wallet,
*static_values::MAIN_CHAIN_PAYMENT,
)
.unwrap();

println!("Added new block! {:?}\n", block.hash().unwrap());

rt.block_on(tree.flush()).unwrap();
break;
}
nonce += U256::one();
}
}
}
74 changes: 74 additions & 0 deletions examples/mine_derivative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use blockchaintree::block::Block as _;
use blockchaintree::tools;
use blockchaintree::{blockchaintree::BlockChainTree, static_values};
use primitive_types::U256;
use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
let rt = tokio::runtime::Runtime::new().unwrap();

let mut tree = BlockChainTree::new().unwrap();

let wallet = [1u8; 33];

let chain = tree.get_derivative_chain(&wallet).unwrap();

loop {
println!("Current height: {}", chain.get_height());
println!(
"Current miner gas amount: {}",
tree.get_gas(&wallet).unwrap()
);
let mut nonce = U256::zero();
let (prev_hash, difficulty, _prev_timestamp, _height) =
if let Some(block) = chain.get_last_block().unwrap() {
(
block.hash().unwrap(),
block.get_info().difficulty,
block.get_info().timestamp,
block.get_info().height,
)
} else {
let block = tree
.get_main_chain()
.find_by_hash(&chain.genesis_hash)
.unwrap()
.unwrap();
(
block.hash().unwrap(),
static_values::BEGINNING_DIFFICULTY,
block.get_info().timestamp,
U256::zero(),
)
};
println!(
"Current difficulty: {}",
tools::count_leading_zeros(&difficulty)
);
while nonce < U256::MAX {
let mut pow = [0u8; 32];
nonce.to_big_endian(&mut pow);
if tools::check_pow(&prev_hash, &difficulty, &pow) {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();

println!("Found nonce! {}", nonce);

let block = rt
.block_on(tree.emmit_new_derivative_block(&pow, &wallet, timestamp))
.unwrap();

tree.add_gas(&wallet, *static_values::MAIN_CHAIN_PAYMENT)
.unwrap();

println!("Added new block! {:?}\n", block.hash().unwrap());

rt.block_on(chain.flush()).unwrap();
break;
}
nonce += U256::one();
}
}
}
51 changes: 51 additions & 0 deletions gen_test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import hashlib
import os


def leading_zeros(num):
if num == 0:
return 8

leading_zeros = 0
while num & 0b10000000 == 0:
leading_zeros += 1
num = num << 1
num = num & 0b11111111
return leading_zeros


def total_leading_zeros(hash):
to_return = 0
for byte in hash:
l_zeros = leading_zeros(byte)
to_return += l_zeros
if l_zeros < 8:
break

return to_return

def to_hex_list(bt: bytes) -> list:
r = []
for b in bt:
r.append(f'0x{hex(b)[2:].upper()}')
return r

def gen(hash, difficulty):
difficulty = total_leading_zeros(difficulty)
for i in range(1000):
pow = b'' + os.urandom(10)
hasher = hashlib.sha256()
hasher.update(hash)
hasher.update(pow)

generated_hash = hasher.digest()
ghash_leadin_zeros = total_leading_zeros(generated_hash)

if ghash_leadin_zeros >= difficulty:
print(', '.join(to_hex_list(pow)), True)
else:
print(', '.join(to_hex_list(pow)), False)


gen(hashlib.sha256(b'text').digest(),
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')
Loading

0 comments on commit 8b0662c

Please sign in to comment.