sails-rs 0.10.4

Main abstractions for the Sails framework
Documentation
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
use crate::prelude::*;
use alloy_primitives::Selector;

#[cfg(any(feature = "gtest", all(feature = "gstd", target_arch = "wasm32")))]
pub(crate) const ETH_EVENT_ADDR: gstd::ActorId = gstd::ActorId::new([
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
]);

pub type MethodExpo = (
    &'static [u8], // Method route
    &'static str,  // Method name
    &'static str,  // Method parameters types
    &'static str,  // Method callback parameters types
);

pub type ServiceExpo = (
    &'static str,          // Service expo name
    &'static [u8],         // Service route
    &'static [MethodExpo], // Method routes
);

pub trait ServiceSignature {
    const METHODS: &'static [MethodExpo];
}

pub trait ProgramSignature {
    const CTORS: &'static [MethodExpo];
    const SERVICES: &'static [ServiceExpo];
    const METHODS_LEN: usize;
}

pub fn selector(s: impl AsRef<str>) -> Selector {
    alloy_primitives::keccak256(s.as_ref().as_bytes())[..4]
        .try_into()
        .unwrap()
}

pub const fn const_selector(name: &str) -> [u8; 4] {
    let hash: [u8; 32] = keccak_const::Keccak256::new()
        .update(name.as_bytes())
        .finalize();
    let mut output = [0u8; 4];
    let mut i = 0;
    while i < output.len() {
        output[i] = hash[i];
        i += 1;
    }
    output
}

macro_rules! const_selector {
    () => {
        [0u8; 4]
    };
    ($($s: expr),* $(,)?) => {{
        let mut keccak256 = crate::keccak_const::Keccak256::new();
        $(keccak256 = keccak256.update($s.as_bytes());)*
        let hash: [u8; 32] = keccak256.finalize();
        let mut output = [0u8; 4];
        let mut i = 0;
        while i < output.len() {
            output[i] = hash[i];
            i += 1;
        }
        output
    }};
}

#[macro_export]
macro_rules! const_concat_slices {
    (<$T:ty>, $($A:expr),+ $(,)?) => {{
        use core::mem::MaybeUninit;

        const LEN: usize = $( $A.len() + )* 0;
        const fn combined() -> [$T; LEN] {
            let mut output: [MaybeUninit<$T>; LEN] = [const { MaybeUninit::uninit() }; LEN];
            let offset = 0;
            $(let offset = copy_slice(&mut output, $A, offset);)*
            assert!(offset == LEN);
            unsafe { core::mem::transmute::<_, [$T; LEN]>(output) }
        }
        const fn copy_slice(output: &mut [MaybeUninit<$T>], input: &[$T], offset: usize) -> usize {
            let mut index = 0;
            while index < input.len() {
                output[offset + index].write(input[index]);
                index += 1;
            }
            offset + index
        }
        const RESULT: &[$T] = &combined();
        RESULT
    }};
}

pub struct ConstProgramMeta<T>(marker::PhantomData<T>);

