1
- use crate :: cmd:: serve;
2
- use anyhow:: bail;
3
1
use 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" ;
6
32
7
33
#[ derive( Parser , Debug ) ]
8
34
#[ command( author, version, about, long_about = None ) ]
9
- struct Cli {
35
+ pub struct Cli {
10
36
#[ command( subcommand) ]
11
- command : Commands ,
37
+ pub command : Commands ,
12
38
}
13
39
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
-
18
40
/// 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.
22
41
#[ derive( clap:: Args , Debug ) ]
23
42
pub struct AdapterServerParams {
24
43
/// The address on which the shim should listen for incoming connections from the rollup nodes.
@@ -29,14 +48,22 @@ pub struct AdapterServerParams {
29
48
#[ clap(
30
49
short,
31
50
long,
32
- env = SUGONDAT_SHIM_PORT_ENV ,
51
+ env = ENV_SUGONDAT_SHIM_PORT ,
33
52
default_value = "10995" ,
34
53
group = "listen"
35
54
) ]
36
55
pub port : u16 ,
37
56
// TODO: e.g. --submit-key, prometheus stuff, enabled adapters, etc.
38
57
}
39
58
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
+
40
67
impl AdapterServerParams {
41
68
/// Whether the sovereign adapter should be enabled.
42
69
pub fn enable_sovereign ( & self ) -> bool {
@@ -45,30 +72,70 @@ impl AdapterServerParams {
45
72
}
46
73
47
74
#[ derive( Subcommand , Debug ) ]
48
- enum Commands {
75
+ pub enum Commands {
76
+ /// Connect to the sugondat node and serve requests from the rollup nodes.
49
77
Serve ( serve:: Params ) ,
78
+ /// Serve requests from the rollup nodes by simulating the DA layer.
50
79
Simulate ,
80
+ /// Allows running queries locally. Useful for debugging.
81
+ Query ( query:: Params ) ,
51
82
}
52
83
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 ,
61
97
}
62
- Ok ( ( ) )
63
98
}
64
99
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
+ }
74
141
}
0 commit comments