Skip to content

Commit 1a60bdb

Browse files
committed
add tests for setting init tip:
- get_indexer_tip - rollback
1 parent 9a18abc commit 1a60bdb

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

resource/ckb.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,5 @@ block_uncles_cache_size = 30
205205
# cell_filter = "let script = output.type;script!=() && script.code_hash == \"0x00000000000000000000000000000000000000000000000000545950455f4944\""
206206
# # 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.
207207
# init_tip_number = 10000
208-
# # The initial tip hash must match the tip number; otherwise, it will result in a rollback.
208+
# # The initial tip hash must match the tip number; otherwise, it will result in a rollback to empty.
209209
# init_tip_hash = "0x8fbd0ec887159d2814cee475911600e3589849670f5ee1ed9798b38fdeef4e44"

util/indexer/src/indexer.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ impl CustomFilters {
997997
#[cfg(test)]
998998
mod tests {
999999
use super::*;
1000-
use crate::store::RocksdbStore;
1000+
use crate::{store::RocksdbStore, IndexerService};
10011001
use ckb_types::{
10021002
bytes::Bytes,
10031003
core::{
@@ -2015,6 +2015,20 @@ mod tests {
20152015
)
20162016
}
20172017

2018+
fn new_indexer_with_init_tip(
2019+
prefix: &str,
2020+
init_tip_number: Option<BlockNumber>,
2021+
init_tip_hash: Option<H256>,
2022+
) -> Indexer<RocksdbStore> {
2023+
let tmp_dir = tempfile::Builder::new().prefix(prefix).tempdir().unwrap();
2024+
let store = RocksdbStore::new(
2025+
&RocksdbStore::default_options(),
2026+
tmp_dir.path().to_str().unwrap(),
2027+
);
2028+
IndexerService::apply_init_tip(false, init_tip_number, &init_tip_hash, &store);
2029+
Indexer::new(store, 10, 1, None, CustomFilters::new(None, None))
2030+
}
2031+
20182032
#[test]
20192033
fn with_custom_block_filter() {
20202034
let indexer = new_indexer_with_custom_filters::<RocksdbStore>(
@@ -2364,4 +2378,20 @@ mod tests {
23642378
.len()
23652379
);
23662380
}
2381+
2382+
#[test]
2383+
fn rollback_with_set_init_tip() {
2384+
let indexer = new_indexer_with_init_tip(
2385+
"rollback_with_set_init_tip",
2386+
Some(1000),
2387+
Some(H256::default()),
2388+
);
2389+
2390+
indexer.rollback().unwrap();
2391+
2392+
// tip should be None and store should be empty;
2393+
assert!(indexer.tip().unwrap().is_none());
2394+
let mut iter = indexer.store.iter([], IteratorDirection::Forward).unwrap();
2395+
assert!(iter.next().is_none());
2396+
}
23672397
}

util/indexer/src/service.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ impl IndexerService {
4949
let is_store_initialized = config.store.exists();
5050
let store_opts = Self::indexer_store_options(config);
5151
let store = RocksdbStore::new(&store_opts, &config.store);
52-
Self::apply_init_tip(is_store_initialized, config, &store);
52+
Self::apply_init_tip(
53+
is_store_initialized,
54+
config.init_tip_number,
55+
&config.init_tip_hash,
56+
&store,
57+
);
5358
let pool = if config.index_tx_pool {
5459
Some(Arc::new(RwLock::new(Pool::default())))
5560
} else {
@@ -250,16 +255,21 @@ impl IndexerService {
250255
opts
251256
}
252257

253-
fn apply_init_tip(is_store_initialized: bool, config: &IndexerConfig, store: &RocksdbStore) {
254-
if !is_store_initialized
255-
&& config.init_tip_hash.is_some()
256-
&& config.init_tip_number.is_some()
258+
pub(crate) fn apply_init_tip(
259+
is_store_initialized: bool,
260+
init_tip_number: Option<u64>,
261+
init_tip_hash: &Option<H256>,
262+
store: &RocksdbStore,
263+
) {
264+
if let (true, Some(init_tip_number), Some(init_tip_hash)) =
265+
(!is_store_initialized, init_tip_number, init_tip_hash)
257266
{
258-
let block_number = config.init_tip_number.unwrap();
259-
let block_hash = config.init_tip_hash.as_ref().unwrap();
260267
let mut batch = store.batch().expect("create batch should be OK");
261268
batch
262-
.put_kv(Key::Header(block_number, &block_hash.pack(), true), vec![])
269+
.put_kv(
270+
Key::Header(init_tip_number, &init_tip_hash.pack(), true),
271+
vec![],
272+
)
263273
.expect("insert init tip header should be OK");
264274
batch.commit().expect("commit batch should be OK");
265275
}
@@ -1571,6 +1581,22 @@ mod tests {
15711581
);
15721582
}
15731583

1584+
#[test]
1585+
fn rpc_get_indexer_tip_with_set_init_tip() {
1586+
let store = new_store("rpc_get_indexer_tip_with_set_init_tip");
1587+
IndexerService::apply_init_tip(false, Some(10000), &Some(H256::default()), &store);
1588+
let pool = Arc::new(RwLock::new(Pool::default()));
1589+
let rpc = IndexerHandle {
1590+
store,
1591+
pool: Some(Arc::clone(&pool)),
1592+
};
1593+
1594+
// test get_tip rpc
1595+
let tip = rpc.get_indexer_tip().unwrap().unwrap();
1596+
assert_eq!(H256::default(), tip.block_hash);
1597+
assert_eq!(10000, tip.block_number.value());
1598+
}
1599+
15741600
#[test]
15751601
fn script_search_mode_rpc() {
15761602
let store = new_store("script_search_mode_rpc");

0 commit comments

Comments
 (0)