-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from AploCoin/rewrite
Rewrite
- Loading branch information
Showing
23 changed files
with
2,688 additions
and
3,369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Oops, something went wrong.