Skip to content

Commit

Permalink
rpc: Add gw_get_last_submitted_info
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr committed Dec 20, 2021
1 parent d56a21d commit c514b77
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
14 changes: 13 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,19 @@ impl BlockProducer {
committed_at: Instant::now(),
committed_tip_block_hash: H256::zero(),
},
last_submitted_tx_hash: {
let hash = store.get_tip_block_hash()?;
Arc::new(smol::lock::RwLock::new(hash))
},
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 +315,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.clone().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

0 comments on commit c514b77

Please sign in to comment.