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
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use cfx_types::{Bloom, H160, H256};
use rlp_derive::{RlpDecodable, RlpEncodable};

use super::NodeType;
use crate::message::RequestId;
use cfx_internal_common::ChainIdParamsDeprecated;
use cfx_storage::{NodeMerkleProof, StateProof, TrieProof};
use primitives::{
    BlockHeader, BlockReceipts, Receipt, SignedTransaction, StateRoot,
    StorageRoot,
};

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct StatusPingDeprecatedV1 {
    pub genesis_hash: H256,
    pub node_type: NodeType,
    pub protocol_version: u8,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct StatusPongDeprecatedV1 {
    pub best_epoch: u64,
    pub genesis_hash: H256,
    pub node_type: NodeType,
    pub protocol_version: u8,
    pub terminals: Vec<H256>,
}

#[derive(Clone, Debug, RlpEncodable, RlpDecodable)]
pub struct StatusPingV2 {
    pub chain_id: ChainIdParamsDeprecated,
    pub genesis_hash: H256,
    pub node_type: NodeType,
}

#[derive(Clone, Debug, RlpEncodable, RlpDecodable)]
pub struct StatusPongV2 {
    pub best_epoch: u64,
    pub chain_id: ChainIdParamsDeprecated,
    pub genesis_hash: H256,
    pub node_type: NodeType,
    pub terminals: Vec<H256>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetBlockHashesByEpoch {
    pub request_id: RequestId,
    pub epochs: Vec<u64>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct BlockHashes {
    pub request_id: RequestId,
    pub hashes: Vec<H256>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetBlockHeaders {
    pub request_id: RequestId,
    pub hashes: Vec<H256>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct BlockHeaders {
    pub request_id: RequestId,
    pub headers: Vec<BlockHeader>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct NewBlockHashes {
    pub hashes: Vec<H256>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct SendRawTx {
    pub raw: Vec<u8>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetReceipts {
    pub request_id: RequestId,
    pub epochs: Vec<u64>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct ReceiptsWithEpoch {
    pub epoch: u64,
    pub epoch_receipts: Vec<BlockReceipts>,
}

#[derive(Clone, Debug, RlpEncodable, RlpDecodable)]
pub struct Receipts {
    pub request_id: RequestId,
    pub receipts: Vec<ReceiptsWithEpoch>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetTxs {
    pub request_id: RequestId,
    pub hashes: Vec<H256>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct Txs {
    pub request_id: RequestId,
    pub txs: Vec<SignedTransaction>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetWitnessInfo {
    pub request_id: RequestId,
    pub witnesses: Vec<u64>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct WitnessInfoWithHeight {
    pub height: u64,
    pub state_root_hashes: Vec<H256>,
    pub receipt_hashes: Vec<H256>,
    pub bloom_hashes: Vec<H256>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct WitnessInfo {
    pub request_id: RequestId,
    pub infos: Vec<WitnessInfoWithHeight>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetBlooms {
    pub request_id: RequestId,
    pub epochs: Vec<u64>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct BloomWithEpoch {
    pub epoch: u64,
    pub bloom: Bloom,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct Blooms {
    pub request_id: RequestId,
    pub blooms: Vec<BloomWithEpoch>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetBlockTxs {
    pub request_id: RequestId,
    pub hashes: Vec<H256>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct BlockTxsWithHash {
    pub hash: H256,
    pub block_txs: Vec<SignedTransaction>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct BlockTxs {
    pub request_id: RequestId,
    pub block_txs: Vec<BlockTxsWithHash>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetStateRoots {
    pub request_id: RequestId,
    pub epochs: Vec<u64>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct StateRootWithEpoch {
    pub epoch: u64,

    // state root is validated against witness info retrieved previously;
    // no additional proof needed
    pub state_root: StateRoot,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct StateRoots {
    pub request_id: RequestId,
    pub state_roots: Vec<StateRootWithEpoch>,
}

#[derive(
    Clone,
    Debug,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    RlpEncodable,
    RlpDecodable,
)]
pub struct StateKey {
    pub epoch: u64,
    pub key: Vec<u8>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetStateEntries {
    pub request_id: RequestId,
    pub keys: Vec<StateKey>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct StateEntryProof {
    pub state_proof: StateProof,

    // state root is validated against witness info retrieved previously;
    // no additional proof needed
    pub state_root: StateRoot,
    pub prev_snapshot_state_root: Option<StateRoot>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct StateEntryWithKey {
    pub key: StateKey,
    pub entry: Option<Vec<u8>>,
    pub proof: StateEntryProof,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct StateEntries {
    pub request_id: RequestId,
    pub entries: Vec<StateEntryWithKey>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetTxInfos {
    pub request_id: RequestId,
    pub hashes: Vec<H256>,
}

// TODO(thegaram): consider combining `receipt_proof` and
// `maybe_prev_receipt_proof` into a single proof in later protocol versions.
#[derive(Clone, Debug, RlpEncodable, RlpDecodable)]
pub struct TxInfo {
    pub epoch: u64,

    // tx-related fields
    pub tx: SignedTransaction,
    pub tx_index_in_block: usize,
    pub num_txs_in_block: usize,
    pub tx_proof: TrieProof,

    // receipt-related fields
    pub receipt: Receipt,
    pub block_index_in_epoch: usize,
    pub num_blocks_in_epoch: usize,
    pub block_index_proof: TrieProof,
    pub receipt_proof: TrieProof,

    // prior_gas_used-related fields
    pub maybe_prev_receipt: Option<Receipt>,
    pub maybe_prev_receipt_proof: Option<TrieProof>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct TxInfos {
    pub request_id: RequestId,
    pub infos: Vec<TxInfo>,
}

#[derive(
    Clone,
    Debug,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    RlpEncodable,
    RlpDecodable,
)]
pub struct StorageRootKey {
    pub epoch: u64,
    pub address: H160,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct GetStorageRoots {
    pub request_id: RequestId,
    pub keys: Vec<StorageRootKey>,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct StorageRootProof {
    pub merkle_proof: NodeMerkleProof,

    // state root is validated against witness info retrieved previously;
    // no additional proof needed
    pub state_root: StateRoot,
    pub prev_snapshot_state_root: Option<StateRoot>,
}

#[derive(Clone, Debug, RlpEncodable, RlpDecodable)]
pub struct StorageRootWithKey {
    pub key: StorageRootKey,
    pub root: StorageRoot,
    pub proof: StorageRootProof,
}

#[derive(Clone, Debug, Default, RlpEncodable, RlpDecodable)]
pub struct StorageRoots {
    pub request_id: RequestId,
    pub roots: Vec<StorageRootWithKey>,
}