Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
chain_head: Rename runtimeUpdates flag to withRuntime (#14244)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv authored and Ank4n committed Jul 8, 2023
1 parent ec77ab3 commit ec86b50
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 76 deletions.
2 changes: 1 addition & 1 deletion client/rpc-spec-v2/src/chain_head/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub trait ChainHeadApi<Hash> {
unsubscribe = "chainHead_unstable_unfollow",
item = FollowEvent<Hash>,
)]
fn chain_head_unstable_follow(&self, runtime_updates: bool);
fn chain_head_unstable_follow(&self, with_runtime: bool);

/// Retrieves the body (list of transactions) of a pinned block.
///
Expand Down
10 changes: 5 additions & 5 deletions client/rpc-spec-v2/src/chain_head/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ where
fn chain_head_unstable_follow(
&self,
mut sink: SubscriptionSink,
runtime_updates: bool,
with_runtime: bool,
) -> SubscriptionResult {
let sub_id = match self.accept_subscription(&mut sink) {
Ok(sub_id) => sub_id,
Expand All @@ -162,7 +162,7 @@ where
},
};
// Keep track of the subscription.
let Some(rx_stop) = self.subscriptions.insert_subscription(sub_id.clone(), runtime_updates) else {
let Some(rx_stop) = self.subscriptions.insert_subscription(sub_id.clone(), with_runtime) else {
// Inserting the subscription can only fail if the JsonRPSee
// generated a duplicate subscription ID.
debug!(target: LOG_TARGET, "[follow][id={:?}] Subscription already accepted", sub_id);
Expand All @@ -179,7 +179,7 @@ where
client,
backend,
subscriptions.clone(),
runtime_updates,
with_runtime,
sub_id.clone(),
);

Expand Down Expand Up @@ -410,8 +410,8 @@ where
};

let fut = async move {
// Reject subscription if runtime_updates is false.
if !block_guard.has_runtime_updates() {
// Reject subscription if with_runtime is false.
if !block_guard.has_runtime() {
let _ = sink.reject(ChainHeadRpcError::InvalidParam(
"The runtime updates flag must be set".into(),
));
Expand Down
14 changes: 7 additions & 7 deletions client/rpc-spec-v2/src/chain_head/chain_head_follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct ChainHeadFollower<BE: Backend<Block>, Block: BlockT, Client> {
/// Subscriptions handle.
sub_handle: Arc<SubscriptionManagement<Block, BE>>,
/// Subscription was started with the runtime updates flag.
runtime_updates: bool,
with_runtime: bool,
/// Subscription ID.
sub_id: String,
/// The best reported block by this subscription.
Expand All @@ -65,10 +65,10 @@ impl<BE: Backend<Block>, Block: BlockT, Client> ChainHeadFollower<BE, Block, Cli
client: Arc<Client>,
backend: Arc<BE>,
sub_handle: Arc<SubscriptionManagement<Block, BE>>,
runtime_updates: bool,
with_runtime: bool,
sub_id: String,
) -> Self {
Self { client, backend, sub_handle, runtime_updates, sub_id, best_block_cache: None }
Self { client, backend, sub_handle, with_runtime, sub_id, best_block_cache: None }
}
}

Expand Down Expand Up @@ -144,7 +144,7 @@ where
parent: Option<Block::Hash>,
) -> Option<RuntimeEvent> {
// No runtime versions should be reported.
if !self.runtime_updates {
if !self.with_runtime {
return None
}

Expand Down Expand Up @@ -228,7 +228,7 @@ where
let initialized_event = FollowEvent::Initialized(Initialized {
finalized_block_hash,
finalized_block_runtime,
runtime_updates: self.runtime_updates,
with_runtime: self.with_runtime,
});

let mut finalized_block_descendants = Vec::with_capacity(initial_blocks.len() + 1);
Expand All @@ -243,7 +243,7 @@ where
block_hash: child,
parent_block_hash: parent,
new_runtime,
runtime_updates: self.runtime_updates,
with_runtime: self.with_runtime,
});

finalized_block_descendants.push(event);
Expand Down Expand Up @@ -274,7 +274,7 @@ where
block_hash,
parent_block_hash,
new_runtime,
runtime_updates: self.runtime_updates,
with_runtime: self.with_runtime,
});

