Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema BitFlags: Enable fine grained schema controls via optional settings #1079

Merged
merged 65 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
8b5e72d
schema setting poc
aramikm Feb 7, 2023
90dfa64
Merge remote-tracking branch 'origin/schema_settings_poc' into schema…
saraswatpuneet Feb 10, 2023
2fa9d1c
make settings optional for generic schemas
saraswatpuneet Feb 10, 2023
3b1cf2a
Merge branch 'private-graph-main' into schema_settings
saraswatpuneet Feb 10, 2023
5719dcc
Know your schema setting flag
saraswatpuneet Feb 10, 2023
a465d23
placeholder for future int tests
saraswatpuneet Feb 10, 2023
ca7c2d3
update
saraswatpuneet Feb 11, 2023
8788899
schema grants
saraswatpuneet Feb 11, 2023
9282429
schema grants : align with design docs
saraswatpuneet Feb 13, 2023
7d602fc
add unit test for grants
saraswatpuneet Feb 13, 2023
07e7e4f
update stateful pallet mocks and messages mock
saraswatpuneet Feb 13, 2023
677cb88
update existing code
saraswatpuneet Feb 13, 2023
2b2efe4
cleanup int tests
saraswatpuneet Feb 13, 2023
402020e
update rpc to return grants
saraswatpuneet Feb 13, 2023
6d7b2f8
unit tests and cleanups
saraswatpuneet Feb 13, 2023
22dc71c
update integration tests
saraswatpuneet Feb 13, 2023
1f5ac3e
cleanup and ready
saraswatpuneet Feb 13, 2023
bac1e87
remove dead code
saraswatpuneet Feb 13, 2023
4eb8f0b
call it SchemaSetting for now
saraswatpuneet Feb 13, 2023
223db86
allow both settings to be applied
saraswatpuneet Feb 13, 2023
1fcb1c8
add new extrinsic
saraswatpuneet Feb 13, 2023
d08f886
renaming
saraswatpuneet Feb 13, 2023
e764ac5
BoundedVec needs u32
saraswatpuneet Feb 13, 2023
d144fc1
refactor
saraswatpuneet Feb 13, 2023
073b540
fix mock
saraswatpuneet Feb 13, 2023
2ef25d8
update int tests to use new extrinsic
saraswatpuneet Feb 14, 2023
aae76a6
update documentation
saraswatpuneet Feb 14, 2023
868fa6d
rename
saraswatpuneet Feb 14, 2023
f59235e
base work migration
saraswatpuneet Feb 15, 2023
8903312
add migration to V2 for schemas
saraswatpuneet Feb 15, 2023
4c80875
migration related updated
saraswatpuneet Feb 15, 2023
bce5e0e
add todos for post migration
saraswatpuneet Feb 15, 2023
528f268
add on_runtime_upgrade hook for migrations
saraswatpuneet Feb 15, 2023
5072895
tested
saraswatpuneet Feb 15, 2023
3a2cf68
simplify migration code
saraswatpuneet Feb 15, 2023
6ce2dd3
finalize migration works
saraswatpuneet Feb 16, 2023
3527de5
rename
saraswatpuneet Feb 16, 2023
ea09fcf
cleanups
saraswatpuneet Feb 16, 2023
db03545
rpc works
saraswatpuneet Feb 16, 2023
90eb3a4
rpc works
saraswatpuneet Feb 16, 2023
e5db783
documentation
saraswatpuneet Feb 16, 2023
3e465fb
some placeholder documentation
saraswatpuneet Feb 16, 2023
a4f6e43
update api js with updated rpc
saraswatpuneet Feb 16, 2023
f2b02f3
Merge branch 'private-graph-main' into schema_settings
saraswatpuneet Feb 17, 2023
2c9ba61
initial commit
aramikm Feb 18, 2023
2d7546f
added some tests for itemized with signature
aramikm Feb 22, 2023
8e57ee3
Merge branch 'private-graph-main' into schema_settings
saraswatpuneet Feb 22, 2023
380212c
Merge branch 'private_graph_require_signature' into schema_settings
saraswatpuneet Feb 22, 2023
6bb5dea
set schema check
saraswatpuneet Feb 23, 2023
1f68059
schema checks and unit test
saraswatpuneet Feb 23, 2023
1e8f16e
add unit tests for schema setting check
saraswatpuneet Feb 23, 2023
dae1b80
add tests
saraswatpuneet Feb 23, 2023
af8d11d
add failure test for signed itemized ext
saraswatpuneet Feb 23, 2023
bd361c7
finalize and set
saraswatpuneet Feb 23, 2023
62f1e22
cleanups
saraswatpuneet Feb 23, 2023
a7fac03
try-runtime setup
saraswatpuneet Feb 24, 2023
44d098c
added unit tests and benchmarks
aramikm Feb 24, 2023
66345c0
move runtime upgrade of schemas to runtime
saraswatpuneet Feb 24, 2023
3415e6e
added integration tests
aramikm Feb 24, 2023
2eff97e
add some docs
aramikm Feb 25, 2023
cb9d1cb
Merge branch 'private_graph_require_signature' into schema_settings
saraswatpuneet Feb 27, 2023
fe981ea
rebase and re-test
saraswatpuneet Feb 27, 2023
a7c8802
fix some int tests due to checks
saraswatpuneet Feb 28, 2023
925b8e1
finalize integration tests
saraswatpuneet Feb 28, 2023
0747620
use storage version for idempotent mig
saraswatpuneet Feb 28, 2023
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
4 changes: 4 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions common/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ targets = ['x86_64-unknown-linux-gnu']
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = [
"derive",
] }
enumflags2 = "0.7.5"
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
impl-serde = { version = "0.4.0", default-features = false }
scale-info = { version = "2.2.0", default-features = false, features = [
Expand Down
2 changes: 2 additions & 0 deletions common/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
rustdoc::invalid_codeblock_attributes,
missing_docs
)]
/// macros
pub mod macros;
/// Structs and traits for the Messages pallet.
pub mod messages;
/// Structs and traits for the MSA pallet.
Expand Down
76 changes: 76 additions & 0 deletions common/primitives/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// This file is part of Substrate.

// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// macro_rules! impl_incrementable {
// ($($type:ty),+) => {
// $(
// impl Incrementable for $type {
// fn increment(&self) -> Self {
// let mut val = self.clone();
// val.saturating_inc();
// val
// }
//
// fn initial_value() -> Self {
// 0
// }
// }
// )+
// };
// }
// pub(crate) use impl_incrementable;

#[macro_export]
#[doc(hidden)]
macro_rules! impl_codec_bitflags {
($wrapper:ty, $size:ty, $bitflag_enum:ty) => {
impl MaxEncodedLen for $wrapper {
fn max_encoded_len() -> usize {
<$size>::max_encoded_len()
}
}
impl Encode for $wrapper {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.bits().using_encoded(f)
}
}
impl EncodeLike for $wrapper {}
impl Decode for $wrapper {
fn decode<I: codec::Input>(
input: &mut I,
) -> sp_std::result::Result<Self, codec::Error> {
let field = <$size>::decode(input)?;
Ok(Self(BitFlags::from_bits(field as $size).map_err(|_| "invalid value")?))
}
}

impl TypeInfo for $wrapper {
type Identity = Self;

fn type_info() -> Type {
Type::builder()
.path(Path::new("BitFlags", module_path!()))
.type_params(vec![TypeParameter::new("T", Some(meta_type::<$bitflag_enum>()))])
.composite(
Fields::unnamed()
.field(|f| f.ty::<$size>().type_name(stringify!($bitflag_enum))),
)
}
}
};
}
// pub(crate) use impl_codec_bitflags;
saraswatpuneet marked this conversation as resolved.
Show resolved Hide resolved
51 changes: 49 additions & 2 deletions common/primitives/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::impl_codec_bitflags;
#[cfg(feature = "std")]
use crate::utils;
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use enumflags2::{bitflags, BitFlags};
use frame_support::RuntimeDebug;
use scale_info::{build::Fields, meta_type, Path, Type, TypeInfo, TypeParameter};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_std::prelude::*;
Expand Down Expand Up @@ -35,6 +38,24 @@ pub enum PayloadLocation {
Paginated,
}

/// Support for up to 16 user-enabled features on a collection.
#[bitflags]
#[repr(u16)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, RuntimeDebug, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub enum SchemaSetting {
/// Schema setting to enforce append-only behavior on payload.
/// Applied to schemas of type `PayloadLocation::Itemized` or `PayloadLocation::Paginated`.
AppendOnly,
/// Schema may enforce signature requirement on payload.
/// Applied to schemas of type `PayloadLocation::Paginated`.
SignatureRequired,
saraswatpuneet marked this conversation as resolved.
Show resolved Hide resolved
}

/// Wrapper type for `BitFlags<SchemaSetting>` that implements `Codec`.
#[derive(Clone, Copy, PartialEq, Eq, Default, RuntimeDebug)]
pub struct SchemaSettings(pub BitFlags<SchemaSetting>);

/// RPC Response form for a Schema
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
Expand All @@ -48,6 +69,8 @@ pub struct SchemaResponse {
pub model_type: ModelType,
/// The payload location
pub payload_location: PayloadLocation,
/// grants for the schema
pub settings: Vec<SchemaSetting>,
aramikm marked this conversation as resolved.
Show resolved Hide resolved
}

/// This allows other pallets to resolve Schema information. With generic SchemaId
Expand All @@ -65,3 +88,27 @@ pub trait SchemaValidator<SchemaId> {
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
fn set_schema_count(n: SchemaId);
}

impl SchemaSettings {
/// Set all settings to disabled
pub fn all_disabled() -> Self {
Self(BitFlags::EMPTY)
}
/// Get all setting enabled
pub fn get_enabled(&self) -> BitFlags<SchemaSetting> {
self.0
}
/// Check if a setting is enabled
pub fn is_enabled(&self, grant: SchemaSetting) -> bool {
self.0.contains(grant)
}
/// Enable a setting
pub fn set(&mut self, grant: SchemaSetting) {
self.0.insert(grant)
}
/// Copy the settings from a BitFlags
pub fn from(settings: BitFlags<SchemaSetting>) -> Self {
Self(settings)
}
}
impl_codec_bitflags!(SchemaSettings, u16, SchemaSetting);
Loading