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

Data object type registry fixes #18

Merged
merged 4 commits into from
Mar 27, 2019
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@

# JetBrains IDEs
.idea

# Vim
.*.sw*
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl memo::Trait for Runtime {

impl storage::data_object_type_registry::Trait for Runtime {
type Event = Event;
type DataObjectTypeID = u64;
type DataObjectTypeId = u64;
}

impl members::Trait for Runtime {
Expand Down
61 changes: 23 additions & 38 deletions src/storage/data_object_type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ use runtime_primitives::traits::{SimpleArithmetic, As, Member, MaybeSerializeDeb
use system::{self, ensure_root};
use crate::traits;

pub trait Trait: system::Trait + MaybeDebug
{
pub trait Trait: system::Trait + MaybeDebug {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;

type DataObjectTypeID: Parameter + Member + SimpleArithmetic + Codec + Default + Copy
type DataObjectTypeId: Parameter + Member + SimpleArithmetic + Codec + Default + Copy
+ As<usize> + As<u64> + MaybeSerializeDebug + PartialEq;
}

Expand All @@ -25,10 +24,8 @@ const DEFAULT_FIRST_DATA_OBJECT_TYPE_ID: u64 = 1;

#[derive(Clone, Encode, Decode, PartialEq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct DataObjectType<T: Trait>
{
// If the OT is registered, an ID must exist, otherwise it's a new OT.
pub id: Option<T::DataObjectTypeID>,
pub struct DataObjectType<T: Trait> {
pub id: Option<T::DataObjectTypeId>,
pub description: Vec<u8>,
pub active: bool,

Expand All @@ -39,36 +36,31 @@ pub struct DataObjectType<T: Trait>
}

decl_storage! {
trait Store for Module<T: Trait> as DataObjectTypeRegistry
{
trait Store for Module<T: Trait> as DataObjectTypeRegistry {
// Start at this value
pub FirstDataObjectTypeID get(first_data_object_type_id) config(first_data_object_type_id): T::DataObjectTypeID = T::DataObjectTypeID::sa(DEFAULT_FIRST_DATA_OBJECT_TYPE_ID);
pub FirstDataObjectTypeId get(first_data_object_type_id) config(first_data_object_type_id): T::DataObjectTypeId = T::DataObjectTypeId::sa(DEFAULT_FIRST_DATA_OBJECT_TYPE_ID);

// Increment
pub NextDataObjectTypeID get(next_data_object_type_id) build(|config: &GenesisConfig<T>| config.first_data_object_type_id): T::DataObjectTypeID = T::DataObjectTypeID::sa(DEFAULT_FIRST_DATA_OBJECT_TYPE_ID);
pub NextDataObjectTypeId get(next_data_object_type_id) build(|config: &GenesisConfig<T>| config.first_data_object_type_id): T::DataObjectTypeId = T::DataObjectTypeId::sa(DEFAULT_FIRST_DATA_OBJECT_TYPE_ID);

// Mapping of Data object types
pub DataObjectTypeMap get(data_object_type): map T::DataObjectTypeID => Option<DataObjectType<T>>;
pub DataObjectTypeMap get(data_object_type): map T::DataObjectTypeId => Option<DataObjectType<T>>;
}
}

decl_event! {
pub enum Event<T> where
<T as Trait>::DataObjectTypeID
{
DataObjectTypeAdded(DataObjectTypeID),
DataObjectTypeUpdated(DataObjectTypeID),
<T as Trait>::DataObjectTypeId {
DataObjectTypeRegistered(DataObjectTypeId),
DataObjectTypeUpdated(DataObjectTypeId),
}
}



impl<T: Trait> traits::IsActiveDataObjectType<T> for Module<T>
{
fn is_active_data_object_type(which: &T::DataObjectTypeID) -> bool
{
match Self::ensure_data_object_type(*which)
{
impl<T: Trait> traits::IsActiveDataObjectType<T> for Module<T> {
fn is_active_data_object_type(which: &T::DataObjectTypeId) -> bool {
match Self::ensure_data_object_type(*which) {
Ok(do_type) => do_type.active,
Err(_err) => false
}
Expand All @@ -77,12 +69,10 @@ impl<T: Trait> traits::IsActiveDataObjectType<T> for Module<T>


decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin
{
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default;

pub fn register_data_object_type(origin, data_object_type: DataObjectType<T>)
{
pub fn register_data_object_type(origin, data_object_type: DataObjectType<T>) {
ensure_root(origin)?;
ensure!(data_object_type.id.is_none(), MSG_REQUIRE_NEW_DO_TYPE);

Expand All @@ -94,13 +84,12 @@ decl_module! {
};

<DataObjectTypeMap<T>>::insert(new_do_type_id, do_type);
<NextDataObjectTypeID<T>>::mutate(|n| { *n += T::DataObjectTypeID::sa(1); });
<NextDataObjectTypeId<T>>::mutate(|n| { *n += T::DataObjectTypeId::sa(1); });

Self::deposit_event(RawEvent::DataObjectTypeAdded(new_do_type_id));
Self::deposit_event(RawEvent::DataObjectTypeRegistered(new_do_type_id));
}

pub fn update_data_object_type(origin, data_object_type: DataObjectType<T>)
{
pub fn update_data_object_type(origin, data_object_type: DataObjectType<T>) {
ensure_root(origin)?;
ensure!(data_object_type.id.is_some(), MSG_REQUIRE_DO_TYPE_ID);

Expand All @@ -118,8 +107,7 @@ decl_module! {
// Activate and deactivate functions as separate functions, because
// toggling DO types is likely a more common operation than updating
// other aspects.
pub fn activate_data_object_type(origin, id: T::DataObjectTypeID)
{
pub fn activate_data_object_type(origin, id: T::DataObjectTypeId) {
ensure_root(origin)?;
let mut do_type = Self::ensure_data_object_type(id)?;

Expand All @@ -130,8 +118,7 @@ decl_module! {
Self::deposit_event(RawEvent::DataObjectTypeUpdated(id));
}

pub fn deactivate_data_object_type(origin, id: T::DataObjectTypeID)
{
pub fn deactivate_data_object_type(origin, id: T::DataObjectTypeId) {
ensure_root(origin)?;
let mut do_type = Self::ensure_data_object_type(id)?;

Expand All @@ -145,10 +132,8 @@ decl_module! {
}
}

impl <T: Trait> Module<T>
{
fn ensure_data_object_type(id: T::DataObjectTypeID) -> Result<DataObjectType<T>, &'static str>
{
impl <T: Trait> Module<T> {
fn ensure_data_object_type(id: T::DataObjectTypeId) -> Result<DataObjectType<T>, &'static str> {
return Self::data_object_type(&id).ok_or(MSG_DO_TYPE_NOT_FOUND);
}
}
29 changes: 10 additions & 19 deletions src/storage/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ impl_outer_origin! {
}

impl_outer_event! {
pub enum MetaEvent for Test
{
pub enum MetaEvent for Test {
data_object_type_registry<T>,
}
}
Expand All @@ -29,8 +28,7 @@ impl_outer_event! {
// configuration traits of modules we want to use.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct Test;
impl system::Trait for Test
{
impl system::Trait for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
Expand All @@ -43,36 +41,29 @@ impl system::Trait for Test
type Log = DigestItem;
type Lookup = IdentityLookup<u64>;
}
impl data_object_type_registry::Trait for Test
{
impl data_object_type_registry::Trait for Test {
type Event = MetaEvent;
type DataObjectTypeID = u64;
type DataObjectTypeId = u64;
}

pub struct ExtBuilder
{
pub struct ExtBuilder {
first_data_object_type_id: u64,
}

impl Default for ExtBuilder
{
fn default() -> Self
{
impl Default for ExtBuilder {
fn default() -> Self {
Self {
first_data_object_type_id: 1,
}
}
}

impl ExtBuilder
{
pub fn first_data_object_type_id(mut self, first_data_object_type_id: u64) -> Self
{
impl ExtBuilder {
pub fn first_data_object_type_id(mut self, first_data_object_type_id: u64) -> Self {
self.first_data_object_type_id = first_data_object_type_id;
self
}
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher>
{
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap().0;

t.extend(data_object_type_registry::GenesisConfig::<Test>{
Expand Down
34 changes: 12 additions & 22 deletions src/storage/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@ use srml_support::*;
use system::{self, Phase, EventRecord};

#[test]
fn initial_state()
{
fn initial_state() {
const DEFAULT_FIRST_ID: u64 = 1000;

with_externalities(&mut ExtBuilder::default()
.first_data_object_type_id(DEFAULT_FIRST_ID).build(), ||
{
.first_data_object_type_id(DEFAULT_FIRST_ID).build(), || {
assert_eq!(TestDataObjectTypeRegistry::first_data_object_type_id(), DEFAULT_FIRST_ID);
});
}

#[test]
fn fail_register_without_root()
{
fn fail_register_without_root() {
const DEFAULT_FIRST_ID: u64 = 1000;

with_externalities(&mut ExtBuilder::default()
.first_data_object_type_id(DEFAULT_FIRST_ID).build(), ||
{
.first_data_object_type_id(DEFAULT_FIRST_ID).build(), || {
let data: TestDataObjectType = TestDataObjectType {
id: None,
description: "foo".as_bytes().to_vec(),
Expand All @@ -38,13 +34,11 @@ fn fail_register_without_root()
}

#[test]
fn succeed_register_as_root()
{
fn succeed_register_as_root() {
const DEFAULT_FIRST_ID: u64 = 1000;

with_externalities(&mut ExtBuilder::default()
.first_data_object_type_id(DEFAULT_FIRST_ID).build(), ||
{
.first_data_object_type_id(DEFAULT_FIRST_ID).build(), || {
let data: TestDataObjectType = TestDataObjectType {
id: None,
description: "foo".as_bytes().to_vec(),
Expand All @@ -56,13 +50,11 @@ fn succeed_register_as_root()
}

#[test]
fn update_existing()
{
fn update_existing() {
const DEFAULT_FIRST_ID: u64 = 1000;

with_externalities(&mut ExtBuilder::default()
.first_data_object_type_id(DEFAULT_FIRST_ID).build(), ||
{
.first_data_object_type_id(DEFAULT_FIRST_ID).build(), || {
// First register a type
let data: TestDataObjectType = TestDataObjectType {
id: None,
Expand All @@ -74,7 +66,7 @@ fn update_existing()
assert_eq!(*System::events().last().unwrap(),
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: MetaEvent::data_object_type_registry(data_object_type_registry::RawEvent::DataObjectTypeAdded(DEFAULT_FIRST_ID)),
event: MetaEvent::data_object_type_registry(data_object_type_registry::RawEvent::DataObjectTypeRegistered(DEFAULT_FIRST_ID)),
}
);

Expand Down Expand Up @@ -117,13 +109,11 @@ fn update_existing()


#[test]
fn activate_existing()
{
fn activate_existing() {
const DEFAULT_FIRST_ID: u64 = 1000;

with_externalities(&mut ExtBuilder::default()
.first_data_object_type_id(DEFAULT_FIRST_ID).build(), ||
{
.first_data_object_type_id(DEFAULT_FIRST_ID).build(), || {
// First register a type
let data: TestDataObjectType = TestDataObjectType {
id: None,
Expand All @@ -135,7 +125,7 @@ fn activate_existing()
assert_eq!(*System::events().last().unwrap(),
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: MetaEvent::data_object_type_registry(data_object_type_registry::RawEvent::DataObjectTypeAdded(DEFAULT_FIRST_ID)),
event: MetaEvent::data_object_type_registry(data_object_type_registry::RawEvent::DataObjectTypeRegistered(DEFAULT_FIRST_ID)),
}
);

Expand Down
6 changes: 2 additions & 4 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
use crate::storage::data_object_type_registry;
use system;

pub trait IsActiveDataObjectType<T: data_object_type_registry::Trait>
{
fn is_active_data_object_type(which: &T::DataObjectTypeID) -> bool
{
pub trait IsActiveDataObjectType<T: data_object_type_registry::Trait> {
fn is_active_data_object_type(which: &T::DataObjectTypeId) -> bool {
false
}
}
Expand Down