Skip to content

Commit

Permalink
Merge branch 'main' into feat/issue-7-next-prev-timestamps-function
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejka authored Aug 13, 2024
2 parents 43acde5 + 54cb3ad commit 2db05c1
Show file tree
Hide file tree
Showing 23 changed files with 731,493 additions and 30 deletions.
11 changes: 7 additions & 4 deletions scripts/data/block_filter.jq
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def txin_coinbase:
vout: 0xffffffff_u32,
txo_index: 0, // TODO: implement
},
witness: LITERAL_AT_QUOTES
}"
;

Expand All @@ -19,6 +20,7 @@ def txin_regular:
vout: \(.vout),
txo_index: 0, // TODO: implement
},
witness: LITERAL_AT_QUOTES
}"
;

Expand All @@ -32,7 +34,7 @@ def txin:

def txout:
"TxOut {
value: \(.value*100000000)_u64,
value: \((.value*100000000) | round)_u64,
pk_script: from_base16(\"\(.scriptPubKey.hex)\"),
}"
;
Expand All @@ -47,20 +49,21 @@ def tx:
}"
;


def block:
"Block {
header : Header {
version: \(.version)_u32,
time: \(.time)_u32,
bits: 0, // TODO
nonce: \(.nonce)_u32
},
txs: array![\(.tx | map(tx) | join(",\n"))].span()
};"
}"
;

def fixture:
"use super::state::{Block, Header, Transaction, OutPoint, TxIn, TxOut};
"use raito::state::{Block, Header, Transaction, OutPoint, TxIn, TxOut};
use super::super::utils::from_base16;
pub fn block_\(.height)() -> Block {
// block hash: \(.hash)
Expand Down
5 changes: 1 addition & 4 deletions scripts/data/get_block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ curl \
"params": ["'${1}'", 2]
}' \
-H 'content-type: text/plain;' $BITCOIN_RPC \
| jq -r -f scripts/data/block_filter.jq > tests/blocks/block_${HEIGHT}.cairo

validate_target, validate_timestamp, validate_proof_of_work, compute_block_reward,
compute_total_work,
| jq -r -f scripts/data/block_filter.jq | sed 's/LITERAL_AT_QUOTES/@""/g' > tests/blocks/block_${HEIGHT}.cairo
36 changes: 36 additions & 0 deletions scripts/data/get_blocks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash
set -e;
set -o pipefail;

get_single_block() {
local block_hash=$1
./scripts/data/get_block.sh "$block_hash"
}

main() {
local block_hashes=(
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" # Genesis block (0)
"00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee" # Block containing first P2P tx to Hal Finney (170)
"00000000132fbe8314fc571c0be60b31ccd461c9ee85f42bde8c6d160a9dacc0" # Bloc containing first off ramp tx from Martti Malmi (24835)
"00000000152340ca42227603908689183edc47355204e7aca59383b0aaac1fd8" # Block containing pizza tx (57043)
"000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e" # First halving block (210000)
"000000000000000002cce816c0ab2c5c269cb081896b7dcb34b8422d6b74ffa1" # Second halving block (420000)
"0000000000000000011865af4122fe3b144e2cbeea86142e8ff2fb4107352d43" # Bitcoin Cash hard fork block (478558)
"0000000000000000001c8018d9cb3b742ef25114f27563e3fc4a1902167f9893" # Segwit soft fork block (481824)
"000000000000000000e5438564434edaf41e63829a637521a96235adf4653e1b" # Bitcoin Gold hard fork block (491407)
"000000000000000000024bead8df69990852c202db0e0097c1a12ea637d7e96d" # Third halving block (630000)
"0000000000000000000687bca986194dc2c1f949318629b44bb54ec0a94d8244" # Taproot soft fort block (709632)
"0000000000000000000515e202c8ae73c8155fc472422d7593af87aa74f2cf3d" # Biggest block in Bitcoin history - Taproot Wizards (774628)
"0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5" # Fourth halving block (840000)
)

# Loop through the block hashes and call get_block.sh for each
for block_hash in "${block_hashes[@]}"; do
echo "Getting block: $block_hash"
get_single_block "$block_hash"
done

echo "All blocks retrieved successfully."
}

main
2 changes: 1 addition & 1 deletion src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod utils;
pub mod validation;
pub mod state;

