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(standalone): Function to get latest/earliest block #482

Merged
merged 1 commit into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions engine-standalone-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ impl Storage {
})
}

pub fn get_latest_block(&self) -> Result<(H256, u64), error::Error> {
self.block_read(rocksdb::IteratorMode::End)
}

pub fn get_earliest_block(&self) -> Result<(H256, u64), error::Error> {
self.block_read(rocksdb::IteratorMode::Start)
}

fn block_read(&self, mode: rocksdb::IteratorMode) -> Result<(H256, u64), error::Error> {
let upper_bound = construct_storage_key(StoragePrefix::BlockHash, &u64::MAX.to_be_bytes());
let lower_bound = construct_storage_key(StoragePrefix::BlockHash, &[]);
let prefix_len = lower_bound.len();
let mut opt = rocksdb::ReadOptions::default();
opt.set_iterate_upper_bound(upper_bound);
opt.set_iterate_lower_bound(lower_bound);

let mut iter = self.db.iterator_opt(mode, opt);
iter.next()
.map(|(key, value)| {
let block_height = {
let mut buf = [0u8; 8];
buf.copy_from_slice(&key[prefix_len..]);
u64::from_be_bytes(buf)
};
let block_hash = H256::from_slice(&value);
(block_hash, block_height)
})
.ok_or(error::Error::NoBlockAtHeight(0))
}

pub fn get_block_hash_by_height(&self, block_height: u64) -> Result<H256, error::Error> {
let storage_key =
construct_storage_key(StoragePrefix::BlockHash, &block_height.to_be_bytes());
Expand Down
42 changes: 42 additions & 0 deletions engine-tests/src/tests/standalone/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ fn test_block_index() {
block_metadata,
storage.get_block_metadata(block_hash).unwrap()
);
assert_eq!(
(block_hash, block_height),
storage.get_latest_block().unwrap(),
);
assert_eq!(
(block_hash, block_height),
storage.get_earliest_block().unwrap(),
);

// block hash / height that do not exist are errors
let missing_block_height = block_height + 1;
Expand All @@ -214,6 +222,40 @@ fn test_block_index() {
other => panic!("Unexpected response: {:?}", other),
}

// insert later block
let next_height = block_height + 1;
let next_hash = H256([0xaa; 32]);
storage
.set_block_data(next_hash, next_height, block_metadata.clone())
.unwrap();

// check earliest+latest blocks are still correct
assert_eq!(
(next_hash, next_height),
storage.get_latest_block().unwrap(),
);
assert_eq!(
(block_hash, block_height),
storage.get_earliest_block().unwrap(),
);

// insert earlier block
let prev_height = block_height - 1;
let prev_hash = H256([0xbb; 32]);
storage
.set_block_data(prev_hash, prev_height, block_metadata.clone())
.unwrap();

// check earliest+latest blocks are still correct
assert_eq!(
(next_hash, next_height),
storage.get_latest_block().unwrap(),
);
assert_eq!(
(prev_hash, prev_height),
storage.get_earliest_block().unwrap(),
);

drop(storage);
temp_dir.close().unwrap();
}
Expand Down