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 crate::blueprints::package::BlueprintType;
use crate::internal_prelude::*;

pub const PACKAGE_CODE_ID: u64 = 0u64;
pub const RESOURCE_CODE_ID: u64 = 1u64;
pub const IDENTITY_CODE_ID: u64 = 2u64;
pub const CONSENSUS_MANAGER_CODE_ID: u64 = 3u64;
pub const ACCOUNT_CODE_ID: u64 = 5u64;
pub const ACCESS_CONTROLLER_CODE_ID: u64 = 6u64;
pub const TRANSACTION_PROCESSOR_CODE_ID: u64 = 7u64;
pub const METADATA_CODE_ID: u64 = 10u64;
pub const ROYALTY_CODE_ID: u64 = 11u64;
pub const ROLE_ASSIGNMENT_CODE_ID: u64 = 12u64;
pub const POOL_V1_0_CODE_ID: u64 = 13u64;
pub const TRANSACTION_TRACKER_CODE_ID: u64 = 14u64;
pub const TEST_UTILS_CODE_ID: u64 = 15u64;
pub const CONSENSUS_MANAGER_SECONDS_PRECISION_CODE_ID: u64 = 16u64;
pub const POOL_V1_1_CODE_ID: u64 = 17u64;

pub const PACKAGE_FIELDS_PARTITION_OFFSET: PartitionOffset = PartitionOffset(0u8);
pub const PACKAGE_BLUEPRINTS_PARTITION_OFFSET: PartitionOffset = PartitionOffset(1u8);
pub const PACKAGE_BLUEPRINT_DEPENDENCIES_PARTITION_OFFSET: PartitionOffset = PartitionOffset(2u8);
// There is no partition offset for the package schema collection as it is directly mapped to SCHEMAS_PARTITION
pub const PACKAGE_ROYALTY_PARTITION_OFFSET: PartitionOffset = PartitionOffset(3u8);
pub const PACKAGE_AUTH_TEMPLATE_PARTITION_OFFSET: PartitionOffset = PartitionOffset(4u8);
pub const PACKAGE_VM_TYPE_PARTITION_OFFSET: PartitionOffset = PartitionOffset(5u8);
pub const PACKAGE_ORIGINAL_CODE_PARTITION_OFFSET: PartitionOffset = PartitionOffset(6u8);
pub const PACKAGE_INSTRUMENTED_CODE_PARTITION_OFFSET: PartitionOffset = PartitionOffset(7u8);

define_wrapped_hash!(
    /// Represents a particular instance of code under a package
    CodeHash
);

#[derive(Copy, Debug, Clone, PartialEq, Eq, Sbor)]
pub enum VmType {
    Native,
    ScryptoV1,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Sbor)]
pub enum BlueprintPayloadDef {
    Static(ScopedTypeId), // Fully Resolved type is defined in package
    Generic(u8),          // Fully Resolved type is mapped directly to a generic defined by instance
                          // TODO: How to represent a structure containing a generic?
}

impl BlueprintPayloadDef {
    pub fn from_type_ref(type_ref: TypeRef<LocalTypeId>, schema_hash: SchemaHash) -> Self {
        match type_ref {
            TypeRef::Static(type_id) => {
                BlueprintPayloadDef::Static(ScopedTypeId(schema_hash, type_id))
            }
            TypeRef::Generic(index) => BlueprintPayloadDef::Generic(index),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Sbor)]
pub struct FunctionSchema {
    pub receiver: Option<ReceiverInfo>,
    pub input: BlueprintPayloadDef,
    pub output: BlueprintPayloadDef,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ScryptoSbor, Ord, PartialOrd, Hash)]
pub struct BlueprintVersion {
    pub major: u32,
    pub minor: u32,
    pub patch: u32,
}

impl Default for BlueprintVersion {
    fn default() -> Self {
        Self {
            major: 1,
            minor: 0,
            patch: 0,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor, Ord, PartialOrd, Hash)]
pub struct CanonicalBlueprintId {
    pub address: PackageAddress,
    pub blueprint: String,
    pub version: BlueprintVersion,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor, Ord, PartialOrd, Hash)]
