-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtypes.rs
151 lines (129 loc) · 3.38 KB
/
types.rs
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
use std::{fmt, str::FromStr};
use ethers::types::{Bytes, H256};
use serde::{Deserialize, Serialize};
use crate::slots_processor::BlockData;
#[derive(Serialize, Debug, Clone)]
pub enum BlockId {
Head,
Finalized,
Slot(u32),
Hash(H256),
}
#[derive(Serialize, Debug)]
pub enum Topic {
Head,
FinalizedCheckpoint,
}
#[derive(Deserialize, Debug)]
pub struct ExecutionPayload {
pub block_hash: H256,
#[serde(deserialize_with = "deserialize_number")]
pub block_number: u32,
}
#[derive(Deserialize, Debug)]
pub struct BlockBody {
pub execution_payload: Option<ExecutionPayload>,
pub blob_kzg_commitments: Option<Vec<String>>,
}
#[derive(Deserialize, Debug)]
pub struct BlockMessage {
#[serde(deserialize_with = "deserialize_number")]
pub slot: u32,
pub body: BlockBody,
pub parent_root: H256,
}
#[derive(Deserialize, Debug)]
pub struct Block {
pub message: BlockMessage,
}
#[derive(Deserialize, Debug)]
pub struct BlockResponse {
pub data: Block,
}
#[derive(Deserialize, Debug)]
pub struct Blob {
pub index: String,
pub kzg_commitment: String,
pub blob: Bytes,
}
#[derive(Deserialize, Debug)]
pub struct BlobsResponse {
pub data: Vec<Blob>,
}
#[derive(Deserialize, Debug)]
pub struct BlockHeaderResponse {
pub data: BlockHeader,
}
#[derive(Deserialize, Debug)]
pub struct BlockHeader {
pub root: H256,
pub header: InnerBlockHeader,
}
#[derive(Deserialize, Debug)]
pub struct InnerBlockHeader {
pub message: BlockHeaderMessage,
}
#[derive(Deserialize, Debug)]
pub struct BlockHeaderMessage {
pub parent_root: H256,
#[serde(deserialize_with = "deserialize_number")]
pub slot: u32,
}
#[derive(Deserialize, Debug)]
pub struct HeadBlockEventData {
#[serde(deserialize_with = "deserialize_number")]
pub slot: u32,
pub block: H256,
}
#[derive(Deserialize, Debug)]
pub struct FinalizedCheckpointEventData {
pub block: H256,
}
fn deserialize_number<'de, D>(deserializer: D) -> Result<u32, D::Error>
where
D: serde::Deserializer<'de>,
{
let slot = String::deserialize(deserializer)?;
slot.parse::<u32>().map_err(serde::de::Error::custom)
}
impl fmt::Display for BlockId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BlockId::Head => write!(f, "head"),
BlockId::Finalized => write!(f, "finalized"),
BlockId::Slot(slot) => write!(f, "{}", slot),
BlockId::Hash(hash) => write!(f, "{}", hash),
}
}
}
impl FromStr for BlockId {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"head" => Ok(BlockId::Head),
"finalized" => Ok(BlockId::Finalized),
_ => match s.parse::<u32>() {
Ok(num) => Ok(BlockId::Slot(num)),
Err(_) => {
Err("Invalid block ID. Expected 'head', 'finalized' or a number.".to_string())
}
},
}
}
}
impl From<&Topic> for String {
fn from(value: &Topic) -> Self {
match value {
Topic::Head => String::from("head"),
Topic::FinalizedCheckpoint => String::from("finalized_checkpoint"),
}
}
}
impl From<HeadBlockEventData> for BlockData {
fn from(event_data: HeadBlockEventData) -> Self {
Self {
root: event_data.block,
slot: event_data.slot,
}
}
}