1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use proc_macro2::TokenStream;
use quote::quote;
use syn::parse_quote;

use crate::emit_compile_error;

/// Deriver for the `MethodHandler` trait.
pub struct DeriveMethodHandler {
    handlers: Vec<ParsedImplItem>,
}

impl DeriveMethodHandler {
    pub fn new() -> Box<Self> {
        Box::new(Self { handlers: vec![] })
    }
}

impl super::Deriver for DeriveMethodHandler {
    fn preprocess(&mut self, item: syn::ImplItem) -> Option<syn::ImplItem> {
        let method = match item {
            syn::ImplItem::Fn(ref f) => f,
            _ => return Some(item),
        };

        let attrs = if let Some(attrs) = parse_attrs(&method.attrs) {
            attrs
        } else {
            return Some(item);
        };

        self.handlers.push(ParsedImplItem {
            handler: Some(HandlerInfo {
                attrs,
                ident: method.sig.ident.clone(),
            }),
            item,
        });

        None // Take the item.
    }

    fn derive(&mut self, generics: &syn::Generics, ty: &Box<syn::Type>) -> TokenStream {
        let handlers = &self.handlers;
        let handler_items = handlers
            .iter()
            .map(|ParsedImplItem { item, .. }| item)
            .collect::<Vec<_>>();

        /// Generates parallel vectors of rpc names and handler idents for all handlers of kind `kind`.
        fn filter_by_kind(
            handlers: &[ParsedImplItem],
            kind: HandlerKind,
        ) -> (Vec<syn::Expr>, Vec<syn::Ident>) {
            handlers
                .iter()
                .filter_map(|h| h.handler.as_ref())
                .filter(|h| h.attrs.kind == kind)
                .map(|h| (h.attrs.rpc_name.clone(), h.ident.clone()))
                .unzip()
        }

        let prefetch_impl = {
            let (handler_names, handler_idents) = filter_by_kind(handlers, HandlerKind::Prefetch);

            // Find call handlers; for every call handler without a corresponding prefetch handler, we'll
            // generate a dummy prefetch handler.
            let (call_handler_names, _) = filter_by_kind(handlers, HandlerKind::Call);
            let handler_names_without_impl: Vec<&syn::Expr> = call_handler_names
                .iter()
                .filter(|n| !handler_names.contains(n))
                .collect();

            if handler_names.is_empty() {
                quote! {}
            } else {
                quote! {
                    fn prefetch(
                        prefixes: &mut BTreeSet<Prefix>,
                        method: &str,
                        body: cbor::Value,
                        auth_info: &AuthInfo,
                    ) -> module::DispatchResult<cbor::Value, Result<(), sdk::error::RuntimeError>> {
                        let mut add_prefix = |p| {prefixes.insert(p);};
                        match method {
                            // "Real", user-defined prefetch handlers.
                            #(
                              #handler_names => module::DispatchResult::Handled(
                                Self::#handler_idents(&mut add_prefix, body, auth_info)
                              ),
                            )*
                            // No-op prefetch handlers.
                            #(
                              #handler_names_without_impl => module::DispatchResult::Handled(Ok(())),
                            )*
                            _ => module::DispatchResult::Unhandled(body),
                        }
                    }
                }
            }
        };

        let dispatch_call_impl = {
            let (handler_names, handler_fns): (Vec<_>, Vec<_>) = handlers
                .iter()
                .filter_map(|h| h.handler.as_ref())
                .filter(|h| h.attrs.kind == HandlerKind::Call)
                .map(|h| {
                    (h.attrs.rpc_name.clone(), {
                        let ident = &h.ident;

                        if h.attrs.is_internal {
                            quote! {
                                |ctx, body| {
                                    if !sdk::state::CurrentState::with_env(|env| env.is_internal()) {
                                        return Err(sdk::modules::core::Error::Forbidden.into());
                                    }
                                    Self::#ident(ctx, body)
                                }
                            }
                        } else {
                            quote! { Self::#ident }
                        }
                    })
                })
                .unzip();

            if handler_names.is_empty() {
                quote! {}
            } else {
                quote! {
                    fn dispatch_call<C: Context>(
                        ctx: &C,
                        method: &str,
                        body: cbor::Value,
                    ) -> DispatchResult<cbor::Value, CallResult> {
                        match method {
                            #(
                              #handler_names => module::dispatch_call(ctx, body, #handler_fns),
                            )*
                            _ => DispatchResult::Unhandled(body),
                        }
                    }
                }
            }
        };

        let query_parameters_impl = {
            quote! {
                fn query_parameters<C: Context>(_ctx: &C, _args: ()) -> Result<<Self as module::Module>::Parameters, <Self as module::Module>::Error> {
                    Ok(Self::params())
                }
            }
        };

        let dispatch_query_impl = {
            let (handler_names, handler_idents) = filter_by_kind(handlers, HandlerKind::Query);

            if handler_names.is_empty() {
                quote! {
                    fn dispatch_query<C: Context>(
                        ctx: &C,
                        method: &str,
                        args: cbor::Value,
                    ) -> DispatchResult<cbor::Value, Result<cbor::Value, sdk::error::RuntimeError>> {
                        match method {
                            q if q == format!("{}.Parameters", Self::NAME) => module::dispatch_query(ctx, args, Self::query_parameters),
                            _ => DispatchResult::Unhandled(args),
                        }
                    }
                }
            } else {
                quote! {
                    fn dispatch_query<C: Context>(
                        ctx: &C,
                        method: &str,
                        args: cbor::Value,
                    ) -> DispatchResult<cbor::Value, Result<cbor::Value, sdk::error::RuntimeError>> {
                        match method {
                            #(
                              #handler_names => module::dispatch_query(ctx, args, Self::#handler_idents),
                            )*
                            q if q == format!("{}.Parameters", Self::NAME) => module::dispatch_query(ctx, args, Self::query_parameters),
                            _ => DispatchResult::Unhandled(args),
                        }
                    }
                }
            }
        };

        let dispatch_message_result_impl = {
            let (handler_names, handler_idents) =
                filter_by_kind(handlers, HandlerKind::MessageResult);

            if handler_names.is_empty() {
                quote! {}
            } else {
                quote! {
                    fn dispatch_message_result<C: Context>(
                        ctx: &C,
                        handler_name: &str,
                        result: MessageResult,
                    ) -> DispatchResult<MessageResult, ()> {
                        match handler_name {
                            #(
                              #handler_names => {
                                  Self::#handler_idents(
                                      ctx,
                                      result.event,
                                      cbor::from_value(result.context).expect("invalid message handler context"),
                                  );
                                  DispatchResult::Handled(())
                              }
                            )*
                            _ => DispatchResult::Unhandled(result),
                        }
                    }
                }
            }
        };

        let supported_methods_impl = {
            let (handler_names, handler_kinds): (Vec<syn::Expr>, Vec<syn::Path>) = handlers
                .iter()
                .filter_map(|h| h.handler.as_ref())
                // `prefetch` is an implementation detail of `call` handlers, so we don't list them
                .filter(|h| h.attrs.kind != HandlerKind::Prefetch)
                .map(|h| (h.attrs.rpc_name.clone(), h.attrs.kind.as_sdk_ident()))
                .unzip();
            if handler_names.is_empty() {
                quote! {}
            } else {
                quote! {
                    fn supported_methods() -> Vec<core_types::MethodHandlerInfo> {
                        vec![ #(
                            core_types::MethodHandlerInfo {
                                kind: #handler_kinds,
                                name: #handler_names.to_string(),
                            },
                        )* ]
                    }
                }
            }
        };

        let expensive_queries_impl = {
            let handler_names: Vec<syn::Expr> = handlers
                .iter()
                .filter_map(|h| h.handler.as_ref())
                .filter(|h| h.attrs.kind == HandlerKind::Query && h.attrs.is_expensive)
                .map(|h| h.attrs.rpc_name.clone())
                .collect();
            if handler_names.is_empty() {
                quote! {}
            } else {
                quote! {
                    fn is_expensive_query(method: &str) -> bool {
                        [ #( #handler_names, )* ].contains(&method)
                    }
                }
            }
        };

        let allowed_private_km_queries_impl = {
            let handler_names: Vec<syn::Expr> = handlers
                .iter()
                .filter_map(|h| h.handler.as_ref())
                .filter(|h| h.attrs.kind == HandlerKind::Query && h.attrs.allow_private_km)
                .map(|h| h.attrs.rpc_name.clone())
                .collect();
            if handler_names.is_empty() {
                quote! {}
            } else {
                quote! {
                    fn is_allowed_private_km_query(method: &str) -> bool {
                        [ #( #handler_names, )* ].contains(&method)
                    }
                }
            }
        };

        let allowed_interactive_calls_impl = {
            let handler_names: Vec<syn::Expr> = handlers
                .iter()
                .filter_map(|h| h.handler.as_ref())
                .filter(|h| h.attrs.kind == HandlerKind::Call && h.attrs.allow_interactive)
                .map(|h| h.attrs.rpc_name.clone())
                .collect();
            if handler_names.is_empty() {
                quote! {}
            } else {
                quote! {
                    fn is_allowed_interactive_call(method: &str) -> bool {
                        [ #( #handler_names, )* ].contains(&method)
                    }
                }
            }
        };

        quote! {
            #[automatically_derived]
            impl #generics sdk::module::MethodHandler for #ty {
                #prefetch_impl
                #dispatch_call_impl
                #dispatch_query_impl
                #dispatch_message_result_impl
                #supported_methods_impl
                #expensive_queries_impl
                #allowed_private_km_queries_impl
                #allowed_interactive_calls_impl
            }

            #[automatically_derived]
            impl #generics #ty {
                #query_parameters_impl

                #(#handler_items)*
            }
        }
    }
}

