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 14 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
54 changes: 49 additions & 5 deletions src/validation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ impl BlockValidatorImpl of BlockValidator {
// validate_and_apply_transactions

let prev_timestamps = next_prev_timestamps(@self, @block);
let total_work = compute_total_work(@self, @block);
let (current_target, epoch_start_time) = adjust_difficulty(@self, @block);
let bits_to_target: u256 = block.header.bits.into(); //will replace with bits to target implementation
let total_work = compute_total_work(self.total_work, bits_to_target);

Result::Ok(
ChainState { total_work, current_target, epoch_start_time, prev_timestamps, ..self, }
Expand Down Expand Up @@ -55,9 +56,16 @@ fn next_prev_timestamps(self: @ChainState, block: @Block) -> Span<u32> {
*self.prev_timestamps
}

fn compute_total_work(self: @ChainState, block: @Block) -> u256 {
// TODO: implement
*self.total_work
fn compute_total_work(current_total_work: u256, target: u256) -> u256 {
current_total_work + compute_work_from_target(target)
}

// 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 {
(~target / (target + 1_u256)) + 1_u256
}

fn adjust_difficulty(self: @ChainState, block: @Block) -> (u32, u32) {
Expand All @@ -72,7 +80,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 +148,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');
}
}