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: validate proof of work #33

Merged
Merged
Changes from all 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
47 changes: 42 additions & 5 deletions src/validation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::state::{Block, ChainState, UtreexoState};
impl BlockValidatorImpl of BlockValidator {
fn validate_and_apply(self: ChainState, block: Block) -> Result<ChainState, ByteArray> {
validate_prev_block_hash(@self, @block)?;
validate_proof_of_work(@self, @block)?;
validate_proof_of_work(@0_u256, @block)?;
validate_target(@self, @block)?;
validate_timestamp(@self, @block)?;

Expand All @@ -29,9 +29,14 @@ fn validate_prev_block_hash(self: @ChainState, block: @Block) -> Result<(), Byte
}
}

fn validate_proof_of_work(self: @ChainState, block: @Block) -> Result<(), ByteArray> {
// TODO: implement
Result::Ok(())
fn validate_proof_of_work(target: @u256, block: @Block) -> Result<(), ByteArray> {
if block.header.prev_block_hash <= target {
Result::Ok(())
} else {
Result::Err(
"Insufficient proof of work. Expected block hash {block.header.prev_block_hash} to be less than or equal to {target}."
)
}
}

fn validate_target(self: @ChainState, block: @Block) -> Result<(), ByteArray> {
Expand Down Expand Up @@ -72,7 +77,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, validate_proof_of_work};
use super::{Block, ChainState, UtreexoState};
use super::super::state::{Header, Transaction, TxIn, TxOut};

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

#[test]
fn test_validate_proof_of_work() {
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(),
};

// target is less than prev block hash
let result = validate_proof_of_work(@0_u256, @block);
assert!(result.is_err(), "Expect target less than prev block hash");

// target is greater than prev block hash
let result = validate_proof_of_work(@2_u256, @block);
assert!(result.is_ok(), "Expect target gt prev block hash");

// target is equal to prev block hash
let result = validate_proof_of_work(@1_u256, @block);
assert!(result.is_ok(), "Expect target equal to prev block hash");

// block prev block hash is greater than target
block.header.prev_block_hash = 2;
let result = validate_proof_of_work(@1_u256, @block);
assert!(result.is_err(), "Expect prev block hash gt target");

// block prev block hash is less than target
block.header.prev_block_hash = 9;
let result = validate_proof_of_work(@10_u256, @block);
assert!(result.is_ok(), "Expect prev block hash lt target");
}
}