1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright Rivtower Technologies LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![allow(non_camel_case_types)]

use crate::internals::construct_params;
use crate::rpc_types::ethereum_types::{
    EthBlock, EthCallRequest, EthFilter, EthLog, EthReceipt, EthRpcTransaction,
    EthTransactionRequest,
};
use crate::rpc_types::{
    Block, BlockNumber, Boolean, CallRequest, CallResult, CensorAddrs, Data, Data20, Data32,
    Filter, FilterChanges, Id, Integer, LicenseInfo, Log, MetaData, OneItemTupleTrick, PeersInfo,
    PoolTxNum, Quantity, Receipt, RpcTransaction, SoftwareVersion, TxResponse, Version,
};
/// JSON-RPC Request.
use serde_json;

pub type Logs = Vec<Log>;
pub type EthLogs = Vec<EthLog>;
pub type Accounts = Vec<Data20>;

#[derive(Debug, Clone, PartialEq)]
pub struct RequestInfo {
    pub jsonrpc: Option<Version>,
    pub id: Id,
}

impl RequestInfo {
    pub fn new(jsonrpc: Option<Version>, id: Id) -> Self {
        RequestInfo { jsonrpc, id }
    }
    pub fn null() -> Self {
        RequestInfo {
            jsonrpc: None,
            id: Id::Null,
        }
    }
}

impl Default for RequestInfo {
    fn default() -> Self {
        RequestInfo::new(Some(Version::default()), Id::default())
    }
}

/// JSON-RPC 2.0 Request object (http://www.jsonrpc.org/specification#request_object)
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Request {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jsonrpc: Option<Version>,
    #[serde(default, skip_serializing_if = "Id::is_null")]
    pub id: Id,
    /// Contain method and params.
    #[serde(flatten)]
    pub call: Call,
}

impl Request {
    pub fn new(jsonrpc: Option<Version>, id: Id, call: Call) -> Self {
        Request { jsonrpc, id, call }
    }
    pub fn get_method(&self) -> &str {
        self.call.get_method()
    }
    pub fn get_info(&self) -> RequestInfo {
        RequestInfo::new(self.jsonrpc.clone(), self.id.clone())
    }
}

