Skip to content

Commit

Permalink
feat: use snforge for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Oct 17, 2024
1 parent 6a3cac1 commit 5731685
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 71 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ jobs:
run: |
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh -s -- -v $SCARB_VERSION
- name: Install Starknet Foundry
run: |
curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh
export PATH="$PATH:$HOME/.local/bin"
snfoundryup
- name: Install project dependencies
run: scarb fetch

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
Scarb.lock
Scarb.lock
.snfoundry_cache
7 changes: 4 additions & 3 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ name = "utu_relay"
version = "0.1.0"
edition = "2024_07"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
starknet = "2.8.4"

[dev-dependencies]
cairo_test = "2.8.4"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.31.0" }

[scripts]
test = "snforge test"

[[target.starknet-contract]]
casm = true
Expand Down
44 changes: 20 additions & 24 deletions src/bitcoin/block.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mod tests {
"00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee"
);

assert_eq!(computed_hash, expected_hash, "Computed hash does not match expected hash");
assert(computed_hash == expected_hash, 'Block hash mismatch');
}


Expand All @@ -197,7 +197,7 @@ mod tests {
let header: BlockHeader = human_readable_header.into();
let pow = header.compute_pow();
// This is an estimation of the amount of hashes to compute a valid block hash
assert_eq!(pow, 4_295_032_833);
assert(pow == 4_295_032_833, 'Incorrect PoW computation');
}


Expand All @@ -221,7 +221,7 @@ mod tests {
let target = header.compute_target_threshold();
let pow = compute_pow_from_target(target);
// This is an estimation of the amount of hashes to compute a valid block hash
assert_eq!(pow, 4_295_032_833);
assert(pow == 4_295_032_833, 'Incorrect PoW from target');
}

