-
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.
- Loading branch information
1 parent
d42ce34
commit f5dbd5b
Showing
6 changed files
with
169 additions
and
79 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
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
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
Oops, something went wrong.