Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start compute block reward impl #37

Merged
44 changes: 43 additions & 1 deletion src/validation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use super::state::{Block, ChainState, Transaction, UtreexoState};
use super::utils::{shl, shr};

const MAX_TARGET: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000;
pub const REWARD_INITIAL: u256 = 50; // 50 BTC in satoshis => 5000000000 SATS
pub const POW_SATS_AMOUNT: u256 = 8; // Pow to convert in SATS

#[generate_trait]
impl BlockValidatorImpl of BlockValidator {
Expand Down Expand Up @@ -208,7 +210,10 @@ fn validate_coinbase(block: @Block, total_fees: u256) -> Result<(), ByteArray> {

#[cfg(test)]
mod tests {
use super::{validate_target, validate_timestamp, validate_proof_of_work};
use super::{
validate_target, validate_timestamp, validate_proof_of_work, compute_block_reward, shr,
REWARD_INITIAL, POW_SATS_AMOUNT
};
use super::{Block, ChainState, UtreexoState};
use super::super::state::{Header, Transaction, TxIn, TxOut};

Expand Down Expand Up @@ -302,4 +307,41 @@ mod tests {
let result = validate_proof_of_work(@10_u256, @block);
assert!(result.is_ok(), "Expect prev block hash lt target");
}


#[test]
fn test_compute_block_reward() {
let max_halvings: u32 = 64;
MSghais marked this conversation as resolved.
Show resolved Hide resolved
let reward_initial: u32 = REWARD_INITIAL.try_into().unwrap();
let halving_block_range = 210_000; // every 210 000 blocks
let mut nprevious_subsidy = REWARD_INITIAL * 2;
let mut loop_index: u32 = 0;
loop {
MSghais marked this conversation as resolved.
Show resolved Hide resolved
if loop_index == max_halvings {
break;
}
let block_height: u32 = loop_index * halving_block_range;
let reward = compute_block_reward(block_height);
if let Result::Ok(r) = reward {
let cast_reward_initial: u64 = reward_initial.try_into().unwrap();
assert!(r <= cast_reward_initial);
// TODO check this assert
// let cast_reward_previous: u64 = nprevious_subsidy.try_into().unwrap();
// assert_eq!(r, cast_reward_previous/2);
nprevious_subsidy = r.try_into().unwrap();
}
loop_index = loop_index + 1;
};
let last_reward = compute_block_reward(max_halvings);
if let Result::Ok(r) = last_reward {
assert_eq!(r, 0);
};

let height_test = compute_block_reward(300000);
if let Result::Ok(r) = height_test {
println!("last reward compute {:?}", r);
let cast_r: u256 = r.try_into().unwrap();
assert_eq!(shr(cast_r, 8), 250000000);
};
}
}
Loading