@@ -133,11 +133,6 @@ pub mod serve {
133
133
pub mod query {
134
134
//! CLI definition for the `query` subcommand.
135
135
136
- // TODO: I envision several subcommands here. For example:
137
- // - query block <block_hash/number> — returns the information about a block and header.
138
- // - query blob <id> - returns the blob for a given key. The key here is the same sense as
139
- // described here https://github.com/thrumdev/blobs/issues/9#issuecomment-1814005570.
140
-
141
136
use super :: { KeyManagementParams , SugondatRpcParams , ENV_SUGONDAT_NAMESPACE } ;
142
137
use clap:: { Args , Subcommand } ;
143
138
@@ -153,64 +148,105 @@ pub mod query {
153
148
Submit ( submit:: Params ) ,
154
149
/// Queries information about a block and header.
155
150
Block ( block:: Params ) ,
151
+ /// Queries information about a specific blob.
152
+ Blob ( blob:: Params ) ,
156
153
}
157
154
158
- pub mod block {
159
- //! CLI definition for the `query block` subcommand.
155
+ /// A reference to a block to query.
156
+ #[ derive( Debug , Clone ) ]
157
+ pub enum BlockRef {
158
+ /// The current best finalized block known by the node.
159
+ Best ,
160
+ /// The number of the block to query.
161
+ Number ( u64 ) ,
162
+ /// The hex-encoded hash of the block to query, prefixed with "0x".
163
+ Hash ( [ u8 ; 32 ] ) ,
164
+ }
160
165
161
- use clap:: Args ;
166
+ impl std:: fmt:: Display for BlockRef {
167
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
168
+ match * self {
169
+ BlockRef :: Best => write ! ( f, "best" ) ,
170
+ BlockRef :: Number ( n) => write ! ( f, "{}" , n) ,
171
+ BlockRef :: Hash ( h) => write ! ( f, "0x{}" , hex:: encode( & h[ ..] ) ) ,
172
+ }
173
+ }
174
+ }
162
175
163
- use super :: SugondatRpcParams ;
164
-
165
- /// A reference to a block to query.
166
- #[ derive( Debug , Clone ) ]
167
- pub enum BlockRef {
168
- /// The current best finalized block known by the node.
169
- Best ,
170
- /// The number of the block to query.
171
- Number ( u64 ) ,
172
- /// The hex-encoded hash of the block to query, prefixed with "0x".
173
- Hash ( [ u8 ; 32 ] ) ,
176
+ impl std:: str:: FromStr for BlockRef {
177
+ type Err = String ;
178
+
179
+ fn from_str ( input : & str ) -> Result < Self , Self :: Err > {
180
+ if input == "best" {
181
+ return Ok ( BlockRef :: Best ) ;
182
+ }
183
+
184
+ if let Some ( hash) = decode_hash ( input) ? {
185
+ return Ok ( BlockRef :: Hash ( hash) ) ;
186
+ }
187
+
188
+ if let Ok ( n) = input. parse :: < u64 > ( ) {
189
+ Ok ( BlockRef :: Number ( n) )
190
+ } else {
191
+ Err ( format ! ( "parse error. see `--help`" ) )
192
+ }
174
193
}
194
+ }
175
195
176
- impl std:: fmt:: Display for BlockRef {
177
- fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
178
- match * self {
179
- BlockRef :: Best => write ! ( f, "best" ) ,
180
- BlockRef :: Number ( n) => write ! ( f, "{}" , n) ,
181
- BlockRef :: Hash ( h) => write ! ( f, "0x{}" , hex:: encode( & h[ ..] ) ) ,
182
- }
196
+ fn decode_hash ( input : & str ) -> Result < Option < [ u8 ; 32 ] > , String > {
197
+ if let Some ( s) = input. strip_prefix ( "0x" ) {
198
+ let bytes =
199
+ hex:: decode ( s) . map_err ( |_| "Invalid parameter: not hex encoded" . to_owned ( ) ) ?;
200
+
201
+ let mut hash = [ 0u8 ; 32 ] ;
202
+ if bytes. len ( ) != 32 {
203
+ return Err ( "Invalid parameter: hash not 32 bytes" . to_owned ( ) ) ;
183
204
}
205
+
206
+ hash. copy_from_slice ( & bytes[ ..] ) ;
207
+ Ok ( Some ( hash) )
208
+ } else {
209
+ Ok ( None )
184
210
}
211
+ }
185
212
186
- impl std :: str :: FromStr for BlockRef {
187
- type Err = String ;
213
+ pub mod blob {
214
+ use clap :: Args ;
188
215
189
- fn from_str ( input : & str ) -> Result < Self , Self :: Err > {
190
- if input == "best" {
191
- return Ok ( BlockRef :: Best ) ;
192
- }
216
+ use super :: { BlockRef , SugondatRpcParams } ;
193
217
194
- if let Some ( s) = input. strip_prefix ( "0x" ) {
195
- let bytes = hex:: decode ( s)
196
- . map_err ( |_| "Invalid parameter: not hex encoded" . to_owned ( ) ) ?;
218
+ #[ derive( Debug , Args ) ]
219
+ pub struct Params {
220
+ #[ clap( flatten) ]
221
+ pub rpc : SugondatRpcParams ,
197
222
198
- let mut hash = [ 0u8 ; 32 ] ;
199
- if bytes. len ( ) != 32 {
200
- return Err ( "Invalid parameter: hash not 32 bytes" . to_owned ( ) ) ;
201
- }
223
+ /// The block containing the blob to query.
224
+ ///
225
+ /// Possible values: ["best", number, hash]
226
+ ///
227
+ /// "best" is the highest finalized block.
228
+ ///
229
+ /// Hashes must be 32 bytes, hex-encoded, and prefixed with "0x".
230
+ #[ arg( value_name = "BLOCK_REF" ) ]
231
+ pub block : BlockRef ,
202
232
203
- hash . copy_from_slice ( & bytes [ .. ] ) ;
204
- return Ok ( BlockRef :: Hash ( hash ) ) ;
205
- }
233
+ /// The index of the extrinsic (transaction) containing the blob.
234
+ # [ arg ( value_name = "INDEX" ) ]
235
+ pub index : u32 ,
206
236
207
- if let Ok ( n) = input. parse :: < u64 > ( ) {
208
- Ok ( BlockRef :: Number ( n) )
209
- } else {
210
- Err ( format ! ( "parse error. see `--help`" ) )
211
- }
212
- }
237
+ /// Output the blob data as binary to stdout rather than hex, and omits
238
+ /// any other details intended for human consumption.
239
+ #[ arg( long) ]
240
+ pub raw : bool ,
213
241
}
242
+ }
243
+
244
+ pub mod block {
245
+ //! CLI definition for the `query block` subcommand.
246
+
247
+ use clap:: Args ;
248
+
249
+ use super :: { BlockRef , SugondatRpcParams } ;
214
250
215
251
#[ derive( Debug , Args ) ]
216
252
pub struct Params {
0 commit comments