Skip to content

Commit

Permalink
Merge pull request Joystream#18 from siman/alex-substrate-v2
Browse files Browse the repository at this point in the history
Switch to Substrate v2
  • Loading branch information
bedeho authored Oct 22, 2019
2 parents fb60b0b + 7e6a171 commit 4a659d3
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 104 deletions.
60 changes: 29 additions & 31 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
[package]
name = "substrate-versioned-store"
version = "1.0.0"
authors = ["Joystream"]
edition = "2018"
name = 'substrate-versioned-store'
version = '1.1.0'
authors = ['Joystream']
edition = '2018'

[dependencies]
hex-literal = "0.2.0"
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
parity-codec = { version = "3.2", default-features = false }
parity-codec-derive = { version = "3.1", default-features = false }
rstd = { package = 'sr-std', default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "v1.0"}
runtime-primitives = { package = 'sr-primitives', default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "v1.0"}
srml-support = { package = 'srml-support', default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "v1.0"}
srml-support-procedural = { package = 'srml-support-procedural', git = "https://github.com/paritytech/substrate.git", branch = "v1.0"}
system = { package = 'srml-system', default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "v1.0"}
balances = { package = 'srml-balances', default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "v1.0"}
hex-literal = '0.1.0'
serde = { version = '1.0', optional = true }
serde_derive = { version = '1.0', optional = true }
rstd = { package = 'sr-std', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a2a0eb5398d6223e531455b4c155ef053a4a3a2b'}
runtime-primitives = { package = 'sr-primitives', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a2a0eb5398d6223e531455b4c155ef053a4a3a2b'}
srml-support = { package = 'srml-support', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a2a0eb5398d6223e531455b4c155ef053a4a3a2b'}
srml-support-procedural = { package = 'srml-support-procedural', git = 'https://github.com/paritytech/substrate.git', rev = 'a2a0eb5398d6223e531455b4c155ef053a4a3a2b'}
system = { package = 'srml-system', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a2a0eb5398d6223e531455b4c155ef053a4a3a2b'}
balances = { package = 'srml-balances', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a2a0eb5398d6223e531455b4c155ef053a4a3a2b'}
codec = { package = 'parity-scale-codec', version = '1.0.0', default-features = false, features = ['derive'] }

[dependencies.timestamp]
default_features = false
git = 'https://github.com/paritytech/substrate.git'
package = 'srml-timestamp'
branch = "v1.0"
rev = 'a2a0eb5398d6223e531455b4c155ef053a4a3a2b'

[dependencies.runtime-io]
default_features = false
git = 'https://github.com/paritytech/substrate.git'
package = 'sr-io'
branch = "v1.0"
rev = 'a2a0eb5398d6223e531455b4c155ef053a4a3a2b'

[dev-dependencies]
runtime-io = { package = 'sr-io', default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "v1.0"}
primitives = { package = 'substrate-primitives', git = "https://github.com/paritytech/substrate.git", branch = "v1.0"}
runtime-io = { package = 'sr-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a2a0eb5398d6223e531455b4c155ef053a4a3a2b'}
primitives = { package = 'substrate-primitives', git = 'https://github.com/paritytech/substrate.git', rev = 'a2a0eb5398d6223e531455b4c155ef053a4a3a2b'}

[features]
default = ["std"]
default = ['std']
std = [
"serde",
"serde_derive",
"parity-codec/std",
"parity-codec-derive/std",
"rstd/std",
"runtime-io/std",
"runtime-primitives/std",
"srml-support/std",
"system/std",
"balances/std",
"timestamp/std",
'serde',
'serde_derive',
'codec/std',
'rstd/std',
'runtime-io/std',
'runtime-primitives/std',
'srml-support/std',
'system/std',
'balances/std',
'timestamp/std',
]
108 changes: 52 additions & 56 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use serde_derive::{Serialize, Deserialize};

use rstd::prelude::*;
use rstd::collections::btree_map::BTreeMap;
use parity_codec_derive::{Decode, Encode};
use rstd::collections::btree_set::BTreeSet;
use codec::{Decode, Encode};
use srml_support::{decl_event, decl_module, decl_storage, dispatch, ensure, StorageValue, StorageMap};
use system;

Expand Down Expand Up @@ -104,55 +104,55 @@ pub type EntityId = u64;
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
pub struct Class {
id: ClassId,
pub id: ClassId,

/// All properties that have been used on this class across different class schemas.
/// Unlikely to be more than roughly 20 properties per class, often less.
/// For Person, think "height", "weight", etc.
properties: Vec<Property>,
pub properties: Vec<Property>,

/// All scehmas that are available for this class, think v0.0 Person, v.1.0 Person, etc.
schemas: Vec<ClassSchema>,
pub schemas: Vec<ClassSchema>,

name: Vec<u8>,
description: Vec<u8>,
pub name: Vec<u8>,
pub description: Vec<u8>,
}

#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
pub struct Entity {
id: EntityId,
pub id: EntityId,

/// The class id of this entity.
class_id: ClassId,
pub class_id: ClassId,

/// What schemas under which this entity of a class is available, think
/// v.2.0 Person schema for John, v3.0 Person schema for John
/// Unlikely to be more than roughly 20ish, assuming schemas for a given class eventually stableize, or that very old schema are eventually removed.
in_class_schema_indexes: Vec<u16>, // indices of schema in corresponding class
pub in_class_schema_indexes: Vec<u16>, // indices of schema in corresponding class

/// Values for properties on class that are used by some schema used by this entity!
/// Length is no more than Class.properties.
values: Vec<ClassPropertyValue>,
pub values: Vec<ClassPropertyValue>,

// deleted: bool,
// pub deleted: bool,
}

/// A schema defines what properties describe an entity
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
pub struct ClassSchema {
/// Indices into properties vector for the corresponding class.
properties: Vec<u16>,
pub properties: Vec<u16>,
}

#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
pub struct Property {
prop_type: PropertyType,
required: bool,
name: Vec<u8>,
description: Vec<u8>,
pub prop_type: PropertyType,
pub required: bool,
pub name: Vec<u8>,
pub description: Vec<u8>,
}

#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
Expand Down Expand Up @@ -243,10 +243,10 @@ impl Default for PropertyValue {
pub struct ClassPropertyValue {

/// Index is into properties vector of class.
in_class_index: u16,
pub in_class_index: u16,

/// Value of property with index `in_class_index` in a given class.
value: PropertyValue
pub value: PropertyValue
}

pub trait Trait: system::Trait + Sized {
Expand Down Expand Up @@ -285,14 +285,10 @@ decl_event!(
<T as system::Trait>::AccountId,
{
ClassCreated(ClassId),
ClassPropertyAdded(ClassId, u16),
ClassPropertyUpdated(ClassId, u16),
ClassSchemaAdded(ClassId, u16),

EntityCreated(EntityId),
EntityUpdated(EntityId),
// EntityDeleted(EntityId),
EntityNameUpdated(EntityId),
EntityPropertiesUpdated(EntityId),
EntitySchemaAdded(EntityId, u16),

Expand Down Expand Up @@ -323,7 +319,7 @@ impl<T: Trait> Module<T> {

Self::ensure_class_description_is_valid(&description)?;

let class_id = <NextClassId<T>>::get();
let class_id = NextClassId::get();

let new_class = Class {
id: class_id,
Expand All @@ -334,10 +330,10 @@ impl<T: Trait> Module<T> {
};

// Save newly created class:
<ClassById<T>>::insert(class_id, new_class);
ClassById::insert(class_id, new_class);

// Increment the next class id:
<NextClassId<T>>::mutate(|n| *n += 1);
NextClassId::mutate(|n| *n += 1);

Self::deposit_event(RawEvent::ClassCreated(class_id));
Ok(class_id)
Expand All @@ -358,14 +354,14 @@ impl<T: Trait> Module<T> {

ensure!(non_empty_schema, ERROR_NO_PROPS_IN_CLASS_SCHEMA);

let class = <ClassById<T>>::get(class_id);
let class = ClassById::get(class_id);

// TODO Use BTreeSet for prop unique names when switched to Substrate 2.
// There is no support for BTreeSet in Substrate 1 runtime.
// use rstd::collections::btree_set::BTreeSet;
let mut unique_prop_names = BTreeMap::new();
let mut unique_prop_names = BTreeSet::new();
for prop in class.properties.iter() {
unique_prop_names.insert(prop.name.clone(), true);
unique_prop_names.insert(prop.name.clone());
}

for prop in new_properties.iter() {
Expand All @@ -374,10 +370,10 @@ impl<T: Trait> Module<T> {

// Check that the name of a new property is unique within its class.
ensure!(
!unique_prop_names.contains_key(&prop.name),
!unique_prop_names.contains(&prop.name),
ERROR_PROP_NAME_NOT_UNIQUE_IN_CLASS
);
unique_prop_names.insert(prop.name.clone(), true);
unique_prop_names.insert(prop.name.clone());
}

// Check that existing props are valid indices of class properties vector:
Expand All @@ -390,7 +386,7 @@ impl<T: Trait> Module<T> {
let has_unknown_internal_id = new_properties.iter().any(|prop| {
match prop.prop_type {
PropertyType::Internal(other_class_id) =>
!<ClassById<T>>::exists(other_class_id),
!ClassById::exists(other_class_id),
_ => false
}
});
Expand All @@ -411,7 +407,7 @@ impl<T: Trait> Module<T> {
schema.properties.push(prop_id);
});

<ClassById<T>>::mutate(class_id, |class| {
ClassById::mutate(class_id, |class| {
class.properties = updated_class_props;
class.schemas.push(schema);
});
Expand All @@ -426,7 +422,7 @@ impl<T: Trait> Module<T> {

Self::ensure_known_class_id(class_id)?;

let entity_id = <NextEntityId<T>>::get();
let entity_id = NextEntityId::get();

let new_entity = Entity {
id: entity_id,
Expand All @@ -437,10 +433,10 @@ impl<T: Trait> Module<T> {
};

// Save newly created entity:
<EntityById<T>>::insert(entity_id, new_entity);
EntityById::insert(entity_id, new_entity);

// Increment the next entity id:
<NextEntityId<T>>::mutate(|n| *n += 1);
NextEntityId::mutate(|n| *n += 1);

Self::deposit_event(RawEvent::EntityCreated(entity_id));
Ok(entity_id)
Expand Down Expand Up @@ -514,7 +510,7 @@ impl<T: Trait> Module<T> {
}
}

<EntityById<T>>::mutate(entity_id, |entity| {
EntityById::mutate(entity_id, |entity| {

// Add a new schema to the list of schemas supported by this entity.
entity.in_class_schema_indexes.push(schema_id);
Expand Down Expand Up @@ -582,7 +578,7 @@ impl<T: Trait> Module<T> {

// If at least one of the entity property values should be update:
if updates_count > 0 {
<EntityById<T>>::mutate(entity_id, |entity| {
EntityById::mutate(entity_id, |entity| {
entity.values = updated_values;
});
Self::deposit_event(RawEvent::EntityPropertiesUpdated(entity_id));
Expand All @@ -595,10 +591,10 @@ impl<T: Trait> Module<T> {
// pub fn delete_entity(entity_id: EntityId) -> dispatch::Result {
// Self::ensure_known_entity_id(entity_id)?;

// let is_deleted = <EntityById<T>>::get(entity_id).deleted;
// let is_deleted = EntityById::get(entity_id).deleted;
// ensure!(!is_deleted, ERROR_ENTITY_ALREADY_DELETED);

// <EntityById<T>>::mutate(entity_id, |x| {
// EntityById::mutate(entity_id, |x| {
// x.deleted = true;
// });

Expand All @@ -609,13 +605,13 @@ impl<T: Trait> Module<T> {
// Helper functions:
// ----------------------------------------------------------------

fn ensure_known_class_id(class_id: ClassId) -> dispatch::Result {
ensure!(<ClassById<T>>::exists(class_id), ERROR_CLASS_NOT_FOUND);
pub fn ensure_known_class_id(class_id: ClassId) -> dispatch::Result {
ensure!(ClassById::exists(class_id), ERROR_CLASS_NOT_FOUND);
Ok(())
}

fn ensure_known_entity_id(entity_id: EntityId) -> dispatch::Result {
ensure!(<EntityById<T>>::exists(entity_id), ERROR_ENTITY_NOT_FOUND);
pub fn ensure_known_entity_id(entity_id: EntityId) -> dispatch::Result {
ensure!(EntityById::exists(entity_id), ERROR_ENTITY_NOT_FOUND);
Ok(())
}

Expand All @@ -637,15 +633,15 @@ impl<T: Trait> Module<T> {

pub fn is_unknown_internal_entity_id(id: PropertyValue) -> bool {
if let PropertyValue::Internal(entity_id) = id {
!<EntityById<T>>::exists(entity_id)
!EntityById::exists(entity_id)
} else {
false
}
}

fn get_entity_and_class(entity_id: EntityId) -> (Entity, Class) {
let entity = <EntityById<T>>::get(entity_id);
let class = <ClassById<T>>::get(entity.class_id);
pub fn get_entity_and_class(entity_id: EntityId) -> (Entity, Class) {
let entity = EntityById::get(entity_id);
let class = ClassById::get(entity.class_id);
(entity, class)
}

Expand Down Expand Up @@ -793,32 +789,32 @@ impl<T: Trait> Module<T> {
}
}

fn ensure_property_name_is_valid(text: &Vec<u8>) -> dispatch::Result {
<PropertyNameConstraint<T>>::get().ensure_valid(
pub fn ensure_property_name_is_valid(text: &Vec<u8>) -> dispatch::Result {
PropertyNameConstraint::get().ensure_valid(
text.len(),
ERROR_PROPERTY_NAME_TOO_SHORT,
ERROR_PROPERTY_NAME_TOO_LONG
)
}

fn ensure_property_description_is_valid(text: &Vec<u8>) -> dispatch::Result {
PropertyDescriptionConstraint::<T>::get().ensure_valid(
pub fn ensure_property_description_is_valid(text: &Vec<u8>) -> dispatch::Result {
PropertyDescriptionConstraint::get().ensure_valid(
text.len(),
ERROR_PROPERTY_DESCRIPTION_TOO_SHORT,
ERROR_PROPERTY_DESCRIPTION_TOO_LONG
)
}

fn ensure_class_name_is_valid(text: &Vec<u8>) -> dispatch::Result {
<ClassNameConstraint<T>>::get().ensure_valid(
pub fn ensure_class_name_is_valid(text: &Vec<u8>) -> dispatch::Result {
ClassNameConstraint::get().ensure_valid(
text.len(),
ERROR_CLASS_NAME_TOO_SHORT,
ERROR_CLASS_NAME_TOO_LONG
)
}

fn ensure_class_description_is_valid(text: &Vec<u8>) -> dispatch::Result {
ClassDescriptionConstraint::<T>::get().ensure_valid(
pub fn ensure_class_description_is_valid(text: &Vec<u8>) -> dispatch::Result {
ClassDescriptionConstraint::get().ensure_valid(
text.len(),
ERROR_CLASS_DESCRIPTION_TOO_SHORT,
ERROR_CLASS_DESCRIPTION_TOO_LONG
Expand Down
Loading

0 comments on commit 4a659d3

Please sign in to comment.