fc_rpc_core/types/
pubsub.rs

1// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
2// This file is part of Frontier.
3//
4// Copyright (c) 2015-2020 Parity Technologies (UK) Ltd.
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Pub-Sub types.
20
21use ethereum_types::H256;
22use serde::{Deserialize, Deserializer, Serialize, Serializer};
23use serde::de::Error;
24use serde_json::{Value, from_value};
25use crate::types::{RichHeader, Filter, Log};
26
27/// Subscription result.
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub enum Result {
30	/// New block header.
31	Header(Box<RichHeader>),
32	/// Log
33	Log(Box<Log>),
34	/// Transaction hash
35	TransactionHash(H256),
36	/// SyncStatus
37	SyncState(PubSubSyncStatus)
38}
39
40/// PubSbub sync status
41#[derive(Debug, Serialize, Eq, PartialEq, Clone)]
42#[serde(rename_all="camelCase")]
43pub struct PubSubSyncStatus {
44	/// is_major_syncing?
45	pub syncing: bool,
46}
47
48impl Serialize for Result {
49	fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
50		where S: Serializer
51	{
52		match *self {
53			Result::Header(ref header) => header.serialize(serializer),
54			Result::Log(ref log) => log.serialize(serializer),
55			Result::TransactionHash(ref hash) => hash.serialize(serializer),
56			Result::SyncState(ref sync) => sync.serialize(serializer),
57		}
58	}
59}
60
61/// Subscription kind.
62#[derive(Debug, Deserialize, PartialEq, Eq, Hash, Clone)]
63#[serde(deny_unknown_fields)]
64#[serde(rename_all = "camelCase")]
65pub enum Kind {
66	/// New block headers subscription.
67	NewHeads,
68	/// Logs subscription.
69	Logs,
70	/// New Pending Transactions subscription.
71	NewPendingTransactions,
72	/// Node syncing status subscription.
73	Syncing,
74}
75
76/// Subscription kind.
77#[derive(Debug, PartialEq, Eq, Hash, Clone)]
78pub enum Params {
79	/// No parameters passed.
80	None,
81	/// Log parameters.
82	Logs(Filter),
83}
84
85impl Default for Params {
86	fn default() -> Self {
87		Params::None
88	}
89}
90
91impl<'a> Deserialize<'a> for Params {
92	fn deserialize<D>(deserializer: D) -> ::std::result::Result<Params, D::Error>
93	where D: Deserializer<'a> {
94		let v: Value = Deserialize::deserialize(deserializer)?;
95
96		if v.is_null() {
97			return Ok(Params::None);
98		}
99
100		from_value(v.clone()).map(Params::Logs)
101			.map_err(|e| D::Error::custom(format!("Invalid Pub-Sub parameters: {}", e)))
102	}
103}