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

[chore]: reorganize Tests crate #59

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 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 Down Expand Up @@ -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
25 changes: 25 additions & 0 deletions scripts/data/get_blocks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/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
"00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee" # Block containing first P2P tx to Hal Finney
)

# 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;
4 changes: 2 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 Down
2 changes: 2 additions & 0 deletions tests/blocks.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod block_0;
mod block_170;
11 changes: 8 additions & 3 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 @@ -17,6 +21,7 @@ pub fn block_0() -> Block {
previous_output: OutPoint {
txid: 0_u256, vout: 0xffffffff_u32, txo_index: 0, // TODO: implement
},
witness: @""
}
]
.span(),
Expand All @@ -33,5 +38,5 @@ pub fn block_0() -> Block {
}
]
.span()
};
}
}
12 changes: 9 additions & 3 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 @@ -15,6 +19,7 @@ pub fn block_170() -> Block {
previous_output: OutPoint {
txid: 0_u256, vout: 0xffffffff_u32, txo_index: 0, // TODO: implement
},
witness: @""
}
]
.span(),
Expand Down Expand Up @@ -43,6 +48,7 @@ pub fn block_170() -> Block {
vout: 0,
txo_index: 0, // TODO: implement
},
witness: @""
}
]
.span(),
Expand All @@ -65,5 +71,5 @@ pub fn block_170() -> Block {
}
]
.span()
};
}
}
3 changes: 3 additions & 0 deletions tests/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod tests;
mod utils;
mod blocks;
5 changes: 3 additions & 2 deletions tests/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ fn hex_to_byte(h: u8) -> u8 {
0
}

pub fn from_base16(hexs: @ByteArray) -> ByteArray {
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

@result
}