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
Show file tree
Hide file tree
Changes from 8 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
55 changes: 55 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "alexandria_bytes"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#bcdca70afdf59c9976148e95cebad5cf63d75a7f"
dependencies = [
"alexandria_data_structures",
"alexandria_math",
]

[[package]]
name = "alexandria_data_structures"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#bcdca70afdf59c9976148e95cebad5cf63d75a7f"
dependencies = [
"alexandria_encoding",
]

[[package]]
name = "alexandria_encoding"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#bcdca70afdf59c9976148e95cebad5cf63d75a7f"
dependencies = [
"alexandria_bytes",
"alexandria_math",
"alexandria_numeric",
]

[[package]]
name = "alexandria_math"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#bcdca70afdf59c9976148e95cebad5cf63d75a7f"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "alexandria_numeric"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#bcdca70afdf59c9976148e95cebad5cf63d75a7f"
dependencies = [
"alexandria_math",
"alexandria_searching",
]

[[package]]
name = "alexandria_searching"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#bcdca70afdf59c9976148e95cebad5cf63d75a7f"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "raito"
version = "0.1.0"
dependencies = [
"alexandria_math",
]
1 change: 1 addition & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2023_11"
gen_data_test= "chmod -x ./scripts/data/launch.sh && bash ./scripts/data/launch.sh"

[dependencies]
alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git" }
maciejka marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
cairo_test = "2.7.0"
73 changes: 70 additions & 3 deletions src/validation.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use alexandria_math::i257::{i257, I257Impl};
use super::state::{Block, ChainState, UtreexoState};

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

#[generate_trait]
impl BlockValidatorImpl of BlockValidator {
fn validate_and_apply(self: ChainState, block: Block) -> Result<ChainState, ByteArray> {
Expand Down Expand Up @@ -56,8 +60,41 @@ 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
}

fn compute_bitwise_not_for_u256(val: u256) -> u256 {
let mut not_val: u256 = 0;
not_val.low = ALL_ONES_U128 - val.low - 1_u128;
not_val.high = ALL_ONES_U128 - val.high - 1_u128;
not_val
}


// 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 one_i257: i257 = I257Impl::new(ONE_256, false);
maciejka marked this conversation as resolved.
Show resolved Hide resolved
let target_i257: i257 = target.into();
let not_target: i257 = compute_bitwise_not_for_u256(target).into();
maciejka marked this conversation as resolved.
Show resolved Hide resolved
let div_i257: i257 = not_target / (target_i257 + one_i257);
let result_i257: i257 = div_i257 + one_i257;
result_i257.abs()
}

fn adjust_difficulty(self: @ChainState, block: @Block) -> (u32, u32) {
Expand All @@ -72,9 +109,11 @@ 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};
use alexandria_math::i257::{i257, I257Impl};
const ONE_256: u256 = 1_u256;

#[test]
fn test_validate_target() {
Expand Down Expand Up @@ -140,4 +179,32 @@ 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_total_work() {
let mut chain_state = ChainState {
block_height: 1,
total_work: 1,
best_block_hash: 1,
current_target: 1,
epoch_start_time: 1,
prev_timestamps: array![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].span(),
};
let mut block = Block {
header: Header {
version: 1, prev_block_hash: 1, merkle_root_hash: 1, time: 12, bits: 1, nonce: 1,
},
txs: ArrayTrait::new().span(),
};
let expected_work = compute_work_from_target(ONE_256) + chain_state.total_work;
let total_work: u256 = compute_total_work(@chain_state, @block);
assert(total_work == expected_work, 'failed to compute target');
}

#[test]
fn test_compute_work_from_target() {
maciejka marked this conversation as resolved.
Show resolved Hide resolved
let expected_work = 0x0100010001;
let target: u256 = 0x00000000ffff0000000000000000000000000000000000000000000000000000;
let work = compute_work_from_target(target);
assert(expected_work == work, 'failed to compute target');
}
}
Loading