Skip to content

Commit

Permalink
add tests for setting init tip:
Browse files Browse the repository at this point in the history
- get_indexer_tip
- rollback
  • Loading branch information
EthanYuan committed Nov 22, 2023
1 parent 9a18abc commit 1a60bdb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion resource/ckb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,5 @@ block_uncles_cache_size = 30
# cell_filter = "let script = output.type;script!=() && script.code_hash == \"0x00000000000000000000000000000000000000000000000000545950455f4944\""
# # The initial tip number and hash can be set as the starting height for building indexes in indexer-r. Effective only during the initial index creation.
# init_tip_number = 10000
# # The initial tip hash must match the tip number; otherwise, it will result in a rollback.
# # The initial tip hash must match the tip number; otherwise, it will result in a rollback to empty.
# init_tip_hash = "0x8fbd0ec887159d2814cee475911600e3589849670f5ee1ed9798b38fdeef4e44"
32 changes: 31 additions & 1 deletion util/indexer/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ impl CustomFilters {
#[cfg(test)]
mod tests {
use super::*;
use crate::store::RocksdbStore;
use crate::{store::RocksdbStore, IndexerService};
use ckb_types::{
bytes::Bytes,
core::{
Expand Down Expand Up @@ -2015,6 +2015,20 @@ mod tests {
)
}

fn new_indexer_with_init_tip(
prefix: &str,
init_tip_number: Option<BlockNumber>,
init_tip_hash: Option<H256>,
) -> Indexer<RocksdbStore> {
let tmp_dir = tempfile::Builder::new().prefix(prefix).tempdir().unwrap();
let store = RocksdbStore::new(
&RocksdbStore::default_options(),
tmp_dir.path().to_str().unwrap(),
);
IndexerService::apply_init_tip(false, init_tip_number, &init_tip_hash, &store);
Indexer::new(store, 10, 1, None, CustomFilters::new(None, None))
}

#[test]
fn with_custom_block_filter() {
let indexer = new_indexer_with_custom_filters::<RocksdbStore>(
Expand Down Expand Up @@ -2364,4 +2378,20 @@ mod tests {
.len()
);
}

#[test]
fn rollback_with_set_init_tip() {
let indexer = new_indexer_with_init_tip(
"rollback_with_set_init_tip",
Some(1000),
Some(H256::default()),
);

indexer.rollback().unwrap();

// tip should be None and store should be empty;
assert!(indexer.tip().unwrap().is_none());
let mut iter = indexer.store.iter([], IteratorDirection::Forward).unwrap();
assert!(iter.next().is_none());
}
}
42 changes: 34 additions & 8 deletions util/indexer/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ impl IndexerService {
let is_store_initialized = config.store.exists();
let store_opts = Self::indexer_store_options(config);
let store = RocksdbStore::new(&store_opts, &config.store);
Self::apply_init_tip(is_store_initialized, config, &store);
Self::apply_init_tip(
is_store_initialized,
config.init_tip_number,
&config.init_tip_hash,
&store,
);
let pool = if config.index_tx_pool {
Some(Arc::new(RwLock::new(Pool::default())))
} else {
Expand Down Expand Up @@ -250,16 +255,21 @@ impl IndexerService {
opts
}

fn apply_init_tip(is_store_initialized: bool, config: &IndexerConfig, store: &RocksdbStore) {
if !is_store_initialized
&& config.init_tip_hash.is_some()
&& config.init_tip_number.is_some()
pub(crate) fn apply_init_tip(
is_store_initialized: bool,
init_tip_number: Option<u64>,
init_tip_hash: &Option<H256>,
store: &RocksdbStore,
) {
if let (true, Some(init_tip_number), Some(init_tip_hash)) =
(!is_store_initialized, init_tip_number, init_tip_hash)
{
let block_number = config.init_tip_number.unwrap();
let block_hash = config.init_tip_hash.as_ref().unwrap();
let mut batch = store.batch().expect("create batch should be OK");
batch
.put_kv(Key::Header(block_number, &block_hash.pack(), true), vec![])
.put_kv(
Key::Header(init_tip_number, &init_tip_hash.pack(), true),
vec![],
)
.expect("insert init tip header should be OK");
batch.commit().expect("commit batch should be OK");
}
Expand Down Expand Up @@ -1571,6 +1581,22 @@ mod tests {
);
}

#[test]
fn rpc_get_indexer_tip_with_set_init_tip() {
let store = new_store("rpc_get_indexer_tip_with_set_init_tip");
IndexerService::apply_init_tip(false, Some(10000), &Some(H256::default()), &store);
let pool = Arc::new(RwLock::new(Pool::default()));
let rpc = IndexerHandle {
store,
pool: Some(Arc::clone(&pool)),
};

// test get_tip rpc
let tip = rpc.get_indexer_tip().unwrap().unwrap();
assert_eq!(H256::default(), tip.block_hash);
assert_eq!(10000, tip.block_number.value());
}

#[test]
fn script_search_mode_rpc() {
let store = new_store("script_search_mode_rpc");
Expand Down

0 comments on commit 1a60bdb

Please sign in to comment.