diff --git a/.gitignore b/.gitignore index b2c7f1ca..2fb090f2 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,4 @@ Cargo.lock .python-version __pycache__ -.raito \ No newline at end of file +.client_cache/ \ No newline at end of file diff --git a/README.md b/README.md index 92ca81be..76b31c63 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ Extend light client with partial transaction validation, but without UTXO checks Tasks: -* [ ] reassess validation check list (analyze Bitcoin core codebase) +* [x] reassess validation check list (analyze Bitcoin core codebase) * [x] generate & run integration tests e2e instead of Cairo codegen * [x] transaction ID calculation * [x] transaction root computation @@ -135,40 +135,18 @@ Raito is a reference to Light Yagami (夜神月, Yagami Raito) from the manga/an ## Usage -This will compile all the components: +This will compile all the packages: ```bash scarb build ``` -This will run unit and integration tests: +This will run tests for all the packages: ```bash scarb test ``` -For integration tests ony: - -```bash -scarb run integration_tests -``` - -Run for specific test file(s): - -```bash -scarb run integration_tests tests/data/light_481823.json -``` - -Re-generate integration test data: - -```base -scarb run regenerate_tests --force -``` - -* Without `--force` flag only non-existent files will be created -* Files are located in [tests/data/](https://github.com/keep-starknet-strange/raito/blob/main/tests/data) -* If you want to add a new test case, edit [scripts/data/regenerate_tests.sh](https://github.com/keep-starknet-strange/raito/blob/main/scripts/data/regenerate_tests.sh) - ## Build dependencies Install necessary packages required by Python scripts: diff --git a/Scarb.lock b/Scarb.lock index a0e0284f..8beea944 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -2,5 +2,19 @@ version = 1 [[package]] -name = "raito" +name = "client" +version = "0.1.0" +dependencies = [ + "consensus", +] + +[[package]] +name = "consensus" +version = "0.1.0" +dependencies = [ + "utils", +] + +[[package]] +name = "utils" version = "0.1.0" diff --git a/Scarb.toml b/Scarb.toml index 7ee245b7..95012dcc 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,16 +1,13 @@ -[package] -name = "raito" -version = "0.1.0" -edition = "2024_07" - -[scripts] -regenerate_tests= "./scripts/data/regenerate_tests.sh" -integration_tests = "scarb build && ./scripts/data/integration_tests.sh" -client= "scarb build && ./scripts/data/client.sh" -test = "scarb cairo-test && scarb run integration_tests" -lint = "flake8 scripts/ && black --check scripts/" +[workspace] +members = ["packages/*"] -[dependencies] +[workspace.package] +description = "Bitcoin ZK client." +cairo-version = "2.8.2" +version = "0.1.0" +readme = "README.md" +repository = "https://github.com/keep-starknet-strange/raito" +license-file = "LICENSE" -[dev-dependencies] +[workspace.dependencies] cairo_test = "2.8.0" diff --git a/packages/client/README.md b/packages/client/README.md new file mode 100644 index 00000000..f99b53d9 --- /dev/null +++ b/packages/client/README.md @@ -0,0 +1,45 @@ +# Bitcoin client in Cairo + +This package is a standalone Cairo program (outside of Starknet context) that implements a Bitcoin client which can work in two modes: +- Light mode: block header validation only +- Full mode: full Bitcoin consensus validation + +## Usage + +```sh +# You have to be in the "packages/client" directory +scarb run client START_HEIGHT END_HEIGHT BATCH_SIZE MODE STRATEGY +``` + +Client expects the following arguments: +* `START_HEIGHT` height of the initial chain state +* `END_HEIGHT` height of the final (resulting) chain state +* `BATCH_SIZE` number of blocks applied per single program run +* `MODE` either `light` or `full` (default is light) +* `STRATEGY` either `sequential` or `random` (default is sequential) + +## Integration tests + +In order to run integration tests: + +```sh +scarb test +``` + +Run a specific test file (or several files): + +```sh +# You have to be in the "packages/client" directory +scarb test tests/data/light_481823.json +``` + +Re-generate integration test data: + +```sh +# You have to be in the "packages/client" directory +scarb run regenerate_tests --force +``` + +If you want to just add a new test case, edit `scripts/data/regenerate_tests.sh` and run without `--force` flag. + +You can also add/remove ignored scenarios, check out `scripts/data/regenerate_tests.sh` as well. diff --git a/packages/client/Scarb.toml b/packages/client/Scarb.toml new file mode 100644 index 00000000..b2189723 --- /dev/null +++ b/packages/client/Scarb.toml @@ -0,0 +1,16 @@ +[package] +name = "client" +version = "0.1.0" +edition = "2024_07" + +[dependencies] +consensus = { path = "../consensus" } + +[dev-dependencies] +cairo_test.workspace = true + +[scripts] +test = "scarb build && ../../scripts/data/integration_tests.sh" +regenerate_tests= "../../scripts/data/regenerate_tests.sh" +client = "scarb build && ../../scripts/data/client.sh" +lint = "flake8 scripts/ && black --check scripts/ && scarb fmt" diff --git a/packages/client/src/lib.cairo b/packages/client/src/lib.cairo new file mode 100644 index 00000000..c1bd37d3 --- /dev/null +++ b/packages/client/src/lib.cairo @@ -0,0 +1,4 @@ +mod main; +// TODO: scarb cairo-run should support "features" argument +// so that we can conditionally compile this module +mod test; diff --git a/src/main.cairo b/packages/client/src/main.cairo similarity index 88% rename from src/main.cairo rename to packages/client/src/main.cairo index 714279e5..34c65fc2 100644 --- a/src/main.cairo +++ b/packages/client/src/main.cairo @@ -1,5 +1,5 @@ -use crate::types::block::Block; -use crate::types::chain_state::{ChainState, BlockValidator}; +use consensus::types::block::Block; +use consensus::types::chain_state::{ChainState, BlockValidator}; /// Raito program arguments. #[derive(Serde)] diff --git a/src/test.cairo b/packages/client/src/test.cairo similarity index 94% rename from src/test.cairo rename to packages/client/src/test.cairo index da9f7d80..a8d03acf 100644 --- a/src/test.cairo +++ b/packages/client/src/test.cairo @@ -1,5 +1,5 @@ -use crate::types::block::Block; -use crate::types::chain_state::{ChainState, BlockValidator}; +use consensus::types::block::Block; +use consensus::types::chain_state::{ChainState, BlockValidator}; use core::testing::get_available_gas; /// Integration testing program arguments. diff --git a/tests/data/full_169.json b/packages/client/tests/data/full_169.json similarity index 100% rename from tests/data/full_169.json rename to packages/client/tests/data/full_169.json diff --git a/tests/data/full_757738.json b/packages/client/tests/data/full_757738.json similarity index 100% rename from tests/data/full_757738.json rename to packages/client/tests/data/full_757738.json diff --git a/tests/data/light_150012.json b/packages/client/tests/data/light_150012.json similarity index 100% rename from tests/data/light_150012.json rename to packages/client/tests/data/light_150012.json diff --git a/tests/data/light_169.json b/packages/client/tests/data/light_169.json similarity index 100% rename from tests/data/light_169.json rename to packages/client/tests/data/light_169.json diff --git a/tests/data/light_2015.json b/packages/client/tests/data/light_2015.json similarity index 100% rename from tests/data/light_2015.json rename to packages/client/tests/data/light_2015.json diff --git a/tests/data/light_209999.json b/packages/client/tests/data/light_209999.json similarity index 100% rename from tests/data/light_209999.json rename to packages/client/tests/data/light_209999.json diff --git a/tests/data/light_24834.json b/packages/client/tests/data/light_24834.json similarity index 100% rename from tests/data/light_24834.json rename to packages/client/tests/data/light_24834.json diff --git a/tests/data/light_32255.json b/packages/client/tests/data/light_32255.json similarity index 100% rename from tests/data/light_32255.json rename to packages/client/tests/data/light_32255.json diff --git a/tests/data/light_478557.json b/packages/client/tests/data/light_478557.json similarity index 100% rename from tests/data/light_478557.json rename to packages/client/tests/data/light_478557.json diff --git a/tests/data/light_481823.json b/packages/client/tests/data/light_481823.json similarity index 100% rename from tests/data/light_481823.json rename to packages/client/tests/data/light_481823.json diff --git a/tests/data/light_491406.json b/packages/client/tests/data/light_491406.json similarity index 100% rename from tests/data/light_491406.json rename to packages/client/tests/data/light_491406.json diff --git a/tests/data/light_57042.json b/packages/client/tests/data/light_57042.json similarity index 100% rename from tests/data/light_57042.json rename to packages/client/tests/data/light_57042.json diff --git a/tests/data/light_629999.json b/packages/client/tests/data/light_629999.json similarity index 100% rename from tests/data/light_629999.json rename to packages/client/tests/data/light_629999.json diff --git a/tests/data/light_709631.json b/packages/client/tests/data/light_709631.json similarity index 100% rename from tests/data/light_709631.json rename to packages/client/tests/data/light_709631.json diff --git a/tests/data/light_757738.json b/packages/client/tests/data/light_757738.json similarity index 100% rename from tests/data/light_757738.json rename to packages/client/tests/data/light_757738.json diff --git a/tests/data/light_757752.json b/packages/client/tests/data/light_757752.json similarity index 100% rename from tests/data/light_757752.json rename to packages/client/tests/data/light_757752.json diff --git a/tests/data/light_774627.json b/packages/client/tests/data/light_774627.json similarity index 100% rename from tests/data/light_774627.json rename to packages/client/tests/data/light_774627.json diff --git a/tests/data/light_839999.json b/packages/client/tests/data/light_839999.json similarity index 100% rename from tests/data/light_839999.json rename to packages/client/tests/data/light_839999.json diff --git a/packages/consensus/README.md b/packages/consensus/README.md new file mode 100644 index 00000000..1c89183f --- /dev/null +++ b/packages/consensus/README.md @@ -0,0 +1,8 @@ +# Bitcoin consensus in Cairo + +This package is a Cairo library providing primitives for validating Bitcoin consensus. + +It is structured as follows: +* `types` module contains all Bitcoin specific entities (start your codebase tour with this folder) adapted for recursive verification; +* `validation` module contains most of the consensus validation logic; +* `codec` module contains implementation of Bitcoin binary codec for transaction types. diff --git a/packages/consensus/Scarb.toml b/packages/consensus/Scarb.toml new file mode 100644 index 00000000..7ca44ee0 --- /dev/null +++ b/packages/consensus/Scarb.toml @@ -0,0 +1,14 @@ +[package] +name = "consensus" +version = "0.1.0" +edition = "2024_07" + +[dependencies] +utils = { path = "../utils" } + +[dev-dependencies] +cairo_test.workspace = true + +[scripts] +# TODO: cairo lint +lint = "scarb fmt" diff --git a/src/codec.cairo b/packages/consensus/src/codec.cairo similarity index 99% rename from src/codec.cairo rename to packages/consensus/src/codec.cairo index d967b12e..ac5820d4 100644 --- a/src/codec.cairo +++ b/packages/consensus/src/codec.cairo @@ -1,7 +1,7 @@ //! Bitcoin binary codec traits, implementations, and helpers. use super::types::transaction::{Transaction, TxIn, TxOut, OutPoint}; -use raito::utils::hash::Digest; +use utils::hash::Digest; pub trait Encode { /// Encode using Bitcoin codec and append to the buffer. @@ -133,8 +133,8 @@ pub fn encode_compact_size(len: usize, ref dest: ByteArray) { } #[cfg(test)] mod tests { - use raito::types::transaction::{Transaction, TxIn, TxOut, OutPoint}; - use raito::utils::hex::{from_hex, hex_to_hash_rev}; + use utils::hex::{from_hex, hex_to_hash_rev}; + use crate::types::transaction::{Transaction, TxIn, TxOut, OutPoint}; use super::{Encode, TransactionCodec, encode_compact_size}; #[test] diff --git a/packages/consensus/src/lib.cairo b/packages/consensus/src/lib.cairo new file mode 100644 index 00000000..238e5741 --- /dev/null +++ b/packages/consensus/src/lib.cairo @@ -0,0 +1,17 @@ +pub mod validation { + pub mod difficulty; + pub mod coinbase; + pub mod locktime; + pub mod timestamp; + pub mod transaction; + pub mod work; + pub mod block; +} +pub mod codec; +pub mod types { + pub mod utreexo; + pub mod chain_state; + pub mod block; + pub mod transaction; + pub mod utxo_set; +} diff --git a/src/types/block.cairo b/packages/consensus/src/types/block.cairo similarity index 96% rename from src/types/block.cairo rename to packages/consensus/src/types/block.cairo index a404b19e..d9c13502 100644 --- a/src/types/block.cairo +++ b/packages/consensus/src/types/block.cairo @@ -2,9 +2,9 @@ //! //! The data is expected to be prepared in advance and passed as program arguments. -use crate::utils::hash::Digest; -use crate::utils::sha256::double_sha256_u32_array; -use crate::utils::numeric::u32_byte_reverse; +use utils::hash::Digest; +use utils::sha256::double_sha256_u32_array; +use utils::numeric::u32_byte_reverse; use super::transaction::Transaction; /// Represents a block in the blockchain. @@ -79,8 +79,8 @@ pub impl TransactionDataDefault of Default { #[cfg(test)] mod tests { use super::{Header, BlockHash}; - use raito::types::chain_state::ChainState; - use raito::utils::hash::Digest; + use crate::types::chain_state::ChainState; + use utils::hash::Digest; #[test] fn test_block_hash() { diff --git a/src/types/chain_state.cairo b/packages/consensus/src/types/chain_state.cairo similarity index 99% rename from src/types/chain_state.cairo rename to packages/consensus/src/types/chain_state.cairo index f1a50a11..58eb337b 100644 --- a/src/types/chain_state.cairo +++ b/packages/consensus/src/types/chain_state.cairo @@ -4,7 +4,7 @@ //! Chain state alone is not enough to do full block validation, however //! it is sufficient to validate block headers. -use crate::utils::hash::Digest; +use utils::hash::Digest; use crate::validation::{ difficulty::{validate_bits, adjust_difficulty}, coinbase::validate_coinbase, timestamp::{validate_timestamp, next_prev_timestamps}, diff --git a/src/types/transaction.cairo b/packages/consensus/src/types/transaction.cairo similarity index 98% rename from src/types/transaction.cairo rename to packages/consensus/src/types/transaction.cairo index 41471ca0..19027361 100644 --- a/src/types/transaction.cairo +++ b/packages/consensus/src/types/transaction.cairo @@ -3,7 +3,7 @@ //! Types are extended with extra information required for validation. //! The data is expected to be prepared in advance and passed as program arguments. -use crate::utils::{hash::Digest, bytearray::{ByteArraySnapHash, ByteArraySnapSerde}}; +use utils::{hash::Digest, bytearray::{ByteArraySnapHash, ByteArraySnapSerde}}; /// Represents a transaction. /// https://learnmeabitcoin.com/technical/transaction/ @@ -134,7 +134,7 @@ mod tests { use core::hash::HashStateExTrait; use core::poseidon::PoseidonTrait; use super::{OutPoint, TxOut}; - use crate::utils::{hash::{DigestTrait}}; + use utils::hash::{DigestTrait}; #[test] pub fn test_outpoint_poseidon_hash() { diff --git a/src/types/utreexo.cairo b/packages/consensus/src/types/utreexo.cairo similarity index 100% rename from src/types/utreexo.cairo rename to packages/consensus/src/types/utreexo.cairo diff --git a/src/types/utxo_set.cairo b/packages/consensus/src/types/utxo_set.cairo similarity index 100% rename from src/types/utxo_set.cairo rename to packages/consensus/src/types/utxo_set.cairo diff --git a/src/validation/block.cairo b/packages/consensus/src/validation/block.cairo similarity index 96% rename from src/validation/block.cairo rename to packages/consensus/src/validation/block.cairo index 5c13888c..fb32443c 100644 --- a/src/validation/block.cairo +++ b/packages/consensus/src/validation/block.cairo @@ -1,7 +1,7 @@ //! Block validation helpers. use crate::types::transaction::{Transaction}; use crate::codec::{Encode, TransactionCodec}; -use crate::utils::{hash::Digest, merkle_tree::merkle_root, sha256::double_sha256_byte_array}; +use utils::{hash::Digest, merkle_tree::merkle_root, sha256::double_sha256_byte_array}; use super::transaction::validate_transaction; const MAX_BLOCK_WEIGHT_LEGACY: usize = 1_000_000; diff --git a/src/validation/coinbase.cairo b/packages/consensus/src/validation/coinbase.cairo similarity index 99% rename from src/validation/coinbase.cairo rename to packages/consensus/src/validation/coinbase.cairo index 53baa73c..e6777a89 100644 --- a/src/validation/coinbase.cairo +++ b/packages/consensus/src/validation/coinbase.cairo @@ -3,7 +3,7 @@ //! https://learnmeabitcoin.com/technical/mining/coinbase-transaction/ use crate::types::transaction::{Transaction, TxIn}; -use crate::utils::{bit_shifts::shr, hash::Digest}; +use utils::{bit_shifts::shr, hash::Digest}; const BIP_34_BLOCK_HEIGHT: u32 = 227_836; const BIP_141_BLOCK_HEIGHT: u32 = 481_824; @@ -108,7 +108,7 @@ fn compute_block_reward(block_height: u32) -> u64 { #[cfg(test)] mod tests { use crate::types::transaction::{TxIn, TxOut, Transaction, OutPoint}; - use crate::utils::hex::from_hex; + use utils::hex::from_hex; use super::{ compute_block_reward, validate_coinbase, validate_coinbase_input, validate_coinbase_sig_script, validate_coinbase_witness diff --git a/src/validation/difficulty.cairo b/packages/consensus/src/validation/difficulty.cairo similarity index 99% rename from src/validation/difficulty.cairo rename to packages/consensus/src/validation/difficulty.cairo index 3e894691..1f4bf558 100644 --- a/src/validation/difficulty.cairo +++ b/packages/consensus/src/validation/difficulty.cairo @@ -4,7 +4,7 @@ //! - https://learnmeabitcoin.com/technical/mining/target/ //! - https://learnmeabitcoin.com/technical/block/bits/ -use crate::utils::{bit_shifts::{shl, shr}}; +use utils::{bit_shifts::{shl, shr}}; /// Maximum difficulty target allowed const MAX_TARGET: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000; diff --git a/src/validation/locktime.cairo b/packages/consensus/src/validation/locktime.cairo similarity index 99% rename from src/validation/locktime.cairo rename to packages/consensus/src/validation/locktime.cairo index a12c5a60..d930702c 100644 --- a/src/validation/locktime.cairo +++ b/packages/consensus/src/validation/locktime.cairo @@ -116,7 +116,7 @@ pub fn validate_relative_locktime( #[cfg(test)] mod tests { use crate::types::transaction::{TxIn, OutPoint, TxOut}; - use crate::utils::hex::{from_hex, hex_to_hash_rev}; + use utils::hex::{from_hex, hex_to_hash_rev}; use super::{validate_absolute_locktime, validate_relative_locktime}; // TODO: tests for invalid relative locktime diff --git a/src/validation/timestamp.cairo b/packages/consensus/src/validation/timestamp.cairo similarity index 100% rename from src/validation/timestamp.cairo rename to packages/consensus/src/validation/timestamp.cairo diff --git a/src/validation/transaction.cairo b/packages/consensus/src/validation/transaction.cairo similarity index 99% rename from src/validation/transaction.cairo rename to packages/consensus/src/validation/transaction.cairo index e1c887d8..d21a13d4 100644 --- a/src/validation/transaction.cairo +++ b/packages/consensus/src/validation/transaction.cairo @@ -97,7 +97,7 @@ fn validate_coinbase_maturity(output_height: u32, block_height: u32) -> Result<( #[cfg(test)] mod tests { use crate::types::transaction::{Transaction, TxIn, TxOut, OutPoint}; - use crate::utils::hex::{from_hex, hex_to_hash_rev}; + use utils::hex::{from_hex, hex_to_hash_rev}; use super::validate_transaction; // TODO: tests for coinbase maturity diff --git a/src/validation/work.cairo b/packages/consensus/src/validation/work.cairo similarity index 99% rename from src/validation/work.cairo rename to packages/consensus/src/validation/work.cairo index c9b272d8..cfaeaf9c 100644 --- a/src/validation/work.cairo +++ b/packages/consensus/src/validation/work.cairo @@ -1,6 +1,6 @@ //! Proof-of-work validation helpers. -use crate::utils::hash::Digest; +use utils::hash::Digest; /// Check if the work done (by calculating the block hash) satisfies the difficulty target. pub fn validate_proof_of_work(target: u256, block_hash: Digest) -> Result<(), ByteArray> { diff --git a/packages/utils/README.md b/packages/utils/README.md new file mode 100644 index 00000000..8d47ad55 --- /dev/null +++ b/packages/utils/README.md @@ -0,0 +1,3 @@ +# Common utilities + +This package contains common helpers that are not Bitcoin-specific. diff --git a/packages/utils/Scarb.toml b/packages/utils/Scarb.toml new file mode 100644 index 00000000..81475f48 --- /dev/null +++ b/packages/utils/Scarb.toml @@ -0,0 +1,11 @@ +[package] +name = "utils" +version = "0.1.0" +edition = "2024_07" + +[dev-dependencies] +cairo_test.workspace = true + +[scripts] +# TODO: cairo lint +lint = "scarb fmt" diff --git a/src/utils/bit_shifts.cairo b/packages/utils/src/bit_shifts.cairo similarity index 100% rename from src/utils/bit_shifts.cairo rename to packages/utils/src/bit_shifts.cairo diff --git a/src/utils/bytearray.cairo b/packages/utils/src/bytearray.cairo similarity index 100% rename from src/utils/bytearray.cairo rename to packages/utils/src/bytearray.cairo diff --git a/src/utils/hash.cairo b/packages/utils/src/hash.cairo similarity index 99% rename from src/utils/hash.cairo rename to packages/utils/src/hash.cairo index ceed2390..bfb4eed4 100644 --- a/src/utils/hash.cairo +++ b/packages/utils/src/hash.cairo @@ -112,7 +112,7 @@ pub impl DigestHash, +Drop> of Hash { #[cfg(test)] mod tests { - use crate::utils::hex::from_hex; + use crate::hex::from_hex; use super::Digest; #[test] diff --git a/src/utils/hex.cairo b/packages/utils/src/hex.cairo similarity index 97% rename from src/utils/hex.cairo rename to packages/utils/src/hex.cairo index 52830eed..736a403c 100644 --- a/src/utils/hex.cairo +++ b/packages/utils/src/hex.cairo @@ -1,5 +1,5 @@ //! Hex helpers -use raito::utils::hash::Digest; +use crate::hash::Digest; /// Get bytes from hex (base16) pub fn from_hex(hex_string: ByteArray) -> ByteArray { @@ -86,7 +86,7 @@ fn hex_char_to_nibble(hex_char: u8) -> u8 { #[cfg(test)] mod tests { use super::{from_hex, to_hex, hex_to_hash_rev}; - use raito::utils::hash::Digest; + use crate::hash::Digest; #[test] fn test_bytes_from_hex() { diff --git a/packages/utils/src/lib.cairo b/packages/utils/src/lib.cairo new file mode 100644 index 00000000..e85c50e3 --- /dev/null +++ b/packages/utils/src/lib.cairo @@ -0,0 +1,9 @@ +pub mod bytearray; +pub mod sha256; +pub mod hash; +pub mod bit_shifts; +pub mod merkle_tree; +pub mod numeric; + +#[cfg(target: 'test')] +pub mod hex; diff --git a/src/utils/merkle_tree.cairo b/packages/utils/src/merkle_tree.cairo similarity index 99% rename from src/utils/merkle_tree.cairo rename to packages/utils/src/merkle_tree.cairo index 18c967dd..026dd4b7 100644 --- a/src/utils/merkle_tree.cairo +++ b/packages/utils/src/merkle_tree.cairo @@ -29,7 +29,7 @@ pub fn merkle_root(ref hashes: Array) -> Digest { #[cfg(test)] mod tests { - use crate::utils::{hash::{Digest, U256IntoDigest}, hex::hex_to_hash_rev}; + use crate::{hash::{Digest, U256IntoDigest}, hex::hex_to_hash_rev}; use super::{merkle_root}; #[test] diff --git a/src/utils/numeric.cairo b/packages/utils/src/numeric.cairo similarity index 100% rename from src/utils/numeric.cairo rename to packages/utils/src/numeric.cairo diff --git a/src/utils/sha256.cairo b/packages/utils/src/sha256.cairo similarity index 97% rename from src/utils/sha256.cairo rename to packages/utils/src/sha256.cairo index 57d83951..707c69af 100644 --- a/src/utils/sha256.cairo +++ b/packages/utils/src/sha256.cairo @@ -36,7 +36,7 @@ pub fn double_sha256_u32_array(words: Array) -> Digest { #[cfg(test)] mod tests { - use crate::utils::{hex::from_hex, hash::Digest}; + use crate::{hex::from_hex, hash::Digest}; use super::{double_sha256_byte_array, double_sha256_u32_array, double_sha256_parent}; #[test] diff --git a/scripts/data/client.sh b/scripts/data/client.sh index a8235da9..5b39d1a2 100644 --- a/scripts/data/client.sh +++ b/scripts/data/client.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash -set -e; -set -o pipefail; +#set -e; +#set -o pipefail; -base_dir=".raito" +base_dir=".client_cache" start=${1:-0} end=${2:-100} @@ -23,11 +23,11 @@ run_client() { batch_file=${base_dir}/${mode}_${initial_height}_${num_blocks}.json if [ ! -f "$batch_file" ]; then - python scripts/data/generate_data.py $mode $initial_height $num_blocks true $batch_file + python ../../scripts/data/generate_data.py $mode $initial_height $num_blocks true $batch_file fi - arguments=$(python scripts/data/format_args.py $batch_file) - output=$(scarb cairo-run --no-build --function test "$arguments") + arguments=$(python ../../scripts/data/format_args.py $batch_file) + output=$(scarb cairo-run --no-build --package client --function test "$arguments") if [[ "$output" == *"FAIL"* ]]; then echo " fail" echo $output diff --git a/scripts/data/integration_tests.sh b/scripts/data/integration_tests.sh index 9c247157..bd51c13d 100755 --- a/scripts/data/integration_tests.sh +++ b/scripts/data/integration_tests.sh @@ -21,15 +21,17 @@ if [ $# -gt 0 ]; then test_files="${args[@]}" fi +echo "running integration tests ..." + for test_file in $test_files; do if [ -f "$test_file" ]; then - echo -n "test e2e:$test_file ..." + echo -n "test $test_file ..." if [[ "$ignored" =~ "$test_file" ]]; then echo " ignored" num_ignored=$((num_ignored + 1)) else - arguments=$(python scripts/data/format_args.py ${test_file}) + arguments=$(python ../../scripts/data/format_args.py ${test_file}) output=$(scarb cairo-run --no-build --function test "$arguments") gas_spent=$(echo $output | grep -o 'gas_spent=[0-9]*' | sed 's/gas_spent=//') @@ -37,7 +39,7 @@ for test_file in $test_files; do echo -e "${RED} fail ${RESET}(gas usage est.: $gas_spent)" num_fail=$((num_fail + 1)) error=$(echo $output | grep -o "error='[^']*'" | sed "s/error=//") - failures+="\te2e:$test_file — Panicked with $error\n" + failures+="\t$test_file — Panicked with $error\n" elif [[ "$output" == *"OK"* ]]; then echo -e "${GREEN} ok ${RESET}(gas usage est.: $gas_spent)" num_ok=$((num_ok + 1)) @@ -45,7 +47,7 @@ for test_file in $test_files; do echo -e "${RED} fail ${RESET}(gas usage est.: 0)" num_fail=$((num_fail + 1)) error=$(echo "$output" | sed '1d') - failures+="\te2e:$test_file — $error\n" + failures+="\t$test_file — $error\n" fi fi fi diff --git a/scripts/data/regenerate_tests.sh b/scripts/data/regenerate_tests.sh index 10397a5c..fabc833e 100755 --- a/scripts/data/regenerate_tests.sh +++ b/scripts/data/regenerate_tests.sh @@ -9,6 +9,7 @@ then fi force=0 +data_dir="tests/data" if [[ "$1" == "--force" ]]; then force=1 @@ -38,15 +39,15 @@ full_test_cases=( 757738 # Block with witness (757739) ) -mkdir tests/data || true +mkdir $data_dir || true # Generate test file if it does not exist yet or if "force" flag is set generate_test() { local mode=$1 local height=$2 - test_file="tests/data/${mode}_${test_case}.json" + test_file="${data_dir}/${mode}_${test_case}.json" if [[ ! -f "$test_file" || $force -eq 1 ]]; then - python scripts/data/generate_data.py $mode $height 1 true $test_file + python ../../scripts/data/generate_data.py $mode $height 1 true $test_file fi } diff --git a/src/lib.cairo b/src/lib.cairo deleted file mode 100644 index 737bd8af..00000000 --- a/src/lib.cairo +++ /dev/null @@ -1,35 +0,0 @@ -pub mod utils { - pub mod bytearray; - pub mod sha256; - pub mod hash; - pub mod bit_shifts; - pub mod merkle_tree; - pub mod numeric; - - #[cfg(target: 'test')] - pub mod hex; -} -pub mod validation { - pub mod difficulty; - pub mod coinbase; - pub mod locktime; - pub mod timestamp; - pub mod transaction; - pub mod work; - pub mod block; -} -pub mod codec; -pub mod types { - pub mod utreexo; - pub mod chain_state; - pub mod block; - pub mod transaction; - pub mod utxo_set; -} - -mod main; - -// TODO: move this module to a separate package -// Scarb does not support features when using cairo-run -// neither it allows to run function from the "tests" folder -mod test;