impl<T> ConstProgramMeta<T>
where
    T: ProgramSignature,
{
    pub const fn ctor_sigs<const N: usize>() -> [[u8; 4]; N] {
        let mut sigs = [[0u8; 4]; N];
        let mut ctor_idx = 0;
        while ctor_idx < <T as ProgramSignature>::CTORS.len() {
            let (_, name, params, _) = <T as ProgramSignature>::CTORS[ctor_idx];
            let selector = const_selector!(name, params);
            Self::assert_selector_not_equals_ctor_routes(selector);
            sigs[ctor_idx] = selector;
            ctor_idx += 1;
        }
        sigs
    }

    pub const fn ctor_callback_sigs<const N: usize>() -> [[u8; 4]; N] {
        let mut sigs = [[0u8; 4]; N];
        let mut ctor_idx = 0;
        while ctor_idx < <T as ProgramSignature>::CTORS.len() {
            let (_, name, _, callback) = <T as ProgramSignature>::CTORS[ctor_idx];
            sigs[ctor_idx] = const_selector!("replyOn_", name, callback);
            ctor_idx += 1;
        }
        sigs
    }

    pub const fn method_sigs<const N: usize>() -> [[u8; 4]; N] {
        let mut sigs = [[0u8; 4]; N];
        let mut sigs_idx = 0;
        let mut svc_idx = 0;
        while svc_idx < <T as ProgramSignature>::SERVICES.len() {
            let (svc_name, _, methods) = <T as ProgramSignature>::SERVICES[svc_idx];
            let mut method_idx = 0;
            while method_idx < methods.len() {
                let (_, name, params, _) = methods[method_idx];
                let selector = const_selector!(svc_name, name, params);
                Self::assert_selector_not_equals_method_routes(selector);
                sigs[sigs_idx] = selector;
                method_idx += 1;
                sigs_idx += 1;
            }
            svc_idx += 1;
        }
        sigs
    }

    pub const fn method_routes<const N: usize>() -> [(&'static [u8], &'static [u8]); N] {
        let mut routes: [(&'static [u8], &'static [u8]); N] = [(&[], &[]); N];
        let mut map_idx = 0;
        let mut svc_idx = 0;
        while svc_idx < <T as ProgramSignature>::SERVICES.len() {
            let (_, svc_route, methods) = <T as ProgramSignature>::SERVICES[svc_idx];
            let mut method_idx = 0;
            while method_idx < methods.len() {
                let (route, ..) = methods[method_idx];
                routes[map_idx] = (svc_route, route);
                method_idx += 1;
                map_idx += 1;
            }
            svc_idx += 1;
        }
        routes
    }

    pub const fn callback_sigs<const N: usize>() -> [[u8; 4]; N] {
        let mut sigs = [[0u8; 4]; N];
        let mut sigs_idx = 0;
        let mut svc_idx = 0;
        while svc_idx < <T as ProgramSignature>::SERVICES.len() {
            let (svc_name, _, methods) = <T as ProgramSignature>::SERVICES[svc_idx];
            let mut method_idx = 0;
            while method_idx < methods.len() {
                let (_, name, _, callback) = methods[method_idx];
                sigs[sigs_idx] = const_selector!("replyOn_", svc_name, name, callback);
                method_idx += 1;
                sigs_idx += 1;
            }
            svc_idx += 1;
        }
        sigs
    }

    const fn assert_selector_not_equals_method_routes(selector: [u8; 4]) {
        let mut svc_idx = 0;
        while svc_idx < <T as ProgramSignature>::SERVICES.len() {
            let (_, svc_route, methods) = <T as ProgramSignature>::SERVICES[svc_idx];
            let mut method_idx = 0;
            while method_idx < methods.len() {
                let (route, ..) = methods[method_idx];
                assert!(!selector_equals(selector, svc_route, route));
                method_idx += 1;
            }
            svc_idx += 1;
        }
    }

    const fn assert_selector_not_equals_ctor_routes(selector: [u8; 4]) {
        let mut ctor_idx = 0;
        while ctor_idx < <T as ProgramSignature>::CTORS.len() {
            let (route, ..) = <T as ProgramSignature>::CTORS[ctor_idx];
            assert!(!selector_equals(selector, route, &[]));
            ctor_idx += 1;
        }
    }
}

