Skip to content

Commit

Permalink
Merge pull request #33 from nervosnetwork/quake/get_indexer_info
Browse files Browse the repository at this point in the history
feat: add get_indexer_info rpc
  • Loading branch information
quake authored Jul 6, 2021
2 parents d247556 + f7740c2 commit 3cb3ffa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,25 @@ echo '{
| curl -H 'content-type: application/json' -d @- \
http://localhost:8116
```

### `get_indexer_info`

Returns the indexer service information.

#### Returns

version - indexer version

#### Examples

```bash
echo '{
"id": 2,
"jsonrpc": "2.0",
"method": "get_indexer_info",
"params": []
}' \
| tr -d '\n' \
| curl -H 'content-type: application/json' -d @- \
http://localhost:8116
```
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.46.0
1.51.0
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async fn main() {
matches.value_of("store_path").expect("required arg"),
matches.value_of("listen_uri").unwrap_or("127.0.0.1:8116"),
std::time::Duration::from_secs(2),
crate_version!().to_string(),
);

let rpc_server = service.start();
Expand Down
33 changes: 31 additions & 2 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,30 @@ pub struct Service {
store: RocksdbStore,
poll_interval: Duration,
listen_address: String,
version: String,
}

impl Service {
pub fn new(store_path: &str, listen_address: &str, poll_interval: Duration) -> Self {
pub fn new(
store_path: &str,
listen_address: &str,
poll_interval: Duration,
version: String,
) -> Self {
let store = RocksdbStore::new(store_path);
Self {
store,
listen_address: listen_address.to_string(),
poll_interval,
version,
}
}

pub fn start(&self) -> Server {
let mut io_handler = IoHandler::new();
let rpc_impl = IndexerRpcImpl {
store: self.store.clone(),
version: self.version.clone(),
};
io_handler.extend_with(rpc_impl.to_delegate());

Expand Down Expand Up @@ -183,6 +191,9 @@ pub trait IndexerRpc {

#[rpc(name = "get_cells_capacity")]
fn get_cells_capacity(&self, search_key: SearchKey) -> Result<Option<CellsCapacity>>;

#[rpc(name = "get_indexer_info")]
fn get_indexer_info(&self) -> Result<IndexerInfo>;
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -227,6 +238,11 @@ pub struct CellsCapacity {
block_number: BlockNumber,
}

#[derive(Serialize)]
pub struct IndexerInfo {
version: String,
}

#[derive(Serialize)]
pub struct Cell {
output: CellOutput,
Expand Down Expand Up @@ -260,6 +276,7 @@ pub struct Pagination<T> {

pub struct IndexerRpcImpl {
pub store: RocksdbStore,
pub version: String,
}

impl IndexerRpc for IndexerRpcImpl {
Expand Down Expand Up @@ -617,6 +634,12 @@ impl IndexerRpc for IndexerRpcImpl {
.into(),
}))
}

fn get_indexer_info(&self) -> Result<IndexerInfo> {
Ok(IndexerInfo {
version: self.version.clone(),
})
}
}

const MAX_PREFIX_SEARCH_SIZE: usize = u16::max_value() as usize;
Expand Down Expand Up @@ -744,7 +767,10 @@ mod tests {
fn rpc() {
let store = new_store("rpc");
let indexer = Indexer::new(store.clone(), 10, 100);
let rpc = IndexerRpcImpl { store };
let rpc = IndexerRpcImpl {
store,
version: "0.2.1".to_owned(),
};

// setup test data
let lock_script1 = ScriptBuilder::default()
Expand Down Expand Up @@ -1114,5 +1140,8 @@ mod tests {
capacity.capacity.value(),
"last block live cell"
);

// test get_indexer_info rpc
assert_eq!("0.2.1", rpc.get_indexer_info().unwrap().version);
}
}

0 comments on commit 3cb3ffa

Please sign in to comment.