Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: Add gw_get_last_submitted_info #504

Merged
merged 3 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion crates/block-producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub struct BlockProducer {
ckb_genesis_info: CKBGenesisInfo,
tests_control: Option<TestModeControl>,
last_committed_l2_block: LastCommittedL2Block,
last_submitted_tx_hash: Arc<smol::lock::RwLock<H256>>,
}

impl BlockProducer {
Expand All @@ -183,7 +184,6 @@ impl BlockProducer {

let block_producer = BlockProducer {
rollup_config_hash,
store,
generator,
chain,
mem_pool,
Expand All @@ -198,10 +198,24 @@ impl BlockProducer {
committed_at: Instant::now(),
committed_tip_block_hash: H256::zero(),
},
last_submitted_tx_hash: {
let tip_block_hash = store.get_tip_block_hash()?;
let committed_info = store
.get_l2block_committed_info(&tip_block_hash)?
.ok_or_else(|| anyhow!("can't find committed info for tip block"))?;
Arc::new(smol::lock::RwLock::new(
committed_info.transaction_hash().unpack(),
))
},
store,
};
Ok(block_producer)
}

pub fn last_submitted_tx_hash(&self) -> Arc<smol::lock::RwLock<H256>> {
self.last_submitted_tx_hash.clone()
}

pub async fn handle_event(&mut self, event: ChainEvent) -> Result<()> {
if let Some(ref tests_control) = self.tests_control {
match tests_control.payload().await {
Expand Down Expand Up @@ -306,12 +320,15 @@ impl BlockProducer {
return Ok(());
}

let submitted_tx_hash = tx.hash();
match self.submit_block_tx(block_number, tx).await {
Ok(SubmitResult::Submitted) => {
self.last_committed_l2_block = LastCommittedL2Block {
committed_tip_block_hash: l2_tip_block_hash,
committed_at: Instant::now(),
};
let mut last_submitted_tx_hash = self.last_submitted_tx_hash.write().await;
*last_submitted_tx_hash = submitted_tx_hash.into();
}
Ok(SubmitResult::Skip) => {}
Err(err) => {
Expand Down
3 changes: 3 additions & 0 deletions crates/block-producer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ pub fn run(config: Config, skip_config_check: bool) -> Result<()> {
send_tx_rate_limit: config.rpc.send_tx_rate_limit.clone(),
server_config: config.rpc_server.clone(),
fee_config: config.fee.clone(),
last_submitted_tx_hash: block_producer
.as_ref()
.map(|bp| bp.last_submitted_tx_hash()),
};

let rpc_registry = Registry::new(args);
Expand Down
6 changes: 6 additions & 0 deletions crates/jsonrpc-types/src/godwoken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,12 @@ impl From<packed::Fee> for Fee {
}
}

#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Debug, Default)]
#[serde(rename_all = "snake_case")]
pub struct LastL2BlockCommittedInfo {
pub transaction_hash: H256,
}

#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Debug, Default)]
#[serde(rename_all = "snake_case")]
pub struct L2BlockCommittedInfo {
Expand Down
26 changes: 24 additions & 2 deletions crates/rpc-server/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use gw_jsonrpc_types::{
ckb_jsonrpc_types::{JsonBytes, Uint128, Uint32},
godwoken::{
BackendInfo, ErrorTxReceipt, GlobalState, L2BlockCommittedInfo, L2BlockStatus, L2BlockView,
L2BlockWithStatus, L2TransactionStatus, L2TransactionWithStatus, NodeInfo, RunResult,
SUDTFeeConfig, TxReceipt, WithdrawalStatus, WithdrawalWithStatus,
L2BlockWithStatus, L2TransactionStatus, L2TransactionWithStatus, LastL2BlockCommittedInfo,
NodeInfo, RunResult, SUDTFeeConfig, TxReceipt, WithdrawalStatus, WithdrawalWithStatus,
},
test_mode::{ShouldProduceBlock, TestModePayload},
};
Expand Down Expand Up @@ -120,6 +120,7 @@ pub struct RegistryArgs<T> {
pub send_tx_rate_limit: Option<RPCRateLimit>,
pub server_config: RPCServerConfig,
pub fee_config: FeeConfig,
pub last_submitted_tx_hash: Option<Arc<smol::lock::RwLock<H256>>>,
}

pub struct Registry {
Expand All @@ -136,6 +137,7 @@ pub struct Registry {
send_tx_rate_limit: Option<RPCRateLimit>,
server_config: RPCServerConfig,
fee_config: FeeConfig,
last_submitted_tx_hash: Option<Arc<smol::lock::RwLock<H256>>>,
}

impl Registry {
Expand All @@ -155,6 +157,7 @@ impl Registry {
send_tx_rate_limit,
server_config,
fee_config,
last_submitted_tx_hash,
} = args;

let backend_info = get_backend_info(generator.clone());
Expand Down Expand Up @@ -186,6 +189,7 @@ impl Registry {
send_tx_rate_limit,
server_config,
fee_config,
last_submitted_tx_hash,
}
}

Expand Down Expand Up @@ -240,12 +244,19 @@ impl Registry {
)
.with_method("gw_get_fee_config", get_fee_config)
.with_method("gw_get_node_info", get_node_info);

if self.node_mode != NodeMode::ReadOnly {
server = server
.with_method("gw_submit_l2transaction", submit_l2transaction)
.with_method("gw_submit_withdrawal_request", submit_withdrawal_request);
}

if let Some(last_submitted_tx_hash) = self.last_submitted_tx_hash {
server = server
.with_data(Data(last_submitted_tx_hash))
.with_method("gw_get_last_submitted_info", get_last_submitted_info);
}

// Tests
if let Some(tests_rpc_impl) = self.tests_rpc_impl {
server = server
Expand Down Expand Up @@ -1227,6 +1238,17 @@ async fn get_node_info(backend_info: Data<Vec<BackendInfo>>) -> Result<NodeInfo>
})
}

async fn get_last_submitted_info(
last_submitted_tx_hash: Data<smol::lock::RwLock<H256>>,
) -> Result<LastL2BlockCommittedInfo> {
Ok(LastL2BlockCommittedInfo {
transaction_hash: {
let hash: [u8; 32] = (*last_submitted_tx_hash.read().await).into();
hash.into()
},
})
}

async fn get_fee_config(fee: Data<FeeConfig>) -> Result<gw_jsonrpc_types::godwoken::FeeConfig> {
let fee_config = gw_jsonrpc_types::godwoken::FeeConfig {
meta_cycles_limit: fee.meta_cycles_limit.into(),
Expand Down
29 changes: 29 additions & 0 deletions docs/RPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -923,3 +923,32 @@ Response
"result": "0xb57c6da2f803413b5781f8c6508320a0ada61a2992bb59ab38f16da2d02099c1"
}
```

### Method `gw_get_last_submitted_info`

Get node last submitted info.

#### Examples

Request

``` json
{
"id": 42,
"jsonrpc": "2.0",
"method": "gw_get_last_submitted_info",
"params": []
}
```

Response

``` json
{
"id": 42,
"jsonrpc": "2.0",
"result": {
"transaction_hash": "0x1536b5af1e42707e0278cf16dd086ec630485883ce3d1c1388f9eb4d8169b119"
}
}
```