-
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
f5dbd5b
commit ddd1cf2
Showing
7 changed files
with
190 additions
and
169 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
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
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.