add reconnecting tests for unstable_backend (#1765)

* add tests for unstable_backend

---------

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
Pavlo Khrystenko
2024-09-30 00:03:11 +02:00
committed by GitHub
parent ddb5d4c9d7
commit 8f2c92f0ef
2 changed files with 962 additions and 193 deletions
+897 -191
View File
File diff suppressed because it is too large Load Diff
+65 -2
View File
@@ -321,6 +321,7 @@ impl<T: Config> UnstableRpcMethods<T> {
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "event")]
#[cfg_attr(test, derive(Serialize))]
pub enum FollowEvent<Hash> {
/// The latest finalized block.
///
@@ -363,6 +364,8 @@ pub enum FollowEvent<Hash> {
/// This is the first event generated by the `follow` subscription
/// and is submitted only once.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(Serialize))]
#[cfg_attr(test, serde(rename_all = "camelCase"))]
pub struct Initialized<Hash> {
/// The hashes of the last finalized blocks.
pub finalized_block_hashes: Vec<Hash>,
@@ -404,6 +407,7 @@ impl<'de, Hash: Deserialize<'de>> Deserialize<'de> for Initialized<Hash> {
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "type")]
#[cfg_attr(test, derive(Serialize))]
pub enum RuntimeEvent {
/// The runtime version of this block.
Valid(RuntimeVersionEvent),
@@ -418,6 +422,7 @@ pub enum RuntimeEvent {
/// - blocks that suffered a change in runtime compared with their parents
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct RuntimeVersionEvent {
/// Details about this runtime.
pub spec: RuntimeSpec,
@@ -427,6 +432,7 @@ pub struct RuntimeVersionEvent {
/// the "initialized" event of `chainHead_follow` if the `withRuntime` flag is set.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct RuntimeSpec {
/// Opaque string indicating the name of the chain.
pub spec_name: String,
@@ -453,13 +459,18 @@ pub struct RuntimeSpec {
///
/// **Note:** In Substrate, the keys in the apis field consists of the hexadecimal-encoded 8-bytes blake2
/// hash of the name of the API. For example, the `TaggedTransactionQueue` API is 0xd2bc9897eed08f15.
#[serde(with = "hashmap_as_tuple_list")]
#[serde(deserialize_with = "hashmap_as_tuple_list::deserialize")]
#[cfg_attr(
test,
serde(serialize_with = "hashmap_as_tuple_list::for_test::serialize")
)]
pub apis: HashMap<String, u32>,
}
/// The operation could not be processed due to an error.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct ErrorEvent {
/// Reason of the error.
pub error: String,
@@ -468,6 +479,7 @@ pub struct ErrorEvent {
/// Indicate a new non-finalized block.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct NewBlock<Hash> {
/// The hash of the new block.
pub block_hash: Hash,
@@ -485,6 +497,7 @@ pub struct NewBlock<Hash> {
/// Indicate the block hash of the new best block.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct BestBlockChanged<Hash> {
/// The block hash of the new best block.
pub best_block_hash: Hash,
@@ -493,6 +506,7 @@ pub struct BestBlockChanged<Hash> {
/// Indicate the finalized and pruned block hashes.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct Finalized<Hash> {
/// Block hashes that are finalized.
pub finalized_block_hashes: Vec<Hash>,
@@ -503,6 +517,7 @@ pub struct Finalized<Hash> {
/// Indicate the operation id of the event.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationId {
/// The operation id of the event.
pub operation_id: String,
@@ -511,6 +526,7 @@ pub struct OperationId {
/// The response of the `chainHead_body` method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationBodyDone {
/// The operation id of the event.
pub operation_id: String,
@@ -521,6 +537,7 @@ pub struct OperationBodyDone {
/// The response of the `chainHead_call` method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationCallDone {
/// The operation id of the event.
pub operation_id: String,
@@ -531,6 +548,7 @@ pub struct OperationCallDone {
/// The response of the `chainHead_call` method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationStorageItems {
/// The operation id of the event.
pub operation_id: String,
@@ -541,6 +559,7 @@ pub struct OperationStorageItems {
/// Indicate a problem during the operation.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct OperationError {
/// The operation id of the event.
pub operation_id: String,
@@ -551,6 +570,7 @@ pub struct OperationError {
/// The storage result.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct StorageResult {
/// The hex-encoded key of the result.
pub key: Bytes,
@@ -562,6 +582,7 @@ pub struct StorageResult {
/// The type of the storage query.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub enum StorageResultType {
/// Fetch the value of the provided key.
Value(Bytes),
@@ -575,6 +596,7 @@ pub enum StorageResultType {
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "result")]
#[cfg_attr(test, derive(Serialize))]
pub enum MethodResponse {
/// The method has started.
Started(MethodResponseStarted),
@@ -585,6 +607,7 @@ pub enum MethodResponse {
/// The `started` result of a method.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(test, derive(Serialize))]
pub struct MethodResponseStarted {
/// The operation id of the response.
pub operation_id: String,
@@ -704,6 +727,7 @@ impl<Hash: BlockHash> Stream for TransactionSubscription<Hash> {
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "event")]
#[cfg_attr(test, derive(Serialize))]
pub enum TransactionStatus<Hash> {
/// Transaction is part of the future queue.
Validated,
@@ -745,11 +769,16 @@ pub enum TransactionStatus<Hash> {
/// Details of a block that a transaction is seen in.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct TransactionBlockDetails<Hash> {
/// The block hash.
pub hash: Hash,
/// The index of the transaction in the block.
#[serde(with = "unsigned_number_as_string")]
#[serde(deserialize_with = "unsigned_number_as_string::deserialize")]
#[cfg_attr(
test,
serde(serialize_with = "unsigned_number_as_string::for_test::serialize")
)]
pub index: u64,
}
@@ -804,6 +833,18 @@ pub(crate) mod unsigned_number_as_string {
Ok(v.into())
}
}
#[cfg(test)]
pub mod for_test {
use serde::ser::Serializer;
/// Serialize a number as string
pub fn serialize<S>(item: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&item.to_string())
}
}
}
/// A temporary shim to decode "spec.apis" if it comes back as an array like:
@@ -880,6 +921,28 @@ pub(crate) mod hashmap_as_tuple_list {
Ok(map)
}
}
#[cfg(test)]
pub mod for_test {
use std::collections::HashMap;
use std::hash::Hash;
use serde::ser::{Serialize, SerializeSeq, Serializer};
/// Serialize hashmap as list of tuples
pub fn serialize<S, K: Eq + Hash + Serialize, V: Serialize>(
item: &HashMap<K, V>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(None)?;
for i in item {
seq.serialize_element(&i)?;
}
seq.end()
}
}
}
#[cfg(test)]