Skip to content

Commit 88d6def

Browse files
committed
Cleanup
1 parent 93b33a2 commit 88d6def

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/l1_rpc_prefetcher.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'retriable'
33

44
class L1RpcPrefetcher
5+
include Memery
56
def initialize(ethereum_client:,
67
ahead: ENV.fetch('L1_PREFETCH_FORWARD', Rails.env.test? ? 5 : 200).to_i,
78
threads: ENV.fetch('L1_PREFETCH_THREADS', 2).to_i)
@@ -17,19 +18,20 @@ def initialize(ethereum_client:,
1718
end
1819

1920
def ensure_prefetched(from_block)
20-
to_block = from_block + @ahead
21+
# Check current chain tip first
22+
latest = get_latest_block_number
23+
24+
# Don't prefetch beyond chain tip
25+
to_block = [from_block + @ahead, latest].min
26+
2127
# Only create promises for blocks we don't have yet
2228
blocks_to_fetch = (from_block..to_block).reject { |n| @promises.key?(n) }
2329

2430
return if blocks_to_fetch.empty?
2531

26-
# Only enqueue a reasonable number at once to avoid overwhelming the promise system
27-
max_to_enqueue = [@threads * 10, 50].min
28-
29-
to_enqueue = blocks_to_fetch.first(max_to_enqueue)
30-
Rails.logger.debug "Enqueueing #{to_enqueue.size} of #{blocks_to_fetch.size} blocks: #{to_enqueue.first}..#{to_enqueue.last}"
32+
Rails.logger.debug "Enqueueing #{blocks_to_fetch.size} blocks: #{blocks_to_fetch.first}..#{blocks_to_fetch.last}"
3133

32-
to_enqueue.each { |block_number| enqueue_single(block_number) }
34+
blocks_to_fetch.each { |block_number| enqueue_single(block_number) }
3335
end
3436

3537
def fetch(block_number)
@@ -104,6 +106,11 @@ def shutdown
104106

105107
private
106108

109+
def get_latest_block_number
110+
@eth.get_block_number
111+
end
112+
memoize :get_latest_block_number, ttl: 12.seconds
113+
107114
def enqueue_single(block_number)
108115
@promises.compute_if_absent(block_number) do
109116
Rails.logger.debug "Creating promise for block #{block_number}"

spec/support/ethscriptions_test_helper.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,6 @@ def import_l1_block_with_geth(l1_transactions)
515515
)
516516
ethscription_transactions.each { |tx| tx.ethscriptions_block = template_ethscriptions_block }
517517

518-
mock_ethereum_client = instance_double(EthRpcClient,
519-
get_block_number: block_number,
520-
get_block: block_data,
521-
get_transaction_receipts: receipts_data
522-
)
523-
524518
# Mock the prefetcher to return our mock data in the correct format
525519
eth_block = EthBlock.from_rpc_result(block_data)
526520
ethscriptions_block = EthscriptionsBlock.from_eth_block(eth_block)
@@ -541,7 +535,6 @@ def import_l1_block_with_geth(l1_transactions)
541535
old_client = importer.ethereum_client
542536
old_prefetcher = importer.prefetcher
543537

544-
importer.ethereum_client = mock_ethereum_client
545538
importer.instance_variable_set(:@prefetcher, mock_prefetcher)
546539

547540
l2_blocks, eth_blocks = importer.import_next_block

0 commit comments

Comments
 (0)