Skip to content

Commit be6989f

Browse files
committed
fix(update-validator-rewards): pulling mev blocks
current API was dropped. switched to new API to get the same information.
1 parent 9e122f9 commit be6989f

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

src/bin/update-validator-rewards/mev_blocks.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ pub async fn sync_mev_blocks(
2323
let mut start_slot = last_synced_slot.map_or(EARLIEST_AVAILABLE_SLOT, |slot| slot.0 + 1);
2424

2525
while start_slot < last_header.slot().0 {
26-
let end_slot = start_slot + 199;
26+
let end_slot = start_slot + 200;
2727

28-
let blocks = relay_api.fetch_mev_blocks(start_slot, end_slot).await;
28+
let blocks = relay_api.fetch_mev_blocks(start_slot..end_slot).await;
2929

3030
debug!(start_slot, end_slot, "got {} blocks", blocks.len());
3131

3232
mev_blocks_store.store_blocks(&blocks).await;
3333

34-
start_slot = end_slot + 1;
34+
start_slot = end_slot;
3535
}
3636

3737
info!(start_slot, "no more blocks to process");
@@ -117,9 +117,9 @@ mod tests {
117117

118118
mock_relay
119119
.expect_fetch_mev_blocks()
120-
.with(eq(1), eq(1 + 199))
120+
.with(eq(1..(1 + 200)))
121121
.times(1)
122-
.return_once(move |_start, _end| fetched_blocks);
122+
.return_once(move |_slots| fetched_blocks);
123123

124124
mock_store
125125
.expect_store_blocks()

src/mev_blocks/relay_api.rs

+40-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ops::Range;
2+
13
use async_trait::async_trait;
24
use mockall::{automock, predicate::*};
35
use serde::Deserialize;
@@ -12,11 +14,8 @@ pub const EARLIEST_AVAILABLE_SLOT: i32 = 5616303;
1214
// These are accepted blocks only.
1315
#[derive(Deserialize)]
1416
pub struct MaybeMevBlock {
15-
#[serde(rename = "slotNumber")]
16-
slot_number: i32,
17-
#[serde(rename = "blockNumber")]
17+
slot: i32,
1818
block_number: i32,
19-
#[serde(rename = "blockHash")]
2019
block_hash: String,
2120
#[serde(rename = "value")]
2221
bid: Option<WeiNewtype>,
@@ -28,7 +27,7 @@ impl TryFrom<MaybeMevBlock> for MevBlock {
2827
fn try_from(raw: MaybeMevBlock) -> Result<Self, Self::Error> {
2928
match raw.bid {
3029
Some(bid) => Ok(MevBlock {
31-
slot: raw.slot_number,
30+
slot: raw.slot,
3231
block_number: raw.block_number,
3332
block_hash: raw.block_hash,
3433
bid,
@@ -41,7 +40,8 @@ impl TryFrom<MaybeMevBlock> for MevBlock {
4140
#[automock]
4241
#[async_trait]
4342
pub trait RelayApi {
44-
async fn fetch_mev_blocks(&self, start_slot: i32, end_slot: i32) -> Vec<MevBlock>;
43+
async fn fetch_mev_block(&self, slot: i32) -> Option<MevBlock>;
44+
async fn fetch_mev_blocks(&self, slots: Range<i32>) -> Vec<MevBlock>;
4545
}
4646

4747
pub struct RelayApiHttp {
@@ -52,7 +52,7 @@ pub struct RelayApiHttp {
5252
impl RelayApiHttp {
5353
pub fn new() -> Self {
5454
Self {
55-
server_url: "https://relay.ultrasound.money".into(),
55+
server_url: "https://relay-analytics.ultrasound.money".into(),
5656
client: reqwest::Client::new(),
5757
}
5858
}
@@ -73,21 +73,36 @@ impl Default for RelayApiHttp {
7373

7474
#[async_trait]
7575
impl RelayApi for RelayApiHttp {
76-
async fn fetch_mev_blocks(&self, start_slot: i32, end_slot: i32) -> Vec<MevBlock> {
76+
async fn fetch_mev_block(&self, slot: i32) -> Option<MevBlock> {
77+
let url = format!(
78+
"{}/relay/v1/data/bidtraces/proposer_payload_delivered?slot={}",
79+
self.server_url, slot
80+
);
7781
self.client
78-
.get(format!(
79-
"{}/api/block-production?start_slot={}&end_slot={}",
80-
self.server_url, start_slot, end_slot
81-
))
82+
.get(url)
8283
.send()
8384
.await
8485
.unwrap()
8586
.json::<Vec<MaybeMevBlock>>()
8687
.await
87-
.unwrap()
88-
.into_iter()
89-
.filter_map(|block| block.try_into().ok())
90-
.collect()
88+
.ok()
89+
.and_then(|blocks| blocks.into_iter().next())
90+
.and_then(|block| block.try_into().ok())
91+
}
92+
93+
async fn fetch_mev_blocks(&self, slots: Range<i32>) -> Vec<MevBlock> {
94+
let mut blocks = Vec::new();
95+
96+
for slot in slots {
97+
if let Some(block) = self.fetch_mev_block(slot).await {
98+
blocks.push(block);
99+
}
100+
101+
// add small delay to avoid rate limiting
102+
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
103+
}
104+
105+
blocks
91106
}
92107
}
93108

@@ -101,13 +116,16 @@ mod tests {
101116
async fn fetch_mev_blocks_test() {
102117
let mut server = mockito::Server::new();
103118
server
104-
.mock("GET", "/api/block-production?start_slot=0&end_slot=10")
119+
.mock(
120+
"GET",
121+
"/relay/v1/data/bidtraces/proposer_payload_delivered?slot=0",
122+
)
105123
.with_status(200)
106124
.with_body(
107125
json!([{
108-
"slotNumber": 1,
109-
"blockNumber": 9191911,
110-
"blockHash": "abc",
126+
"slot": 0,
127+
"block_number": 9191911,
128+
"block_hash": "abc",
111129
"value": "100"
112130
}])
113131
.to_string(),
@@ -116,11 +134,11 @@ mod tests {
116134

117135
let relay_api = RelayApiHttp::new_with_url(&server.url());
118136

119-
let blocks = relay_api.fetch_mev_blocks(0, 10).await;
137+
let blocks = relay_api.fetch_mev_blocks(0..1).await;
120138
assert_eq!(blocks.len(), 1);
121139

122140
let block = &blocks[0];
123-
assert_eq!(block.slot, 1);
141+
assert_eq!(block.slot, 0);
124142
assert_eq!(block.block_number, 9191911);
125143
assert_eq!(block.block_hash, "abc");
126144
assert_eq!(block.bid.0, 100);

0 commit comments

Comments
 (0)