src/transactions/typs.rs
pub fn fairness_difficulty(block_height: u32, miner_wallet: &str) -> bool {
// adjust every 840,000 until
// the first halvingevent
let interval = 840000;
// check what interval we are
// on based on current height
let current_interval = (block_height / interval) + 1;
// set max number of consecutive
// blocks a miner can mine starting
// at 10
let max_consecutive_blocks = u8::max(10 - current_interval as u8, 1) as u32;
// set the start block
let start_block = block_height.saturating_sub(max_consecutive_blocks);
let mut consecutive_blocks = 0;
// check who mined each block from
// start block to current height
for i in (start_block..=block_height).rev() {
// load the block in question
if let Ok(json_data_str) = load_block(i) {
if let Ok(json_data) = serde_json::from_str::<Value>(&json_data_str) {
// get the miner details from
// the rewards transaction
let mined_by_miner = json_data["transactions"]
.as_array()
.map_or(false, |transactions| {
transactions.iter().any(|transaction| {
transaction["Rewards"]
.as_object()
.and_then(|rewards_transaction| rewards_transaction["receiver"].as_str())
.map_or(false, |receiver| receiver == miner_wallet)
})
});
// check how many blocks in a row
// the miner has mined
if mined_by_miner {
consecutive_blocks += 1;
// if they have mined more than
// allowed reject the block with
// an error
if consecutive_blocks > max_consecutive_blocks {
println!("Error: Miner has exceeded the allowed consecutive block limit");
return false;
}
} else {
consecutive_blocks = 0;
}
}
}
}
true
}