Skip to content

Commit

Permalink
rename the rpc methods of rich-indexer, keep the same as indexer.
Browse files Browse the repository at this point in the history
indexer and rich-indexer cannot be both set. Only one can be enabled at a time.
  • Loading branch information
EthanYuan committed Jan 3, 2024
1 parent 37720e3 commit 513eae5
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 54 deletions.
2 changes: 2 additions & 0 deletions ckb-bin/src/subcommand/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub fn run(args: RunArgs, version: Version, async_handle: Handle) -> Result<(),
let block_assembler_config = launcher.sanitize_block_assembler_config()?;
let miner_enable = block_assembler_config.is_some();

launcher.check_indexer_config()?;

let (shared, mut pack) = launcher.build_shared(block_assembler_config)?;

// spawn freezer background process
Expand Down
24 changes: 12 additions & 12 deletions rpc/src/module/rich_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,29 @@ pub trait RichIndexerRpc {
/// "id": 2
/// }
/// ```
#[rpc(name = "get_rich_indexer_tip")]
async fn get_rich_indexer_tip(&self) -> Result<Option<IndexerTip>>;
#[rpc(name = "get_indexer_tip")]
async fn get_indexer_tip(&self) -> Result<Option<IndexerTip>>;

#[rpc(name = "get_rich_indexer_cells")]
async fn get_rich_indexer_cells(
#[rpc(name = "get_cells")]
async fn get_cells(
&self,
search_key: IndexerSearchKey,
order: IndexerOrder,
limit: Uint32,
after: Option<JsonBytes>,
) -> Result<IndexerPagination<IndexerCell>>;

#[rpc(name = "get_rich_indexer_transactions")]
async fn get_rich_indexer_transactions(
#[rpc(name = "get_transactions")]
async fn get_transactions(
&self,
search_key: IndexerSearchKey,
order: IndexerOrder,
limit: Uint32,
after: Option<JsonBytes>,
) -> Result<IndexerPagination<IndexerTx>>;

#[rpc(name = "get_rich_indexer_cells_capacity")]
async fn get_rich_indexer_cells_capacity(
#[rpc(name = "get_cells_capacity")]
async fn get_cells_capacity(
&self,
search_key: IndexerSearchKey,
) -> Result<Option<IndexerCellsCapacity>>;
Expand All @@ -83,14 +83,14 @@ impl RichIndexerRpcImpl {

#[async_trait]
impl RichIndexerRpc for RichIndexerRpcImpl {
async fn get_rich_indexer_tip(&self) -> Result<Option<IndexerTip>> {
async fn get_indexer_tip(&self) -> Result<Option<IndexerTip>> {
self.handle
.get_indexer_tip()
.await
.map_err(|e| RPCError::custom(RPCError::Indexer, e))
}

async fn get_rich_indexer_cells(
async fn get_cells(
&self,
search_key: IndexerSearchKey,
order: IndexerOrder,
Expand All @@ -103,7 +103,7 @@ impl RichIndexerRpc for RichIndexerRpcImpl {
.map_err(|e| RPCError::custom(RPCError::Indexer, e))
}

async fn get_rich_indexer_transactions(
async fn get_transactions(
&self,
search_key: IndexerSearchKey,
order: IndexerOrder,
Expand All @@ -116,7 +116,7 @@ impl RichIndexerRpc for RichIndexerRpcImpl {
.map_err(|e| RPCError::custom(RPCError::Indexer, e))
}

async fn get_rich_indexer_cells_capacity(
async fn get_cells_capacity(
&self,
search_key: IndexerSearchKey,
) -> Result<Option<IndexerCellsCapacity>> {
Expand Down
67 changes: 35 additions & 32 deletions rpc/src/service_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,51 +195,54 @@ impl<'a> ServiceBuilder<'a> {
let mut pool_service =
PoolService::new(indexer_config.index_tx_pool, shared.async_handle().clone());

// Init indexer service.
let indexer = IndexerService::new(
ckb_secondary_db.clone(),
pool_service.clone(),
indexer_config,
shared.async_handle().clone(),
);
let indexer_handle = indexer.handle();
let methods = IndexerRpcImpl::new(indexer_handle);
if self.config.indexer_enable() {
// Init indexer service.
let indexer = IndexerService::new(
ckb_secondary_db.clone(),
pool_service.clone(),
indexer_config,
shared.async_handle().clone(),
);
indexer.spawn_poll(shared.notify_controller().clone());
if indexer_config.index_tx_pool {
pool_service.index_tx_pool(shared.notify_controller().clone());
}

let indexer_handle = indexer.handle();
let methods = IndexerRpcImpl::new(indexer_handle);
self = set_rpc_module_methods!(
self,
"Indexer",
indexer_enable,
add_indexer_rpc_methods,
methods
);
}
self = set_rpc_module_methods!(
self,
"Indexer",
indexer_enable,
add_indexer_rpc_methods,
methods
);

// Init rich-indexer service
let rich_indexer = RichIndexerService::new(
ckb_secondary_db,
pool_service.clone(),
indexer_config,
shared.async_handle().clone(),
);
let rich_indexer_handle = rich_indexer.async_handle();
let rich_indexer_methods = RichIndexerRpcImpl::new(rich_indexer_handle);
if self.config.rich_indexer_enable() {
// Init rich-indexer service
let rich_indexer = RichIndexerService::new(
ckb_secondary_db,
pool_service.clone(),
indexer_config,
shared.async_handle().clone(),
);
rich_indexer.spawn_poll(shared.notify_controller().clone());
if indexer_config.index_tx_pool {
pool_service.index_tx_pool(shared.notify_controller().clone());
}

let rich_indexer_handle = rich_indexer.async_handle();
let rich_indexer_methods = RichIndexerRpcImpl::new(rich_indexer_handle);
self = set_rpc_module_methods!(
self,
"RichIndexer",
rich_indexer_enable,
add_rich_indexer_rpc_methods,
rich_indexer_methods
)
}
set_rpc_module_methods!(
self,
"RichIndexer",
rich_indexer_enable,
add_rich_indexer_rpc_methods,
rich_indexer_methods
)
self
}

pub fn enable_subscription(&mut self, shared: Shared) {
Expand Down
74 changes: 65 additions & 9 deletions util/indexer/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ impl IndexerHandle {
limit: Uint32,
after_cursor: Option<JsonBytes>,
) -> Result<IndexerPagination<IndexerCell>, Error> {
if search_key
.script_search_mode
.as_ref()
.map(|mode| *mode == IndexerSearchMode::Partial)
.unwrap_or(false)
{
return Err(Error::invalid_params(
"the CKB indexer doesn't support partial search mode, \
please use the CKB rich-indexer for such search",
));
}

if search_key
.filter
.as_ref()
Expand Down Expand Up @@ -299,29 +311,49 @@ impl IndexerHandle {
limit: Uint32,
after_cursor: Option<JsonBytes>,
) -> Result<IndexerPagination<IndexerTx>, Error> {
let limit = limit.value() as usize;
if limit == 0 {
return Err(Error::invalid_params("limit should be greater than 0"));
}

if search_key
.script_search_mode
.as_ref()
.map(|mode| *mode == IndexerSearchMode::Partial)
.unwrap_or(false)
{
return Err(Error::invalid_params(
"the CKB indexer doesn't support partial search mode, \
please use the CKB rich-indexer for such search",
));
}

if search_key
.filter
.as_ref()
.map(|filter| filter.output_data.is_some())
.unwrap_or(false)
{
return Err(Error::invalid_params(
"the CKB indexer doesn't support data filtering, \
please use the CKB rich-indexer for such filtering",
));
}

let (prefix, from_key, direction, skip) = build_query_options(
&search_key,
KeyPrefix::TxLockScript,
KeyPrefix::TxTypeScript,
order,
after_cursor,
)?;
let limit = limit.value() as usize;
if limit == 0 {
return Err(Error::invalid_params("limit should be greater than 0"));
}

let (filter_script, filter_block_range) = if let Some(filter) = search_key.filter.as_ref() {
if filter.script_len_range.is_some() {
return Err(Error::invalid_params(
"doesn't support search_key.filter.script_len_range parameter",
));
}
if filter.output_data.is_some() {
return Err(Error::invalid_params(
"doesn't support search_key.filter.output_data parameter",
));
}
if filter.output_data_len_range.is_some() {
return Err(Error::invalid_params(
"doesn't support search_key.filter.output_data_len_range parameter",
Expand Down Expand Up @@ -571,6 +603,30 @@ impl IndexerHandle {
&self,
search_key: IndexerSearchKey,
) -> Result<Option<IndexerCellsCapacity>, Error> {
if search_key
.script_search_mode
.as_ref()
.map(|mode| *mode == IndexerSearchMode::Partial)
.unwrap_or(false)
{
return Err(Error::invalid_params(
"the CKB indexer doesn't support partial search mode, \
please use the CKB rich-indexer for such search",
));
}

if search_key
.filter
.as_ref()
.map(|filter| filter.output_data.is_some())
.unwrap_or(false)
{
return Err(Error::invalid_params(
"the CKB indexer doesn't support data filtering, \
please use the CKB rich-indexer for such filtering",
));
}

let (prefix, from_key, direction, skip) = build_query_options(
&search_key,
KeyPrefix::CellLockScript,
Expand Down
2 changes: 1 addition & 1 deletion util/jsonrpc-types/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Default for IndexerSearchKey {
}

/// IndexerScriptSearchMode represent script search mode, default is prefix search
#[derive(Deserialize)]
#[derive(Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum IndexerSearchMode {
/// Mode `prefix` search with prefix
Expand Down
11 changes: 11 additions & 0 deletions util/launcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ impl Launcher {
config
}

pub fn check_indexer_config(&self) -> Result<(), ExitCode> {
// check if indexer and rich-indexer are both set
if (self.args.indexer || self.args.config.rpc.indexer_enable())
&& (self.args.rich_indexer || self.args.config.rpc.rich_indexer_enable())
{
eprintln!("Config Error: indexer and rich-indexer cannot be both set");
return Err(ExitCode::Config);
}
Ok(())
}

/// start block filter service
pub fn start_block_filter(&self, shared: &Shared) {
if self
Expand Down

0 comments on commit 513eae5

Please sign in to comment.