Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion designdocs/schemas_protocols_intents.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 📘 Design Discussion: Schema, Protocols, and Intent-Based Delegation in Frequency
# 📘 Schema, Protocols, and Intent-Based Delegation in Frequency

## 1. **Background and Motivation** <a id="section_1"></a>

Expand Down
16 changes: 8 additions & 8 deletions pallets/schemas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ details.

### Runtime API

| Name | Description | Call | API Version Added | Runtime Added |
|-------------------------------------------------------|----------------------------------------------------------------|-------------------------------|-------------------|---------------|
| Get Schema by Id _(deprecated)_ | Retrieves the schema for the given Schema Id | `getBySchemaId` | 1 | 1 |
| Get Schema by Id (version 2) | Retrieves the schema for the given SchemaId | `getSchemaById` | 3 | ? |
| Get Schema Versions by Name (_deprecated_) | Retrieves the ordered list of Schema Ids for the given name(s) | `getSchemaVersionsByName` | 2 | 66 |
| Get registered entities (Intent, IntentGroup) by Name | Retrieves the entities belonging to the given name(s) | `getRegisteredEntitiesByName` | 3 | ? |
| Get Intent by Id | Retrieves the Intent for the given IntentId | `getIntentById` | 3 | ? |
| Get IntentGroup by Id | Retrieves the IntentGroup for the given IntentGroupId | `getIntentGroupById` | 3 | ? |
| Name | Description | Call | API Version Added | Runtime Added |
|-------------------------------------------------------|---------------------------------------------------------------------------------------------------------|-------------------------------|-------------------|---------------|
| Get Schema by Id _(deprecated)_ | Retrieves the schema for the given Schema Id | `getBySchemaId` | 1 | 1 |
| Get Schema by Id (version 2) | Retrieves the schema for the given SchemaId | `getSchemaById` | 3 | ? |
| Get Schema Versions by Name (_deprecated_) | Retrieves the ordered list of Schema Ids for the given name(s) | `getSchemaVersionsByName` | 2 | 66 |
| Get registered entities (Intent, IntentGroup) by Name | Retrieves the entities belonging to the given name(s) | `getRegisteredEntitiesByName` | 3 | ? |
| Get Intent by Id | Retrieves the Intent for the given IntentId, optionally with the list of supported implementing Schemas | `getIntentById` | 3 | ? |
| Get IntentGroup by Id | Retrieves the IntentGroup for the given IntentGroupId | `getIntentGroupById` | 3 | ? |

See [Rust Docs](https://frequency-chain.github.io/frequency/pallet_schemas_runtime_api/trait.SchemasRuntimeApi.html) for
more details.
Expand Down
16 changes: 9 additions & 7 deletions pallets/schemas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,17 +1024,19 @@ pub mod pallet {
})
}

/// Retrieve an Intent by its ID
/// Retrieve an Intent by its ID, with a list of implementing SchemaIDs in sorted order
/// NOTE: This must not be called on-chain due to inability to determine weight
pub fn get_intent_by_id_with_schemas(intent_id: IntentId) -> Option<IntentResponse> {
let response = Self::get_intent_by_id(intent_id);
response.map(|mut intent| {
intent.schema_ids = Some(
SchemaInfos::<T>::iter()
.filter(|(_, info)| info.intent_id == intent_id)
.map(|(id, _)| id)
.collect::<Vec<SchemaId>>(),
);
let mut schema_ids = SchemaInfos::<T>::iter()
.filter(|(_, info)| {
info.intent_id == intent_id && info.status != SchemaStatus::Unsupported
})
.map(|(id, _)| id)
.collect::<Vec<SchemaId>>();
schema_ids.sort();
intent.schema_ids = Some(schema_ids);
intent
})
}
Expand Down
88 changes: 87 additions & 1 deletion pallets/schemas/src/tests/other_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::mock::*;
use crate::{
CurrentSchemaIdentifierMaximum, Error, Event as AnnouncementEvent,
GovernanceSchemaModelMaxBytes, SchemaDescriptor, SchemaName, SchemaNamePayload,
GovernanceSchemaModelMaxBytes, SchemaDescriptor, SchemaInfo, SchemaName, SchemaNamePayload,
SchemaProtocolName, SchemaVersionId, MAX_NUMBER_OF_VERSIONS,
};
use common_primitives::{
Expand Down Expand Up @@ -961,3 +961,89 @@ fn genesis_config_build_genesis_schemas() {
assert!(res.is_some());
});
}

