Skip to content

Commit 4857b18

Browse files
committed
fix: header submission logic
1 parent 2b311f9 commit 4857b18

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

relayer/src/relay.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ impl EthRelayer {
5959
let signer = InMemorySigner::from_secret_key(signer_account_id, secret_key);
6060
let client = near_fetch::Client::new(&config.near.endpoint);
6161

62-
Ok(ContractClient::new(eth_light_client_account_id, signer, client, config.relayer.clone()))
62+
Ok(ContractClient::new(
63+
eth_light_client_account_id,
64+
signer,
65+
client,
66+
config.relayer.clone(),
67+
))
6368
}
6469

6570
pub async fn run(&self) -> Result<()> {
@@ -179,40 +184,39 @@ impl EthRelayer {
179184
return Ok(RelayResult::Submitted);
180185
}
181186

182-
let last_block = self.near_client.get_last_block_number().await?;
183-
let max_block = self.get_max_block().await?;
187+
let finalized_block = self.near_client.get_last_block_number().await?;
188+
let high_block = self.get_max_block().await?;
189+
let low_block = finalized_block + 1;
184190

185-
if max_block <= last_block {
191+
if high_block < low_block {
186192
debug!(
187-
"No new blocks to submit (last: {}, max: {})",
188-
last_block, max_block
193+
"No blocks to submit (finalized: {}, high: {})",
194+
finalized_block, high_block
189195
);
190196
return Ok(RelayResult::Skipped);
191197
}
192198

193-
let start_block = last_block + 1;
194-
let capped_max_block = std::cmp::min(
195-
max_block,
196-
start_block + self.config.relayer.max_headers_per_period as u64 - 1,
197-
);
198-
199+
let fetch_end = high_block;
200+
let fetch_start = high_block
201+
.saturating_sub(self.config.relayer.max_headers_per_period as u64 - 1)
202+
.max(low_block);
203+
199204
info!(
200-
"Fetching {} execution headers for blocks {} to {} (capped from {})",
201-
capped_max_block - last_block,
202-
start_block,
203-
capped_max_block,
204-
max_block
205+
"Fetching {} execution headers for blocks {} to {}",
206+
fetch_end - fetch_start + 1,
207+
fetch_start,
208+
fetch_end
205209
);
206210

207211
let mut headers = self
208212
.execution_client
209-
.fetch_block_range(start_block..=capped_max_block)
213+
.fetch_block_range(fetch_start..=fetch_end)
210214
.await?;
211215

212216
if headers.is_empty() {
213217
warn!(
214218
"No headers fetched for range {}..={}",
215-
start_block, capped_max_block
219+
fetch_start, fetch_end
216220
);
217221
return Ok(RelayResult::Skipped);
218222
}

relayer/tests/sepolia_sandbox.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ async fn test_relayer_mainloop_hybrid() -> Result<()> {
4646

4747
// Create relayer with real Ethereum clients + sandbox NEAR
4848
let mut config = Config::default();
49-
config.relayer.max_iterations = Some(3); // Just a couple iterations
49+
config.relayer.max_iterations = Some(5);
5050
config.relayer.headers_batch_size = 100;
51+
config.relayer.max_headers_per_period = 3000; // Test a large batch size for integration only
5152

5253
let relayer = EthRelayer::with_clients(
5354
beacon_client, // Real Sepolia beacon

0 commit comments

Comments
 (0)