Skip to content

Commit 3c70fc0

Browse files
committed
add test rpc_get_indexer_tip_with_set_init_tip.
1 parent 08924b1 commit 3c70fc0

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

util/indexer/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ serde_json = "1.0"
2727
numext-fixed-uint = "0.1"
2828

2929
[dev-dependencies]
30+
ckb-db = { path = "../../db", version = "= 0.113.0-pre" }
31+
ckb-db-schema = { path = "../../db-schema", version = "= 0.113.0-pre" }
3032
tempfile.workspace = true
3133
rand = "0.8"

util/indexer/src/service.rs

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,16 @@ impl IndexerService {
260260
opts
261261
}
262262

263-
pub(crate) fn apply_init_tip(
263+
pub(crate) fn apply_init_tip<T: ChainStore>(
264264
is_store_initialized: bool,
265265
init_tip_hash: &Option<H256>,
266266
store: &RocksdbStore,
267-
secondary_db: &SecondaryDB,
267+
secondary_db: &T,
268268
) -> Result<(), Error> {
269269
if let (true, Some(init_tip_hash)) = (!is_store_initialized, init_tip_hash) {
270270
let init_tip_number = secondary_db
271271
.get_block_header(&init_tip_hash.pack())
272-
.ok_or(Error::Params("Setting the initial tip failed: could not find the block corresponding to the init tip hash."
272+
.ok_or(Error::Params("setting the initial tip failed: could not find the block corresponding to the init tip hash."
273273
.to_string(),
274274
))?
275275
.number();
@@ -983,13 +983,20 @@ impl TryInto<FilterOptions> for IndexerSearchKey {
983983
mod tests {
984984
use super::*;
985985
use crate::store::RocksdbStore;
986+
use ckb_db::{
987+
iter::{DBIter, IteratorMode},
988+
DBPinnableSlice,
989+
};
990+
use ckb_db_schema::Col;
986991
use ckb_jsonrpc_types::{IndexerRange, IndexerSearchKeyFilter};
992+
use ckb_store::{Freezer, StoreCache};
987993
use ckb_types::{
988994
bytes::Bytes,
989995
core::{
990996
capacity_bytes, BlockBuilder, Capacity, EpochNumberWithFraction, HeaderBuilder,
991-
ScriptHashType, TransactionBuilder,
997+
HeaderView, ScriptHashType, TransactionBuilder,
992998
},
999+
h256,
9931000
packed::{CellInput, CellOutputBuilder, OutPoint, Script, ScriptBuilder},
9941001
H256,
9951002
};
@@ -1003,6 +1010,49 @@ mod tests {
10031010
// Indexer::new(store, 10, 1)
10041011
}
10051012

1013+
fn new_chain_store(prefix: &str) -> impl ChainStore {
1014+
struct DummyChainStore {
1015+
inner: RocksdbStore,
1016+
}
1017+
impl ChainStore for DummyChainStore {
1018+
fn cache(&self) -> Option<&StoreCache> {
1019+
None
1020+
}
1021+
fn freezer(&self) -> Option<&Freezer> {
1022+
None
1023+
}
1024+
fn get(&self, _col: Col, _key: &[u8]) -> Option<DBPinnableSlice> {
1025+
None
1026+
}
1027+
fn get_iter(&self, _col: Col, mode: IteratorMode) -> DBIter {
1028+
let opts = ReadOptions::default();
1029+
self.inner.inner().get_iter(&opts, mode)
1030+
}
1031+
fn get_block_header(&self, hash: &packed::Byte32) -> Option<HeaderView> {
1032+
match hash {
1033+
_ if hash
1034+
== &h256!(
1035+
"0x8fbd0ec887159d2814cee475911600e3589849670f5ee1ed9798b38fdeef4e44"
1036+
)
1037+
.pack() =>
1038+
{
1039+
let epoch = EpochNumberWithFraction::new(0, 0, 10);
1040+
Some(
1041+
HeaderView::new_advanced_builder()
1042+
.number(100000.pack())
1043+
.epoch(epoch.pack())
1044+
.build(),
1045+
)
1046+
}
1047+
_ => None,
1048+
}
1049+
}
1050+
}
1051+
DummyChainStore {
1052+
inner: new_store(prefix),
1053+
}
1054+
}
1055+
10061056
#[test]
10071057
fn rpc() {
10081058
let store = new_store("rpc");
@@ -1591,6 +1641,38 @@ mod tests {
15911641
);
15921642
}
15931643

1644+
#[test]
1645+
fn rpc_get_indexer_tip_with_set_init_tip() {
1646+
let store = new_store("rpc_get_indexer_tip_with_set_init_tip");
1647+
let ckb_db = new_chain_store("rpc_get_indexer_tip_with_set_init_tip_ckb");
1648+
1649+
// test setting the initial tip failed
1650+
let ret = IndexerService::apply_init_tip(false, &Some(H256::default()), &store, &ckb_db);
1651+
assert_eq!(ret.unwrap_err().to_string(), "Invalid params setting the initial tip failed: could not find the block corresponding to the init tip hash.");
1652+
1653+
// test get_tip rpc
1654+
IndexerService::apply_init_tip(
1655+
false,
1656+
&Some(h256!(
1657+
"0x8fbd0ec887159d2814cee475911600e3589849670f5ee1ed9798b38fdeef4e44"
1658+
)),
1659+
&store,
1660+
&ckb_db,
1661+
)
1662+
.unwrap();
1663+
let pool = Arc::new(RwLock::new(Pool::default()));
1664+
let rpc = IndexerHandle {
1665+
store,
1666+
pool: Some(Arc::clone(&pool)),
1667+
};
1668+
let tip = rpc.get_indexer_tip().unwrap().unwrap();
1669+
assert_eq!(
1670+
h256!("0x8fbd0ec887159d2814cee475911600e3589849670f5ee1ed9798b38fdeef4e44"),
1671+
tip.block_hash
1672+
);
1673+
assert_eq!(100000, tip.block_number.value());
1674+
}
1675+
15941676
#[test]
15951677
fn script_search_mode_rpc() {
15961678
let store = new_store("script_search_mode_rpc");

0 commit comments

Comments
 (0)