Skip to content

Commit

Permalink
Feat(standalone): Function to get latest/earliest block (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
birchmd authored Apr 8, 2022
1 parent 1360538 commit 9c59b0c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
30 changes: 30 additions & 0 deletions engine-standalone-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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 @@ -197,6 +197,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 @@ -216,6 +224,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

0 comments on commit 9c59b0c

Please sign in to comment.