Skip to content

Commit

Permalink
fix SubscriptionRpc document issue
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Oct 26, 2023
1 parent 66114bc commit 02854ae
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 9 deletions.
116 changes: 116 additions & 0 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4835,6 +4835,122 @@ socket.send(`{"id": 2, "jsonrpc": "2.0", "method": "unsubscribe", "params": ["0x
* `topic`: `string`
* result: `string`

Subscribes to a topic.

###### Params

* `topic` - Subscription topic (enum: new_tip_header | new_tip_block | new_transaction | proposed_transaction | rejected_transaction)

###### Returns

This RPC returns the subscription ID as the result. CKB node will push messages in the subscribed topics to the current RPC connection. The subscript ID is also attached as `params.subscription` in the push messages.

Example push message:


```
{
"jsonrpc": "2.0",
"method": "subscribe",
"params": {
"result": { ... },
"subscription": "0x2a"
}
}
```


###### Topics

###### `new_tip_header`

Whenever there’s a block that is appended to the canonical chain, the CKB node will publish the block header to subscribers.

The type of the `params.result` in the push message is [`HeaderView`](#type-headerview).

###### `new_tip_block`

Whenever there’s a block that is appended to the canonical chain, the CKB node will publish the whole block to subscribers.

The type of the `params.result` in the push message is [`BlockView`](#type-blockview).

###### `new_transaction`

Subscribers will get notified when a new transaction is submitted to the pool.

The type of the `params.result` in the push message is [`PoolTransactionEntry`](#type-pooltransactionentry).

###### `proposed_transaction`

Subscribers will get notified when an in-pool transaction is proposed by chain.

The type of the `params.result` in the push message is [`PoolTransactionEntry`](#type-pooltransactionentry).

###### `rejected_transaction`

Subscribers will get notified when a pending transaction is rejected by tx-pool.

The type of the `params.result` in the push message is an array contain:

The type of the `params.result` in the push message is a two-elements array, where

* the first item type is [`PoolTransactionEntry`](#type-pooltransactionentry), and

* the second item type is [`PoolTransactionReject`](#type-pooltransactionreject).

###### Examples

Subscribe Request


```
{
"id": 42,
"jsonrpc": "2.0",
"method": "subscribe",
"params": [
"new_tip_header"
]
}
```


Subscribe Response


```
{
"id": 42,
"jsonrpc": "2.0",
"result": "0xf3"
}
Unsubscribe Request
```json
{
"id": 42,
"jsonrpc": "2.0",
"method": "unsubscribe",
"params": [
"0xf3"
]
}
```


Unsubscribe Response


```
{
"id": 42,
"jsonrpc": "2.0",
"result": true
}
```



## RPC Errors

Expand Down
25 changes: 16 additions & 9 deletions rpc/src/module/subscription.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(missing_docs)]

use async_trait::async_trait;
use ckb_async_runtime::Handle;
use ckb_jsonrpc_types::Topic;
Expand Down Expand Up @@ -45,16 +44,14 @@ use tokio::sync::broadcast;
///
/// socket.send(`{"id": 2, "jsonrpc": "2.0", "method": "unsubscribe", "params": ["0x0"]}`)
/// ```
///
///
#[allow(clippy::needless_return)]
#[rpc]
#[async_trait]
pub trait SubscriptionRpc {
/// Context to implement the subscription RPC.
/// type Metadata;
/// The stream of subscription messages.
type S: Stream<Item = PublishMsg<String>> + Send + 'static;
/// Subscribes to a topic.
///
/// ## Params
Expand Down Expand Up @@ -121,7 +118,7 @@ pub trait SubscriptionRpc {
///
/// ## Examples
///
/// Request
/// Subscribe Request
///
/// ```json
/// {
Expand All @@ -134,17 +131,18 @@ pub trait SubscriptionRpc {
/// }
/// ```
///
/// Response
/// Subscribe Response
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "result": "0xf3"
/// }
/// ```
///
/// Unsubscribe Request
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
Expand All @@ -153,9 +151,18 @@ pub trait SubscriptionRpc {
/// "0xf3"
/// ]
/// }
/// ```
///
/// Unsubscribe Response
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "result": true
/// }
/// ```
///
type S: Stream<Item = PublishMsg<String>> + Send + 'static;
#[rpc(pub_sub(notify = "subscribe", unsubscribe = "unsubscribe"))]
fn subscribe(&self, topic: Topic) -> Result<Self::S>;
}
Expand Down

0 comments on commit 02854ae

Please sign in to comment.