// compute_target_threshold tests
Expand All @@ -230,78 +230,74 @@ mod tests {
fn test_target_threshold_01003456() {
let header = BlockHeader { bits: 0x01003456, ..Default::default() };
let result = header.compute_target_threshold();
assert_eq!(result, 0x00_u256, "Incorrect target for 0x01003456");
assert(result == 0x00_u256, 'Wrong target for 0x01003456');
}

#[test]
fn test_target_threshold_01123456() {
let header = BlockHeader { bits: 0x01123456, ..Default::default() };
let result = header.compute_target_threshold();
assert_eq!(result, 0x12_u256, "Incorrect target for 0x01123456");
assert(result == 0x12_u256, 'Wrong target for 0x01123456');
}

#[test]
fn test_target_threshold_02008000() {
let header = BlockHeader { bits: 0x02008000, ..Default::default() };
let result = header.compute_target_threshold();
assert_eq!(result, 0x80_u256, "Incorrect target for 0x02008000");
assert(result == 0x80_u256, 'Wrong target for 0x02008000');
}

#[test]
fn test_target_threshold_181bc330() {
let header = BlockHeader { bits: 0x181bc330, ..Default::default() };
let result = header.compute_target_threshold();
assert_eq!(
result,
0x1bc330000000000000000000000000000000000000000000_u256,
"Incorrect target for 0x181bc330"
assert(
result == 0x1bc330000000000000000000000000000000000000000000_u256,
'Wrong target for 0x181bc330'
);
}

#[test]
fn test_target_threshold_05009234() {
let header = BlockHeader { bits: 0x05009234, ..Default::default() };
let result = header.compute_target_threshold();
assert_eq!(result, 0x92340000_u256, "Incorrect target for 0x05009234");
assert(result == 0x92340000_u256, 'Wrong target for 0x05009234');
}

#[test]
fn test_target_threshold_04123456() {
let header = BlockHeader { bits: 0x04123456, ..Default::default() };
let result = header.compute_target_threshold();
assert_eq!(result, 0x12345600_u256, "Incorrect target for 0x04123456");
assert(result == 0x12345600_u256, 'Wrong target for 0x04123456');
}

#[test]
fn test_target_threshold_1d00ffff() {
let header = BlockHeader { bits: 0x1d00ffff, ..Default::default() };
let result = header.compute_target_threshold();
assert_eq!(
result,
0x00000000ffff0000000000000000000000000000000000000000000000000000_u256,
"Incorrect target for 0x1d00ffff"
assert(
result == 0x00000000ffff0000000000000000000000000000000000000000000000000000_u256,
'Wrong target for 0x1d00ffff'
);
}

#[test]
fn test_target_threshold_1c0d3142() {
let header = BlockHeader { bits: 0x1c0d3142, ..Default::default() };
let result = header.compute_target_threshold();
assert_eq!(
result,
0x000000000d314200000000000000000000000000000000000000000000000000_u256,
"Incorrect target for 0x1c0d3142"
assert(
result == 0x000000000d314200000000000000000000000000000000000000000000000000_u256,
'Wrong target for 0x1c0d3142'
);
}

#[test]
fn test_target_threshold_1707a429() {
let header = BlockHeader { bits: 0x1707a429, ..Default::default() };
let result = header.compute_target_threshold();
assert_eq!(
result,
0x00000000000000000007a4290000000000000000000000000000000000000000_u256,
"Incorrect target for 0x1707a429"
assert(
result == 0x00000000000000000007a4290000000000000000000000000000000000000000_u256,
'Wrong target for 0x1707a429'
);
}

Expand Down
34 changes: 34 additions & 0 deletions src/tests/blocks_registration.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::{
utils::hex::hex_to_hash_rev,
bitcoin::block::{BlockHeader, BlockHashTrait, HumanReadableBlockHeader}
};

#[test]
fn test_single_block_registration() {
// Create a HumanReadablBlockHeader from block 170
let human_readable_header = HumanReadableBlockHeader {
version: 1_u32,
prev_block_hash: hex_to_hash_rev(
"000000002a22cfee1f2c846adbd12b3e183d4f97683f85dad08a79780a84bd55"
),
merkle_root_hash: hex_to_hash_rev(
"7dac2c5666815c17a3b36427de37bb9d2e2c5ccec3f8633eb91a4205cb4c10ff"
),
time: 1231731025_u32,
bits: 0x1d00ffff_u32,
nonce: 1889418792,
};

// Convert to full little endian BlockHeader
let header: BlockHeader = human_readable_header.into();

// Compute the hash
let computed_hash = header.hash();

// Expected hash (reversed because we're working with big-endian)
let expected_hash = hex_to_hash_rev(
"00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee"
);

assert(computed_hash == expected_hash, 'Block hash mismatch');
}
33 changes: 21 additions & 12 deletions src/utils/double_sha256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,37 @@ mod tests {
#[test]
fn test_double_sha256_byte_array() {
// hashlib.sha256(sha256(b"bitcoin").digest()).hexdigest()
assert_eq!(
double_sha256_byte_array(@"bitcoin").into(),
from_hex("f1ef1bf105d788352c052453b15a913403be59b90ddf9f7c1f937edee8938dc5")
)
assert(
double_sha256_byte_array(@"bitcoin")
.into() == from_hex(
"f1ef1bf105d788352c052453b15a913403be59b90ddf9f7c1f937edee8938dc5"
),
'double_sha256(bitcoin) mismatch'
);
}

#[test]
fn test_double_sha256_u32_array() {
// hashlib.sha256(sha256(bytes.fromhex("00000001000000020000000300000004000000050000000600000007")).digest()).hexdigest()
assert_eq!(
double_sha256_u32_array(array![1, 2, 3, 4, 5, 6, 7]).into(),
from_hex("489b8eeb4024cb77ab057616ebf7f8d4405aa0bd3ad5f42e6b4c20580e011ac4")
)
assert(
double_sha256_u32_array(array![1, 2, 3, 4, 5, 6, 7])
.into() == from_hex(
"489b8eeb4024cb77ab057616ebf7f8d4405aa0bd3ad5f42e6b4c20580e011ac4"
),
'double_sha256(u32 arr) mismatch'
);
}

#[test]
fn test_double_sha256_parent() {
// hashlib.sha256(sha256(bytes.fromhex("00000001" * 8 + "00000002" *
// 8)).digest()).hexdigest()
assert_eq!(
double_sha256_parent(@Digest { value: [1; 8] }, @Digest { value: [2; 8] }).into(),
from_hex("14a6e4a4caef969126944266724d11866b39b3390cee070b0aa4c9390cd77f47")
)
assert(
double_sha256_parent(@Digest { value: [1; 8] }, @Digest { value: [2; 8] })
.into() == from_hex(
"14a6e4a4caef969126944266724d11866b39b3390cee070b0aa4c9390cd77f47"
),
'double_sha256_parent mismatch'
);
}
}
11 changes: 5 additions & 6 deletions src/utils/hash.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ mod tests {
],
};

assert_eq!(result_hash, expected_hash, "invalid results");
assert(result_hash == expected_hash, 'u256 to hash conversion failed');
}

#[test]
Expand All @@ -180,10 +180,9 @@ mod tests {
low: 0x2143658709badcfe2143658709badcfe_u128,
};

assert_eq!(result_u256, expected_u256, "invalid results");
assert(result_u256 == expected_u256, 'hash to u256 conversion failed');
}


#[test]
fn test_hash_to_u256_to_hash() {
let hash_value = Digest {
Expand All @@ -202,7 +201,7 @@ mod tests {
let u256_value: u256 = hash_value.into();
let result_hash: Digest = u256_value.into();

assert_eq!(result_hash, hash_value, "invalid results");
assert(result_hash == hash_value, 'hash->u256->hash failed');
}

#[test]
Expand All @@ -215,7 +214,7 @@ mod tests {
let hash_value: Digest = u256_value.into();
let result_u256: u256 = hash_value.into();

assert_eq!(result_u256, u256_value, "invalid results");
assert(result_u256 == u256_value, 'u256->hash->u256 failed');
}

#[test]
Expand All @@ -239,6 +238,6 @@ mod tests {
"123456789abcdef01122334455667788aabbccddeeff00112233445566778899"
);

assert_eq!(byte_array, expected_byte_array, "invalid results");
assert(byte_array == expected_byte_array, 'hash to ByteArray failed');
}
}
12 changes: 8 additions & 4 deletions src/utils/hex.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,17 @@ mod tests {

#[test]
fn test_bytes_from_hex() {
assert_eq!(@"hello starknet", @from_hex("68656c6c6f20737461726b6e6574"));
assert_eq!(@"hello starknet", @from_hex("68656C6C6F20737461726B6E6574"));
assert(
@"hello starknet" == @from_hex("68656c6c6f20737461726b6e6574"), 'from_hex lowercase'
);
assert(
@"hello starknet" == @from_hex("68656C6C6F20737461726B6E6574"), 'from_hex mixed case'
);
}

#[test]
fn test_bytes_to_hex() {
assert_eq!(@"68656c6c6f20737461726b6e6574", @to_hex(@"hello starknet"));
assert(@"68656c6c6f20737461726b6e6574" == @to_hex(@"hello starknet"), 'to_hex conversion');
}

#[test]
Expand All @@ -116,6 +120,6 @@ mod tests {
]
};
let hex = "99887766554433221100ffeeddccbbaa8877665544332211f0debc9a78563412";
assert_eq!(hash, hex_to_hash_rev(hex));
assert(hash == hex_to_hash_rev(hex), 'hex_to_hash_rev conversion');
}
}
10 changes: 5 additions & 5 deletions src/utils/numeric.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ mod tests {
let input: u32 = 0x12345678;
let expected_output: u32 = 0x78563412;
let result = u32_byte_reverse(input);
assert_eq!(result, expected_output);
assert(result == expected_output, '0x12345678 reverse failed');

let input: u32 = 0xAABBCCDD;
let expected_output: u32 = 0xDDCCBBAA;
let result = u32_byte_reverse(input);
assert_eq!(result, expected_output);
assert(result == expected_output, '0xAABBCCDD reverse failed');

let input: u32 = 0x000000FF;
let expected_output: u32 = 0xFF000000;
let result = u32_byte_reverse(input);
assert_eq!(result, expected_output);
assert(result == expected_output, '0x000000FF reverse failed');

let input: u32 = 0x00FF00FF;
let expected_output: u32 = 0xFF00FF00;
let result = u32_byte_reverse(input);
assert_eq!(result, expected_output);
assert(result == expected_output, '0x00FF00FF reverse failed');

let input: u32 = 0x00000000;
let expected_output: u32 = 0x00000000;
let result = u32_byte_reverse(input);
assert_eq!(result, expected_output);
assert(result == expected_output, '0x00000000 reverse failed');
}
}
27 changes: 11 additions & 16 deletions src/utils/pow2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -162,27 +162,22 @@ mod tests {

#[test]
fn test_fast_pow2_u128() {
assert_eq!(pow2_u128(0), 1);
assert_eq!(pow2_u128(18), 262144);
assert_eq!(pow2_u128(127), 170141183460469231731687303715884105728);
assert(pow2_u128(0) == 1, '2^0 should be 1');
assert(pow2_u128(18) == 262144, '2^18 should be 262144');
assert(pow2_u128(127) == 170141183460469231731687303715884105728, '2^127 correct');
}


#[test]
#[available_gas(1000000000)]
fn test_fast_pow2_u256() {
assert_eq!(pow2_u256(0), 1, "2^0 should be 1");
assert_eq!(pow2_u256(1), 2, "2^1 should be 2");
assert_eq!(
pow2_u256(128),
340282366920938463463374607431768211456,
"2^128 should be 340282366920938463463374607431768211456"
);
assert_eq!(
pow2_u256(255),
57896044618658097711785492504343953926634992332820282019728792003956564819968,
"2^255 should be 57896044618658097711785492504343953926634992332820282019728792003956564819968"
assert(pow2_u256(0) == 1, '2^0 should be 1');
assert(pow2_u256(1) == 2, '2^1 should be 2');
assert(pow2_u256(128) == 340282366920938463463374607431768211456, '2^128 correct');
assert(
pow2_u256(
255
) == 57896044618658097711785492504343953926634992332820282019728792003956564819968,
'2^255 correct'
);
}
}

0 comments on commit 5731685

Please sign in to comment.