-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
288 lines (233 loc) · 8.65 KB
/
lib.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#![allow(unexpected_cfgs)]
#[ink::contract]
pub mod catalog {
use codec::{Decode, Encode};
use ink::{
env::{
hash::{HashOutput, Keccak256},
hash_bytes, DefaultEnvironment, Environment as InkEnv,
},
prelude::{vec, vec::Vec},
storage::Mapping,
};
pub type HashId = <Keccak256 as HashOutput>::Type;
type Workers = Mapping<AccountId, u32>;
type JobMetaData = Mapping<AccountId, Vec<(HashId, <DefaultEnvironment as InkEnv>::Balance)>>;
#[derive(Debug, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum CatalogError {
AccountNotFoud,
FailedTransfer,
JobNotFound,
}
#[derive(Debug)]
#[ink(event)]
pub struct WorkerRegistered {
pub who: AccountId,
pub val: u32,
}
#[derive(Debug)]
#[ink(event)]
pub struct JobRequestSubmitted {
pub who: AccountId,
pub id: HashId,
}
impl JobRequestSubmitted {
pub fn id(&self) -> HashId {
self.id
}
}
#[derive(Debug)]
#[ink(event)]
pub struct PaidWorker {
pub destination: AccountId,
}
#[derive(Debug, Encode, Decode, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct JobRequest {
id: HashId,
}
impl JobRequest {
pub fn new(code: &[u8], params: &Vec<Vec<u8>>) -> Self {
let id = Self::hash(code, params);
Self { id }
}
pub fn id(&self) -> HashId {
self.id
}
pub fn hash(code: &[u8], params: &Vec<Vec<u8>>) -> HashId {
code.encode_to(&mut params.encode());
hash(code)
}
}
#[ink(storage)]
pub struct Catalog {
workers: Workers,
job_metadata: JobMetaData,
}
impl Default for Catalog {
fn default() -> Self {
Self::new()
}
}
impl Catalog {
#[ink(constructor)]
pub fn new() -> Self {
Self {
workers: Mapping::new(),
job_metadata: Mapping::new(),
}
}
#[ink(message)]
pub fn get_worker(&self) -> u32 {
let caller = self.env().caller();
self.workers.get(caller).unwrap_or(0)
}
#[ink(message)]
pub fn register_worker(&mut self, val: u32) {
let caller = self.env().caller();
self.workers.insert(caller, &val);
self.env().emit_event(WorkerRegistered { who: caller, val });
}
#[ink(message, payable)]
pub fn submit_job_request(&mut self, job_request: JobRequest) {
let who = self.env().caller();
let id = job_request.id();
let value = self.env().transferred_value();
let metadatas = if let Some(mut metadatas) = self.job_metadata.get(who) {
metadatas.push((id, value));
metadatas
} else {
vec![(id, value)]
};
self.job_metadata.insert(who, &metadatas);
self.env().emit_event(JobRequestSubmitted { who, id });
}
#[ink(message)]
pub fn pay_worker(
&mut self,
destination: AccountId,
job_id: HashId,
) -> Result<(), CatalogError> {
let caller = self.env().caller();
let mut who = self
.job_metadata
.get(caller)
.ok_or(CatalogError::AccountNotFoud)?;
let value = who.iter().find(|(id, _)| id == &job_id).map(|(_, val)| val);
if let Some(val) = value {
self.env()
.transfer(destination, *val)
.map_err(|_| CatalogError::FailedTransfer)?;
who.retain(|(id, _)| id != &job_id);
self.job_metadata.insert(caller, &who);
self.env().emit_event(PaidWorker { destination });
} else {
return Err(CatalogError::JobNotFound);
}
Ok(())
}
}
fn hash(data: &[u8]) -> HashId {
let mut output = HashId::default();
hash_bytes::<Keccak256>(data, &mut output);
output
}
#[cfg(test)]
mod tests {
use super::*;
use ink::{
env::{
pay_with_call,
test::{recorded_events, EmittedEvent},
},
primitives::AccountId,
scale::Decode,
};
impl JobRequest {
fn test(code: Vec<u8>) -> Self {
let params: Vec<Vec<u8>> = vec![];
Self::new(code.as_slice(), ¶ms)
}
}
#[ink::test]
fn register_worker_emits_event() {
let val = 10;
let who = AccountId::from([1; 32]);
let mut catalog = Catalog::default();
catalog.register_worker(val);
let emitted_events = recorded_events().collect::<Vec<EmittedEvent>>();
let worker_set_event =
<WorkerRegistered as Decode>::decode(&mut emitted_events[0].data.as_slice())
.unwrap();
assert_eq!(emitted_events.len(), 1);
assert_eq!(worker_set_event.val, val);
assert_eq!(worker_set_event.who, who);
}
#[ink::test]
fn submit_job_request_emits_event() {
let who = AccountId::from([1; 32]);
let mut catalog = Catalog::default();
let code = vec![1, 2, 3, 4];
let value = 100;
let job_request = JobRequest::test(code.clone());
pay_with_call!(catalog.submit_job_request(job_request.clone()), value);
let emitted_events = recorded_events().collect::<Vec<EmittedEvent>>();
let job_submitted_event =
<JobRequestSubmitted as Decode>::decode(&mut emitted_events[0].data.as_slice())
.unwrap();
let metadatas = catalog.job_metadata.get(who).unwrap();
assert_eq!(job_submitted_event.who, who);
assert_eq!(job_submitted_event.id, job_request.id());
assert_eq!(metadatas[0].0, job_request.id());
assert_eq!(metadatas[0].1, value);
}
#[ink::test]
fn submit_job_request_appends_to_existing_jobs() {
let who = AccountId::from([1; 32]);
let mut catalog = Catalog::default();
let value = 100;
let code_1 = vec![1, 2, 3, 4];
let code_2 = vec![1, 2, 3, 5];
let job_1_request = JobRequest::test(code_1.clone());
let job_2_request = JobRequest::test(code_2.clone());
pay_with_call!(catalog.submit_job_request(job_1_request.clone()), value);
pay_with_call!(catalog.submit_job_request(job_2_request.clone()), value);
let emitted_events = recorded_events().collect::<Vec<EmittedEvent>>();
let job_submitted_event_1 =
<JobRequestSubmitted as Decode>::decode(&mut emitted_events[0].data.as_slice())
.unwrap();
let job_submitted_event_2 =
<JobRequestSubmitted as Decode>::decode(&mut emitted_events[1].data.as_slice())
.unwrap();
let jobs = catalog.job_metadata.get(who).unwrap();
assert_eq!(job_submitted_event_1.id, job_1_request.id());
assert_eq!(job_submitted_event_2.id, job_2_request.id());
assert_eq!(jobs.len(), 2);
assert_eq!(jobs[0].0, job_1_request.id());
assert_eq!(jobs[0].1, value);
assert_eq!(jobs[1].0, job_2_request.id());
assert_eq!(jobs[1].1, value);
}
#[ink::test]
fn pay_worker_emits_event_and_updates_storage() {
let requester = AccountId::from([1; 32]);
let worker = AccountId::from([3; 32]);
let mut catalog = Catalog::default();
let job_request = JobRequest::test(vec![1, 2, 3, 4]);
let value = 100;
let job_id = job_request.id();
catalog
.job_metadata
.insert(requester, &vec![(job_id, value)]);
catalog.pay_worker(worker, job_id).unwrap();
let metadata_after = catalog.job_metadata.get(requester).unwrap();
let emitted_events = recorded_events().collect::<Vec<EmittedEvent>>();
let paid_event =
<PaidWorker as Decode>::decode(&mut emitted_events[0].data.as_slice()).unwrap();
assert_eq!(paid_event.destination, worker);
assert!(metadata_after.len() == 0);
}
}
}