mod state;
mod main;
mod merkle_tree;
6 changes: 4 additions & 2 deletions src/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub struct Transaction {
#[derive(Drop, Copy)]
pub struct TxOut {
/// The value of the output in satoshis.
pub value: i64,
pub value: u64,
/// The spending script (aka locking code) for this output.
pub pk_script: @ByteArray,
}
Expand All @@ -127,7 +127,7 @@ pub struct TxIn {
/// The reference to the previous output that is being used as an input.
pub previous_output: OutPoint,
/// The witness data for transactions.
pub witness: Span<ByteArray>,
pub witness: @ByteArray
}


Expand All @@ -140,4 +140,6 @@ pub struct OutPoint {
pub vout: u32,
/// The index of output in the utreexo set (meta field).
pub txo_index: u64,
// Amount calculated with the txid and vout
pub amount: u64
}
24 changes: 24 additions & 0 deletions src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,27 @@ pub fn double_sha256(a: u256, b: u256) -> u256 {
low: x4.into() * TWO_POW_96 + x5.into() * TWO_POW_64 + x6.into() * TWO_POW_32 + x7.into(),
}
}

fn hex_to_byte(h: u8) -> u8 {
if h >= 48 && h <= 57 {
return h - 48;
} else if h >= 65 && h <= 70 {
return h - 55;
} else if h >= 97 && h <= 102 {
return h - 87;
}
panic!("Wrong hex character: {h}");
0
}

pub fn from_base16(hexs: ByteArray) -> @ByteArray {
let mut result: ByteArray = Default::default();
let mut i = 0;
let len = hexs.len();
while i < len {
result.append_word(hex_to_byte(hexs.at(i).unwrap()).into(), 4);
i += 1;
};

@result
}
73 changes: 65 additions & 8 deletions src/validation.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::merkle_tree::merkle_root;
use super::utils::{shl, shr};
use super::state::{Block, ChainState, Transaction, UtreexoState};
use super::state::{Block, ChainState, Transaction, UtreexoState, UtreexoSet, TxIn, TxOut};

const MAX_TARGET: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000;

Expand Down Expand Up @@ -43,9 +43,27 @@ impl TransactionValidatorImpl of TransactionValidator {
// TODO: implement
0
}
fn fee(self: @Transaction) -> u256 {
// TODO: implement
0
fn fee(self: @Transaction) -> u64 {
let mut total_input_amount = 0;
let mut total_output_amount = 0;
// Inputs of a transaction
let inputs = *self.inputs;
// Outputs of a transaction
let outputs = *self.outputs;

for input in inputs {
let amount = *input.previous_output.amount;
total_input_amount += amount;
};

for output in outputs {
let value = *output.value;
total_output_amount += value;
};

let tx_fee = total_input_amount - total_output_amount;

tx_fee
}
}

Expand Down Expand Up @@ -184,7 +202,7 @@ fn validate_bits(block: @Block, target: u256) -> Result<(), ByteArray> {
}
}