/// Compares a 4-byte selector array against the concatenation of two input byte slices.
///
/// This function checks whether the **first 4 bytes** of the concatenated `first` and `second` slices
/// exactly match the `selector`. It does not require `first` or `second` to be of any specific length,
/// but the **combined length must be at least 4** for the function to return `true`.
///
/// Any mismatch in the first 4 bytes results in `false`.
const fn selector_equals(selector: [u8; 4], first: &[u8], second: &[u8]) -> bool {
    let mut i = 0;
    while i < first.len() && i < selector.len() {
        if selector[i] != first[i] {
            return false;
        }
        i += 1;
    }
    let mut j = 0;
    while j < second.len() && i < selector.len() {
        if selector[i] != second[j] {
            return false;
        }
        i += 1;
        j += 1;
    }
    // False if we didn't match full len
    i == selector.len()
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloy_primitives::B256;
    use alloy_sol_types::{SolType, SolValue};

    #[test]
    fn type_names() {
        let s = <() as SolValue>::SolType::SOL_NAME;
        assert_eq!("()", s);

        let s = <(u32,) as SolValue>::SolType::SOL_NAME;
        assert_eq!("(uint32)", s);

        let s = <(u32, String) as SolValue>::SolType::SOL_NAME;
        assert_eq!("(uint32,string)", s);

        let s = <(Vec<u8>, String) as SolValue>::SolType::SOL_NAME;
        assert_eq!("(bytes,string)", s);

        // let s = <(u32, String, ActorId) as SolValue>::SolType::SOL_NAME;
        // assert_eq!("(uint32,string,address)", s);
    }

    struct Prg;
    struct Svc;
    struct ExtendedSvc;

    impl ServiceSignature for Svc {
        const METHODS: &[MethodExpo] = &[
            (
                &[24u8, 68u8, 111u8, 84u8, 104u8, 105u8, 115u8] as &[u8],
                "DoThis",
                <<(u32, String, u128) as SolValue>::SolType as SolType>::SOL_NAME,
                <<(B256, u32) as SolValue>::SolType as SolType>::SOL_NAME,
            ),
            (
                &[16u8, 84u8, 104u8, 105u8, 115u8] as &[u8],
                "This",
                <<(bool, u128) as SolValue>::SolType as SolType>::SOL_NAME,
                <<(B256, u32) as SolValue>::SolType as SolType>::SOL_NAME,
            ),
        ];
    }

    impl ServiceSignature for ExtendedSvc {
        const METHODS: &[MethodExpo] = const_concat_slices!(
            <MethodExpo>,
            &[
                (
                    &[24u8, 68u8, 111u8, 84u8, 104u8, 105u8, 115u8] as &[u8],
                    "DoThis",
                    <<(u32, String, u128,) as SolValue>::SolType as SolType>::SOL_NAME,
                    <<(B256, u32) as SolValue>::SolType as SolType>::SOL_NAME,
                ),
                (
                    &[16u8, 84u8, 104u8, 105u8, 115u8] as &[u8],
                    "This",
                    <<(bool, u128,) as SolValue>::SolType as SolType>::SOL_NAME,
                    <<(B256, u32) as SolValue>::SolType as SolType>::SOL_NAME,
                ),
            ],
            <Svc as ServiceSignature>::METHODS
        );
    }

    impl ProgramSignature for Prg {
        const METHODS_LEN: usize = <Svc as ServiceSignature>::METHODS.len()
            + <ExtendedSvc as ServiceSignature>::METHODS.len();

        const CTORS: &[MethodExpo] = &[(
            &[28u8, 68u8, 101u8, 102u8, 97u8, 117u8, 108u8, 116u8] as &[u8],
            "create",
            <<(u128,) as SolValue>::SolType as SolType>::SOL_NAME,
            <<(B256,) as SolValue>::SolType as SolType>::SOL_NAME,
        )];

        const SERVICES: &[ServiceExpo] = &[
            (
                "svc1",
                &[16u8, 83u8, 118u8, 99u8, 49u8] as &[u8],
                <Svc as ServiceSignature>::METHODS,
            ),
            (
                "svc2",
                &[16u8, 83u8, 118u8, 99u8, 49u8] as &[u8],
                <ExtendedSvc as ServiceSignature>::METHODS,
            ),
        ];
    }

    #[test]
    fn service_signature_extended() {
        assert_eq!(4, ExtendedSvc::METHODS.len());

        let do_this = (
            &[24u8, 68u8, 111u8, 84u8, 104u8, 105u8, 115u8] as &[u8],
            "DoThis",
            "(uint32,string,uint128)",
            "(bytes32,uint32)",
        );
        let this = (
            &[16u8, 84u8, 104u8, 105u8, 115u8] as &[u8],
            "This",
            <<(bool, u128) as SolValue>::SolType as SolType>::SOL_NAME,
            <<(B256, u32) as SolValue>::SolType as SolType>::SOL_NAME,
        );
        assert_eq!(do_this, ExtendedSvc::METHODS[0]);
        assert_eq!(this, ExtendedSvc::METHODS[1]);
        assert_eq!(do_this, ExtendedSvc::METHODS[2]);
        assert_eq!(this, ExtendedSvc::METHODS[3]);
    }

    #[test]
    fn program_signature() {
        const S1: [u8; 4] = [236, 140, 92, 145];
        const S2: [u8; 4] = [27, 178, 77, 160];
        const SIGS: [[u8; 4]; <Prg as solidity::ProgramSignature>::METHODS_LEN] =
            solidity::ConstProgramMeta::<Prg>::method_sigs();
        assert_eq!(6, SIGS.len());

        let sig1 = selector("svc1DoThis(uint32,string,uint128)");
        assert_eq!(S1, sig1.as_slice());
        assert_eq!(S1, SIGS[0]);

        let sig2 = selector("svc1This(bool,uint128)");
        assert_eq!(S2, sig2.as_slice());
        assert_eq!(S2, SIGS[1]);

        assert_eq!(Some(0), SIGS.iter().position(|s| s == &S1));
        assert_eq!(Some(1), SIGS.iter().position(|s| s == &S2));

        let sig3 = selector("svc2DoThis(uint32,string,uint128)");
        assert_eq!(Some(2), SIGS.iter().position(|s| s == sig3.as_slice()));

        let sig4 = selector("svc2This(bool,uint128)");
        assert_eq!(Some(3), SIGS.iter().position(|s| s == sig4.as_slice()));
    }

    #[test]
    fn program_ctor_sigs() {
        const CTOR_SIGS: [[u8; 4]; <Prg as solidity::ProgramSignature>::CTORS.len()] =
            solidity::ConstProgramMeta::<Prg>::ctor_sigs();
        let sig_ctor = selector("create(uint128)");
        assert_eq!(CTOR_SIGS[0], sig_ctor.as_slice());
    }

    #[test]
    fn program_ctor_callback_sigs() {
        const CTOR_CALLBACK_SIGS: [[u8; 4]; <Prg as solidity::ProgramSignature>::CTORS.len()] =
            solidity::ConstProgramMeta::<Prg>::ctor_callback_sigs();
        let sig_ctor = selector("replyOn_create(bytes32)");
        assert_eq!(CTOR_CALLBACK_SIGS[0], sig_ctor.as_slice());
    }

    #[test]
    fn selector_equals_test_exact_match() {
        let sel = *b"ABCD";
        assert!(selector_equals(sel, b"AB", b"CD"));
    }

    #[test]
    fn selector_equals_test_longer_second() {
        let sel = *b"ABCD";
        assert!(selector_equals(sel, b"AB", b"CDEF"));
    }

    #[test]
    fn selector_equals_test_longer_first() {
        let sel = *b"ABCD";
        assert!(selector_equals(sel, b"ABCDX", b""));
    }

    #[test]
    fn selector_equals_test_short_inputs() {
        let sel = *b"ABCD";
        assert!(selector_equals(sel, b"A", b"BCD"));
        assert!(selector_equals(sel, b"", b"ABCD"));
    }

    #[test]
    fn selector_equals_test_mismatch_in_first() {
        let sel = *b"XBCD";
        assert!(!selector_equals(sel, b"AB", b"CD"));
    }

    #[test]
    fn selector_equals_test_mismatch_in_second() {
        let sel = *b"ABXD";
        assert!(!selector_equals(sel, b"AB", b"CD"));
    }

    #[test]
    fn selector_equals_test_mismatch_due_to_short_concat() {
        let sel = *b"ABCD";
        assert!(!selector_equals(sel, b"A", b"B")); // only "AB" < 4 bytes
    }

    #[test]
    fn encode_decode_sol_types() {
        let original = (false, ActorId::zero(), [1u8, 2, 3, 4]);
        let input = original.clone().abi_encode_sequence();

        type ActorType = <<ActorId as SolValue>::SolType as SolType>::RustType;
        type ArrayType = <<[u8; 4] as SolValue>::SolType as SolType>::RustType;
        let decoded: (bool, ActorType, ArrayType) =
            SolValue::abi_decode_sequence(&input).expect("decode failed");

        let result: (bool, ActorId, [u8; 4]) = (decoded.0, decoded.1.into(), decoded.2.into());
        assert_eq!(original, result);
    }
}