-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.rs
More file actions
419 lines (350 loc) · 12.1 KB
/
common.rs
File metadata and controls
419 lines (350 loc) · 12.1 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Primitive types.
use serde::{Deserialize, Serialize};
use surf_disco::Url;
pub use alloy::primitives::{Address, BlockHash, U256};
pub use tagged_base64::TaggedBase64;
/// An amount of Espresso tokens in WEI.
pub type ESPTokenAmount = U256;
/// A Unix timestamps in seconds since epoch.
pub type Timestamp = u64;
/// A ratio between 0 and 1.
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct Ratio(f32);
impl Ratio {
pub fn new(num: usize, den: usize) -> Self {
Self((num as f32) / (den as f32))
}
}
impl From<f32> for Ratio {
fn from(value: f32) -> Self {
Self(value)
}
}
impl From<Ratio> for f32 {
fn from(val: Ratio) -> Self {
val.0
}
}
/// An entry in the full node set.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct NodeSetEntry {
/// Node's Ethereum address.
pub address: Address,
/// The key used for the node for signing consensus messages.
pub staking_key: TaggedBase64,
/// state verifying key
pub state_key: TaggedBase64,
/// Total stake currently attributed to the node.
pub stake: ESPTokenAmount,
/// How much commission the node charges.
pub commission: Ratio,
/// Optional metadata like a human-readable name and icon.
///
/// May be [`None`] if no metadata URI is registered for this node.
pub metadata: Option<NodeMetadata>,
}
/// Information about an L1 block.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct L1BlockInfo {
/// The block number
pub number: u64,
/// The hash of this block (useful for detecting reorgs)
pub hash: BlockHash,
/// The timestamp of this block.
pub timestamp: Timestamp,
}
/// Minimal information needed to identify an L1 block and check for reorgs.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
pub struct L1BlockId {
/// The block number.
pub number: u64,
/// The hash of this block.
pub hash: BlockHash,
/// The parent of this block, used for reorg detection.
pub parent: BlockHash,
}
/// Information about the exiting of a node from the node set.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct NodeExit {
/// The exiting node.
pub address: Address,
/// The timestamp for the exit escrow delay time.
pub exit_time: Timestamp,
}
/// Information about the current "time" on the Espresso chain.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct EpochAndBlock {
/// The current epoch of the Espresso chain
pub epoch: u64,
/// The current block of the Espresso chain
pub block: u64,
/// The timestamp of the last block
pub timestamp: Timestamp,
}
/// An entry in the active node set.
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
pub struct ActiveNodeSetEntry {
/// The node's address.
pub address: Address,
/// The number of times this node has voted in the current epoch.
pub votes: u64,
/// The number of times this node has been eligible to vote in the current epoch.
pub eligible_votes: u64,
/// The number of times this node has successfully proposed as leader in the current epoch.
pub proposals: u64,
/// The number of times this node has been eligible to propose as leader in the current epoch.
pub slots: u64,
}
/// A single delegation from a particular user to a particular node.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct Delegation {
/// The user delegating.
pub delegator: Address,
/// The node being delegated to.
pub node: Address,
/// Amount of stake delegated by this user to this node.
pub amount: ESPTokenAmount,
}
/// A withdrawal of stake that is waiting to be claimed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct PendingWithdrawal {
/// The owner of the pending stake.
pub delegator: Address,
/// The node which was previously delegated to, which stake is now being withdrawn.
pub node: Address,
/// The amount of stake pending withdrawal.
pub amount: ESPTokenAmount,
/// The timestamp recorded for the exit escrow time.
///
/// Any attempts to withdrawal before this will fail.
pub available_time: Timestamp,
}
/// A completed withdrawal.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct Withdrawal {
/// The owner of the withdrawn stake.
pub delegator: Address,
/// The node which was previously delegated to, which stake is now withdrawn.
pub node: Address,
/// The amount of stake.
pub amount: ESPTokenAmount,
}
/// Optional descriptive information about a node, fetched from a third-party URI.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct NodeMetadata {
/// The URI this metadata is fetched from.
///
/// This URI is registered alongside the node in the staking contraact.
pub uri: Url,
/// The content of the metadata.
///
/// This content is fetched from a third-party URI, and thus should not be considered trusted,
/// reliable, or deterministic. It is informational only.
///
/// May be [`None`] if no (valid) content is available at the published `uri`.
pub content: Option<NodeMetadataContent>,
}
/// Optimal serialization of a struct with optional fields.
///
/// For self-describing formats (e.g. JSON) a [`None`] optional field can be omitted entirely,
/// leading to smaller and cleaner output. For non-self-describing formats (e.g. bincode) a sentinel
/// value must still be serialized indicating a value of [`None`].
///
/// # Syntax
///
/// ```
/// #[derive(Deserialize)]
/// struct Example {
/// #[serde(default)]
/// field1: Option<i32>,
///
/// #[serde(default)]
/// field2: Option<String>,
/// }
///
/// serialize_struct_with_opt_fields!(Example {
/// field1,
/// field2,
/// })
/// ```
///
/// Optionally, fields can be renamed in the human-readable wire format using a string literal
/// followed by `@` (a la the `name @ Pattern { .. }` syntax in rust pattern matching) along with a
/// `#[serde(rename = "...")]` directive.
///
/// ```
/// #[derive(Deserialize)]
/// struct Example {
/// #[serde(default)]
/// field1: Option<i32>,
///
/// #[serde(default, rename = "another field!")]
/// field2: Option<String>,
/// }
///
/// serialize_struct_with_opt_fields!(Example {
/// field1,
/// "another field!" @ field2,
/// })
/// ```
macro_rules! serialize_struct_with_opt_fields {
($t:ident { $($($name:literal@)?$f:ident),+ $(,)?}) => {
impl Serialize for $t {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeStruct;
// Unpack struct to cause a compiler error if we don't mention every field.
let $t { $($f),+ } = self;
if s.is_human_readable() {
// For self-describing formats (e.g. JSON) only serialize fields which are not
// [`None`].
let mut n = 0;
$(
if $f.is_some() {
n += 1;
}
)+
let mut s = s.serialize_struct(stringify!($t), n)?;
$(
if let Some(value) = $f {
s.serialize_field(serialize_struct_with_opt_fields!(@field_name $($name@)?$f), value)?;
}
)+
s.end()
} else {
let n = [$(stringify!($f)),+].len();
let mut s = s.serialize_struct(stringify!($t), n)?;
$(
s.serialize_field(serialize_struct_with_opt_fields!(@field_name $($name@)?$f), $f)?;
)+
s.end()
}
}
}
};
// Helper patterns which allow us to pattern match on the presence or absence of the optional
// `"name" @ field` syntax, returning the name that should be used for this field in the
// serialized output.
(@field_name $name:literal@$f:ident) => { $name };
(@field_name $f:ident) => { stringify!($f) };
}
/// Optional descriptive information about a node.
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
pub struct NodeMetadataContent {
/// Human-readable name for the node.
#[serde(default)]
pub name: Option<String>,
/// Longer description of the node.
#[serde(default)]
pub description: Option<String>,
/// Company or individual operating the node.
#[serde(default)]
pub company_name: Option<String>,
/// Website for `company_name`.
#[serde(default)]
pub company_website: Option<Url>,
/// Consensus client the node is running.
#[serde(default)]
pub client_version: Option<String>,
/// Icon for the node (at different resolutions and pixel aspect ratios).
#[serde(default)]
pub icon: Option<ImageSet>,
}
serialize_struct_with_opt_fields!(NodeMetadataContent {
name,
description,
company_name,
company_website,
client_version,
icon,
});
/// Different versions of the same image, at different resolutions and pixel aspect ratios.
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct ImageSet {
/// 14x14 icons at different pixel ratios.
#[serde(rename = "14x14")]
pub small: RatioSet,
/// 24x24 icons at different pixel ratios.
#[serde(rename = "24x24")]
pub large: RatioSet,
}
/// Different versions of the same image, at different pixel aspect ratios.
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
pub struct RatioSet {
/// Image source for 1:1 pixel aspect ratio
#[serde(rename = "@1x")]
#[serde(default)]
pub ratio1: Option<Url>,
/// Image source for 2:1 pixel aspect ratio
#[serde(rename = "@2x")]
#[serde(default)]
pub ratio2: Option<Url>,
/// Image source for 3:1 pixel aspect ratio
#[serde(rename = "@3x")]
#[serde(default)]
pub ratio3: Option<Url>,
}
serialize_struct_with_opt_fields!(RatioSet {
"@1x" @ ratio1,
"@2x" @ ratio2,
"@3x" @ ratio3,
});
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use serde_json::json;
use super::*;
#[test_log::test]
fn test_ratio_set_json() {
let set = RatioSet {
ratio1: Some("http://example.com/".parse().unwrap()),
ratio2: None,
ratio3: None,
};
let json = serde_json::to_value(set.clone()).unwrap();
// Omitted fields are omitted from the JSON.
assert_eq!(
json,
json!({
"@1x": "http://example.com/"
})
);
// Check round trip.
assert_eq!(set, serde_json::from_value(json).unwrap());
}
#[test_log::test]
fn test_ratio_set_bincode() {
let set = RatioSet {
ratio1: Some("http://example.com/".parse().unwrap()),
ratio2: None,
ratio3: None,
};
let bytes = bincode::serialize(&set).unwrap();
assert_eq!(set, bincode::deserialize(&bytes).unwrap());
}
#[test_log::test]
fn test_node_metadata_content_json() {
let metadata = NodeMetadataContent {
name: Some("test".into()),
..Default::default()
};
let json = serde_json::to_value(metadata.clone()).unwrap();
// Omitted fields are omitted from the JSON.
assert_eq!(
json,
json!({
"name": "test"
})
);
// Check round trip.
assert_eq!(metadata, serde_json::from_value(json).unwrap());
}
#[test_log::test]
fn test_node_metadata_content_bincode() {
let metadata = NodeMetadataContent {
name: Some("test".into()),
..Default::default()
};
let bytes = bincode::serialize(&metadata).unwrap();
assert_eq!(metadata, bincode::deserialize(&bytes).unwrap());
}
}