fn fee_and_merkle_root(block: @Block) -> Result<(u256, u256), ByteArray> {
fn fee_and_merkle_root(block: @Block) -> Result<(u64, u256), ByteArray> {
let mut txids = ArrayTrait::new();
let mut total_fee = 0;

Expand All @@ -196,7 +214,7 @@ fn fee_and_merkle_root(block: @Block) -> Result<(u256, u256), ByteArray> {
Result::Ok((total_fee, merkle_root(ref txids)))
}

fn validate_coinbase(block: @Block, total_fees: u256) -> Result<(), ByteArray> {
fn validate_coinbase(block: @Block, total_fees: u64) -> Result<(), ByteArray> {
//TODO implement
Result::Ok(())
}
Expand All @@ -209,10 +227,12 @@ fn compute_block_reward(block_height: u32) -> u64 {

#[cfg(test)]
mod tests {
use raito::state::{Header, Transaction, TxIn, TxOut};
use raito::state::{Header, Transaction, TxIn, TxOut, OutPoint};
use raito::utils::from_base16;
use super::{
validate_timestamp, validate_proof_of_work, compute_block_reward, compute_total_work,
compute_work_from_target, shr, shl, Block, ChainState, UtreexoState, next_prev_timestamps
compute_work_from_target, shr, shl, Block, ChainState, UtreexoState, next_prev_timestamps,
TransactionValidatorImpl
};


Expand Down Expand Up @@ -247,6 +267,43 @@ mod tests {
assert!(result.is_err(), "Median time is greater than block's timestamp");
}

#[test]
fn test_tx_fee() {
let tx = Transaction {
version: 1,
is_segwit: false,
inputs: array![
TxIn {
script: from_base16(
"01091d8d76a82122082246acbb6cc51c839d9012ddaca46048de07ca8eec221518200241cdb85fab4815c6c624d6e932774f3fdf5fa2a1d3a1614951afb83269e1454e2002443047"
),
sequence: 0xffffffff,
previous_output: OutPoint {
txid: 0x0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9,
vout: 0x00000000,
txo_index: 0,
amount: 100
},
witness: from_base16("")
}
]
.span(),
outputs: array![
TxOut {
value: 90,
pk_script: from_base16(
"ac4cd86c7e4f702ac7d5debaf126068a3b30b7c1212c145fdfa754f59773b3aae71484a22f30718d37cd74f325229b15f7a2996bf0075f90131bf5c509fe621aae0441"
),
}
]
.span(),
lock_time: 0
};

let fee = TransactionValidatorImpl::fee(@tx);
assert_eq!(fee, 10);
}

#[test]
fn test_compute_work_from_target1() {
let expected_work = 0x0100010001;
Expand Down
17 changes: 17 additions & 0 deletions tests/blocks.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod block_0;
mod block_170;
mod block_24835;
mod block_57043;
// mod block_210000;
// mod block_420000;
// mod block_478558;
// mod block_481824;
// mod block_491407;
// mod block_630000;
// mod block_709632;
// mod block_774628;
// mod block_840000;

// Blocks that are commented are too big and make compilation when running tests almost impossible


16 changes: 12 additions & 4 deletions tests/blocks/block_0.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use super::state::{Block, Header, Transaction, OutPoint, TxIn, TxOut};
use raito::state::{Block, Header, Transaction, OutPoint, TxIn, TxOut};
use super::super::utils::from_base16;

pub fn block_0() -> Block {
// block hash: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
Block {
header: Header { version: 1_u32, time: 1231006505_u32, nonce: 2083236893_u32 },
header: Header {
version: 1_u32, time: 1231006505_u32, bits: 0, // TODO
nonce: 2083236893_u32
},
txs: array![
Transaction {
version: 1,
Expand All @@ -15,8 +19,12 @@ pub fn block_0() -> Block {
),
sequence: 4294967295,
previous_output: OutPoint {
txid: 0_u256, vout: 0xffffffff_u32, txo_index: 0, // TODO: implement
txid: 0_u256,
vout: 0xffffffff_u32,
txo_index: 0,
amount: 0 // TODO: implement
},
witness: @""
}
]
.span(),
Expand All @@ -33,5 +41,5 @@ pub fn block_0() -> Block {
}
]
.span()
};
}
}
20 changes: 15 additions & 5 deletions tests/blocks/block_170.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use super::state::{Block, Header, Transaction, OutPoint, TxIn, TxOut};
use raito::state::{Block, Header, Transaction, OutPoint, TxIn, TxOut};
use super::super::utils::from_base16;

pub fn block_170() -> Block {
// block hash: 00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee
Block {
header: Header { version: 1_u32, time: 1231731025_u32, nonce: 1889418792_u32 },
header: Header {
version: 1_u32, time: 1231731025_u32, bits: 0, // TODO
nonce: 1889418792_u32
},
txs: array![
Transaction {
version: 1,
Expand All @@ -13,8 +17,12 @@ pub fn block_170() -> Block {
script: from_base16("04ffff001d0102"),
sequence: 4294967295,
previous_output: OutPoint {
txid: 0_u256, vout: 0xffffffff_u32, txo_index: 0, // TODO: implement
txid: 0_u256,
vout: 0xffffffff_u32,
txo_index: 0,
amount: 0 // TODO: implement
},
witness: @""
}
]
.span(),
Expand All @@ -41,8 +49,10 @@ pub fn block_170() -> Block {
previous_output: OutPoint {
txid: 0x0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9,
vout: 0,
txo_index: 0, // TODO: implement
txo_index: 0,
amount: 0 // TODO: implement
},
witness: @""
}
]
.span(),
Expand All @@ -65,5 +75,5 @@ pub fn block_170() -> Block {
}
]
.span()
};
}
}
Loading

0 comments on commit 2db05c1

Please sign in to comment.