1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use core::result::Result;
use frame_support::{
decl_error, decl_event, decl_module, decl_storage, dispatch, ensure, sp_runtime::RuntimeDebug,
sp_std::prelude::*, traits::EnsureOrigin,
};
use frame_system::{self as system, ensure_signed};
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub const PRODUCT_ID_MAX_LENGTH: usize = 14;
pub const PRODUCT_PROP_NAME_MAX_LENGTH: usize = 10;
pub const PRODUCT_PROP_VALUE_MAX_LENGTH: usize = 20;
pub const PRODUCT_MAX_PROPS: usize = 3;
pub type ProductId = Vec<u8>;
pub type PropName = Vec<u8>;
pub type PropValue = Vec<u8>;
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct Product<AccountId, Moment> {
id: ProductId,
owner: AccountId,
props: Option<Vec<ProductProperty>>,
registered: Moment,
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct ProductProperty {
name: PropName,
value: PropValue,
}
impl ProductProperty {
pub fn new(name: &[u8], value: &[u8]) -> Self {
Self {
name: name.to_vec(),
value: value.to_vec(),
}
}
pub fn name(&self) -> &[u8] {
self.name.as_ref()
}
pub fn value(&self) -> &[u8] {
self.value.as_ref()
}
}
pub trait Trait: system::Trait + timestamp::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
type CreateRoleOrigin: EnsureOrigin<Self::Origin>;
}
decl_storage! {
trait Store for Module<T: Trait> as ProductRegistry {
pub Products get(fn product_by_id): map hasher(blake2_128_concat) ProductId => Option<Product<T::AccountId, T::Moment>>;
pub ProductsOfOrganization get(fn products_of_org): map hasher(blake2_128_concat) T::AccountId => Vec<ProductId>;
pub OwnerOf get(fn owner_of): map hasher(blake2_128_concat) ProductId => Option<T::AccountId>;
}
}
decl_event!(
pub enum Event<T>
where
AccountId = <T as system::Trait>::AccountId,
{
ProductRegistered(AccountId, ProductId, AccountId),
}
);
decl_error! {
pub enum Error for Module<T: Trait> {
ProductIdMissing,
ProductIdTooLong,
ProductIdExists,
ProductTooManyProps,
ProductInvalidPropName,
ProductInvalidPropValue
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
fn deposit_event() = default;
#[weight = 10_000]
pub fn register_product(origin, id: ProductId, owner: T::AccountId, props: Option<Vec<ProductProperty>>) -> dispatch::DispatchResult {
T::CreateRoleOrigin::ensure_origin(origin.clone())?;
let who = ensure_signed(origin)?;
Self::validate_product_id(&id)?;
Self::validate_product_props(&props)?;
Self::validate_new_product(&id)?;
let product = Self::new_product()
.identified_by(id.clone())
.owned_by(owner.clone())
.registered_on(<timestamp::Module<T>>::now())
.with_props(props)
.build();
<Products<T>>::insert(&id, product);
<ProductsOfOrganization<T>>::append(&owner, &id);
<OwnerOf<T>>::insert(&id, &owner);
Self::deposit_event(RawEvent::ProductRegistered(who, id, owner));
Ok(())
}
}
}
impl<T: Trait> Module<T> {
fn new_product() -> ProductBuilder<T::AccountId, T::Moment> {
ProductBuilder::<T::AccountId, T::Moment>::default()
}
pub fn validate_product_id(id: &[u8]) -> Result<(), Error<T>> {
ensure!(!id.is_empty(), Error::<T>::ProductIdMissing);
ensure!(
id.len() <= PRODUCT_ID_MAX_LENGTH,
Error::<T>::ProductIdTooLong
);
Ok(())
}
pub fn validate_new_product(id: &[u8]) -> Result<(), Error<T>> {
ensure!(
!<Products<T>>::contains_key(id),
Error::<T>::ProductIdExists
);
Ok(())
}
pub fn validate_product_props(props: &Option<Vec<ProductProperty>>) -> Result<(), Error<T>> {
if let Some(props) = props {
ensure!(
props.len() <= PRODUCT_MAX_PROPS,
Error::<T>::ProductTooManyProps,
);
for prop in props {
ensure!(
prop.name().len() <= PRODUCT_PROP_NAME_MAX_LENGTH,
Error::<T>::ProductInvalidPropName
);
ensure!(
prop.value().len() <= PRODUCT_PROP_VALUE_MAX_LENGTH,
Error::<T>::ProductInvalidPropValue
);
}
}
Ok(())
}
}
#[derive(Default)]
pub struct ProductBuilder<AccountId, Moment>
where
AccountId: Default,
Moment: Default,
{
id: ProductId,
owner: AccountId,
props: Option<Vec<ProductProperty>>,
registered: Moment,
}
impl<AccountId, Moment> ProductBuilder<AccountId, Moment>
where
AccountId: Default,
Moment: Default,
{
pub fn identified_by(mut self, id: ProductId) -> Self {
self.id = id;
self
}
pub fn owned_by(mut self, owner: AccountId) -> Self {
self.owner = owner;
self
}
pub fn with_props(mut self, props: Option<Vec<ProductProperty>>) -> Self {
self.props = props;
self
}
pub fn registered_on(mut self, registered: Moment) -> Self {
self.registered = registered;
self
}
pub fn build(self) -> Product<AccountId, Moment> {
Product::<AccountId, Moment> {
id: self.id,
owner: self.owner,
props: self.props,
registered: self.registered,
}
}
}