-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmod.rs
239 lines (206 loc) · 7.19 KB
/
mod.rs
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
use crate::catalogs::traits::RmrkCatalog;
use errors::{Error, Result};
use resources::{ComposedResource, PartId, Resource, ResourceId};
use sails_rs::{
calls::Query,
collections::HashMap,
gstd::{service, ExecContext},
prelude::*,
};
pub mod errors;
pub mod resources;
// Fully hidden service state
static mut RESOURCE_STORAGE_DATA: Option<ResourceStorageData> = None;
static mut RESOURCE_STORAGE_ADMIN: Option<ActorId> = None;
#[derive(Default)]
struct ResourceStorageData {
resources: HashMap<ResourceId, Resource>,
}
// Service event type definition
#[derive(TypeInfo, Encode)]
#[codec(crate = sails_rs::scale_codec)]
#[scale_info(crate = sails_rs::scale_info)]
pub enum ResourceStorageEvent {
ResourceAdded {
resource_id: ResourceId,
},
PartAdded {
resource_id: ResourceId,
part_id: PartId,
},
}
pub struct ResourceStorage<TExecContext, TCatalogClient> {
exec_context: TExecContext,
catalog_client: TCatalogClient,
}
// Declare the service can emit events of type ResourceStorageEvent
#[service(events = ResourceStorageEvent)]
impl<TExecContext, TCatalogClient> ResourceStorage<TExecContext, TCatalogClient>
where
TExecContext: ExecContext,
TCatalogClient: RmrkCatalog,
{
// This function needs to be called before any other function
pub fn seed(exec_context: TExecContext) {
unsafe {
RESOURCE_STORAGE_DATA = Some(ResourceStorageData::default());
RESOURCE_STORAGE_ADMIN = Some(exec_context.actor_id());
}
}
pub fn new(exec_context: TExecContext, catalog_client: TCatalogClient) -> Self {
Self {
exec_context,
catalog_client,
}
}
pub fn add_resource_entry(
&mut self,
resource_id: ResourceId,
resource: Resource,
) -> Result<(ResourceId, Resource)> {
self.require_admin()?;
if resource_id == 0 {
return Err(Error::ZeroResourceId);
}
if self
.data_mut()
.resources
.insert(resource_id, resource.clone())
.is_some()
{
return Err(Error::ResourceAlreadyExists);
}
// Emit event right before the method returns via
// the generated `notify_on` method
self.notify_on(ResourceStorageEvent::ResourceAdded { resource_id })
.unwrap();
Ok((resource_id, resource))
}
pub async fn add_part_to_resource(
&mut self,
resource_id: ResourceId,
part_id: PartId,
) -> Result<PartId> {
self.require_admin()?;
let resource = self
.data_mut()
.resources
.get_mut(&resource_id)
.ok_or(Error::ResourceNotFound)?;
if let Resource::Composed(ComposedResource { base, parts, .. }) = resource {
// Caution: The execution of this method pauses right after the call to `recv` method due to
// its asynchronous nature , and all changes made to the state are saved, i.e. if we
// modify the `resource` variable here, the new value will be available to the other
// calls of this or another method (e.g. `add_resource_entry`) working with the same
// data before this method returns.
// Call `Rmrk Catalog` via the generated client and the `recv` method
let part = self.catalog_client.part(part_id).recv(*base).await.unwrap();
// Caution: Reading from the `resource` variable here may yield unexpected value.
// This can happen because execution after asynchronous calls can resume
// after a number of blocks, and the `resources` map can be modified by that time
// by a call of this or another method (e.g. `add_resource_entry`) working
// with the same data.
if part.is_none() {
return Err(Error::PartNotFound);
}
parts.push(part_id);
} else {
return Err(Error::WrongResourceType);
}
// Emit event right before the method returns via
// the generated `notify_on` method
self.notify_on(ResourceStorageEvent::PartAdded {
resource_id,
part_id,
})
.unwrap();
Ok(part_id)
}
pub fn resource(&self, resource_id: ResourceId) -> Result<Resource> {
self.data()
.resources
.get(&resource_id)
.cloned()
.ok_or(Error::ResourceNotFound)
}
fn require_admin(&self) -> Result<()> {
if self.exec_context.actor_id() != resource_storage_admin() {
return Err(Error::NotAuthorized);
}
Ok(())
}
#[allow(static_mut_refs)]
fn data(&self) -> &'static ResourceStorageData {
unsafe { RESOURCE_STORAGE_DATA.as_ref().unwrap() }
}
#[allow(static_mut_refs)]
fn data_mut(&mut self) -> &'static mut ResourceStorageData {
unsafe { RESOURCE_STORAGE_DATA.as_mut().unwrap() }
}
}
fn resource_storage_admin() -> ActorId {
unsafe { RESOURCE_STORAGE_ADMIN.unwrap() }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::catalogs::{mockall::MockRmrkCatalog, FixedPart, Part};
use resources::ComposedResource;
use sails_rs::{gstd::calls::GStdArgs, mockall::MockQuery, ActorId};
#[tokio::test]
async fn test_add_resource_entry() {
ResourceStorage::<ExecContextMock, MockRmrkCatalog<GStdArgs>>::seed(ExecContextMock {
actor_id: 1.into(),
message_id: 1.into(),
});
let mut resource_storage = ResourceStorage::new(
ExecContextMock {
actor_id: 1.into(),
message_id: 1.into(),
},
MockRmrkCatalog::<GStdArgs>::new(),
);
let resource = Resource::Composed(ComposedResource {
src: "src".to_string(),
thumb: "thumb".to_string(),
metadata_uri: "metadata_uri".to_string(),
base: 1.into(),
parts: vec![],
});
let (actual_resource_id, actual_resource) = resource_storage
.add_resource_entry(1, resource.clone())
.unwrap();
assert_eq!(actual_resource_id, 1);
assert_eq!(actual_resource, resource);
// add_part_to_resource
let mut part_query = MockQuery::new();
part_query.expect_recv().returning(move |_| {
Ok(Some(Part::Fixed(FixedPart {
z: None,
metadata_uri: "metadata_uri".to_string(),
})))
});
resource_storage
.catalog_client
.expect_part()
.with(mockall::predicate::eq(1))
.return_once(|_| part_query);
let actual_part_id = resource_storage
.add_part_to_resource(actual_resource_id, 1)
.await
.unwrap();
assert_eq!(actual_part_id, 1);
}
struct ExecContextMock {
actor_id: ActorId,
message_id: MessageId,
}
impl ExecContext for ExecContextMock {
fn actor_id(&self) -> ActorId {
self.actor_id
}
fn message_id(&self) -> MessageId {
self.message_id
}
}
}