Skip to content

Commit d766f00

Browse files
committed
fix: match blob entities to their correct tx when having blocks with blobs with the same versioned hash + add major code refactoring
1 parent 3914100 commit d766f00

File tree

10 files changed

+206
-175
lines changed

10 files changed

+206
-175
lines changed

Diff for: src/beacon_client/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use self::types::{
66
BlockResponse,
77
};
88

9-
mod types;
9+
pub mod types;
1010

1111
#[derive(Debug, Clone)]
1212
pub struct BeaconClient {

Diff for: src/blobscan_client/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ use std::time::Duration;
22

33
use reqwest::{Client, StatusCode};
44

5-
use crate::types::{BlobEntity, BlockEntity, TransactionEntity};
6-
75
use self::{
86
jwt_manager::{Config as JWTManagerConfig, JWTManager},
9-
types::{BlobscanClientError, BlobscanClientResult, IndexRequest, SlotRequest, SlotResponse},
7+
types::{
8+
BlobEntity, BlobscanClientError, BlobscanClientResult, BlockEntity, IndexRequest,
9+
SlotRequest, SlotResponse, TransactionEntity,
10+
},
1011
};
1112

1213
mod jwt_manager;
13-
mod types;
14+
15+
pub mod types;
1416

1517
#[derive(Debug)]
1618
pub struct BlobscanClient {

Diff for: src/blobscan_client/types.rs

+107-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1-
use anyhow::Result;
1+
use anyhow::{Context, Result};
2+
use ethers::types::{
3+
Address, Block as EthersBlock, Bytes, Transaction as EthersTransaction, H256, U256, U64,
4+
};
25
use serde::{Deserialize, Serialize};
36

4-
use crate::types::{BlobEntity, BlockEntity, TransactionEntity};
7+
use crate::{beacon_client::types::BlobData, utils::web3::calculate_versioned_hash};
8+
9+
#[derive(Serialize, Deserialize, Debug)]
10+
pub struct BlockEntity {
11+
pub number: U64,
12+
pub hash: H256,
13+
pub timestamp: U256,
14+
pub slot: u32,
15+
}
16+
17+
#[derive(Serialize, Deserialize, Debug)]
18+
pub struct TransactionEntity {
19+
pub hash: H256,
20+
pub from: Address,
21+
pub to: Address,
22+
#[serde(rename = "blockNumber")]
23+
pub block_number: U64,
24+
}
25+
26+
#[derive(Serialize, Deserialize, Debug)]
27+
pub struct BlobEntity {
28+
#[serde(rename = "versionedHash")]
29+
pub versioned_hash: H256,
30+
pub commitment: String,
31+
pub data: Bytes,
32+
#[serde(rename = "txHash")]
33+
pub tx_hash: H256,
34+
pub index: u32,
35+
}
536

637
#[derive(Serialize, Deserialize, Debug)]
738
pub struct SlotResponse {
@@ -33,3 +64,77 @@ pub enum BlobscanClientError {
3364
}
3465

3566
pub type BlobscanClientResult<T> = Result<T, BlobscanClientError>;
67+
68+
impl<'a> TryFrom<(&'a EthersBlock<EthersTransaction>, u32)> for BlockEntity {
69+
type Error = anyhow::Error;
70+
71+
fn try_from(
72+
(ethers_block, slot): (&'a EthersBlock<EthersTransaction>, u32),
73+
) -> Result<Self, Self::Error> {
74+
let number = ethers_block
75+
.number
76+
.with_context(|| format!("Missing block number field in execution block"))?;
77+
78+
Ok(Self {
79+
number,
80+
hash: ethers_block
81+
.hash
82+
.with_context(|| format!("Missing block hash field in execution block {number}"))?,
83+
timestamp: ethers_block.timestamp,
84+
slot,
85+
})
86+
}
87+
}
88+
89+
impl<'a> TryFrom<(&'a EthersTransaction, &'a EthersBlock<EthersTransaction>)>
90+
for TransactionEntity
91+
{
92+
type Error = anyhow::Error;
93+
94+
fn try_from(
95+
(ethers_tx, ethers_block): (&'a EthersTransaction, &'a EthersBlock<EthersTransaction>),
96+
) -> Result<Self, Self::Error> {
97+
let hash = ethers_tx.hash;
98+
99+
Ok(Self {
100+
block_number: ethers_block
101+
.number
102+
.with_context(|| format!("Missing block number field in execution block"))?,
103+
hash,
104+
from: ethers_tx.from,
105+
to: ethers_tx
106+
.to
107+
.with_context(|| format!("Missing to field in transaction {hash}"))?,
108+
})
109+
}
110+
}
111+
112+
impl<'a> TryFrom<(&'a BlobData, u32, H256)> for BlobEntity {
113+
type Error = anyhow::Error;
114+
115+
fn try_from(
116+
(blob_data, index, tx_hash): (&'a BlobData, u32, H256),
117+
) -> Result<Self, Self::Error> {
118+
Ok(Self {
119+
tx_hash,
120+
index,
121+
commitment: blob_data.kzg_commitment.clone(),
122+
data: blob_data.blob.clone(),
123+
versioned_hash: calculate_versioned_hash(&blob_data.kzg_commitment)?,
124+
})
125+
}
126+
}
127+
128+
impl<'a> From<(&'a BlobData, &'a H256, usize, &'a H256)> for BlobEntity {
129+
fn from(
130+
(blob_data, versioned_hash, index, tx_hash): (&'a BlobData, &'a H256, usize, &'a H256),
131+
) -> Self {
132+
Self {
133+
tx_hash: tx_hash.clone(),
134+
index: index as u32,
135+
commitment: blob_data.kzg_commitment.clone(),
136+
data: blob_data.blob.clone(),
137+
versioned_hash: versioned_hash.clone(),
138+
}
139+
}
140+
}

Diff for: src/utils/context.rs renamed to src/context.rs

File renamed without changes.

Diff for: src/utils/env.rs renamed to src/env.rs

File renamed without changes.

Diff for: src/main.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ use anyhow::Result;
22
use tracing::Instrument;
33

44
use crate::{
5+
context::create_context,
56
slot_processor::SlotProcessor,
6-
utils::{
7-
context::create_context,
8-
telemetry::{get_subscriber, init_subscriber},
9-
},
7+
utils::telemetry::{get_subscriber, init_subscriber},
108
};
119
use std::{thread, time::Duration};
1210

1311
mod beacon_client;
1412
mod blobscan_client;
13+
mod context;
14+
mod env;
1515
mod slot_processor;
16-
mod types;
1716
mod utils;
1817

1918
#[tokio::main]

0 commit comments

Comments
 (0)