impl Into<String> for Request {
    fn into(self) -> String {
        serde_json::to_string(&self).unwrap()
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct PartialRequest {
    pub jsonrpc: Option<Version>,
    pub id: Id,
    /// Contain method and params.
    #[serde(flatten)]
    pub call: Option<PartialCall>,
}

impl PartialRequest {
    pub fn get_info(&self) -> RequestInfo {
        RequestInfo::new(self.jsonrpc.clone(), self.id.clone())
    }
}

macro_rules! define_call {
    ($( ($enum_name:ident, $params_name:ident: $params_list:expr, $result_type:ident) ),+ ,) => {
        define_call!($( ($enum_name, $params_name: $params_list, $result_type) ),+);
    };
    ($( ($enum_name:ident, $params_name:ident: $params_list:expr, $result_type:ident) ),+ ) => {

        $(
            construct_params!($params_name: $params_list, $result_type);
        )+


        #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
        #[serde(untagged)]
        #[allow(clippy::large_enum_variant)]   // TODO
        pub enum ResponseResult {
            #[serde(rename = "null")]
            Null,
            $(
                $enum_name($result_type),
            )+
        }

        impl Default for ResponseResult {
            fn default() -> Self {
                ResponseResult::Null
            }
        }

        #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
        #[serde(tag = "method", rename_all = "camelCase")]
        pub enum Call {
            $(
                $enum_name { params: $params_name},
            )+
        }

        #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
        #[serde(tag = "method", rename_all = "camelCase")]
        pub enum PartialCall {
            $(
                $enum_name {
                    params: Option<serde_json::Value>
                },
            )+
        }

        impl Call {
            pub fn get_method(&self) -> &str {
                match self {
                    $(
                        Call::$enum_name { ref params } => params.method_name(),
                    )+
                }
            }
            pub fn into_request(self, id: u64) -> Request {
                Request::new(
                    Some(Version::default()),
                    Id::Num(id),
                    self,
                )
            }
        }

        $(
            impl Into<$params_name> for Call {
                fn into(self) -> $params_name{
                    if let Call::$enum_name{ params } = self {
                        params
                    } else {
                        // IMHO, in Rust, no static check can do this.
                        // If https://github.com/rust-lang/rfcs/pull/1450 merged,
                        // I think I can remove this panic.
                        panic!("The method and params are one to one correspondence.")
                    }
                }
            }

            impl From<$params_name> for Call {
                fn from(params: $params_name) -> Call {
                    Call::$enum_name{ params }
                }
            }

            impl $params_name {
                pub fn into_request(self, id: u64) -> Request {
                    Request::new(
                        Some(Version::default()),
                        Id::Num(id),
                        self.into(),
                    )
                }
            }
        )+
    };
}

pub trait JsonRpcRequest {
    type Response;
    fn required_len() -> usize;
    fn valid_len() -> usize;
    fn method_name(&self) -> &'static str;
    fn value_vec(self) -> Vec<serde_json::Value>;
}

// Q. How to add a JSON-RPC method?
//
// A.
//  First, add a tuple into the follow macro.
//
//    - The 1st item in tuple is a enum name used in `Call` / `PartialCall`.
//      The enum name will used to generate the JSON-RPC method name.
//      The enum name is PascalCase and JSON-RPC method name is camelCase.
//
//    - The 2st item is params type name and it's structure.
//      The params type has some methods, such as `new()` and `method_name()`.
//      More details can found in the definition of `construct_params`.
//
//    - The 3rd item is the type of result of Response object on success.
//
//  Second, implement `TryInto<ProtoRequest>` for the new params type.
//
//  DONE!
#[macro_export]
macro_rules! impl_for_each_jsonrpc_requests {
    ($macro:ident) => {
        $macro!(
            (BlockNumber, BlockNumberParams: [], Quantity),
            (PeerCount, PeerCountParams: [], Quantity),
            (SendRawTransaction, SendRawTransactionParams: [Data], TxResponse),
            (SendTransaction, SendTransactionParams: [Data], TxResponse),
            (GetBlockByHash, GetBlockByHashParams: [Data32, Boolean], Block),
            (GetBlockByNumber, GetBlockByNumberParams: [BlockNumber, Boolean], Block),
            (GetTransactionReceipt, GetTransactionReceiptParams: [Data32], Receipt),
            (GetLogs, GetLogsParams: [Filter], Logs),
            (GetTransactionCount, GetTransactionCountParams: [Data20, BlockNumber], Quantity),
            (GetCode, GetCodeParams: [Data20, BlockNumber], Data),
            (GetAbi, GetAbiParams: [Data20, BlockNumber], Data),
            (GetBalance, GetBalanceParams: [Data20, BlockNumber], Quantity),
            (NewFilter, NewFilterParams: [Filter], Quantity),
            (NewBlockFilter, NewBlockFilterParams: [], Quantity),
            (UninstallFilter, UninstallFilterParams: [Quantity], Boolean),
            (GetFilterChanges, GetFilterChangesParams: [Quantity], FilterChanges),
            (GetFilterLogs, GetFilterLogsParams: [Quantity], Logs),
            (GetTransactionProof, GetTransactionProofParams: [Data32], Data),
            (GetMetaData, GetMetaDataParams: [BlockNumber], MetaData),
            (GetStateProof, GetStateProofParams: [Data20, Data32, BlockNumber], Data),
            (GetBlockHeader, GetBlockHeaderParams: [BlockNumber], Data),
            (GetStorageAt, GetStorageKeyParams: [Data20, Data32, BlockNumber], Data),
            (GetVersion, GetVersionParams: [], SoftwareVersion),
            (EstimateQuota, EstimateQuotaParams: [CallRequest, BlockNumber], Quantity),
            (LicenseInfo, LicenseInfoParams: [], LicenseInfo),
            (GetPoolTxNum, GetPoolTxNumParams: [], PoolTxNum),
            (OpCensoredAddress, OpCensoredAddressParams: [Integer, Data20], Boolean),
            (PeersInfo, PeersInfoParams: [
                #[serde(default)]
                Boolean
            ], PeersInfo),
            (GetTransaction, GetTransactionParams: [
                Data32,
                #[serde(default)]
                Boolean
            ], RpcTransaction),
            (Call, CallParams: [
                CallRequest,
                BlockNumber,
                #[serde(default)]
                Boolean
            ], CallResult),
            (GetCensoredAddrs, GetCensoredAddrsParams: [], CensorAddrs),
            // ethereum jsonrpc
            (eth_blockNumber, eth_blockNumberParams: [], Quantity),
            (eth_chainId, eth_chainIdParams: [], Quantity),
            (eth_getBlockByHash, eth_getBlockByHashParams: [Data32, Boolean], EthBlock),
            (eth_getBlockByNumber, eth_getBlockByNumberParams: [BlockNumber, Boolean], EthBlock),
            (eth_getTransactionByHash, eth_getTransactionByHashParams: [Data32], EthRpcTransaction),
            (eth_getTransactionByBlockHashAndIndex, eth_getTransactionByBlockHashAndIndexParams: [Data32, Integer], EthRpcTransaction),
            (eth_getTransactionByBlockNumberAndIndex, eth_getTransactionByBlockNumberAndIndexParams: [BlockNumber, Integer], EthRpcTransaction),
            (eth_getBlockTransactionCountByHash, eth_getBlockTransactionCountByHashParams: [Data32], Integer),
            (eth_getBlockTransactionCountByNumber, eth_getBlockTransactionCountByNumberParams: [BlockNumber], Integer),
            (eth_getTransactionReceipt, eth_getTransactionReceiptParams: [Data32], EthReceipt),
            (eth_getBalance, eth_getBalanceParams: [Data20, BlockNumber], Quantity),
            (eth_syncing, eth_syncingParams: [], Boolean),
            (eth_getStorageAt, eth_getStorageAtParams: [Data20, Quantity, BlockNumber], Data),
            (eth_getCode, eth_getCodeParams: [Data20, BlockNumber], Data),
            (eth_getTransactionCount, eth_getTransactionCountParams: [Data20, BlockNumber], Quantity),
            (eth_getLogs, eth_getLogsParams: [EthFilter], EthLogs),
            (eth_call, eth_callParams: [EthCallRequest, BlockNumber], Data),
            (eth_estimateGas, eth_estimateGasParams: [
                EthCallRequest,
                #[serde(default)]
                BlockNumber
            ], Quantity),
            (eth_gasPrice, eth_gasPriceParams: [], Quantity),
            (eth_maxPriorityFeePerGas, eth_maxPriorityFeePerGasParams: [], Quantity),
            (eth_sendTransaction, eth_sendTransactionParams: [EthTransactionRequest], Data32),
            (eth_sendRawTransaction, eth_sendRawTransactionParams: [Data], Data32),
            (eth_accounts, eth_accountsParams: [], Accounts),
            // net jsonrpc
            (net_version, net_versionParams: [], Integer),
        );
    };
}

impl_for_each_jsonrpc_requests!(define_call);