pub struct BlueprintVersionKey {
    pub blueprint: String,
    pub version: BlueprintVersion,
}

impl BlueprintVersionKey {
    pub fn new_default<S: ToString>(blueprint: S) -> Self {
        Self {
            blueprint: blueprint.to_string(),
            version: BlueprintVersion::default(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
#[sbor(transparent)]
pub struct BlueprintDependencies {
    pub dependencies: IndexSet<GlobalAddress>,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
pub struct PackageExport {
    pub code_hash: CodeHash,
    pub export_name: String,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
pub struct BlueprintDefinition {
    // Frontend interface, this must be backward compatible with minor version updates
    pub interface: BlueprintInterface,

    // Backend implementation pointers

    // There is an implicit invariant that must be maintained in that the key set in `function_exports`
    // matches that of the `functions` under `interface`. This is currently maintained since the
    // `publish` interface uses `BlueprintDefinitionInit` rather than `BlueprintDefinition`.
    pub function_exports: IndexMap<String, PackageExport>,
    pub hook_exports: IndexMap<BlueprintHook, PackageExport>,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
pub enum KeyOrValue {
    Key,
    Value,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
pub enum InputOrOutput {
    Input,
    Output,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
pub enum BlueprintPayloadIdentifier {
    Function(String, InputOrOutput),
    Event(String),
    Field(u8),
    KeyValueEntry(u8, KeyOrValue),
    IndexEntry(u8, KeyOrValue),
    SortedIndexEntry(u8, KeyOrValue),
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
pub enum BlueprintPartitionType {
    KeyValueCollection,
    IndexCollection,
    SortedIndexCollection,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor, ManifestSbor)]
pub struct BlueprintInterface {
    pub blueprint_type: BlueprintType,
    pub is_transient: bool,
    pub generics: Vec<GenericBound>,
    pub feature_set: IndexSet<String>,
    pub state: IndexedStateSchema,
    pub functions: IndexMap<String, FunctionSchema>,
    pub events: IndexMap<String, BlueprintPayloadDef>,
    pub types: IndexMap<String, ScopedTypeId>,
}

impl BlueprintInterface {
    pub fn get_field_payload_def(&self, field_index: u8) -> Option<BlueprintPayloadDef> {
        self.state.get_field_payload_def(field_index)
    }

    pub fn get_kv_key_payload_def(&self, collection_index: u8) -> Option<BlueprintPayloadDef> {
        self.state.get_kv_key_payload_def(collection_index)
    }

    pub fn find_function(&self, ident: &str) -> Option<FunctionSchema> {
        if let Some(x) = self.functions.get(ident) {
            if x.receiver.is_none() {
                return Some(x.clone());
            }
        }
        None
    }

    pub fn find_method(&self, ident: &str) -> Option<FunctionSchema> {
        if let Some(x) = self.functions.get(ident) {
            if x.receiver.is_some() {
                return Some(x.clone());
            }
        }
        None
    }

    pub fn get_function_input_payload_def(&self, ident: &str) -> Option<BlueprintPayloadDef> {
        let schema = self.functions.get(ident)?;
        Some(schema.input.clone())
    }

    pub fn get_function_output_payload_def(&self, ident: &str) -> Option<BlueprintPayloadDef> {
        let schema = self.functions.get(ident)?;
        Some(schema.output.clone())
    }

    pub fn get_event_payload_def(&self, event_name: &str) -> Option<BlueprintPayloadDef> {
        self.events.get(event_name).cloned()
    }

    pub fn get_payload_def(
        &self,
        payload_identifier: &BlueprintPayloadIdentifier,
    ) -> Option<(BlueprintPayloadDef, bool, bool)> {
        match payload_identifier {
            BlueprintPayloadIdentifier::Function(function_ident, InputOrOutput::Input) => {
                let payload_def = self.get_function_input_payload_def(function_ident.as_str())?;
                Some((payload_def, true, true))
            }
            BlueprintPayloadIdentifier::Function(function_ident, InputOrOutput::Output) => {
                let payload_def = self.get_function_output_payload_def(function_ident.as_str())?;
                Some((payload_def, true, true))
            }
            BlueprintPayloadIdentifier::Field(field_index) => {
                let payload_def = self.get_field_payload_def(*field_index)?;
                Some((payload_def, true, self.is_transient))
            }
            BlueprintPayloadIdentifier::KeyValueEntry(collection_index, KeyOrValue::Key) => {
                let payload_def = self.get_kv_key_payload_def(*collection_index)?;
                Some((payload_def, false, self.is_transient))
            }
            BlueprintPayloadIdentifier::KeyValueEntry(collection_index, KeyOrValue::Value) => {
                let (payload_def, allow_ownership) =
                    self.state.get_kv_value_payload_def(*collection_index)?;
                Some((payload_def, allow_ownership, self.is_transient))
            }
            BlueprintPayloadIdentifier::IndexEntry(collection_index, KeyOrValue::Key) => {
                let type_pointer = self.state.get_index_payload_def_key(*collection_index)?;
                Some((type_pointer, false, self.is_transient))
            }
            BlueprintPayloadIdentifier::IndexEntry(collection_index, KeyOrValue::Value) => {
                let type_pointer = self.state.get_index_payload_def_value(*collection_index)?;
                Some((type_pointer, false, self.is_transient))
            }
            BlueprintPayloadIdentifier::SortedIndexEntry(collection_index, KeyOrValue::Key) => {
                let type_pointer = self
                    .state
                    .get_sorted_index_payload_def_key(*collection_index)?;
                Some((type_pointer, false, self.is_transient))
            }
            BlueprintPayloadIdentifier::SortedIndexEntry(collection_index, KeyOrValue::Value) => {
                let type_pointer = self
                    .state
                    .get_sorted_index_payload_def_value(*collection_index)?;
                Some((type_pointer, false, self.is_transient))
            }
            BlueprintPayloadIdentifier::Event(event_name) => {
                let type_pointer = self.get_event_payload_def(event_name.as_str())?;
                Some((type_pointer, false, false))
            }
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SystemInstruction {
    MapCollectionToPhysicalPartition {
        collection_index: u8,
        partition_num: PartitionNumber,
    },
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, ScryptoSbor, ManifestSbor, PartialOrd, Ord, Hash)]
pub enum PartitionDescription {
    Logical(PartitionOffset),
    Physical(PartitionNumber),
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor, ManifestSbor)]
pub struct IndexedStateSchema {
    pub fields: Option<(PartitionDescription, Vec<FieldSchema<BlueprintPayloadDef>>)>,
    pub collections: Vec<(
        PartitionDescription,
        BlueprintCollectionSchema<BlueprintPayloadDef>,
    )>,
    pub num_logical_partitions: u8,
}

impl IndexedStateSchema {
    pub fn from_schema(
        schema_hash: SchemaHash,
        schema: BlueprintStateSchemaInit,
        system_mappings: IndexMap<usize, PartitionNumber>,
    ) -> Self {
        let mut partition_offset = 0u8;

        let mut fields = None;
        if !schema.fields.is_empty() {
            let schema_fields = schema
                .fields
                .into_iter()
                .map(|field_schema| FieldSchema {
                    field: BlueprintPayloadDef::from_type_ref(field_schema.field, schema_hash),
                    condition: field_schema.condition,
                    transience: field_schema.transience,
                })
                .collect();
            fields = Some((
                PartitionDescription::Logical(PartitionOffset(partition_offset)),
                schema_fields,
            ));
            partition_offset += 1;
        };

        let mut collections = Vec::new();
        for (collection_index, collection_schema) in schema.collections.into_iter().enumerate() {
            let schema = collection_schema
                .map(|type_ref| BlueprintPayloadDef::from_type_ref(type_ref, schema_hash));

            if let Some(partition_num) = system_mappings.get(&collection_index) {
                collections.push((PartitionDescription::Physical(*partition_num), schema));
            } else {
                collections.push((
                    PartitionDescription::Logical(PartitionOffset(partition_offset)),
                    schema,
                ));
                partition_offset += 1;
            }
        }

        Self {
            fields,
            collections,
            num_logical_partitions: partition_offset,
        }
    }

    pub fn num_logical_partitions(&self) -> u8 {
        self.num_logical_partitions
    }

    pub fn num_fields(&self) -> usize {
        match &self.fields {
            Some((_, indices)) => indices.len(),
            _ => 0usize,
        }
    }

    pub fn get_partition(
        &self,
        collection_index: u8,
    ) -> Option<(PartitionDescription, BlueprintPartitionType)> {
        self.collections
            .get(collection_index as usize)
            .map(|(partition, schema)| {
                let partition_type = match schema {
                    BlueprintCollectionSchema::KeyValueStore(..) => {
                        BlueprintPartitionType::KeyValueCollection
                    }
                    BlueprintCollectionSchema::Index(..) => BlueprintPartitionType::IndexCollection,
                    BlueprintCollectionSchema::SortedIndex(..) => {
                        BlueprintPartitionType::SortedIndexCollection
                    }
                };
                (*partition, partition_type)
            })
    }

    pub fn get_field_payload_def(&self, field_index: u8) -> Option<BlueprintPayloadDef> {
        let (_partition, fields) = self.fields.clone()?;
        let field_schema = fields.get(field_index.clone() as usize)?;
        Some(field_schema.field.clone())
    }

    pub fn get_kv_key_payload_def(&self, collection_index: u8) -> Option<BlueprintPayloadDef> {
        let (_partition, schema) = self.collections.get(collection_index.clone() as usize)?;
        match schema {
            BlueprintCollectionSchema::KeyValueStore(key_value_store) => {
                Some(key_value_store.key.clone())
            }
            _ => None,
        }
    }

    pub fn get_kv_value_payload_def(
        &self,
        collection_index: u8,
    ) -> Option<(BlueprintPayloadDef, bool)> {
        let (_partition, schema) = self.collections.get(collection_index.clone() as usize)?;
        match schema {
            BlueprintCollectionSchema::KeyValueStore(key_value_store) => Some((
                key_value_store.value.clone(),
                key_value_store.allow_ownership,
            )),
            _ => None,
        }
    }

    pub fn get_index_payload_def_key(&self, collection_index: u8) -> Option<BlueprintPayloadDef> {
        let (_partition, schema) = self.collections.get(collection_index.clone() as usize)?;
        match schema {
            BlueprintCollectionSchema::Index(index) => Some(index.key.clone()),
            _ => None,
        }
    }

    pub fn get_index_payload_def_value(&self, collection_index: u8) -> Option<BlueprintPayloadDef> {
        let (_partition, schema) = self.collections.get(collection_index.clone() as usize)?;
        match schema {
            BlueprintCollectionSchema::Index(index) => Some(index.value.clone()),
            _ => None,
        }
    }

    pub fn get_sorted_index_payload_def_key(
        &self,
        collection_index: u8,
    ) -> Option<BlueprintPayloadDef> {
        let (_partition, schema) = self.collections.get(collection_index.clone() as usize)?;
        match schema {
            BlueprintCollectionSchema::SortedIndex(index) => Some(index.key.clone()),
            _ => None,
        }
    }

    pub fn get_sorted_index_payload_def_value(
        &self,
        collection_index: u8,
    ) -> Option<BlueprintPayloadDef> {
        let (_partition, schema) = self.collections.get(collection_index.clone() as usize)?;
        match schema {
            BlueprintCollectionSchema::SortedIndex(index) => Some(index.value.clone()),
            _ => None,
        }
    }

    pub fn fields_partition(&self) -> Option<PartitionDescription> {
        match &self.fields {
            Some((partition, ..)) => Some(partition.clone()),
            _ => None,
        }
    }

    pub fn field(
        &self,
        field_index: u8,
    ) -> Option<(PartitionDescription, FieldSchema<BlueprintPayloadDef>)> {
        match &self.fields {
            Some((partition, fields)) => {
                let field_index: usize = field_index.into();
                fields
                    .get(field_index)
                    .cloned()
                    .map(|f| (partition.clone(), f))
            }
            _ => None,
        }
    }
}