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
// LNP/BP Rust Library
// Written in 2020 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use num_traits::{FromPrimitive, ToPrimitive};
use std::collections::BTreeMap;
use std::io;

/// For now, Simplicity script is not implemented, so we use a byte array as a
/// placeholder for script data
pub type SimplicityScript = Vec<u8>;

/// Marker trait for all node-specific action types
pub trait NodeAction: ToPrimitive + FromPrimitive + Ord
where
    Self: Sized,
{
}

#[non_exhaustive]
#[derive(
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Debug,
    Display,
    ToPrimitive,
    FromPrimitive,
)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", rename_all = "snake_case")
)]
#[display(Debug)]
pub enum GenesisAction {
    Validate = 0,
}

impl NodeAction for GenesisAction {}

impl Default for GenesisAction {
    fn default() -> Self {
        GenesisAction::Validate
    }
}

#[non_exhaustive]
#[derive(
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Debug,
    Display,
    ToPrimitive,
    FromPrimitive,
)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", rename_all = "snake_case")
)]
#[display(Debug)]
pub enum ExtensionAction {
    Validate = 0,
}

impl NodeAction for ExtensionAction {}

impl Default for ExtensionAction {
    fn default() -> Self {
        ExtensionAction::Validate
    }
}

#[non_exhaustive]
#[derive(
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Debug,
    Display,
    ToPrimitive,
    FromPrimitive,
)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", rename_all = "snake_case")
)]
#[display(Debug)]
#[repr(u16)]
pub enum TransitionAction {
    Validate = 0,
    GenerateBlank = 1,
}
impl NodeAction for TransitionAction {}

impl Default for TransitionAction {
    fn default() -> Self {
        TransitionAction::Validate
    }
}

#[non_exhaustive]
#[derive(
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Debug,
    Display,
    ToPrimitive,
    FromPrimitive,
)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", rename_all = "snake_case")
)]
#[display(Debug)]
#[repr(u16)]
pub enum AssignmentAction {
    Validate = 0,
}

impl Default for AssignmentAction {
    fn default() -> Self {
        AssignmentAction::Validate
    }
}

pub type GenesisAbi = BTreeMap<GenesisAction, Procedure>;
pub type ExtensionAbi = BTreeMap<ExtensionAction, Procedure>;
pub type TransitionAbi = BTreeMap<TransitionAction, Procedure>;
pub type AssignmentAbi = BTreeMap<AssignmentAction, Procedure>;

#[non_exhaustive]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Display)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", rename_all = "snake_case")
)]
#[display(Debug)]
pub enum Procedure {
    Embedded(StandardProcedure),
    Simplicity { abi_table_index: u32 },
}

#[non_exhaustive]
#[derive(
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Debug,
    Display,
    ToPrimitive,
    FromPrimitive,
)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", rename_all = "snake_case")
)]
#[display(Debug)]
#[repr(u8)]
pub enum StandardProcedure {
    NoInflationBySum = 0x01,
    FungibleInflation = 0x02,
    NonfungibleInflation = 0x03,
    IdentityTransfer = 0x04,
    ProofOfBurn = 0x10,
    ProofOfReserve = 0x11,
    RightsSplit = 0x20,
}

mod strict_encoding {
    use super::*;
    use lnpbp::strict_encoding::{Error, StrictDecode, StrictEncode};

    impl_enum_strict_encoding!(GenesisAction);
    impl_enum_strict_encoding!(TransitionAction);
    impl_enum_strict_encoding!(AssignmentAction);
    impl_enum_strict_encoding!(ExtensionAction);

    impl_enum_strict_encoding!(StandardProcedure);

    impl StrictEncode for Procedure {
        fn strict_encode<E: io::Write>(
            &self,
            mut e: E,
        ) -> Result<usize, Error> {
            Ok(match self {
                Self::Simplicity {
                    abi_table_index: offset,
                } => strict_encode_list!(e; 0u8, offset),
                Self::Embedded(proc_id) => {
                    strict_encode_list!(e; 0xFFu8, proc_id)
                }
            })
        }
    }

    impl StrictDecode for Procedure {
        fn strict_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
            Ok(match u8::strict_decode(&mut d)? {
                0u8 => Self::Simplicity {
                    abi_table_index: u32::strict_decode(&mut d)?,
                },
                0xFFu8 => {
                    Self::Embedded(StandardProcedure::strict_decode(&mut d)?)
                }
                x => Err(Error::EnumValueNotKnown(
                    "script::Procedure".to_string(),
                    x,
                ))?,
            })
        }
    }

    #[cfg(test)]
    mod test {
        use super::*;
        use lnpbp::strict_encoding::strict_serialize;

        #[test]
        fn test_basics() {
            // Test Actions and Standard procedures
            test_enum_u8_exhaustive!(AssignmentAction; AssignmentAction::Validate => 0);
            test_enum_u8_exhaustive!(TransitionAction;
                TransitionAction::Validate => 0,
                TransitionAction::GenerateBlank => 1
            );
            test_enum_u8_exhaustive!(StandardProcedure;
                StandardProcedure::NoInflationBySum => 0x01,
                StandardProcedure::FungibleInflation => 0x02,
                StandardProcedure::NonfungibleInflation => 0x03,
                StandardProcedure::IdentityTransfer => 0x04,
                StandardProcedure::ProofOfBurn => 0x10,
                StandardProcedure::ProofOfReserve => 0x11,
                StandardProcedure::RightsSplit => 0x20
            );

            // Test Procedures
            assert_eq!(
                vec![0xFF, 0x01],
                strict_serialize(&Procedure::Embedded(
                    StandardProcedure::NoInflationBySum
                ))
                .unwrap()
            );
            assert_eq!(
                vec![0xFF, 0x2],
                strict_serialize(&Procedure::Embedded(
                    StandardProcedure::FungibleInflation
                ))
                .unwrap()
            );
            assert_eq!(
                vec![0xFF, 0x10],
                strict_serialize(&Procedure::Embedded(
                    StandardProcedure::ProofOfBurn
                ))
                .unwrap()
            );
            assert_eq!(
                vec![0x00, 0x58, 0x00, 0x00, 0x00],
                strict_serialize(&Procedure::Simplicity {
                    abi_table_index: 88
                })
                .unwrap()
            );

            // Test Transition and Assignment ABI
            let mut trans_abi = TransitionAbi::new();
            trans_abi.insert(
                TransitionAction::Validate,
                Procedure::Embedded(StandardProcedure::NoInflationBySum),
            );
            assert_eq!(
                vec![0x01, 0x00, 0x00, 0xff, 0x01],
                strict_serialize(&trans_abi).unwrap()
            );

            let mut assignment_abi = AssignmentAbi::new();
            assignment_abi.insert(
                AssignmentAction::Validate,
                Procedure::Simplicity {
                    abi_table_index: 45,
                },
            );
            assert_eq!(
                vec![0x01, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x00, 0x00],
                strict_serialize(&assignment_abi).unwrap()
            );
        }
    }
}