kythera-common 0.1.0

Common utilities for the Filecoin ecosystem
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
// Copyright 2023 Polyphene.
// SPDX-License-Identifier: Apache-2.0, MIT

use std::borrow::Borrow;
use std::fmt;

use anyhow::Result;
use frc42_dispatch::hash::MethodResolver;
use serde::de::SeqAccess;

use crate::abi::blake2b::Blake2bHasher;
use crate::error::{self, Error};

mod blake2b;

/// Split a PascalCase string into a vector of its components.
/// If the string is not PascalCase function returns an empty [`Vec`].
pub fn pascal_case_split(s: &str) -> Vec<&str> {
    let mut split = vec![];
    // Work with indices to avoid allocations.
    let mut chars = s.char_indices();

    // Check if first character is capitalized.
    let mut beg = match chars.next() {
        Some((i, c)) if c.is_uppercase() => i,
        _ => return split,
    };

    // Iterate the rest of the characters.
    for (i, c) in chars {
        if c.is_uppercase() || c.is_numeric() {
            split.push(&s[beg..i]);
            beg = i;
        }
    }

    // Push the last word, this word is not covered by the iterator
    // as it doesn't know when it's last element.
    split.push(&s[beg..s.len()]);
    split
}

/// `Abi` is the structure we use internally to deal with Actor Binary Interface. It contains all
/// exposed [`Method`] from a given Actor.
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Abi {
    pub constructor: Option<Method>,
    pub set_up: Option<Method>,
    pub methods: Vec<Method>,
}

impl Abi {
    /// Get the `Constructor` method on the Abi if it exists.
    pub fn constructor(&self) -> Option<&Method> {
        self.constructor.as_ref()
    }

    /// Get the `Setup` method on the Abi if it exists.
    pub fn set_up(&self) -> Option<&Method> {
        self.set_up.as_ref()
    }

    pub fn methods(&self) -> &[Method] {
        &self.methods
    }
}

/// Custom implementation of [`Serialize`] so that we join `Constructor` and `Setup`
/// into the rest of the methods.
impl serde::Serialize for Abi {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut s = vec![];
        if let Some(constructor) = &self.constructor {
            s.push(vec![constructor.name()]);
        }
        if let Some(constructor) = &self.set_up {
            s.push(vec![constructor.name()]);
        }
        let methods = self.methods.iter().map(|m| vec![m.name()]);
        s.extend(methods);

        serde::Serialize::serialize(&vec![s], serializer)
    }
}

/// Custom implementation of [`Deserialize`] so that we check for `Constructor` and `Setup`
/// existence and assert there's only one of each.
impl<'de> serde::Deserialize<'de> for Abi {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct AbiVisitor;

        impl<'de> serde::de::Visitor<'de> for AbiVisitor {
            type Value = Abi;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("Abi")
            }

            fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                let mut constructor = None;
                let mut set_up = None;

                let mut methods = vec![];
                let seq_methods = seq
                    .next_element::<Vec<Method>>()?
                    .ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;

                // TODO: Can't we parse each method sequentially instead? and not have to
                // iterate again all over here?
                for method in seq_methods {
                    match (constructor.is_some(), set_up.is_some(), &method.r#type()) {
                        (false, _, MethodType::Constructor) => constructor = Some(method),
                        (_, false, MethodType::Setup) => set_up = Some(method),
                        (true, _, MethodType::Constructor) | (_, true, MethodType::Setup) => {
                            return Err(serde::de::Error::custom(
                                "Abi can only have one Constructor and one SetUp function",
                            ))
                        }
                        (_, _, _) => methods.push(method),
                    }
                }

                Ok(Abi {
                    constructor,
                    set_up,
                    methods,
                })
            }
        }
        deserializer.deserialize_seq(AbiVisitor)
    }
}

/// Method number indicator for calling Actor methods.
pub type MethodNum = u64;

/// [`Method`] describes an exposed method from an actor entrypoint.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Method {
    number: MethodNum,
    name: String,
    r#type: MethodType,
}

impl fmt::Display for Method {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name())
    }
}

impl Borrow<u64> for Method {
    fn borrow(&self) -> &u64 {
        &self.number
    }
}

/// Type of the [`Abi`] [`Method`] of the test Actor.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum MethodType {
    Constructor,
    Entrypoint,
    Setup,
    Test,
    TestFail,
}

impl Method {
    /// Get the [`Method`] number.
    pub fn number(&self) -> MethodNum {
        self.number
    }

    /// Get the [`Method`] number.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the [`Method`] type.
    pub fn r#type(&self) -> MethodType {
        self.r#type
    }
}

impl Method {
    pub fn new_from_name(name: &str) -> Result<Self, Error> {
        let number = derive_method_num(name)?;
        let name = name.to_string();

        let split = pascal_case_split(&name);
        let r#type = match &split[..] {
            ["Constructor", ..] => MethodType::Constructor,
            ["Setup", ..] => MethodType::Setup,
            ["Test", "Fail", ..] => MethodType::TestFail,
            ["Test", ..] => MethodType::Test,
            _ => MethodType::Entrypoint,
        };

        Ok(Method {
            number,
            name,
            r#type,
        })
    }
}

/// Implement custom deserialization method for [`Method`] as we expect the bytes to be deserialized to only contain
/// the `name` and not the `number` property that is generated at deserialization time.
impl<'de> serde::de::Deserialize<'de> for Method {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        struct MethodVisitor;

        impl<'de> serde::de::Visitor<'de> for MethodVisitor {
            type Value = Method;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("Method")
            }

            fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                let name = seq
                    .next_element::<String>()?
                    .ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;

                Self::Value::new_from_name(&name).map_err(|_| {
                    serde::de::Error::custom(format!("Couldn't deserialize method: {}", &name))
                })
            }
        }

        deserializer.deserialize_seq(MethodVisitor)
    }
}

/// `derive_method_num` will return the method number for a given method name based on the FRC-042:
/// https://github.com/filecoin-project/FIPs/blob/master/FRCs/frc-0042.md .
pub fn derive_method_num(name: &str) -> Result<MethodNum, error::Error> {
    let resolver = MethodResolver::new(Blake2bHasher {});

    match resolver.method_number(name) {
        Ok(method_number) => Ok(method_number),
        Err(err) => Err(Error::MethodNumberGeneration {
            name: name.into(),
            source: err.into(),
        }),
    }
}

#[cfg(test)]
mod test {
    use super::{derive_method_num, pascal_case_split};
    use crate::abi::{Abi, Method, MethodType};

    #[test]
    fn test_method_derivation() {
        let method_name = String::from("TestTransfer");

        match derive_method_num(&method_name) {
            Ok(method_num) => {
                assert_eq!(method_num, 3760293944);
            }
            Err(_) => {
                panic!("derive_method_num failed for {}", method_name);
            }
        }
    }

    #[test]
    fn test_fail_method_derivation() {
        // Using function with lower case as first character in method name to fail the test.
        let method_name = String::from("test_transfer");

        match derive_method_num(&method_name) {
            Ok(_) => {
                panic!("derive_method_num success for {}", method_name);
            }
            Err(err) => {
                assert_eq!(
                    format!("Could not generate method number for `{}`", method_name),
                    err.to_string()
                )
            }
        }
    }

    #[test]
    fn test_pascal_case() {
        assert_eq!(pascal_case_split("TestOne"), vec!["Test", "One"]);
        assert_eq!(
            pascal_case_split("TestFailWithMultipleWords"),
            vec!["Test", "Fail", "With", "Multiple", "Words"]
        );
        assert_eq!(pascal_case_split("Test1"), vec!["Test", "1"]);
        assert_eq!(pascal_case_split("testOne"), Vec::<&str>::new());
    }

    #[test]
    fn test_tuple_serde() {
        // Test assets.
        let test_transfer_name = String::from("TestTransfer");
        let test_transfer_fail_name = String::from("TestFailTransfer");

        let abi = Abi {
            constructor: None,
            set_up: None,
            methods: vec![
                Method {
                    number: derive_method_num(&test_transfer_name).unwrap(),
                    name: test_transfer_name,
                    r#type: MethodType::Test,
                },
                Method {
                    number: derive_method_num(&test_transfer_fail_name).unwrap(),
                    name: test_transfer_fail_name,
                    r#type: MethodType::TestFail,
                },
            ],
        };

        let serialized_abi: Vec<u8> = vec![
            129, 130, 129, 108, 84, 101, 115, 116, 84, 114, 97, 110, 115, 102, 101, 114, 129, 112,
            84, 101, 115, 116, 70, 97, 105, 108, 84, 114, 97, 110, 115, 102, 101, 114,
        ];

        // Serialize.
        let abi_vec = crate::to_vec(&abi).unwrap();
        assert_eq!(abi_vec, serialized_abi);

        // Deserialize.
        let deserialized_abi: Abi = crate::from_slice(&serialized_abi).unwrap();
        assert_eq!(deserialized_abi, abi);
    }

    #[test]
    fn test_fail_tuple_serde() {
        // Test assets.
        let test_transfer_name = String::from("TestTransfer");
        let test_transfer_fail_name = String::from("testFailTransfer");

        let abi = Abi {
            constructor: None,
            set_up: None,
            methods: vec![
                Method {
                    number: derive_method_num(&test_transfer_name).unwrap(),
                    name: test_transfer_name,
                    r#type: MethodType::Test,
                },
                Method {
                    number: 3280706483,
                    name: test_transfer_fail_name,
                    r#type: MethodType::TestFail,
                },
            ],
        };

        let serialized_abi: Vec<u8> = vec![
            129, 130, 129, 108, 84, 101, 115, 116, 84, 114, 97, 110, 115, 102, 101, 114, 129, 112,
            116, 101, 115, 116, 70, 97, 105, 108, 84, 114, 97, 110, 115, 102, 101, 114,
        ];

        // Serialize.
        let abi_vec = crate::to_vec(&abi).unwrap();
        assert_eq!(abi_vec, serialized_abi);

        // Deserialize.
        match crate::from_slice::<Abi>(&serialized_abi) {
            Ok(_) => panic!("Deserialization should fail"),
            Err(err) => {
                assert!(err
                    .to_string()
                    .contains("Couldn't deserialize method: testFailTransfer"));
            }
        };
    }

    #[test]
    fn test_method_constructor() {
        assert_eq!(
            Method::new_from_name("TestOne").unwrap().r#type,
            MethodType::Test
        );
        assert_eq!(
            Method::new_from_name("TestFailOne").unwrap().r#type,
            MethodType::TestFail
        );
        assert_eq!(
            Method::new_from_name("Constructor").unwrap().r#type,
            MethodType::Constructor
        );
        assert_eq!(
            Method::new_from_name("Setup").unwrap().r#type,
            MethodType::Setup
        );

        assert!(Method::new_from_name("testOne").is_err());
        assert!(Method::new_from_name("").is_err());
    }
}