Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] add next_prev_timestamps function implementation #55

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/validation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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();
maciejka marked this conversation as resolved.
Show resolved Hide resolved
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 {
Expand Down Expand Up @@ -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
};


Expand Down Expand Up @@ -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');
maciejka marked this conversation as resolved.
Show resolved Hide resolved
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');
}
}