From 098222c8984da912e12be30f044309c59162c19d Mon Sep 17 00:00:00 2001 From: TAdev0 Date: Sun, 4 Aug 2024 22:09:17 +0200 Subject: [PATCH] validate_timestamp --- src/validation.cairo | 47 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/validation.cairo b/src/validation.cairo index 478c8e71..055d117a 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -43,8 +43,15 @@ fn validate_target(self: @ChainState, block: @Block) -> Result<(), ByteArray> { } fn validate_timestamp(self: @ChainState, block: @Block) -> Result<(), ByteArray> { - // TODO: implement - Result::Ok(()) + let timestamp: u32 = *block.header.time; + let prev_timestamps = self.prev_timestamps; + let median_time = (*prev_timestamps).at((*prev_timestamps).len() - 6); + + if timestamp > *median_time { + Result::Ok(()) + } else { + Result::Err("Median time is greater than block's timestamp") + } } fn next_prev_timestamps(self: @ChainState, block: @Block) -> Span { @@ -69,11 +76,9 @@ fn validate_merkle_root(self: @ChainState, block: @Block) -> Result<(), ByteArra #[cfg(test)] mod tests { - use core::result::ResultTrait; - use super::validate_target; + use super::{validate_target, validate_timestamp}; use super::{Block, ChainState}; use super::super::state::{Header, Transaction, TxIn, TxOut}; - use core::array::{Span}; #[test] fn test_validate_target() { @@ -105,4 +110,36 @@ mod tests { let result = validate_target(@chain_state, @block); assert(result.is_err(), 'Expected target to be invalid'); } + + #[test] + fn test_validate_timestamp() { + let mut chain_state = ChainState { + block_height: 1, + total_work: 1, + best_block_hash: 1, + current_target: 1, + epoch_start_time: 1, + prev_timestamps: array![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].span(), + }; + let mut block = Block { + header: Header { + version: 1, prev_block_hash: 1, merkle_root_hash: 1, time: 12, bits: 1, nonce: 1, + }, + txs: ArrayTrait::new().span(), + }; + + // new timestamp is greater than the last timestamp + let result = validate_timestamp(@chain_state, @block); + assert(result.is_ok(), 'Expected target to be valid'); + + // new timestamp is strictly greater than the median of the last 11 timestamps + block.header.time = 7; + let result = validate_timestamp(@chain_state, @block); + assert(result.is_ok(), 'Expected target to be valid'); + + // new timestamp is equal to the median of the last 11 timestamps + block.header.time = 6; + let result = validate_timestamp(@chain_state, @block); + assert!(result.is_err(), "Median time is greater than block's timestamp"); + } }