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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use std::{collections::BTreeMap, ops::Deref};
use ethereum_types::{Bloom as H2048, H160, H256, U256};
use serde::{ser::Error, Serialize, Serializer};
use types::{encoded::Header as EthHeader, BlockNumber};
use v1::types::{Bytes, Transaction};
#[derive(Debug)]
pub enum BlockTransactions {
Hashes(Vec<H256>),
Full(Vec<Transaction>),
}
impl Serialize for BlockTransactions {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
BlockTransactions::Hashes(ref hashes) => hashes.serialize(serializer),
BlockTransactions::Full(ref ts) => ts.serialize(serializer),
}
}
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Block {
pub hash: Option<H256>,
pub parent_hash: H256,
#[serde(rename = "sha3Uncles")]
pub uncles_hash: H256,
pub author: H160,
pub miner: H160,
pub state_root: H256,
pub transactions_root: H256,
pub receipts_root: H256,
pub number: Option<U256>,
pub gas_used: U256,
pub gas_limit: U256,
pub extra_data: Bytes,
pub logs_bloom: Option<H2048>,
pub timestamp: U256,
pub difficulty: U256,
pub total_difficulty: Option<U256>,
pub seal_fields: Vec<Bytes>,
#[serde(skip_serializing_if = "Option::is_none")]
pub base_fee_per_gas: Option<U256>,
pub uncles: Vec<H256>,
pub transactions: BlockTransactions,
pub size: Option<U256>,
}
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Header {
pub hash: Option<H256>,
pub parent_hash: H256,
#[serde(rename = "sha3Uncles")]
pub uncles_hash: H256,
pub author: H160,
pub miner: H160,
pub state_root: H256,
pub transactions_root: H256,
pub receipts_root: H256,
pub number: Option<U256>,
pub gas_used: U256,
pub gas_limit: U256,
pub extra_data: Bytes,
pub logs_bloom: H2048,
pub timestamp: U256,
pub difficulty: U256,
pub seal_fields: Vec<Bytes>,
#[serde(skip_serializing_if = "Option::is_none")]
pub base_fee_per_gas: Option<U256>,
pub size: Option<U256>,
}
impl Header {
pub fn new(h: &EthHeader, eip1559_transition: BlockNumber) -> Self {
let eip1559_enabled = h.number() >= eip1559_transition;
Header {
hash: Some(h.hash()),
size: Some(h.rlp().as_raw().len().into()),
parent_hash: h.parent_hash(),
uncles_hash: h.uncles_hash(),
author: h.author(),
miner: h.author(),
state_root: h.state_root(),
transactions_root: h.transactions_root(),
receipts_root: h.receipts_root(),
number: Some(h.number().into()),
gas_used: h.gas_used(),
gas_limit: h.gas_limit(),
logs_bloom: h.log_bloom(),
timestamp: h.timestamp().into(),
difficulty: h.difficulty(),
extra_data: h.extra_data().into(),
seal_fields: h.view().decode_seal(eip1559_enabled)
.expect("Client/Miner returns only valid headers. We only serialize headers from Client/Miner; qed")
.into_iter().map(Into::into).collect(),
base_fee_per_gas: {
if eip1559_enabled {
Some(h.base_fee())
} else {
None
}
},
}
}
}
pub type RichBlock = Rich<Block>;
pub type RichHeader = Rich<Header>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Rich<T> {
pub inner: T,
pub extra_info: BTreeMap<String, String>,
}
impl<T> Deref for Rich<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T: Serialize> Serialize for Rich<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use serde_json::{to_value, Value};
let serialized = (to_value(&self.inner), to_value(&self.extra_info));
if let (Ok(Value::Object(mut value)), Ok(Value::Object(extras))) = serialized {
value.extend(extras);
value.serialize(serializer)
} else {
Err(S::Error::custom(
"Unserializable structures: expected objects",
))
}
}
}
#[cfg(test)]
mod tests {
use super::{Block, BlockTransactions, Header, RichBlock, RichHeader};
use ethereum_types::{Bloom as H2048, H160, H256, H64, U256};
use serde_json;
use std::collections::BTreeMap;
use v1::types::{Bytes, Transaction};
#[test]
fn test_serialize_block_transactions() {
let t = BlockTransactions::Full(vec![Transaction::default()]);
let serialized = serde_json::to_string(&t).unwrap();
assert_eq!(
serialized,
r#"[{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":"0x","creates":null,"raw":"0x","publicKey":null,"chainId":null,"v":"0x0","r":"0x0","s":"0x0","condition":null}]"#
);
let t = BlockTransactions::Hashes(vec![H256::default().into()]);
let serialized = serde_json::to_string(&t).unwrap();
assert_eq!(
serialized,
r#"["0x0000000000000000000000000000000000000000000000000000000000000000"]"#
);
}
#[test]
fn test_serialize_block() {
let block = Block {
hash: Some(H256::default()),
parent_hash: H256::default(),
uncles_hash: H256::default(),
author: H160::default(),
miner: H160::default(),
state_root: H256::default(),
transactions_root: H256::default(),
receipts_root: H256::default(),
number: Some(U256::default()),
gas_used: U256::default(),
gas_limit: U256::default(),
extra_data: Bytes::default(),
logs_bloom: Some(H2048::default()),
timestamp: U256::default(),
difficulty: U256::default(),
total_difficulty: Some(U256::default()),
seal_fields: vec![Bytes::default(), Bytes::default()],
base_fee_per_gas: None,
uncles: vec![],
transactions: BlockTransactions::Hashes(vec![].into()),
size: Some(69.into()),
};
let serialized_block = serde_json::to_string(&block).unwrap();
let rich_block = RichBlock {
inner: block,
extra_info: map![
"mixHash".into() => format!("{:?}", H256::default()),
"nonce".into() => format!("{:?}", H64::default())
],
};
let serialized_rich_block = serde_json::to_string(&rich_block).unwrap();
assert_eq!(
serialized_block,
r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","author":"0x0000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","number":"0x0","gasUsed":"0x0","gasLimit":"0x0","extraData":"0x","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","difficulty":"0x0","totalDifficulty":"0x0","sealFields":["0x","0x"],"uncles":[],"transactions":[],"size":"0x45"}"#
);
assert_eq!(
serialized_rich_block,
r#"{"author":"0x0000000000000000000000000000000000000000","difficulty":"0x0","extraData":"0x","gasLimit":"0x0","gasUsed":"0x0","hash":"0x0000000000000000000000000000000000000000000000000000000000000000","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","sealFields":["0x","0x"],"sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","size":"0x45","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","totalDifficulty":"0x0","transactions":[],"transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","uncles":[]}"#
);
}
#[test]
fn none_size_null() {
let block = Block {
hash: Some(H256::default()),
parent_hash: H256::default(),
uncles_hash: H256::default(),
author: H160::default(),
miner: H160::default(),
state_root: H256::default(),
transactions_root: H256::default(),
receipts_root: H256::default(),
number: Some(U256::default()),
gas_used: U256::default(),
gas_limit: U256::default(),
extra_data: Bytes::default(),
logs_bloom: Some(H2048::default()),
timestamp: U256::default(),
difficulty: U256::default(),
total_difficulty: Some(U256::default()),
seal_fields: vec![Bytes::default(), Bytes::default()],
base_fee_per_gas: None,
uncles: vec![],
transactions: BlockTransactions::Hashes(vec![].into()),
size: None,
};
let serialized_block = serde_json::to_string(&block).unwrap();
let rich_block = RichBlock {
inner: block,
extra_info: map![
"mixHash".into() => format!("{:?}", H256::default()),
"nonce".into() => format!("{:?}", H64::default())
],
};
let serialized_rich_block = serde_json::to_string(&rich_block).unwrap();
assert_eq!(
serialized_block,
r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","author":"0x0000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","number":"0x0","gasUsed":"0x0","gasLimit":"0x0","extraData":"0x","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","difficulty":"0x0","totalDifficulty":"0x0","sealFields":["0x","0x"],"uncles":[],"transactions":[],"size":null}"#
);
assert_eq!(
serialized_rich_block,
r#"{"author":"0x0000000000000000000000000000000000000000","difficulty":"0x0","extraData":"0x","gasLimit":"0x0","gasUsed":"0x0","hash":"0x0000000000000000000000000000000000000000000000000000000000000000","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","sealFields":["0x","0x"],"sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","size":null,"stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","totalDifficulty":"0x0","transactions":[],"transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","uncles":[]}"#
);
}
#[test]
fn test_serialize_header() {
let header = Header {
hash: Some(H256::default()),
parent_hash: H256::default(),
uncles_hash: H256::default(),
author: H160::default(),
miner: H160::default(),
state_root: H256::default(),
transactions_root: H256::default(),
receipts_root: H256::default(),
number: Some(U256::default()),
gas_used: U256::default(),
gas_limit: U256::default(),
extra_data: Bytes::default(),
logs_bloom: H2048::default(),
timestamp: U256::default(),
difficulty: U256::default(),
seal_fields: vec![Bytes::default(), Bytes::default()],
base_fee_per_gas: None,
size: Some(69.into()),
};
let serialized_header = serde_json::to_string(&header).unwrap();
let rich_header = RichHeader {
inner: header,
extra_info: map![
"mixHash".into() => format!("{:?}", H256::default()),
"nonce".into() => format!("{:?}", H64::default())
],
};
let serialized_rich_header = serde_json::to_string(&rich_header).unwrap();
assert_eq!(
serialized_header,
r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","author":"0x0000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","number":"0x0","gasUsed":"0x0","gasLimit":"0x0","extraData":"0x","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","difficulty":"0x0","sealFields":["0x","0x"],"size":"0x45"}"#
);
assert_eq!(
serialized_rich_header,
r#"{"author":"0x0000000000000000000000000000000000000000","difficulty":"0x0","extraData":"0x","gasLimit":"0x0","gasUsed":"0x0","hash":"0x0000000000000000000000000000000000000000000000000000000000000000","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","sealFields":["0x","0x"],"sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","size":"0x45","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000"}"#
);
}
}