11use std:: collections:: HashSet ;
2+ use std:: fs:: File ;
23use std:: net:: SocketAddr ;
34use std:: num:: NonZeroUsize ;
45use std:: path:: PathBuf ;
@@ -11,6 +12,7 @@ use ipnet::IpNet;
1112use p2p:: libp2p:: Multiaddr ;
1213use pathfinder_common:: consts:: VERGEN_GIT_DESCRIBE ;
1314use pathfinder_common:: AllowedOrigins ;
15+ use pathfinder_executor:: VersionedConstants ;
1416use pathfinder_storage:: JournalMode ;
1517use reqwest:: Url ;
1618
@@ -256,6 +258,13 @@ This should only be enabled for debugging purposes as it adds substantial proces
256258 value_parser = parse_state_tries
257259 ) ]
258260 state_tries : Option < StateTries > ,
261+
262+ #[ arg(
263+ long = "rpc.custom-versioned-constants-json-path" ,
264+ long_help = "Path to a JSON file containing the versioned constants to use for execution" ,
265+ env = "PATHFINDER_RPC_CUSTOM_VERSIONED_CONSTANTS_JSON_PATH"
266+ ) ]
267+ custom_versioned_constants_path : Option < PathBuf > ,
259268}
260269
261270#[ derive( clap:: ValueEnum , Debug , Clone , Copy , PartialEq ) ]
@@ -618,6 +627,35 @@ enum RpcCorsDomainsParseError {
618627 WildcardAmongOtherValues ,
619628}
620629
630+ fn parse_versioned_constants (
631+ path : PathBuf ,
632+ ) -> Result < VersionedConstants , ParseVersionedConstantsError > {
633+ let file = File :: open ( path) ?;
634+ let reader = std:: io:: BufReader :: new ( file) ;
635+ let versioned_constants = serde_json:: from_reader ( reader) ?;
636+
637+ Ok ( versioned_constants)
638+ }
639+
640+ pub fn parse_versioned_constants_or_exit ( path : PathBuf ) -> VersionedConstants {
641+ use clap:: error:: ErrorKind ;
642+
643+ match parse_versioned_constants ( path) {
644+ Ok ( versioned_constants) => versioned_constants,
645+ Err ( error) => Cli :: command ( )
646+ . error ( ErrorKind :: ValueValidation , error)
647+ . exit ( ) ,
648+ }
649+ }
650+
651+ #[ derive( Debug , thiserror:: Error ) ]
652+ enum ParseVersionedConstantsError {
653+ #[ error( "IO error while reading versioned constants: {0}." ) ]
654+ Io ( #[ from] std:: io:: Error ) ,
655+ #[ error( "Parse error while loading versioned constants: {0}." ) ]
656+ Parse ( #[ from] serde_json:: Error ) ,
657+ }
658+
621659pub struct Config {
622660 pub data_directory : PathBuf ,
623661 pub ethereum : Ethereum ,
@@ -644,6 +682,7 @@ pub struct Config {
644682 pub get_events_max_blocks_to_scan : NonZeroUsize ,
645683 pub get_events_max_uncached_bloom_filters_to_load : NonZeroUsize ,
646684 pub state_tries : Option < StateTries > ,
685+ pub custom_versioned_constants : Option < VersionedConstants > ,
647686}
648687
649688pub struct Ethereum {
@@ -928,6 +967,9 @@ impl Config {
928967 . get_events_max_uncached_bloom_filters_to_load ,
929968 gateway_timeout : Duration :: from_secs ( cli. gateway_timeout . get ( ) ) ,
930969 state_tries : cli. state_tries ,
970+ custom_versioned_constants : cli
971+ . custom_versioned_constants_path
972+ . map ( parse_versioned_constants_or_exit) ,
931973 }
932974 }
933975}
@@ -966,8 +1008,10 @@ pub struct WebsocketConfig {
9661008
9671009#[ cfg( test) ]
9681010mod tests {
1011+ use assert_matches:: assert_matches;
1012+
9691013 use super :: { AllowedOrigins , RpcCorsDomainsParseError } ;
970- use crate :: config:: parse_cors;
1014+ use crate :: config:: { parse_cors, ParseVersionedConstantsError } ;
9711015
9721016 #[ test]
9731017 fn parse_cors_domains ( ) {
@@ -1043,4 +1087,29 @@ mod tests {
10431087 )
10441088 } ) ;
10451089 }
1090+
1091+ #[ test]
1092+ fn parse_versioned_constants_fails_if_file_not_found ( ) {
1093+ assert_matches ! (
1094+ super :: parse_versioned_constants( "./nonexistent_versioned_constants.json" . into( ) ) . unwrap_err( ) ,
1095+ ParseVersionedConstantsError :: Io ( err) => assert_eq!( err. kind( ) , std:: io:: ErrorKind :: NotFound )
1096+ ) ;
1097+ }
1098+
1099+ #[ test]
1100+ fn parse_versioned_constants_fails_on_parse_error ( ) {
1101+ assert_matches ! (
1102+ super :: parse_versioned_constants( "resources/invalid_versioned_constants.json" . into( ) )
1103+ . unwrap_err( ) ,
1104+ ParseVersionedConstantsError :: Parse ( _)
1105+ )
1106+ }
1107+
1108+ #[ test]
1109+ fn parse_versioned_constants_success ( ) {
1110+ super :: parse_versioned_constants (
1111+ "../executor/resources/versioned_constants_13_1_1.json" . into ( ) ,
1112+ )
1113+ . unwrap ( ) ;
1114+ }
10461115}
0 commit comments