|
@@ -25,18 +25,13 @@ pub trait Trait: system::Trait + MaybeDebug {
|
|
|
+ PartialEq;
|
|
|
}
|
|
|
|
|
|
-static MSG_REQUIRE_NEW_DO_TYPE: &str =
|
|
|
- "New Data Object Type required; the provided one seems to be in use already!";
|
|
|
static MSG_DO_TYPE_NOT_FOUND: &str = "Data Object Type with the given ID not found!";
|
|
|
-static MSG_REQUIRE_DO_TYPE_ID: &str =
|
|
|
- "Can only update Data Object Types that are already registered (with an ID)!";
|
|
|
|
|
|
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> {
|
|
|
- pub id: Option<T::DataObjectTypeId>,
|
|
|
+pub struct DataObjectType {
|
|
|
pub description: Vec<u8>,
|
|
|
pub active: bool,
|
|
|
// TODO in future releases
|
|
@@ -54,7 +49,7 @@ decl_storage! {
|
|
|
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>;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -79,13 +74,11 @@ decl_module! {
|
|
|
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) {
|
|
|
ensure_root(origin)?;
|
|
|
- ensure!(data_object_type.id.is_none(), MSG_REQUIRE_NEW_DO_TYPE);
|
|
|
|
|
|
let new_do_type_id = Self::next_data_object_type_id();
|
|
|
- let do_type: DataObjectType<T> = DataObjectType {
|
|
|
- id: Some(new_do_type_id),
|
|
|
+ let do_type: DataObjectType = DataObjectType {
|
|
|
description: data_object_type.description.clone(),
|
|
|
active: data_object_type.active,
|
|
|
};
|
|
@@ -96,11 +89,8 @@ decl_module! {
|
|
|
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, id: T::DataObjectTypeId, data_object_type: DataObjectType) {
|
|
|
ensure_root(origin)?;
|
|
|
- ensure!(data_object_type.id.is_some(), MSG_REQUIRE_DO_TYPE_ID);
|
|
|
-
|
|
|
- let id = data_object_type.id.unwrap();
|
|
|
let mut do_type = Self::ensure_data_object_type(id)?;
|
|
|
|
|
|
do_type.description = data_object_type.description.clone();
|
|
@@ -140,7 +130,7 @@ decl_module! {
|
|
|
}
|
|
|
|
|
|
impl<T: Trait> Module<T> {
|
|
|
- fn ensure_data_object_type(id: T::DataObjectTypeId) -> Result<DataObjectType<T>, &'static str> {
|
|
|
+ fn ensure_data_object_type(id: T::DataObjectTypeId) -> Result<DataObjectType, &'static str> {
|
|
|
return Self::data_object_type(&id).ok_or(MSG_DO_TYPE_NOT_FOUND);
|
|
|
}
|
|
|
}
|
|
@@ -166,7 +156,6 @@ mod tests {
|
|
|
fn fail_register_without_root() {
|
|
|
with_default_mock_builder(|| {
|
|
|
let data: TestDataObjectType = TestDataObjectType {
|
|
|
- id: None,
|
|
|
description: "foo".as_bytes().to_vec(),
|
|
|
active: false,
|
|
|
};
|
|
@@ -180,7 +169,6 @@ mod tests {
|
|
|
fn succeed_register_as_root() {
|
|
|
with_default_mock_builder(|| {
|
|
|
let data: TestDataObjectType = TestDataObjectType {
|
|
|
- id: None,
|
|
|
description: "foo".as_bytes().to_vec(),
|
|
|
active: false,
|
|
|
};
|
|
@@ -194,59 +182,47 @@ mod tests {
|
|
|
with_default_mock_builder(|| {
|
|
|
// First register a type
|
|
|
let data: TestDataObjectType = TestDataObjectType {
|
|
|
- id: None,
|
|
|
description: "foo".as_bytes().to_vec(),
|
|
|
active: false,
|
|
|
};
|
|
|
let id_res = TestDataObjectTypeRegistry::register_data_object_type(Origin::ROOT, data);
|
|
|
assert!(id_res.is_ok());
|
|
|
- assert_eq!(
|
|
|
- *System::events().last().unwrap(),
|
|
|
- EventRecord {
|
|
|
- phase: Phase::ApplyExtrinsic(0),
|
|
|
- event: MetaEvent::data_object_type_registry(
|
|
|
- data_object_type_registry::RawEvent::DataObjectTypeRegistered(
|
|
|
- TEST_FIRST_DATA_OBJECT_TYPE_ID
|
|
|
- )
|
|
|
- ),
|
|
|
- }
|
|
|
- );
|
|
|
|
|
|
- // Now update it with new data - we need the ID to be the same as in
|
|
|
- // returned by the previous call. First, though, try and fail without
|
|
|
- let updated1: TestDataObjectType = TestDataObjectType {
|
|
|
- id: None,
|
|
|
- description: "bar".as_bytes().to_vec(),
|
|
|
- active: false,
|
|
|
+ let dot_id = match System::events().last().unwrap().event {
|
|
|
+ MetaEvent::data_object_type_registry(
|
|
|
+ data_object_type_registry::RawEvent::DataObjectTypeRegistered(dot_id),
|
|
|
+ ) => dot_id,
|
|
|
+ _ => 0xdeadbeefu64, // unlikely value
|
|
|
};
|
|
|
- let res = TestDataObjectTypeRegistry::update_data_object_type(Origin::ROOT, updated1);
|
|
|
- assert!(res.is_err());
|
|
|
+ assert_ne!(dot_id, 0xdeadbeefu64);
|
|
|
|
|
|
- // Now try with a bad ID
|
|
|
- let updated2: TestDataObjectType = TestDataObjectType {
|
|
|
- id: Some(TEST_FIRST_DATA_OBJECT_TYPE_ID + 1),
|
|
|
+ // Now update it with new data - we need the ID to be the same as in
|
|
|
+ // returned by the previous call. First, though, try and fail with a bad ID
|
|
|
+ let updated1: TestDataObjectType = TestDataObjectType {
|
|
|
description: "bar".as_bytes().to_vec(),
|
|
|
active: false,
|
|
|
};
|
|
|
- let res = TestDataObjectTypeRegistry::update_data_object_type(Origin::ROOT, updated2);
|
|
|
+ let res = TestDataObjectTypeRegistry::update_data_object_type(
|
|
|
+ Origin::ROOT,
|
|
|
+ dot_id + 1,
|
|
|
+ updated1,
|
|
|
+ );
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
// Finally with an existing ID, it should work.
|
|
|
let updated3: TestDataObjectType = TestDataObjectType {
|
|
|
- id: Some(TEST_FIRST_DATA_OBJECT_TYPE_ID),
|
|
|
description: "bar".as_bytes().to_vec(),
|
|
|
active: false,
|
|
|
};
|
|
|
- let res = TestDataObjectTypeRegistry::update_data_object_type(Origin::ROOT, updated3);
|
|
|
+ let res =
|
|
|
+ TestDataObjectTypeRegistry::update_data_object_type(Origin::ROOT, dot_id, updated3);
|
|
|
assert!(res.is_ok());
|
|
|
assert_eq!(
|
|
|
*System::events().last().unwrap(),
|
|
|
EventRecord {
|
|
|
phase: Phase::ApplyExtrinsic(0),
|
|
|
event: MetaEvent::data_object_type_registry(
|
|
|
- data_object_type_registry::RawEvent::DataObjectTypeUpdated(
|
|
|
- TEST_FIRST_DATA_OBJECT_TYPE_ID
|
|
|
- )
|
|
|
+ data_object_type_registry::RawEvent::DataObjectTypeUpdated(dot_id)
|
|
|
),
|
|
|
}
|
|
|
);
|
|
@@ -258,7 +234,6 @@ mod tests {
|
|
|
with_default_mock_builder(|| {
|
|
|
// First register a type
|
|
|
let data: TestDataObjectType = TestDataObjectType {
|
|
|
- id: None,
|
|
|
description: "foo".as_bytes().to_vec(),
|
|
|
active: false,
|
|
|
};
|