-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: past blocks median time based header timestamp verification
- Loading branch information
Showing
12 changed files
with
134 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ fnv = "1.0.3" | |
env_logger = "0.6" | ||
tempfile = "3.0" | ||
rand = "0.6" | ||
faketime = "0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod shared; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use crate::{ | ||
shared::{ChainProvider, Shared, SharedBuilder}, | ||
store::{ChainKVStore, ChainStore}, | ||
}; | ||
use ckb_core::{block::BlockBuilder, header::HeaderBuilder}; | ||
use ckb_db::{kvdb::KeyValueDB, memorydb::MemoryKeyValueDB}; | ||
use numext_fixed_hash::H256; | ||
|
||
fn new_shared() -> Shared<ChainKVStore<MemoryKeyValueDB>> { | ||
SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_memory().build() | ||
} | ||
|
||
fn insert_block_timestamps<T>(store: &ChainKVStore<T>, timestamps: &[u64]) -> Vec<H256> | ||
where | ||
T: KeyValueDB + 'static, | ||
{ | ||
let mut blocks = Vec::with_capacity(timestamps.len()); | ||
let mut hashes = Vec::with_capacity(timestamps.len()); | ||
let mut parent_hash = H256::zero(); | ||
for timestamp in timestamps { | ||
let header = HeaderBuilder::default() | ||
.timestamp(*timestamp) | ||
.parent_hash(parent_hash.clone()) | ||
.build(); | ||
parent_hash = header.hash(); | ||
hashes.push(parent_hash.clone()); | ||
blocks.push(BlockBuilder::default().header(header).build()); | ||
} | ||
store | ||
.save_with_batch(|batch| { | ||
for b in blocks { | ||
store.insert_block(batch, &b); | ||
} | ||
Ok(()) | ||
}) | ||
.expect("insert blocks"); | ||
hashes | ||
} | ||
|
||
#[test] | ||
fn test_block_median_time() { | ||
let shared = new_shared(); | ||
assert!(shared.block_median_time(&H256::zero()).is_none()); | ||
let now = faketime::unix_time_as_millis(); | ||
let block_hashes = insert_block_timestamps(shared.store(), &vec![now]); | ||
assert_eq!( | ||
shared | ||
.block_median_time(&block_hashes[0]) | ||
.expect("median time"), | ||
now | ||
); | ||
let block_hashes = insert_block_timestamps(shared.store(), &(0..=22).collect::<Vec<_>>()); | ||
assert_eq!( | ||
shared | ||
.block_median_time(&block_hashes.last().expect("last")) | ||
.expect("median time"), | ||
17 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters