Skip to content

Commit

Permalink
schemas: PoV compatible changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aramikm committed Oct 25, 2023
1 parent c54260e commit f2c3a2a
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 224 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions common/primitives/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,27 @@ pub struct SchemaResponse {
pub settings: Vec<SchemaSetting>,
}

/// RPC Response form for a Schema Info
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
pub struct SchemaInfoResponse {
/// The unique identifier for this Schema
pub schema_id: SchemaId,
/// The model format type for how the schema model is represented
pub model_type: ModelType,
/// The payload location
pub payload_location: PayloadLocation,
/// grants for the schema
pub settings: Vec<SchemaSetting>,
}

/// This allows other pallets to resolve Schema information. With generic SchemaId
pub trait SchemaProvider<SchemaId> {
/// Gets the Schema details associated with this `SchemaId` if any
fn get_schema_by_id(schema_id: SchemaId) -> Option<SchemaResponse>;

/// Gets the Schema Info associated with this `SchemaId` if any
fn get_schema_info_by_id(schema_id: SchemaId) -> Option<SchemaInfoResponse>;
}

/// This allows other Pallets to check validity of schema ids.
Expand Down
2 changes: 1 addition & 1 deletion e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pallets/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub mod pallet {
.try_into()
.map_err(|_| Error::<T>::ExceedsMaxMessagePayloadSizeBytes)?;

if let Some(schema) = T::SchemaProvider::get_schema_by_id(schema_id) {
if let Some(schema) = T::SchemaProvider::get_schema_info_by_id(schema_id) {
ensure!(
schema.payload_location == PayloadLocation::IPFS,
Error::<T>::InvalidPayloadLocation
Expand Down Expand Up @@ -283,7 +283,7 @@ pub mod pallet {
let bounded_payload: BoundedVec<u8, T::MessagesMaxPayloadSizeBytes> =
payload.try_into().map_err(|_| Error::<T>::ExceedsMaxMessagePayloadSizeBytes)?;

if let Some(schema) = T::SchemaProvider::get_schema_by_id(schema_id) {
if let Some(schema) = T::SchemaProvider::get_schema_info_by_id(schema_id) {
ensure!(
schema.payload_location == PayloadLocation::OnChain,
Error::<T>::InvalidPayloadLocation
Expand Down
11 changes: 11 additions & 0 deletions pallets/messages/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ impl SchemaProvider<u16> for SchemaHandler {
settings: Vec::new(),
})
}

fn get_schema_info_by_id(schema_id: u16) -> Option<SchemaInfoResponse> {
Self::get_schema_by_id(schema_id).and_then(|schema| {
Some(SchemaInfoResponse {
schema_id: schema.schema_id,
settings: schema.settings,
model_type: schema.model_type,
payload_location: schema.payload_location,
})
})
}
}

impl pallet_messages::Config for Test {
Expand Down
106 changes: 53 additions & 53 deletions pallets/messages/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
//! Autogenerated weights for pallet_messages
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-07-07, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2023-10-25, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `benchmark-runner-qpqf8-fp5d5`, CPU: `Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("frequency-bench"), DB CACHE: 1024
//! HOSTNAME: `AramikMarkarianNewMacBookPro.local`, CPU: `<UNKNOWN>`
//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("frequency-bench"), DB CACHE: 1024
// Executed Command:
// ./scripts/../target/bench-dev/frequency
Expand Down Expand Up @@ -56,77 +56,77 @@ pub trait WeightInfo {
/// Weights for pallet_messages using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Storage: Schemas Schemas (r:1 w:0)
/// Proof: Schemas Schemas (max_values: None, max_size: Some(65518), added: 67993, mode: Measured)
/// Storage: Msa PublicKeyToMsaId (r:1 w:0)
/// Proof: Msa PublicKeyToMsaId (max_values: None, max_size: Some(48), added: 2523, mode: Measured)
/// Storage: Msa DelegatorAndProviderToDelegation (r:1 w:0)
/// Proof: Msa DelegatorAndProviderToDelegation (max_values: None, max_size: Some(217), added: 2692, mode: Measured)
/// Storage: Messages Messages (r:1 w:1)
/// Proof: Messages Messages (max_values: None, max_size: Some(771174), added: 773649, mode: Measured)
/// The range of component `n` is `[0, 5119]`.
/// Storage: `Schemas::SchemaInfos` (r:1 w:0)
/// Proof: `Schemas::SchemaInfos` (`max_values`: None, `max_size`: Some(14), added: 2489, mode: `Measured`)
/// Storage: `Msa::PublicKeyToMsaId` (r:1 w:0)
/// Proof: `Msa::PublicKeyToMsaId` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `Measured`)
/// Storage: `Msa::DelegatorAndProviderToDelegation` (r:1 w:0)
/// Proof: `Msa::DelegatorAndProviderToDelegation` (`max_values`: None, `max_size`: Some(217), added: 2692, mode: `Measured`)
/// Storage: `Messages::Messages` (r:1 w:1)
/// Proof: `Messages::Messages` (`max_values`: None, `max_size`: Some(618624), added: 621099, mode: `Measured`)
/// The range of component `n` is `[0, 3071]`.
fn add_onchain_message(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `7339`
// Estimated: `19714`
// Minimum execution time: 39_000_000 picoseconds.
Weight::from_parts(179_112_822, 19714)
// Standard Error: 35
.saturating_add(Weight::from_parts(459, 0).saturating_mul(n.into()))
// Measured: `9623`
// Estimated: `21998`
// Minimum execution time: 45_000_000 picoseconds.
Weight::from_parts(48_931_384, 21998)
// Standard Error: 358
.saturating_add(Weight::from_parts(560, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(4_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: Schemas Schemas (r:1 w:0)
/// Proof: Schemas Schemas (max_values: None, max_size: Some(65518), added: 67993, mode: Measured)
/// Storage: Msa PublicKeyToMsaId (r:1 w:0)
/// Proof: Msa PublicKeyToMsaId (max_values: None, max_size: Some(48), added: 2523, mode: Measured)
/// Storage: Messages Messages (r:1 w:1)
/// Proof: Messages Messages (max_values: None, max_size: Some(771174), added: 773649, mode: Measured)
/// Storage: `Schemas::SchemaInfos` (r:1 w:0)
/// Proof: `Schemas::SchemaInfos` (`max_values`: None, `max_size`: Some(14), added: 2489, mode: `Measured`)
/// Storage: `Msa::PublicKeyToMsaId` (r:1 w:0)
/// Proof: `Msa::PublicKeyToMsaId` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `Measured`)
/// Storage: `Messages::Messages` (r:1 w:1)
/// Proof: `Messages::Messages` (`max_values`: None, `max_size`: Some(618624), added: 621099, mode: `Measured`)
fn add_ipfs_message() -> Weight {
// Proof Size summary in bytes:
// Measured: `6183`
// Estimated: `18558`
// Minimum execution time: 36_000_000 picoseconds.
Weight::from_parts(174_485_000, 18558)
// Measured: `7833`
// Estimated: `20208`
// Minimum execution time: 44_000_000 picoseconds.
Weight::from_parts(48_000_000, 20208)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
/// Storage: Schemas Schemas (r:1 w:0)
/// Proof: Schemas Schemas (max_values: None, max_size: Some(65518), added: 67993, mode: Measured)
/// Storage: Msa PublicKeyToMsaId (r:1 w:0)
/// Proof: Msa PublicKeyToMsaId (max_values: None, max_size: Some(48), added: 2523, mode: Measured)
/// Storage: Msa DelegatorAndProviderToDelegation (r:1 w:0)
/// Proof: Msa DelegatorAndProviderToDelegation (max_values: None, max_size: Some(217), added: 2692, mode: Measured)
/// Storage: Messages Messages (r:1 w:1)
/// Proof: Messages Messages (max_values: None, max_size: Some(771174), added: 773649, mode: Measured)
/// The range of component `n` is `[0, 5119]`.
/// Storage: `Schemas::SchemaInfos` (r:1 w:0)
/// Proof: `Schemas::SchemaInfos` (`max_values`: None, `max_size`: Some(14), added: 2489, mode: `Measured`)
/// Storage: `Msa::PublicKeyToMsaId` (r:1 w:0)
/// Proof: `Msa::PublicKeyToMsaId` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `Measured`)
/// Storage: `Msa::DelegatorAndProviderToDelegation` (r:1 w:0)
/// Proof: `Msa::DelegatorAndProviderToDelegation` (`max_values`: None, `max_size`: Some(217), added: 2692, mode: `Measured`)
/// Storage: `Messages::Messages` (r:1 w:1)
/// Proof: `Messages::Messages` (`max_values`: None, `max_size`: Some(618624), added: 621099, mode: `Measured`)
/// The range of component `n` is `[0, 3071]`.
fn add_onchain_message(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `7339`
// Estimated: `19714`
// Minimum execution time: 39_000_000 picoseconds.
Weight::from_parts(179_112_822, 19714)
// Standard Error: 35
.saturating_add(Weight::from_parts(459, 0).saturating_mul(n.into()))
// Measured: `9623`
// Estimated: `21998`
// Minimum execution time: 45_000_000 picoseconds.
Weight::from_parts(48_931_384, 21998)
// Standard Error: 358
.saturating_add(Weight::from_parts(560, 0).saturating_mul(n.into()))
.saturating_add(RocksDbWeight::get().reads(4_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: Schemas Schemas (r:1 w:0)
/// Proof: Schemas Schemas (max_values: None, max_size: Some(65518), added: 67993, mode: Measured)
/// Storage: Msa PublicKeyToMsaId (r:1 w:0)
/// Proof: Msa PublicKeyToMsaId (max_values: None, max_size: Some(48), added: 2523, mode: Measured)
/// Storage: Messages Messages (r:1 w:1)
/// Proof: Messages Messages (max_values: None, max_size: Some(771174), added: 773649, mode: Measured)
/// Storage: `Schemas::SchemaInfos` (r:1 w:0)
/// Proof: `Schemas::SchemaInfos` (`max_values`: None, `max_size`: Some(14), added: 2489, mode: `Measured`)
/// Storage: `Msa::PublicKeyToMsaId` (r:1 w:0)
/// Proof: `Msa::PublicKeyToMsaId` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `Measured`)
/// Storage: `Messages::Messages` (r:1 w:1)
/// Proof: `Messages::Messages` (`max_values`: None, `max_size`: Some(618624), added: 621099, mode: `Measured`)
fn add_ipfs_message() -> Weight {
// Proof Size summary in bytes:
// Measured: `6183`
// Estimated: `18558`
// Minimum execution time: 36_000_000 picoseconds.
Weight::from_parts(174_485_000, 18558)
// Measured: `7833`
// Estimated: `20208`
// Minimum execution time: 44_000_000 picoseconds.
Weight::from_parts(48_000_000, 20208)
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand Down
1 change: 1 addition & 0 deletions pallets/schemas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ targets = ["x86_64-unknown-linux-gnu"]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = [
"derive",
] }
log = { version = "0.4.17", default-features = false }
frame-benchmarking = { default-features = false, git = "https://github.com/paritytech/substrate", optional = true, branch = "polkadot-v1.0.0" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
Expand Down
6 changes: 3 additions & 3 deletions pallets/schemas/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ benchmarks! {
}: _(RawOrigin::Signed(sender), schema_input, model_type, payload_location)
verify {
ensure!(SchemasPallet::<T>::get_current_schema_identifier_maximum() > 0, "Created schema count should be > 0");
ensure!(SchemasPallet::<T>::get_schema(1).is_some(), "Created schema should exist");
ensure!(SchemasPallet::<T>::get_schema_info(1).is_some(), "Created schema should exist");
}

create_schema_via_governance {
Expand All @@ -55,7 +55,7 @@ benchmarks! {
}: _(RawOrigin::Root, sender.clone(), schema_input, model_type, payload_location, BoundedVec::default())
verify {
ensure!(SchemasPallet::<T>::get_current_schema_identifier_maximum() > 0, "Created schema count should be > 0");
ensure!(SchemasPallet::<T>::get_schema(1).is_some(), "Created schema should exist");
ensure!(SchemasPallet::<T>::get_schema_info(1).is_some(), "Created schema should exist");
}

propose_to_create_schema {
Expand All @@ -80,7 +80,7 @@ benchmarks! {
}: _(RawOrigin::Signed(sender), schema_input, model_type, payload_location, BoundedVec::default())
verify {
ensure!(SchemasPallet::<T>::get_current_schema_identifier_maximum() > 0, "Created schema count should be > 0");
ensure!(SchemasPallet::<T>::get_schema(1).is_some(), "Created schema should exist");
ensure!(SchemasPallet::<T>::get_schema_info(1).is_some(), "Created schema should exist");
}

set_max_schema_model_bytes {
Expand Down
69 changes: 59 additions & 10 deletions pallets/schemas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ mod tests;
mod benchmarking;
#[cfg(feature = "runtime-benchmarks")]
use common_primitives::benchmarks::SchemaBenchmarkHelper;
use common_primitives::schema::SchemaInfoResponse;

mod types;

Expand Down Expand Up @@ -197,13 +198,34 @@ pub mod pallet {
OptionQuery,
>;

/// Storage for message schema info struct data
/// - Key: Schema Id
/// - Value: [`SchemaInfo`](SchemaInfo)
#[pallet::storage]
#[pallet::getter(fn get_schema_info)]
pub(super) type SchemaInfos<T: Config> =
StorageMap<_, Twox64Concat, SchemaId, SchemaInfo, OptionQuery>;

/// Storage for message schema struct data
/// - Key: Schema Id
/// - Value: [`BoundedVec`](BoundedVec<T::SchemaModelMaxBytesBoundedVecLimit>)
#[pallet::storage]
#[pallet::getter(fn get_schema_payload)]
pub(super) type SchemaPayloads<T: Config> = StorageMap<
_,
Twox64Concat,
SchemaId,
BoundedVec<u8, T::SchemaModelMaxBytesBoundedVecLimit>,
OptionQuery,
>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
/// Maximum schema size in bytes at genesis
pub initial_max_schema_model_size: u32,
/// Phantom type
#[serde(skip)]
pub _config: sp_std::marker::PhantomData<T>,
pub _config: PhantomData<T>,
}

impl<T: Config> sp_std::default::Default for GenesisConfig<T> {
Expand Down Expand Up @@ -398,23 +420,46 @@ pub mod pallet {
set_settings.set(i);
}
}
let schema = Schema { model_type, model, payload_location, settings: set_settings };
let schema_info = SchemaInfo { model_type, payload_location, settings: set_settings };
<CurrentSchemaIdentifierMaximum<T>>::set(schema_id);
<Schemas<T>>::insert(schema_id, schema);
<SchemaInfos<T>>::insert(schema_id, schema_info);
<SchemaPayloads<T>>::insert(schema_id, model);
Ok(schema_id)
}

/// Retrieve a schema by id
pub fn get_schema_by_id(schema_id: SchemaId) -> Option<SchemaResponse> {
if let Some(schema) = Self::get_schema(schema_id) {
let model_vec: Vec<u8> = schema.model.into_inner();
let saved_settings = schema.settings;
match (Self::get_schema_info(schema_id), Self::get_schema_payload(schema_id)) {
(Some(schema_info), Some(payload)) => {
let model_vec: Vec<u8> = payload.into_inner();
let saved_settings = schema_info.settings;
let settings = saved_settings.0.iter().collect::<Vec<SchemaSetting>>();
let response = SchemaResponse {
schema_id,
model: model_vec,
model_type: schema_info.model_type,
payload_location: schema_info.payload_location,
settings,
};
Some(response)
},
(None, Some(_)) | (Some(_), None) => {
log::error!("Corrupted state for schema {:?}, Should never happen!", schema_id);
None
},
(None, None) => None,
}
}

/// Retrieve a schema info by id
pub fn get_schema_info_by_id(schema_id: SchemaId) -> Option<SchemaInfoResponse> {
if let Some(schema_info) = Self::get_schema_info(schema_id) {
let saved_settings = schema_info.settings;
let settings = saved_settings.0.iter().collect::<Vec<SchemaSetting>>();
let response = SchemaResponse {
let response = SchemaInfoResponse {
schema_id,
model: model_vec,
model_type: schema.model_type,
payload_location: schema.payload_location,
model_type: schema_info.model_type,
payload_location: schema_info.payload_location,
settings,
};
return Some(response)
Expand Down Expand Up @@ -532,4 +577,8 @@ impl<T: Config> SchemaProvider<SchemaId> for Pallet<T> {
fn get_schema_by_id(schema_id: SchemaId) -> Option<SchemaResponse> {
Self::get_schema_by_id(schema_id)
}

fn get_schema_info_by_id(schema_id: SchemaId) -> Option<SchemaInfoResponse> {
Self::get_schema_info_by_id(schema_id)
}
}
11 changes: 11 additions & 0 deletions pallets/schemas/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ where
/// additional control settings for the schema
pub settings: SchemaSettings,
}

#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq, MaxEncodedLen)]
/// A structure defining a Schema information (excluding the payload)
pub struct SchemaInfo {
/// The type of model (AvroBinary, Parquet, etc.)
pub model_type: ModelType,
/// The payload location
pub payload_location: PayloadLocation,
/// additional control settings for the schema
pub settings: SchemaSettings,
}
Loading

0 comments on commit f2c3a2a

Please sign in to comment.