Skip to content

Commit 40d9cbd

Browse files
committed
refactor: add small tweaks to clients' types
1 parent cc9f030 commit 40d9cbd

File tree

9 files changed

+72
-83
lines changed

9 files changed

+72
-83
lines changed

src/beacon_client/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use reqwest::{Client, StatusCode};
22
use std::time::Duration;
33

44
use self::types::{
5-
BeaconClientError, BeaconClientResult, BlobData, BlobsResponse, BlockMessage as Block,
5+
BeaconClientError, BeaconClientResult, Blob, BlobsResponse, BlockMessage as Block,
66
BlockResponse,
77
};
88

@@ -47,7 +47,7 @@ impl BeaconClient {
4747
}
4848
}
4949

50-
pub async fn get_blobs(&self, slot: u32) -> BeaconClientResult<Option<Vec<BlobData>>> {
50+
pub async fn get_blobs(&self, slot: u32) -> BeaconClientResult<Option<Vec<Blob>>> {
5151
let url = self.build_url(&format!("eth/v1/beacon/blobs/{slot}"));
5252

5353
let blobs_response = self.client.get(url).send().await?;

src/beacon_client/types.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
use anyhow::Result;
22
use ethers::types::{Bytes, H256};
3-
use serde::{Deserialize, Serialize};
3+
use serde::Deserialize;
44

5-
#[derive(Serialize, Deserialize, Debug)]
5+
#[derive(Deserialize, Debug)]
66
pub struct ExecutionPayload {
77
pub block_hash: H256,
88
}
99

10-
#[derive(Serialize, Deserialize, Debug)]
10+
#[derive(Deserialize, Debug)]
1111
pub struct BlockBody {
1212
pub execution_payload: Option<ExecutionPayload>,
1313
pub blob_kzg_commitments: Option<Vec<String>>,
1414
}
15-
#[derive(Serialize, Deserialize, Debug)]
15+
#[derive(Deserialize, Debug)]
1616
pub struct BlockMessage {
1717
pub slot: String,
1818
pub body: BlockBody,
1919
}
2020

21-
#[derive(Serialize, Deserialize, Debug)]
21+
#[derive(Deserialize, Debug)]
2222
pub struct Block {
2323
pub message: BlockMessage,
2424
}
2525

26-
#[derive(Serialize, Deserialize, Debug)]
26+
#[derive(Deserialize, Debug)]
2727
pub struct BlockResponse {
2828
pub data: Block,
2929
}
3030

31-
#[derive(Serialize, Deserialize, Debug)]
32-
pub struct BlobData {
31+
#[derive(Deserialize, Debug)]
32+
pub struct Blob {
3333
pub index: String,
3434
pub kzg_commitment: String,
3535
pub blob: Bytes,
3636
}
3737

38-
#[derive(Serialize, Deserialize, Debug)]
38+
#[derive(Deserialize, Debug)]
3939
pub struct BlobsResponse {
40-
pub data: Vec<BlobData>,
40+
pub data: Vec<Blob>,
4141
}
4242

4343
#[derive(Debug, thiserror::Error)]

src/blobscan_client/mod.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use reqwest::{Client, StatusCode};
55
use self::{
66
jwt_manager::{Config as JWTManagerConfig, JWTManager},
77
types::{
8-
BlobEntity, BlobscanClientError, BlobscanClientResult, BlockEntity, FailedSlotsChunkEntity,
9-
FailedSlotsChunksRequest, GetFailedSlotsChunksResponse, IndexRequest,
10-
RemoveFailedSlotsChunksRequest, SlotRequest, SlotResponse, TransactionEntity,
8+
Blob, BlobscanClientError, BlobscanClientResult, Block, FailedSlotsChunk,
9+
FailedSlotsChunksRequest, FailedSlotsChunksResponse, IndexRequest,
10+
RemoveFailedSlotsChunksRequest, SlotRequest, SlotResponse, Transaction,
1111
},
1212
};
1313

@@ -47,9 +47,9 @@ impl BlobscanClient {
4747

4848
pub async fn index(
4949
&self,
50-
block: BlockEntity,
51-
transactions: Vec<TransactionEntity>,
52-
blobs: Vec<BlobEntity>,
50+
block: Block,
51+
transactions: Vec<Transaction>,
52+
blobs: Vec<Blob>,
5353
) -> BlobscanClientResult<()> {
5454
let path = String::from("index");
5555
let url = self.build_url(&path);
@@ -70,9 +70,7 @@ impl BlobscanClient {
7070

7171
match index_response.status() {
7272
StatusCode::OK => Ok(()),
73-
_ => Err(BlobscanClientError::BlobscanClientError(
74-
index_response.text().await?,
75-
)),
73+
_ => Err(BlobscanClientError::ApiError(index_response.text().await?)),
7674
}
7775
}
7876

@@ -91,9 +89,7 @@ impl BlobscanClient {
9189

9290
match slot_response.status() {
9391
StatusCode::OK => Ok(()),
94-
_ => Err(BlobscanClientError::BlobscanClientError(
95-
slot_response.text().await?,
96-
)),
92+
_ => Err(BlobscanClientError::ApiError(slot_response.text().await?)),
9793
}
9894
}
9995

@@ -106,15 +102,11 @@ impl BlobscanClient {
106102
match slot_response.status() {
107103
StatusCode::OK => Ok(Some(slot_response.json::<SlotResponse>().await?.slot)),
108104
StatusCode::NOT_FOUND => Ok(None),
109-
_ => Err(BlobscanClientError::BlobscanClientError(
110-
slot_response.text().await?,
111-
)),
105+
_ => Err(BlobscanClientError::ApiError(slot_response.text().await?)),
112106
}
113107
}
114108

115-
pub async fn get_failed_slots_chunks(
116-
&self,
117-
) -> BlobscanClientResult<Vec<FailedSlotsChunkEntity>> {
109+
pub async fn get_failed_slots_chunks(&self) -> BlobscanClientResult<Vec<FailedSlotsChunk>> {
118110
let path = String::from("failed-slots-chunks");
119111
let url = self.build_url(&path);
120112
let token = self.jwt_manager.get_token()?;
@@ -123,18 +115,18 @@ impl BlobscanClient {
123115

124116
match failed_slots_chunks_response.status() {
125117
StatusCode::OK => Ok(failed_slots_chunks_response
126-
.json::<GetFailedSlotsChunksResponse>()
118+
.json::<FailedSlotsChunksResponse>()
127119
.await?
128120
.chunks),
129-
_ => Err(BlobscanClientError::BlobscanClientError(
121+
_ => Err(BlobscanClientError::ApiError(
130122
failed_slots_chunks_response.text().await?,
131123
)),
132124
}
133125
}
134126

135127
pub async fn add_failed_slots_chunks(
136128
&self,
137-
slots_chunks: Vec<FailedSlotsChunkEntity>,
129+
slots_chunks: Vec<FailedSlotsChunk>,
138130
) -> BlobscanClientResult<()> {
139131
let path = String::from("failed-slots-chunks");
140132
let url = self.build_url(&path);
@@ -152,7 +144,7 @@ impl BlobscanClient {
152144

153145
match failed_slots_response.status() {
154146
StatusCode::OK => Ok(()),
155-
_ => Err(BlobscanClientError::BlobscanClientError(
147+
_ => Err(BlobscanClientError::ApiError(
156148
failed_slots_response.text().await?,
157149
)),
158150
}
@@ -176,7 +168,7 @@ impl BlobscanClient {
176168

177169
match failed_slots_response.status() {
178170
StatusCode::OK => Ok(()),
179-
_ => Err(BlobscanClientError::BlobscanClientError(
171+
_ => Err(BlobscanClientError::ApiError(
180172
failed_slots_response.text().await?,
181173
)),
182174
}

src/blobscan_client/types.rs

+26-29
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use ethers::types::{
44
};
55
use serde::{Deserialize, Serialize};
66

7-
use crate::{beacon_client::types::BlobData, utils::web3::calculate_versioned_hash};
7+
use crate::{beacon_client::types::Blob as BeaconBlob, utils::web3::calculate_versioned_hash};
88

99
#[derive(Serialize, Deserialize, Debug)]
10-
pub struct BlockEntity {
10+
pub struct Block {
1111
pub number: U64,
1212
pub hash: H256,
1313
pub timestamp: U256,
@@ -16,7 +16,7 @@ pub struct BlockEntity {
1616

1717
#[derive(Serialize, Deserialize, Debug)]
1818
#[serde(rename_all = "camelCase")]
19-
pub struct TransactionEntity {
19+
pub struct Transaction {
2020
pub hash: H256,
2121
pub from: Address,
2222
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -26,7 +26,7 @@ pub struct TransactionEntity {
2626

2727
#[derive(Serialize, Deserialize, Debug)]
2828
#[serde(rename_all = "camelCase")]
29-
pub struct BlobEntity {
29+
pub struct Blob {
3030
pub versioned_hash: H256,
3131
pub commitment: String,
3232
pub data: Bytes,
@@ -36,7 +36,7 @@ pub struct BlobEntity {
3636

3737
#[derive(Serialize, Deserialize, Debug)]
3838
#[serde(rename_all = "camelCase")]
39-
pub struct FailedSlotsChunkEntity {
39+
pub struct FailedSlotsChunk {
4040
#[serde(default, skip_serializing_if = "Option::is_none")]
4141
pub id: Option<u32>,
4242
pub initial_slot: u32,
@@ -45,12 +45,12 @@ pub struct FailedSlotsChunkEntity {
4545

4646
#[derive(Serialize, Debug)]
4747
pub struct FailedSlotsChunksRequest {
48-
pub chunks: Vec<FailedSlotsChunkEntity>,
48+
pub chunks: Vec<FailedSlotsChunk>,
4949
}
5050

5151
#[derive(Deserialize, Debug)]
52-
pub struct GetFailedSlotsChunksResponse {
53-
pub chunks: Vec<FailedSlotsChunkEntity>,
52+
pub struct FailedSlotsChunksResponse {
53+
pub chunks: Vec<FailedSlotsChunk>,
5454
}
5555

5656
#[derive(Serialize, Deserialize, Debug)]
@@ -59,38 +59,37 @@ pub struct RemoveFailedSlotsChunksRequest {
5959
pub chunk_ids: Vec<u32>,
6060
}
6161

62-
#[derive(Serialize, Deserialize, Debug)]
63-
pub struct SlotResponse {
62+
#[derive(Serialize, Debug)]
63+
pub struct SlotRequest {
6464
pub slot: u32,
6565
}
66-
67-
#[derive(Serialize, Deserialize, Debug)]
68-
pub struct SlotRequest {
66+
#[derive(Deserialize, Debug)]
67+
pub struct SlotResponse {
6968
pub slot: u32,
7069
}
7170

72-
#[derive(Serialize, Deserialize, Debug)]
71+
#[derive(Serialize, Debug)]
7372
pub struct IndexRequest {
74-
pub block: BlockEntity,
75-
pub transactions: Vec<TransactionEntity>,
76-
pub blobs: Vec<BlobEntity>,
73+
pub block: Block,
74+
pub transactions: Vec<Transaction>,
75+
pub blobs: Vec<Blob>,
7776
}
7877

7978
#[derive(Debug, thiserror::Error)]
8079
pub enum BlobscanClientError {
8180
#[error(transparent)]
8281
Reqwest(#[from] reqwest::Error),
8382

84-
#[error("Blobscan client error: {0}")]
85-
BlobscanClientError(String),
83+
#[error("API usage error: {0}")]
84+
ApiError(String),
8685

8786
#[error(transparent)]
88-
JWTError(#[from] anyhow::Error),
87+
Other(#[from] anyhow::Error),
8988
}
9089

9190
pub type BlobscanClientResult<T> = Result<T, BlobscanClientError>;
9291

93-
impl From<(u32, u32)> for FailedSlotsChunkEntity {
92+
impl From<(u32, u32)> for FailedSlotsChunk {
9493
fn from((initial_slot, final_slot): (u32, u32)) -> Self {
9594
Self {
9695
id: None,
@@ -100,7 +99,7 @@ impl From<(u32, u32)> for FailedSlotsChunkEntity {
10099
}
101100
}
102101

103-
impl<'a> TryFrom<(&'a EthersBlock<EthersTransaction>, u32)> for BlockEntity {
102+
impl<'a> TryFrom<(&'a EthersBlock<EthersTransaction>, u32)> for Block {
104103
type Error = anyhow::Error;
105104

106105
fn try_from(
@@ -121,9 +120,7 @@ impl<'a> TryFrom<(&'a EthersBlock<EthersTransaction>, u32)> for BlockEntity {
121120
}
122121
}
123122

124-
impl<'a> TryFrom<(&'a EthersTransaction, &'a EthersBlock<EthersTransaction>)>
125-
for TransactionEntity
126-
{
123+
impl<'a> TryFrom<(&'a EthersTransaction, &'a EthersBlock<EthersTransaction>)> for Transaction {
127124
type Error = anyhow::Error;
128125

129126
fn try_from(
@@ -142,11 +139,11 @@ impl<'a> TryFrom<(&'a EthersTransaction, &'a EthersBlock<EthersTransaction>)>
142139
}
143140
}
144141

145-
impl<'a> TryFrom<(&'a BlobData, u32, H256)> for BlobEntity {
142+
impl<'a> TryFrom<(&'a BeaconBlob, u32, H256)> for Blob {
146143
type Error = anyhow::Error;
147144

148145
fn try_from(
149-
(blob_data, index, tx_hash): (&'a BlobData, u32, H256),
146+
(blob_data, index, tx_hash): (&'a BeaconBlob, u32, H256),
150147
) -> Result<Self, Self::Error> {
151148
Ok(Self {
152149
tx_hash,
@@ -158,9 +155,9 @@ impl<'a> TryFrom<(&'a BlobData, u32, H256)> for BlobEntity {
158155
}
159156
}
160157

161-
impl<'a> From<(&'a BlobData, &'a H256, usize, &'a H256)> for BlobEntity {
158+
impl<'a> From<(&'a BeaconBlob, &'a H256, usize, &'a H256)> for Blob {
162159
fn from(
163-
(blob_data, versioned_hash, index, tx_hash): (&'a BlobData, &'a H256, usize, &'a H256),
160+
(blob_data, versioned_hash, index, tx_hash): (&'a BeaconBlob, &'a H256, usize, &'a H256),
164161
) -> Self {
165162
Self {
166163
tx_hash: *tx_hash,

src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{sync::Arc, time::Duration};
22

3-
use anyhow::Result;
3+
use anyhow::Result as AnyhowResult;
44
use ethers::prelude::*;
55

66
use crate::{
@@ -28,7 +28,7 @@ pub struct Context {
2828
}
2929

3030
impl Context {
31-
pub fn try_new(config: Config) -> Result<Self> {
31+
pub fn try_new(config: Config) -> AnyhowResult<Self> {
3232
let Config {
3333
blobscan_api_endpoint,
3434
beacon_node_rpc,

src/slot_processor_manager/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use tokio::task::{JoinError, JoinHandle};
44
use tracing::{error, Instrument};
55

66
use self::slot_processor::{errors::SlotProcessorError, SlotProcessor};
7-
use crate::{blobscan_client::types::FailedSlotsChunkEntity, context::Context};
7+
use crate::{blobscan_client::types::FailedSlotsChunk, context::Context};
88

99
mod slot_processor;
1010

@@ -16,7 +16,7 @@ pub struct SlotProcessorManager {
1616
#[derive(Debug, thiserror::Error)]
1717
pub enum SlotProcessorManagerError {
1818
#[error("Slot processor manager failed to process the following slots chunks: {chunks:?}")]
19-
FailedSlotsProcessing { chunks: Vec<FailedSlotsChunkEntity> },
19+
FailedSlotsProcessing { chunks: Vec<FailedSlotsChunk> },
2020

2121
#[error(transparent)]
2222
Other(#[from] anyhow::Error),
@@ -99,14 +99,14 @@ impl SlotProcessorManager {
9999
async fn get_failed_slots_chunks(
100100
&self,
101101
thread_outputs: &[Result<Result<u32, SlotProcessorError>, JoinError>],
102-
) -> Result<Vec<FailedSlotsChunkEntity>, SlotProcessorManagerError> {
102+
) -> Result<Vec<FailedSlotsChunk>, SlotProcessorManagerError> {
103103
let failed_threads = thread_outputs
104104
.iter()
105105
.filter(|thread_join| match thread_join {
106106
Ok(thread_result) => thread_result.is_err(),
107107
Err(_) => true,
108108
});
109-
let mut failed_slots_chunks: Vec<FailedSlotsChunkEntity> = vec![];
109+
let mut failed_slots_chunks: Vec<FailedSlotsChunk> = vec![];
110110

111111
for thread in failed_threads.into_iter() {
112112
match thread {
@@ -117,7 +117,7 @@ impl SlotProcessorManager {
117117
reason: _,
118118
} => {
119119
error!("Failed to process slots from {} to {}", slot, target_slot);
120-
failed_slots_chunks.push(FailedSlotsChunkEntity::from((
120+
failed_slots_chunks.push(FailedSlotsChunk::from((
121121
slot.to_owned(),
122122
target_slot.to_owned(),
123123
)))
@@ -133,6 +133,6 @@ impl SlotProcessorManager {
133133
}
134134
}
135135

136-
return Ok(failed_slots_chunks);
136+
Ok(failed_slots_chunks)
137137
}
138138
}

0 commit comments

Comments
 (0)