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

Feat/issue11 compute total work function #34

Merged
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions src/validation.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::state::{Block, ChainState, UtreexoState};

const ONE_256: u256 = 1_u256;
m-kus marked this conversation as resolved.
Show resolved Hide resolved

#[generate_trait]
impl BlockValidatorImpl of BlockValidator {
fn validate_and_apply(self: ChainState, block: Block) -> Result<ChainState, ByteArray> {
Expand Down Expand Up @@ -56,8 +58,30 @@ fn next_prev_timestamps(self: @ChainState, block: @Block) -> Span<u32> {
}

fn compute_total_work(self: @ChainState, block: @Block) -> u256 {
// TODO: implement
*self.total_work
let block_target: u256 = convert_u32_to_u256(*block.header.bits);
maciejka marked this conversation as resolved.
Show resolved Hide resolved
let work_in_block: u256 = compute_work_from_target(block_target);
let new_total_work: u256 = *self.total_work + work_in_block;
new_total_work
}

fn convert_u32_to_u256(val: u32) -> u256 {
maciejka marked this conversation as resolved.
Show resolved Hide resolved
let felt_val: felt252 = val.into();
let mut val_u256: u256 = 1;
let low: u128 = felt_val.try_into().unwrap();
let high: u128 = 0;
val_u256.low = low;
val_u256.high = high;
val_u256
}

// Need to compute 2**256 / (target+1), but we can't represent 2**256
// as it's too large for an u256. However, as 2**256 is at least as large
// as target+1, it is equal to ((2**256 - target - 1) / (target+1)) + 1,
// or ~target / (target+1) + 1.
fn compute_work_from_target(target: u256) -> u256 {
let div: u256 = ~target / (target + ONE_256);
maciejka marked this conversation as resolved.
Show resolved Hide resolved
let result: u256 = div + ONE_256;
result
}

fn adjust_difficulty(self: @ChainState, block: @Block) -> (u32, u32) {
Expand All @@ -72,7 +96,7 @@ fn validate_merkle_root(self: @ChainState, block: @Block) -> Result<(), ByteArra

#[cfg(test)]
mod tests {
use super::{validate_target, validate_timestamp};
use super::{validate_target, validate_timestamp, compute_work_from_target, compute_total_work};
use super::{Block, ChainState, UtreexoState};
use super::super::state::{Header, Transaction, TxIn, TxOut};

Expand Down Expand Up @@ -140,4 +164,40 @@ mod tests {
let result = validate_timestamp(@chain_state, @block);
assert!(result.is_err(), "Median time is greater than block's timestamp");
}

#[test]
fn test_compute_work_from_target1() {
let expected_work = 0x0100010001;
let target: u256 = 0x00000000ffff0000000000000000000000000000000000000000000000000000;
let work = compute_work_from_target(target);
assert(expected_work == work, 'failed to compute target');
}
#[test]
fn test_compute_work_from_target2() {
let expected_work = 0x26d946e509ac00026d;
let target: u256 = 0x00000000000000000696f4000000000000000000000000000000000000000000;
let work = compute_work_from_target(target);
assert(expected_work == work, 'failed to compute target');
}
#[test]
fn test_compute_work_from_target3() {
let expected_work = 0xe10005c64415f04ef3e387b97db388404db9fdfaab2b1918f6783471d;
let target: u256 = 0x12345600;
let work = compute_work_from_target(target);
assert(expected_work == work, 'failed to compute target');
}
#[test]
fn test_compute_work_from_target4() {
let expected_work = 0x1c040c95a099201bcaf85db4e7f2e21e18707c8d55a887643b95afb2f;
let target: u256 = 0x92340000;
let work = compute_work_from_target(target);
assert(expected_work == work, 'failed to compute target');
}
#[test]
fn test_compute_work_from_target5() {
let expected_work = 0x21809b468faa88dbe34f;
let target: u256 = 0x00000000000000000007a4290000000000000000000000000000000000000000;
let work = compute_work_from_target(target);
assert(expected_work == work, 'failed to compute target');
}
}