/// An item (in the `syn` sense, i.e. a fn, type, comment, etc) in an `impl` block,
/// plus parsed data about its #[handler] attribute, if any.
#[derive(Clone)]
struct ParsedImplItem {
    item: syn::ImplItem,
    handler: Option<HandlerInfo>,
}

#[derive(Clone, Debug)]
struct HandlerInfo {
    attrs: MethodHandlerAttr,
    /// Name of the handler function.
    ident: syn::Ident,
}

#[derive(Debug, Copy, Clone, PartialEq)]
enum HandlerKind {
    Call,
    Query,
    MessageResult,
    Prefetch,
}

impl HandlerKind {
    fn as_sdk_ident(&self) -> syn::Path {
        match self {
            HandlerKind::Call => parse_quote!(core_types::MethodHandlerKind::Call),
            HandlerKind::Query => parse_quote!(core_types::MethodHandlerKind::Query),
            HandlerKind::MessageResult => {
                parse_quote!(core_types::MethodHandlerKind::MessageResult)
            }
            HandlerKind::Prefetch => {
                unimplemented!("prefetch cannot be expressed in core::types::MethodHandlerKind")
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
struct MethodHandlerAttr {
    kind: HandlerKind,
    /// Name of the RPC that this handler handles, e.g. "my_module.MyQuery".
    rpc_name: syn::Expr,
    /// Whether this handler is tagged as expensive. Only applies to query handlers.
    is_expensive: bool,
    /// Whether this handler is tagged as allowing access to private key manager state. Only applies
    /// to query handlers.
    allow_private_km: bool,
    /// Whether this handler is tagged as allowing interactive calls. Only applies to call handlers.
    allow_interactive: bool,
    /// Whether this handler is tagged as internal.
    is_internal: bool,
}
impl syn::parse::Parse for MethodHandlerAttr {
    fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
        let kind: syn::Ident = input.parse()?;
        let kind = match kind.to_string().as_str() {
            "call" => HandlerKind::Call,
            "query" => HandlerKind::Query,
            "message_result" => HandlerKind::MessageResult,
            "prefetch" => HandlerKind::Prefetch,
            _ => return Err(syn::Error::new(kind.span(), "invalid handler kind")),
        };
        let _: syn::token::Eq = input.parse()?;
        let rpc_name: syn::Expr = input.parse()?;

        // Parse optional comma-separated tags.
        let mut is_expensive = false;
        let mut allow_private_km = false;
        let mut allow_interactive = false;
        let mut is_internal = false;
        while input.peek(syn::token::Comma) {
            let _: syn::token::Comma = input.parse()?;
            let tag: syn::Ident = input.parse()?;

            if tag == "expensive" {
                if kind != HandlerKind::Query {
                    return Err(syn::Error::new(
                        tag.span(),
                        "`expensive` tag is only allowed on `query` handlers",
                    ));
                }
                is_expensive = true;
            } else if tag == "allow_private_km" {
                if kind != HandlerKind::Query {
                    return Err(syn::Error::new(
                        tag.span(),
                        "`allow_private_km` tag is only allowed on `query` handlers",
                    ));
                }
                allow_private_km = true;
            } else if tag == "allow_interactive" {
                if kind != HandlerKind::Call {
                    return Err(syn::Error::new(
                        tag.span(),
                        "`allow_interactive` tag is only allowed on `call` handlers",
                    ));
                }
                allow_interactive = true;
            } else if tag == "internal" {
                if kind != HandlerKind::Call {
                    return Err(syn::Error::new(
                        tag.span(),
                        "`internal` tag is only allowed on `call` handlers",
                    ));
                }
                is_internal = true;
            } else {
                return Err(syn::Error::new(
                    tag.span(),
                    "invalid handler tag; supported: `expensive`, `allow_private_km`, `allow_interactive`, `internal`",
                ));
            }
        }

        if !input.is_empty() {
            return Err(syn::Error::new(input.span(), "unexpected extra tokens"));
        }
        Ok(Self {
            kind,
            rpc_name,
            is_expensive,
            allow_private_km,
            allow_interactive,
            is_internal,
        })
    }
}

fn parse_attrs(attrs: &[syn::Attribute]) -> Option<MethodHandlerAttr> {
    let handler_meta = attrs.iter().find(|attr| attr.path().is_ident("handler"))?;
    handler_meta
        .parse_args()
        .map_err(|err| {
            emit_compile_error(format!(
                "Unsupported format of #[handler(...)] attribute: {err}"
            ))
        })
        .ok()
}