diff --git a/src/merkle_tree.cairo b/src/merkle_tree.cairo index ca22fc78..c922e916 100644 --- a/src/merkle_tree.cairo +++ b/src/merkle_tree.cairo @@ -21,7 +21,6 @@ pub fn merkle_root(ref txids: Array) -> u256 { merkle_root(ref next_txids) } - #[cfg(test)] mod tests { use super::{merkle_root}; @@ -29,14 +28,14 @@ mod tests { #[test] fn test_merkle_root() { - - let txids = array![0x50ba87bdd484f07c8c55f76a22982f987c0465fdc345381b4634a70dc0ea0b38_u256, + let txids = array![ + 0x50ba87bdd484f07c8c55f76a22982f987c0465fdc345381b4634a70dc0ea0b38_u256, 0x96b8787b1e3abed802cff132c891c2e511edd200b08baa9eb7d8942d7c5423c6_u256, 0x65e5a4862b807c83b588e0f4122d4ca2d46691d17a1ec1ebce4485dccc3380d4_u256, 0x1ee9441ddde02f8ffb910613cd509adbc21282c6e34728599f3ae75e972fb815_u256, 0xec950fc02f71fc06ed71afa4d2c49fcba04777f353a001b0bba9924c63cfe712_u256, 0x5d874040a77de7182f7a68bf47c02898f519cb3b58092b79fa2cff614a0f4d50_u256, - 0x0a1c958af3e30ad07f659f44f708f8648452d1427463637b9039e5b721699615_256, + 0x0a1c958af3e30ad07f659f44f708f8648452d1427463637b9039e5b721699615_u256, 0xd94d24d2dcaac111f5f638983122b0e55a91aeb999e0e4d58e0952fa346a1711_u256, 0xc4709bc9f860e5dff01b5fc7b53fb9deecc622214aba710d495bccc7f860af4a_u256, 0xd4ed5f5e4334c0a4ccce6f706f3c9139ac0f6d2af3343ad3fae5a02fee8df542_u256, @@ -44,10 +43,8 @@ mod tests { 0xf9a14bf211c857f61ff9a1de95fc902faebff67c5d4898da8f48c9d306f1f80f_u256 ]; - let expected_merkle_root = "0x50ba87bdd484f07c8c55f76a22982f987c0465fdc345381b4634a70dc0ea0b38_u256"; + let expected_merkle_root = 0x50ba87bdd484f07c8c55f76a22982f987c0465fdc345381b4634a70dc0ea0b38_u256; assert_eq!(merkle_root(txids), expected_merkle_root); } } - - diff --git a/src/validation.cairo b/src/validation.cairo index 177eae3b..477c0606 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -1,5 +1,5 @@ use super::merkle_tree::merkle_root; -use super::utils::{shl, shr, double_sha256}; +use super::utils::{shl, shr}; use super::state::{Block, ChainState, Transaction, UtreexoState}; const MAX_TARGET: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000; @@ -102,6 +102,40 @@ fn adjust_difficulty(self: @ChainState, block: @Block) -> (u32, u32) { (*self.current_target, *self.epoch_start_time) } +// Helper functions +pub fn bits_to_target(bits: u32) -> Result { + // Extract exponent and mantissa + let exponent: u32 = (bits / 0x1000000); + let mantissa: u32 = bits & 0x00FFFFFF; + + // Check if mantissa is valid (should be less than 0x1000000) + if mantissa > 0x7FFFFF && exponent != 0 { + return Result::Err('Invalid mantissa'); + } + + // Calculate the full target value + let mut target: u256 = mantissa.into(); + + if exponent == 0 { + // Special case: exponent 0 means we use the mantissa as-is + return Result::Ok(target); + } else if exponent <= 3 { + // For exponents 1, 2, and 3, divide by 256^(3 - exponent) i.e right shift + let shift = 8 * (3 - exponent); + target = shr(target, shift); + } else { + let shift = 8 * (exponent - 3); + target = shl(target, shift); + } + + // Ensure the target doesn't exceed the maximum allowed value + if target > MAX_TARGET { + return Result::Err('Target exceeds maximum'); + } + + Result::Ok(target) +} + pub fn target_to_bits(target: u256) -> Result { if target == 0 { return Result::Err('Target is zero');