1313import time
1414from queue import Empty , Full , Queue
1515from threading import Thread
16- from typing import Any , Dict , List , Optional , TypeVar , Union , cast
16+ from typing import Any , Dict , Final , List , Optional , TypeVar , Union , cast
1717from urllib import request
1818
1919from ethereum_rlp import rlp
@@ -111,6 +111,7 @@ class BlockDownloader(ForkTracking):
111111 log : logging .Logger
112112 rpc_url : str
113113 geth : bool
114+ headers : Final [dict [str , str ]]
114115
115116 def __init__ (
116117 self ,
@@ -120,6 +121,8 @@ def __init__(
120121 geth : bool ,
121122 first_block : Uint ,
122123 first_block_timestamp : U256 ,
124+ * ,
125+ headers : dict [str , str ] | None = None ,
123126 ) -> None :
124127 ForkTracking .__init__ (self , forks , first_block , first_block_timestamp )
125128
@@ -136,6 +139,11 @@ def __init__(
136139 self .rpc_url = rpc_url
137140 self .geth = geth
138141
142+ if headers is None :
143+ headers = {}
144+
145+ self .headers = headers
146+
139147 Thread (target = self .download , name = "download" , daemon = True ).start ()
140148
141149 def take_block (self ) -> Optional [Any ]:
@@ -221,14 +229,18 @@ def fetch_blocks_debug(
221229
222230 self .log .debug ("fetching blocks [%d, %d)..." , first , first + count )
223231
232+ headers = {
233+ "Content-Length" : str (len (data )),
234+ "Content-Type" : "application/json" ,
235+ "User-Agent" : "ethereum-spec-sync" ,
236+ }
237+
238+ headers .update (self .headers )
239+
224240 post = request .Request (
225241 self .rpc_url ,
226242 data = data ,
227- headers = {
228- "Content-Length" : str (len (data )),
229- "Content-Type" : "application/json" ,
230- "User-Agent" : "ethereum-spec-sync" ,
231- },
243+ headers = headers ,
232244 )
233245
234246 with request .urlopen (post ) as response :
@@ -412,14 +424,18 @@ def fetch_blocks_eth(
412424
413425 self .log .debug ("fetching blocks [%d, %d)..." , first , first + count )
414426
427+ headers = {
428+ "Content-Length" : str (len (data )),
429+ "Content-Type" : "application/json" ,
430+ "User-Agent" : "ethereum-spec-sync" ,
431+ }
432+
433+ headers .update (self .headers )
434+
415435 post = request .Request (
416436 self .rpc_url ,
417437 data = data ,
418- headers = {
419- "Content-Length" : str (len (data )),
420- "Content-Type" : "application/json" ,
421- "User-Agent" : "ethereum-spec-sync" ,
422- },
438+ headers = headers ,
423439 )
424440
425441 with request .urlopen (post ) as response :
@@ -489,14 +505,18 @@ def fetch_ommers(self, ommers_needed: Dict[Uint, int]) -> Dict[Uint, Any]:
489505 max (ommers_needed ),
490506 )
491507
508+ headers = {
509+ "Content-Length" : str (len (data )),
510+ "Content-Type" : "application/json" ,
511+ "User-Agent" : "ethereum-spec-sync" ,
512+ }
513+
514+ headers .update (self .headers )
515+
492516 post = request .Request (
493517 self .rpc_url ,
494518 data = data ,
495- headers = {
496- "Content-Length" : str (len (data )),
497- "Content-Type" : "application/json" ,
498- "User-Agent" : "ethereum-spec-sync" ,
499- },
519+ headers = headers ,
500520 )
501521
502522 with request .urlopen (post ) as response :
@@ -663,18 +683,26 @@ def parse_arguments() -> argparse.Namespace:
663683 )
664684 parser .add_argument (
665685 "--zhejiang" ,
666- help = "Set the chain to mainnet " ,
686+ help = "Set the chain to zhejiang " ,
667687 action = "store_const" ,
668688 dest = "chain" ,
669689 const = "zhejiang" ,
670690 )
671691 parser .add_argument (
672692 "--sepolia" ,
673- help = "Set the chain to mainnet " ,
693+ help = "Set the chain to sepolia " ,
674694 action = "store_const" ,
675695 dest = "chain" ,
676696 const = "sepolia" ,
677697 )
698+ parser .add_argument (
699+ "--header" ,
700+ "-H" ,
701+ help = "Add a header to RPC requests" ,
702+ nargs = 2 ,
703+ action = "append" ,
704+ dest = "headers" ,
705+ )
678706
679707 return parser .parse_args ()
680708
@@ -687,6 +715,10 @@ def __init__(self) -> None:
687715 self .log = logging .getLogger (__name__ )
688716 self .options = self .parse_arguments ()
689717
718+ headers = None
719+ if self .options .headers is not None :
720+ headers = dict (self .options .headers )
721+
690722 if not self .options .unoptimized :
691723 import ethereum_optimized
692724
@@ -805,6 +837,7 @@ def __init__(self) -> None:
805837 self .options .geth ,
806838 Uint (0 ),
807839 genesis_configuration .timestamp ,
840+ headers = headers ,
808841 )
809842 self .set_block (Uint (0 ), genesis_configuration .timestamp )
810843 else :
@@ -820,6 +853,7 @@ def __init__(self) -> None:
820853 self .options .geth ,
821854 persisted_block - initial_blocks_length ,
822855 persisted_block_timestamp ,
856+ headers = headers ,
823857 )
824858 blocks = []
825859 for _ in range (initial_blocks_length ):
0 commit comments