1- use crate :: cmd:: serve;
2- use anyhow:: bail;
31use clap:: { Parser , Subcommand } ;
4- use tracing_subscriber:: fmt;
5- use tracing_subscriber:: prelude:: * ;
2+
3+ // NOTE:
4+ //
5+ // The architecture of the CLI may seem contrived, but here are some reasons for it:
6+ //
7+ // - We want to push the parameters into the subcommands, instead of having them on the more general
8+ // structs. Specifially, we want to avoid
9+ //
10+ // sugondat-shim -p 10 serve --node-url=...
11+ //
12+ // because the user will have to remember where each flag must be (e.g. here -p before the
13+ // subcommand, but --node-url after the subcommand). Besides, it also looks clunky.
14+ //
15+ // - We want to have the CLI definition not to be scatered all over the codebase. Therefore it is
16+ // defined in a single file.
17+ //
18+ // - We use modules to group the CLI definitions for each subcommand, instead of prefixing and
19+ // dealing with lots of types like `ServeParams`, `QueryParams`, `QuerySubmitParams`, etc.
20+ //
21+ // This approach is more verbose, but it is also more explicit and easier to understand.
22+ // Verbosiness is OK here, because we reserve the entire file for the CLI definitions
23+ // anyway.
24+ //
25+ // When adding a new subcommand or parameter, try to follow the same patterns as the existing
26+ // ones. Ensure that the flags are consistent with the other subcommands, that the help
27+ // messages are present and clear, etc.
28+
29+ const ENV_SUGONDAT_SHIM_PORT : & str = "SUGONDAT_SHIM_PORT" ;
30+ const ENV_SUGONDAT_NAMESPACE : & str = "SUGONDAT_NAMESPACE" ;
31+ const ENV_SUGONDAT_NODE_URL : & str = "SUGONDAT_NODE_URL" ;
632
733#[ derive( Parser , Debug ) ]
834#[ command( author, version, about, long_about = None ) ]
9- struct Cli {
35+ pub struct Cli {
1036 #[ command( subcommand) ]
11- command : Commands ,
37+ pub command : Commands ,
1238}
1339
14- /// The environment variable used to override the default port to listen to when serving or to
15- /// connect to when running RPCs.
16- const SUGONDAT_SHIM_PORT_ENV : & str = "SUGONDAT_SHIM_PORT" ;
17-
1840/// Common parameters for the adapter subcommands.
19- ///
20- /// It's not declared on the `Cli` struct with `clap(flatten)` because of how the syntax
21- /// `sugondat-shim -p 10 serve --node-url` looks unintuitive.
2241#[ derive( clap:: Args , Debug ) ]
2342pub struct AdapterServerParams {
2443 /// The address on which the shim should listen for incoming connections from the rollup nodes.
@@ -29,14 +48,22 @@ pub struct AdapterServerParams {
2948 #[ clap(
3049 short,
3150 long,
32- env = SUGONDAT_SHIM_PORT_ENV ,
51+ env = ENV_SUGONDAT_SHIM_PORT ,
3352 default_value = "10995" ,
3453 group = "listen"
3554 ) ]
3655 pub port : u16 ,
3756 // TODO: e.g. --submit-key, prometheus stuff, enabled adapters, etc.
3857}
3958
59+ /// Common parameters for that commands that connect to the sugondat-node.
60+ #[ derive( clap:: Args , Debug ) ]
61+ pub struct SugondatRpcParams {
62+ /// The address of the sugondat-node to connect to.
63+ #[ clap( long, default_value = "ws://localhost:9944" , env = ENV_SUGONDAT_NODE_URL ) ]
64+ pub node_url : String ,
65+ }
66+
4067impl AdapterServerParams {
4168 /// Whether the sovereign adapter should be enabled.
4269 pub fn enable_sovereign ( & self ) -> bool {
@@ -45,30 +72,70 @@ impl AdapterServerParams {
4572}
4673
4774#[ derive( Subcommand , Debug ) ]
48- enum Commands {
75+ pub enum Commands {
76+ /// Connect to the sugondat node and serve requests from the rollup nodes.
4977 Serve ( serve:: Params ) ,
78+ /// Serve requests from the rollup nodes by simulating the DA layer.
5079 Simulate ,
80+ /// Allows running queries locally. Useful for debugging.
81+ Query ( query:: Params ) ,
5182}
5283
53- pub async fn run ( ) -> anyhow:: Result < ( ) > {
54- init_logging ( ) ?;
55- let cli = Cli :: parse ( ) ;
56- match cli. command {
57- Commands :: Serve ( params) => serve:: run ( params) . await ?,
58- Commands :: Simulate => {
59- bail ! ( "simulate subcommand not yet implemented" )
60- }
84+ pub mod serve {
85+ //! CLI definition for the `serve` subcommand.
86+
87+ use super :: { AdapterServerParams , SugondatRpcParams } ;
88+ use clap:: Args ;
89+
90+ #[ derive( Debug , Args ) ]
91+ pub struct Params {
92+ #[ clap( flatten) ]
93+ pub rpc : SugondatRpcParams ,
94+
95+ #[ clap( flatten) ]
96+ pub adapter : AdapterServerParams ,
6197 }
62- Ok ( ( ) )
6398}
6499
65- fn init_logging ( ) -> anyhow:: Result < ( ) > {
66- let filter = tracing_subscriber:: EnvFilter :: builder ( )
67- . with_default_directive ( tracing_subscriber:: filter:: LevelFilter :: INFO . into ( ) )
68- . from_env_lossy ( ) ;
69- tracing_subscriber:: registry ( )
70- . with ( fmt:: layer ( ) )
71- . with ( filter)
72- . try_init ( ) ?;
73- Ok ( ( ) )
100+ pub mod query {
101+ //! CLI definition for the `query` subcommand.
102+
103+ use super :: { SugondatRpcParams , ENV_SUGONDAT_NAMESPACE } ;
104+ use clap:: { Args , Subcommand } ;
105+
106+ #[ derive( Debug , Args ) ]
107+ pub struct Params {
108+ #[ command( subcommand) ]
109+ pub command : Commands ,
110+ }
111+
112+ #[ derive( Subcommand , Debug ) ]
113+ pub enum Commands {
114+ /// Submits the given blob into a namespace.
115+ Submit ( submit:: Params ) ,
116+ }
117+
118+ pub mod submit {
119+ //! CLI definition for the `query submit` subcommand.
120+
121+ use super :: { SugondatRpcParams , ENV_SUGONDAT_NAMESPACE } ;
122+ use clap:: Args ;
123+
124+ #[ derive( Debug , Args ) ]
125+ pub struct Params {
126+ #[ clap( flatten) ]
127+ pub rpc : SugondatRpcParams ,
128+
129+ /// The namespace to submit the blob into.
130+ ///
131+ /// The namespace can be specified either as a 4-byte vector, or as an unsigned 32-bit
132+ /// integer. To distinguish between the two, the byte vector must be prefixed with
133+ /// `0x`.
134+ #[ clap( long, short, env = ENV_SUGONDAT_NAMESPACE ) ]
135+ pub namespace : String ,
136+
137+ /// The file path of the blob to submit. Pass `-` to read from stdin.
138+ pub blob_path : String ,
139+ }
140+ }
74141}
0 commit comments