[RPC-Spec-V2] chainHead: use integer for block index and adjust RuntimeVersion JSON format (#1666)

This PR adjusts the serialized format of the the returned RuntimeVersion
in the rpc-spec-v2 methods. This is done to match the format defined
here:
https://paritytech.github.io/json-rpc-interface-spec/api/chainHead_unstable_follow.html#about-the-runtime

- ##### `apis` field as object
`apis` field of `RuntimeVersion` is now returned as an object, e.g. 
```
"apis": {
      "0xdf6acb689907609b": 3,
      "0x37e397fc7c91f5e4": 1,
}
```
instead of 
```
"apis": [
      ["0xdf6acb689907609b", 3],
      ["0x37e397fc7c91f5e4", 1],
]
```
- ##### removed `stateVersion` and `authoringVersion`
`stateVersion` and `authoringVersion` are no longer returned in the
`RuntimeVersion` JSON Object.

- ##### block index in chain head events as integer

### Related Issues

Closes: #1507
Closes: #1146

### Testing Done
Adjusted existing tests to make sure data is returned in the correct
format.
This commit is contained in:
Tadeo Hepperle
2023-09-29 09:08:19 +02:00
committed by GitHub
parent 7ca0d65f19
commit 379be3d7c3
6 changed files with 56 additions and 37 deletions
@@ -34,7 +34,6 @@ use serde::{Deserialize, Serialize};
#[serde(rename_all = "camelCase")]
pub struct TransactionBroadcasted {
/// The number of peers the transaction was broadcasted to.
#[serde(with = "as_string")]
pub num_peers: usize,
}
@@ -45,7 +44,6 @@ pub struct TransactionBlock<Hash> {
/// The hash of the block the transaction was included into.
pub hash: Hash,
/// The index (zero-based) of the transaction within the body of the block.
#[serde(with = "as_string")]
pub index: usize,
}
@@ -224,22 +222,6 @@ impl<Hash> From<TransactionEventIR<Hash>> for TransactionEvent<Hash> {
}
}
/// Serialize and deserialize helper as string.
mod as_string {
use super::*;
use serde::{Deserializer, Serializer};
pub fn serialize<S: Serializer>(data: &usize, serializer: S) -> Result<S::Ok, S::Error> {
data.to_string().serialize(serializer)
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<usize, D::Error> {
String::deserialize(deserializer)?
.parse()
.map_err(|e| serde::de::Error::custom(format!("Parsing failed: {}", e)))
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -263,7 +245,7 @@ mod tests {
TransactionEvent::Broadcasted(TransactionBroadcasted { num_peers: 2 });
let ser = serde_json::to_string(&event).unwrap();
let exp = r#"{"event":"broadcasted","numPeers":"2"}"#;
let exp = r#"{"event":"broadcasted","numPeers":2}"#;
assert_eq!(ser, exp);
let event_dec: TransactionEvent<()> = serde_json::from_str(exp).unwrap();
@@ -288,7 +270,7 @@ mod tests {
}));
let ser = serde_json::to_string(&event).unwrap();
let exp = r#"{"event":"bestChainBlockIncluded","block":{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","index":"2"}}"#;
let exp = r#"{"event":"bestChainBlockIncluded","block":{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","index":2}}"#;
assert_eq!(ser, exp);
let event_dec: TransactionEvent<H256> = serde_json::from_str(exp).unwrap();
@@ -303,7 +285,7 @@ mod tests {
});
let ser = serde_json::to_string(&event).unwrap();
let exp = r#"{"event":"finalized","block":{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","index":"10"}}"#;
let exp = r#"{"event":"finalized","block":{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","index":10}}"#;
assert_eq!(ser, exp);
let event_dec: TransactionEvent<H256> = serde_json::from_str(exp).unwrap();