@@ -7,34 +7,49 @@ use std::fs::File;
77use std:: {
88 io:: { self , BufRead } ,
99 path:: Path ,
10+ time:: Duration ,
1011} ;
1112
1213use anyhow:: anyhow;
1314use ethportal_api:: {
14- utils:: bytes:: hex_decode, BlockBodyKey , BlockReceiptsKey , ContentValue , HistoryContentKey ,
15- HistoryContentValue ,
15+ jsonrpsee:: http_client:: { HttpClient , HttpClientBuilder } ,
16+ types:: { cli:: DEFAULT_WEB3_HTTP_ADDRESS , network:: Subnetwork , query_trace:: QueryTrace } ,
17+ utils:: bytes:: hex_decode,
18+ BlockBodyKey , BlockReceiptsKey , ContentValue , HistoryContentKey , HistoryContentValue ,
1619} ;
1720use futures:: { channel:: oneshot, future:: join_all} ;
18- use portalnet:: overlay:: command:: OverlayCommand ;
21+ use portal_bridge:: census:: Census ;
22+ use portalnet:: overlay:: { command:: OverlayCommand , config:: FindContentConfig } ;
1923use tokio:: sync:: mpsc:: UnboundedSender ;
2024use tracing:: { error, info, warn} ;
2125
2226/// The number of blocks to download in a single batch.
23- const BATCH_SIZE : usize = 40 ;
27+ const BATCH_SIZE : usize = 3 ;
2428/// The path to the CSV file with block numbers and block hashes.
2529const CSV_PATH : & str = "ethereum_blocks_14000000_merge.csv" ;
2630
2731#[ derive( Clone ) ]
2832pub struct Downloader {
33+ pub census : Census ,
2934 pub overlay_tx : UnboundedSender < OverlayCommand < HistoryContentKey > > ,
3035}
3136
3237impl Downloader {
3338 pub fn new ( overlay_tx : UnboundedSender < OverlayCommand < HistoryContentKey > > ) -> Self {
34- Self { overlay_tx }
39+ let http_client: HttpClient = HttpClientBuilder :: default ( )
40+ // increase default timeout to allow for trace_gossip requests that can take a long
41+ // time
42+ . request_timeout ( Duration :: from_secs ( 120 ) )
43+ . build ( DEFAULT_WEB3_HTTP_ADDRESS )
44+ . map_err ( |e| e. to_string ( ) )
45+ . expect ( "Failed to build http client" ) ;
46+
47+ // BUild hhtp client binded to the current node web3rpc
48+ let census = Census :: new ( http_client, 0 , vec ! [ ] ) ;
49+ Self { overlay_tx, census }
3550 }
3651
37- pub async fn start ( self ) -> io:: Result < ( ) > {
52+ pub async fn start ( mut self ) -> io:: Result < ( ) > {
3853 // set the csv path to a file in the root trin-history directory
3954 info ! ( "Opening CSV file" ) ;
4055 let csv_path = Path :: new ( CSV_PATH ) ;
@@ -47,6 +62,14 @@ impl Downloader {
4762 // skip the header of the csv file
4863 let lines = & lines[ 1 ..] ;
4964 let blocks: Vec < ( u64 , String ) > = lines. iter ( ) . map ( |line| parse_line ( line) ) . collect ( ) ;
65+ // Initialize the census with the history subnetwork
66+ let _ = Some (
67+ self . census
68+ . init ( [ Subnetwork :: History ] )
69+ . await
70+ . expect ( "Failed to initialize Census" ) ,
71+ ) ;
72+
5073 info ! ( "Processing blocks" ) ;
5174 let batches = blocks. chunks ( BATCH_SIZE ) ;
5275
@@ -91,7 +114,10 @@ impl Downloader {
91114 let overlay_command = OverlayCommand :: FindContentQuery {
92115 target : content_key. clone ( ) ,
93116 callback : tx,
94- config : Default :: default ( ) ,
117+ config : FindContentConfig {
118+ is_trace : true ,
119+ ..Default :: default ( )
120+ } ,
95121 } ;
96122
97123 if let Err ( err) = self . overlay_tx . send ( overlay_command) {
@@ -104,7 +130,14 @@ impl Downloader {
104130 Ok ( result) => match result {
105131 Ok ( result) => {
106132 HistoryContentValue :: decode ( & content_key, & result. 0 ) ?;
107- info ! ( block_number = block_number, "Downloaded content for block" ) ;
133+ let duration_ms = QueryTrace :: timestamp_millis_u64 (
134+ result. 2 . expect ( "QueryTrace not found" ) . started_at_ms ,
135+ ) ;
136+ info ! (
137+ block_number = block_number,
138+ query_duration = duration_ms,
139+ "Downloaded content for block"
140+ ) ;
108141 Ok ( ( ) )
109142 }
110143 Err ( err) => {
0 commit comments