#[test]
fn get_intent_with_schemas_should_return_sorted_schemas() {
new_test_ext().execute_with(|| {
sudo_set_max_schema_size();
SchemasPallet::set_schema_count(16000);

let name = "protocol.descriptor";
let intent_name: SchemaNamePayload =
BoundedVec::try_from(name.to_string().into_bytes()).expect("should convert");
let (intent_id, _) = SchemasPallet::create_intent_for(
intent_name,
PayloadLocation::OnChain,
BoundedVec::default(),
)
.expect("should have created an intent");

let model = create_bounded_schema_vec(r#"{"Name": "Bond", "Code": "007"}"#);

const MAX_SCHEMAS: u16 = 100;
for _ in 0..MAX_SCHEMAS {
SchemasPallet::create_schema_for(intent_id, model.clone(), ModelType::AvroBinary)
.expect("should create schema");
}

let response = SchemasPallet::get_intent_by_id_with_schemas(intent_id)
.expect("should get intent by id");
let mut last_schema_id: SchemaId = 0;
let schema_ids = response.schema_ids.expect("should return schema ids");
assert_eq!(schema_ids.len(), MAX_SCHEMAS as usize, "should get all schema ids");
schema_ids.into_iter().for_each(|schema_id| {
assert!(schema_id > last_schema_id, "Schema IDs should be sorted");
last_schema_id = schema_id;
})
})
}

#[test]
fn get_intent_with_schemas_should_not_return_unsupported_schemas() {
new_test_ext().execute_with(|| {
sudo_set_max_schema_size();

let name = "protocol.descriptor";
let intent_name: SchemaNamePayload =
BoundedVec::try_from(name.to_string().into_bytes()).expect("should convert");
let (intent_id, _) = SchemasPallet::create_intent_for(
intent_name,
PayloadLocation::OnChain,
BoundedVec::default(),
)
.expect("should have created an intent");

let model = create_bounded_schema_vec(r#"{"Name": "Bond", "Code": "007"}"#);

const MAX_SCHEMAS: usize = 100;
for _ in 0..MAX_SCHEMAS {
SchemasPallet::create_schema_for(intent_id, model.clone(), ModelType::AvroBinary)
.expect("should create schema");
}

// Set one of the schemas to Unsupported
// TODO: rework once the pallet supports updating schema status
let unsupported_schema_id: SchemaId = 1;
let mut schema_repsonse = SchemasPallet::get_schema_by_id(unsupported_schema_id)
.expect("should get schema by id");
schema_repsonse.status = SchemaStatus::Unsupported;
let info = SchemaInfo {
intent_id,
model_type: ModelType::AvroBinary,
payload_location: PayloadLocation::OnChain,
settings: Default::default(),
status: SchemaStatus::Unsupported,
};
SchemasPallet::store_schema_info_and_payload(unsupported_schema_id, info, model)
.expect("should store schema");

let response = SchemasPallet::get_intent_by_id_with_schemas(intent_id)
.expect("should get intent by id");
let schema_ids = response.schema_ids.expect("should return schema ids");
assert_eq!(schema_ids.len(), MAX_SCHEMAS - 1, "should get all SUPPORTED schema ids");
assert!(
!schema_ids.contains(&unsupported_schema_id),
"Returned schemas should not contain unsupported schemas"
);
})
}
4 changes: 2 additions & 2 deletions runtime/frequency/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: Cow::Borrowed("frequency"),
impl_name: Cow::Borrowed("frequency"),
authoring_version: 1,
spec_version: 184,
spec_version: 185,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand All @@ -682,7 +682,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: Cow::Borrowed("frequency-testnet"),
impl_name: Cow::Borrowed("frequency"),
authoring_version: 1,
spec_version: 184,
spec_version: 185,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down