From 12e4ee99676067d91fcaf2e68e67d29d64b5c611 Mon Sep 17 00:00:00 2001 From: olufemi-olumaiyegun <femimayegun@gmail.com> Date: Sat, 10 Aug 2024 15:07:25 -0500 Subject: [PATCH 1/5] add next_prev_timestamps function implementation --- src/validation.cairo | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/validation.cairo b/src/validation.cairo index b366c059..a900e6a5 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -73,8 +73,18 @@ fn validate_timestamp(self: @ChainState, block: @Block) -> Result<(), ByteArray> } fn next_prev_timestamps(self: @ChainState, block: @Block) -> Span<u32> { - // TODO: implement - *self.prev_timestamps + let mut timestamps = ArrayTrait::<u32>::new(); + timestamps.append(*block.header.time); + //copy the 10 most recent previous timestamps from the chain + let mut prev_timestamps = *self.prev_timestamps; + let mut index = 1; + loop { + if index > 10 { + break timestamps.span(); + } + timestamps.append(*prev_timestamps.at(index)); + index += 1; + } } fn compute_total_work(current_total_work: u256, target: u256) -> u256 { @@ -209,7 +219,7 @@ mod tests { use raito::state::{Header, Transaction, TxIn, TxOut}; use super::{ validate_timestamp, validate_proof_of_work, compute_block_reward, compute_total_work, - compute_work_from_target, shr, shl, Block, ChainState, UtreexoState, + compute_work_from_target, shr, shl, Block, ChainState, UtreexoState, next_prev_timestamps }; @@ -350,4 +360,26 @@ mod tests { let last_reward = compute_block_reward(max_halvings * block_height); assert_eq!(last_reward, 0); } + + #[test] + fn test_next_prev_timstamps() { + let chain_state = ChainState { + block_height: 1, + total_work: 1, + best_block_hash: 1, + current_target: 1, + epoch_start_time: 1, + prev_timestamps: array![0,1,2,3,4,5,6,7,8,9,10].span(), + utreexo_state: UtreexoState { roots: array![].span() }, + }; + let block = Block { + header: Header { version: 1, time: 12, nonce: 1, bits: 1 }, + txs: ArrayTrait::new().span(), + }; + let next_prev_timestamps = next_prev_timestamps(@chain_state, @block); + assert (*next_prev_timestamps.at(0) == 12, 'Failed to compute'); + assert (*next_prev_timestamps.at(6) == 6, 'Failed to compute'); + assert (*next_prev_timestamps.at(8) == 8, 'Failed to compute'); + assert (*next_prev_timestamps.at(9) == 9, 'Failed to compute'); + } } From c3b84d3fbf5c265497a19ecc8885036a1c4f9052 Mon Sep 17 00:00:00 2001 From: olufemi-olumaiyegun <femimayegun@gmail.com> Date: Sat, 10 Aug 2024 15:10:52 -0500 Subject: [PATCH 2/5] scarb fmt --- src/validation.cairo | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/validation.cairo b/src/validation.cairo index a900e6a5..bb313edf 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -79,11 +79,11 @@ fn next_prev_timestamps(self: @ChainState, block: @Block) -> Span<u32> { let mut prev_timestamps = *self.prev_timestamps; let mut index = 1; loop { - if index > 10 { - break timestamps.span(); - } - timestamps.append(*prev_timestamps.at(index)); - index += 1; + if index > 10 { + break timestamps.span(); + } + timestamps.append(*prev_timestamps.at(index)); + index += 1; } } @@ -361,7 +361,7 @@ mod tests { assert_eq!(last_reward, 0); } - #[test] + #[test] fn test_next_prev_timstamps() { let chain_state = ChainState { block_height: 1, @@ -369,7 +369,7 @@ mod tests { best_block_hash: 1, current_target: 1, epoch_start_time: 1, - prev_timestamps: array![0,1,2,3,4,5,6,7,8,9,10].span(), + prev_timestamps: array![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].span(), utreexo_state: UtreexoState { roots: array![].span() }, }; let block = Block { @@ -377,9 +377,9 @@ mod tests { txs: ArrayTrait::new().span(), }; let next_prev_timestamps = next_prev_timestamps(@chain_state, @block); - assert (*next_prev_timestamps.at(0) == 12, 'Failed to compute'); - assert (*next_prev_timestamps.at(6) == 6, 'Failed to compute'); - assert (*next_prev_timestamps.at(8) == 8, 'Failed to compute'); - assert (*next_prev_timestamps.at(9) == 9, 'Failed to compute'); + assert(*next_prev_timestamps.at(0) == 12, 'Failed to compute'); + assert(*next_prev_timestamps.at(6) == 6, 'Failed to compute'); + assert(*next_prev_timestamps.at(8) == 8, 'Failed to compute'); + assert(*next_prev_timestamps.at(9) == 9, 'Failed to compute'); } } From 865c65adf33d4c22e5e88a81e01624fab8172af0 Mon Sep 17 00:00:00 2001 From: olufemi-olumaiyegun <femimayegun@gmail.com> Date: Mon, 12 Aug 2024 15:56:50 -0500 Subject: [PATCH 3/5] addressing review comments --- src/validation.cairo | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/validation.cairo b/src/validation.cairo index bb313edf..c7239817 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -73,18 +73,12 @@ fn validate_timestamp(self: @ChainState, block: @Block) -> Result<(), ByteArray> } fn next_prev_timestamps(self: @ChainState, block: @Block) -> Span<u32> { - let mut timestamps = ArrayTrait::<u32>::new(); + let mut timestamps: Array<u32> = ArrayTrait::new(); timestamps.append(*block.header.time); - //copy the 10 most recent previous timestamps from the chain let mut prev_timestamps = *self.prev_timestamps; - let mut index = 1; - loop { - if index > 10 { - break timestamps.span(); - } - timestamps.append(*prev_timestamps.at(index)); - index += 1; - } + prev_timestamps.pop_front().unwrap(); + timestamps.append_span(prev_timestamps); + timestamps.span() } fn compute_total_work(current_total_work: u256, target: u256) -> u256 { @@ -361,7 +355,7 @@ mod tests { assert_eq!(last_reward, 0); } - #[test] + #[test] fn test_next_prev_timstamps() { let chain_state = ChainState { block_height: 1, @@ -369,7 +363,7 @@ mod tests { best_block_hash: 1, current_target: 1, epoch_start_time: 1, - prev_timestamps: array![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].span(), + prev_timestamps: array![0,1,2,3,4,5,6,7,8,9,10].span(), utreexo_state: UtreexoState { roots: array![].span() }, }; let block = Block { @@ -377,9 +371,9 @@ mod tests { txs: ArrayTrait::new().span(), }; let next_prev_timestamps = next_prev_timestamps(@chain_state, @block); - assert(*next_prev_timestamps.at(0) == 12, 'Failed to compute'); - assert(*next_prev_timestamps.at(6) == 6, 'Failed to compute'); - assert(*next_prev_timestamps.at(8) == 8, 'Failed to compute'); - assert(*next_prev_timestamps.at(9) == 9, 'Failed to compute'); + assert (*next_prev_timestamps[0] == 12, 'Failed to compute'); + assert (*next_prev_timestamps[6] == 6, 'Failed to compute'); + assert (*next_prev_timestamps[8] == 8, 'Failed to compute'); + assert (*next_prev_timestamps[9] == 9, 'Failed to compute'); } } From b9534479d7ff37e90dc27706e0cc580ef6f45cb1 Mon Sep 17 00:00:00 2001 From: olufemi-olumaiyegun <femimayegun@gmail.com> Date: Mon, 12 Aug 2024 16:03:51 -0500 Subject: [PATCH 4/5] addressing review comments --- src/validation.cairo | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/validation.cairo b/src/validation.cairo index c7239817..c762c240 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -73,10 +73,9 @@ fn validate_timestamp(self: @ChainState, block: @Block) -> Result<(), ByteArray> } fn next_prev_timestamps(self: @ChainState, block: @Block) -> Span<u32> { - let mut timestamps: Array<u32> = ArrayTrait::new(); - timestamps.append(*block.header.time); let mut prev_timestamps = *self.prev_timestamps; - prev_timestamps.pop_front().unwrap(); + prev_timestamps.pop_front().unwrap(); //keep only 10 most recent previous timestamps + let mut timestamps: Array<u32> = array![*block.header.time]; timestamps.append_span(prev_timestamps); timestamps.span() } From 43acde573dd1f42cd796ac67ed8b466fea7eea46 Mon Sep 17 00:00:00 2001 From: olufemi-olumaiyegun <femimayegun@gmail.com> Date: Mon, 12 Aug 2024 16:11:36 -0500 Subject: [PATCH 5/5] scarb fmt --- src/validation.cairo | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/validation.cairo b/src/validation.cairo index c762c240..d224c566 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -354,7 +354,7 @@ mod tests { assert_eq!(last_reward, 0); } - #[test] + #[test] fn test_next_prev_timstamps() { let chain_state = ChainState { block_height: 1, @@ -362,7 +362,7 @@ mod tests { best_block_hash: 1, current_target: 1, epoch_start_time: 1, - prev_timestamps: array![0,1,2,3,4,5,6,7,8,9,10].span(), + prev_timestamps: array![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].span(), utreexo_state: UtreexoState { roots: array![].span() }, }; let block = Block { @@ -370,9 +370,9 @@ mod tests { txs: ArrayTrait::new().span(), }; let next_prev_timestamps = next_prev_timestamps(@chain_state, @block); - assert (*next_prev_timestamps[0] == 12, 'Failed to compute'); - assert (*next_prev_timestamps[6] == 6, 'Failed to compute'); - assert (*next_prev_timestamps[8] == 8, 'Failed to compute'); - assert (*next_prev_timestamps[9] == 9, 'Failed to compute'); + assert(*next_prev_timestamps[0] == 12, 'Failed to compute'); + assert(*next_prev_timestamps[6] == 6, 'Failed to compute'); + assert(*next_prev_timestamps[8] == 8, 'Failed to compute'); + assert(*next_prev_timestamps[9] == 9, 'Failed to compute'); } }