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

add validate_target #25

Merged
merged 4 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ version = "0.1.0"
edition = "2023_11"

[dependencies]

[dev-dependencies]
cairo_test = "2.7.0"
14 changes: 7 additions & 7 deletions src/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Block {
/// Block header
/// https://developer.bitcoin.org/reference/block_chain.html#block-headers
#[derive(Drop, Copy)]
struct Header {
pub struct Header {
/// The version of the block.
pub version: u32,
/// The hash of the previous block in the blockchain.
Expand Down Expand Up @@ -65,22 +65,22 @@ pub struct Transaction {
#[derive(Drop, Copy)]
pub struct TxOut {
/// The value of the output.
value: i64,
pub value: i64,
/// The public key script of the output.
pk_script: @ByteArray,
pub pk_script: @ByteArray,
}

/// Input of a transaction.
/// https://developer.bitcoin.org/reference/transactions.html#txin-a-transaction-input-non-coinbase
#[derive(Drop, Copy)]
pub struct TxIn {
/// The transaction ID of the input.
txid: u256,
pub txid: u256,
/// The index of the input.
index: u32,
pub index: u32,
/// The script of the input.
script: @ByteArray,
pub script: @ByteArray,
/// The sequence of the input.
sequence: u32,
pub sequence: u32,
}

49 changes: 47 additions & 2 deletions src/validation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ fn validate_proof_of_work(self: @ChainState, block: @Block) -> Result<(), ByteAr
}

fn validate_target(self: @ChainState, block: @Block) -> Result<(), ByteArray> {
// TODO: implement
Result::Ok(())
if self.current_target == block.header.bits {
Result::Ok(())
} else {
Result::Err("Target is {block.header.bits}. Expected {self.current_target}")
}
}

fn validate_timestamp(self: @ChainState, block: @Block) -> Result<(), ByteArray> {
Expand All @@ -63,3 +66,45 @@ fn validate_merkle_root(self: @ChainState, block: @Block) -> Result<(), ByteArra
// TODO: implement
Result::Ok(())
}

#[cfg(test)]
mod tests {
use core::result::ResultTrait;
use super::validate_target;
use super::{Block, ChainState,};
use super::super::state::{Header, Transaction, TxIn, TxOut};
use core::array::{Span};

#[test]
fn test_validate_target() {
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].span(),
};
let txIn = TxIn { txid: 1, index: 1, script: @"1", sequence: 1, };
let txOut = TxOut { value: 1, pk_script: @"1", };
let transaction = Transaction {
maciejka marked this conversation as resolved.
Show resolved Hide resolved
version: 1, inputs: array![txIn].span(), outputs: array![txOut].span(), lock_time: 1,
};
let header = Header {
version: 1, prev_block_hash: 1, merkle_root_hash: 1, time: 1, bits: 1, nonce: 1,
};
let mut block = Block { header: header, txs: array![transaction].span() };
let result = validate_target(@chain_state, @block);
assert(result.is_ok(), 'Expected target to be valid');

chain_state.current_target = 2;
block.header.bits = 1;
let result = validate_target(@chain_state, @block);
assert(result.is_err(), 'Expected target to be invalid');

chain_state.current_target = 1;
block.header.bits = 2;
let result = validate_target(@chain_state, @block);
assert(result.is_err(), 'Expected target to be invalid');
}
}