Skip to content

Commit

Permalink
validate_timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
TAdev0 committed Aug 4, 2024
1 parent 1221865 commit 098222c
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/validation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> {
Expand All @@ -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() {
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 098222c

Please sign in to comment.