if !is_best_block {
Expand Down
40 changes: 20 additions & 20 deletions client/rpc-spec-v2/src/chain_head/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct RuntimeVersionEvent {
}

/// The runtime event generated if the `follow` subscription
/// has set the `runtime_updates` flag.
/// has set the `with_runtime` flag.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "type")]
Expand All @@ -88,7 +88,7 @@ impl From<ApiError> for RuntimeEvent {
/// This is the first event generated by the `follow` subscription
/// and is submitted only once.
///
/// If the `runtime_updates` flag is set, then this event contains
/// If the `with_runtime` flag is set, then this event contains
/// the `RuntimeEvent`, otherwise the `RuntimeEvent` is not present.
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -99,23 +99,23 @@ pub struct Initialized<Hash> {
///
/// # Note
///
/// This is present only if the `runtime_updates` flag is set for
/// This is present only if the `with_runtime` flag is set for
/// the `follow` subscription.
pub finalized_block_runtime: Option<RuntimeEvent>,
/// Privately keep track if the `finalized_block_runtime` should be
/// serialized.
#[serde(default)]
pub(crate) runtime_updates: bool,
pub(crate) with_runtime: bool,
}

impl<Hash: Serialize> Serialize for Initialized<Hash> {
/// Custom serialize implementation to include the `RuntimeEvent` depending
/// on the internal `runtime_updates` flag.
/// on the internal `with_runtime` flag.
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if self.runtime_updates {
if self.with_runtime {
let mut state = serializer.serialize_struct("Initialized", 2)?;
state.serialize_field("finalizedBlockHash", &self.finalized_block_hash)?;
state.serialize_field("finalizedBlockRuntime", &self.finalized_block_runtime)?;
Expand All @@ -140,23 +140,23 @@ pub struct NewBlock<Hash> {
///
/// # Note
///
/// This is present only if the `runtime_updates` flag is set for
/// This is present only if the `with_runtime` flag is set for
/// the `follow` subscription.
pub new_runtime: Option<RuntimeEvent>,
/// Privately keep track if the `finalized_block_runtime` should be
/// serialized.
#[serde(default)]
pub(crate) runtime_updates: bool,
pub(crate) with_runtime: bool,
}

impl<Hash: Serialize> Serialize for NewBlock<Hash> {
/// Custom serialize implementation to include the `RuntimeEvent` depending
/// on the internal `runtime_updates` flag.
/// on the internal `with_runtime` flag.
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if self.runtime_updates {
if self.with_runtime {
let mut state = serializer.serialize_struct("NewBlock", 3)?;
state.serialize_field("blockHash", &self.block_hash)?;
state.serialize_field("parentBlockHash", &self.parent_block_hash)?;
Expand Down Expand Up @@ -253,7 +253,7 @@ mod tests {
let event: FollowEvent<String> = FollowEvent::Initialized(Initialized {
finalized_block_hash: "0x1".into(),
finalized_block_runtime: None,
runtime_updates: false,
with_runtime: false,
});

let ser = serde_json::to_string(&event).unwrap();
Expand All @@ -278,7 +278,7 @@ mod tests {
let mut initialized = Initialized {
finalized_block_hash: "0x1".into(),
finalized_block_runtime: Some(runtime_event),
runtime_updates: true,
with_runtime: true,
};
let event: FollowEvent<String> = FollowEvent::Initialized(initialized.clone());

Expand All @@ -291,8 +291,8 @@ mod tests {
assert_eq!(ser, exp);

let event_dec: FollowEvent<String> = serde_json::from_str(exp).unwrap();
// The `runtime_updates` field is used for serialization purposes.
initialized.runtime_updates = false;
// The `with_runtime` field is used for serialization purposes.
initialized.with_runtime = false;
assert!(matches!(
event_dec, FollowEvent::Initialized(ref dec) if dec == &initialized
));
Expand All @@ -305,7 +305,7 @@ mod tests {
block_hash: "0x1".into(),
parent_block_hash: "0x2".into(),
new_runtime: None,
runtime_updates: false,
with_runtime: false,
});

let ser = serde_json::to_string(&event).unwrap();
Expand All @@ -331,7 +331,7 @@ mod tests {
block_hash: "0x1".into(),
parent_block_hash: "0x2".into(),
new_runtime: Some(runtime_event),
runtime_updates: true,
with_runtime: true,
};

let event: FollowEvent<String> = FollowEvent::NewBlock(new_block.clone());
Expand All @@ -345,8 +345,8 @@ mod tests {
assert_eq!(ser, exp);

let event_dec: FollowEvent<String> = serde_json::from_str(exp).unwrap();
// The `runtime_updates` field is used for serialization purposes.
new_block.runtime_updates = false;
// The `with_runtime` field is used for serialization purposes.
new_block.with_runtime = false;
assert!(matches!(
event_dec, FollowEvent::NewBlock(ref dec) if dec == &new_block
));
Expand All @@ -356,15 +356,15 @@ mod tests {
block_hash: "0x1".into(),
parent_block_hash: "0x2".into(),
new_runtime: None,
runtime_updates: true,
with_runtime: true,
};
let event: FollowEvent<String> = FollowEvent::NewBlock(new_block.clone());

let ser = serde_json::to_string(&event).unwrap();
let exp =
r#"{"event":"newBlock","blockHash":"0x1","parentBlockHash":"0x2","newRuntime":null}"#;
assert_eq!(ser, exp);
new_block.runtime_updates = false;
new_block.with_runtime = false;
let event_dec: FollowEvent<String> = serde_json::from_str(exp).unwrap();
assert!(matches!(
event_dec, FollowEvent::NewBlock(ref dec) if dec == &new_block
Expand Down
30 changes: 15 additions & 15 deletions client/rpc-spec-v2/src/chain_head/subscription/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ struct BlockState {

/// The state of a single subscription ID.
struct SubscriptionState<Block: BlockT> {
/// The `runtime_updates` parameter flag of the subscription.
runtime_updates: bool,
/// The `with_runtime` parameter flag of the subscription.
with_runtime: bool,
/// Signals the "Stop" event.
tx_stop: Option<oneshot::Sender<()>>,
/// Track the block hashes available for this subscription.
Expand Down Expand Up @@ -234,35 +234,35 @@ impl<Block: BlockT> SubscriptionState<Block> {
/// executing an RPC method call.
pub struct BlockGuard<Block: BlockT, BE: Backend<Block>> {
hash: Block::Hash,
runtime_updates: bool,
with_runtime: bool,
backend: Arc<BE>,
}

// Custom implementation of Debug to avoid bounds on `backend: Debug` for `unwrap_err()` needed for
// testing.
impl<Block: BlockT, BE: Backend<Block>> std::fmt::Debug for BlockGuard<Block, BE> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "BlockGuard hash {:?} runtime_updates {:?}", self.hash, self.runtime_updates)
write!(f, "BlockGuard hash {:?} with_runtime {:?}", self.hash, self.with_runtime)
}
}

impl<Block: BlockT, BE: Backend<Block>> BlockGuard<Block, BE> {
/// Construct a new [`BlockGuard`] .
fn new(
hash: Block::Hash,
runtime_updates: bool,
with_runtime: bool,
backend: Arc<BE>,
) -> Result<Self, SubscriptionManagementError> {
backend
.pin_block(hash)
.map_err(|err| SubscriptionManagementError::Custom(err.to_string()))?;

Ok(Self { hash, runtime_updates, backend })
Ok(Self { hash, with_runtime, backend })
}

/// The `runtime_updates` flag of the subscription.
pub fn has_runtime_updates(&self) -> bool {
self.runtime_updates
/// The `with_runtime` flag of the subscription.
pub fn has_runtime(&self) -> bool {
self.with_runtime
}
}

Expand Down Expand Up @@ -310,12 +310,12 @@ impl<Block: BlockT, BE: Backend<Block>> SubscriptionsInner<Block, BE> {
pub fn insert_subscription(
&mut self,
sub_id: String,
runtime_updates: bool,
with_runtime: bool,
) -> Option<oneshot::Receiver<()>> {
if let Entry::Vacant(entry) = self.subs.entry(sub_id) {
let (tx_stop, rx_stop) = oneshot::channel();
let state = SubscriptionState::<Block> {
runtime_updates,
with_runtime,
tx_stop: Some(tx_stop),
blocks: Default::default(),
};
Expand Down Expand Up @@ -501,7 +501,7 @@ impl<Block: BlockT, BE: Backend<Block>> SubscriptionsInner<Block, BE> {
return Err(SubscriptionManagementError::BlockHashAbsent)
}

BlockGuard::new(hash, sub.runtime_updates, self.backend.clone())
BlockGuard::new(hash, sub.with_runtime, self.backend.clone())
}
}

Expand Down Expand Up @@ -608,7 +608,7 @@ mod tests {
#[test]
fn sub_state_register_twice() {
let mut sub_state = SubscriptionState::<Block> {
runtime_updates: false,
with_runtime: false,
tx_stop: None,
blocks: Default::default(),
};
Expand All @@ -633,7 +633,7 @@ mod tests {
#[test]
fn sub_state_register_unregister() {
let mut sub_state = SubscriptionState::<Block> {
runtime_updates: false,
with_runtime: false,
tx_stop: None,
blocks: Default::default(),
};
Expand Down Expand Up @@ -709,7 +709,7 @@ mod tests {

let block = subs.lock_block(&id, hash).unwrap();
// Subscription started with runtime updates
assert_eq!(block.has_runtime_updates(), true);
assert_eq!(block.has_runtime(), true);

let invalid_id = "abc-invalid".to_string();
let err = subs.unpin_block(&invalid_id, hash).unwrap_err();
Expand Down
Loading

0 comments on commit ec86b50

Please sign in to comment.