-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
lib.rs
422 lines (395 loc) · 15.9 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use chrono::{DateTime, Duration, Utc};
use ed25519_dalek::{Keypair, Signer};
use hashbrown::{HashMap, HashSet};
use locutus_aft_interface::{AllocationCriteria, TokenAllocationRecord, TokenAssignment};
use locutus_stdlib::{prelude::*, time};
use serde::{Deserialize, Serialize};
#[cfg(test)]
mod tests;
type Assignee = ed25519_dalek::PublicKey;
type AssignmentHash = [u8; 32];
struct TokenComponent;
impl ComponentInterface for TokenComponent {
fn process(message: InboundComponentMsg) -> Result<Vec<OutboundComponentMsg>, ComponentError> {
match message {
InboundComponentMsg::ApplicationMessage(ApplicationMessage {
app,
payload,
context,
processed,
..
}) => {
if processed {
return Err(ComponentError::Other(
"cannot process an already processed message".into(),
));
}
let mut context = Context::try_from(context)?;
let msg = TokenComponentMessage::try_from(&*payload)?;
match msg {
TokenComponentMessage::RequestNewToken(req) => {
allocate_token(&mut context, app, req)
}
TokenComponentMessage::Failure { .. } => Err(ComponentError::Other(
"unexpected message type: failure".into(),
)),
TokenComponentMessage::AllocatedToken { .. } => Err(ComponentError::Other(
"unexpected message type: allocated token".into(),
)),
}
}
InboundComponentMsg::UserResponse(UserInputResponse {
request_id,
response,
context,
}) => {
let mut context = Context::try_from(context)?;
context.waiting_for_user_input.remove(&request_id);
let response = serde_json::from_slice(&response)
.map_err(|err| ComponentError::Deser(format!("{err}")))?;
context.user_response.insert(request_id, response);
let context: ComponentContext = (&context).try_into()?;
Ok(vec![OutboundComponentMsg::ContextUpdated(context)])
}
InboundComponentMsg::GetSecretResponse(GetSecretResponse {
key,
value,
context,
}) => {
let mut context = Context::try_from(context)?;
let secret = value.ok_or_else(|| {
ComponentError::Other(format!("secret not found for key: {key}"))
})?;
let keypair = Keypair::from_bytes(&secret)
.map_err(|err| ComponentError::Deser(format!("{err}")))?;
context.key_pair = Some(keypair);
let context: ComponentContext = (&context).try_into()?;
Ok(vec![OutboundComponentMsg::ContextUpdated(context)])
}
InboundComponentMsg::RandomBytes(_) => Err(ComponentError::Other(
"unexpected message type: radom bytes".into(),
)),
}
}
}
const RESPONSES: &[&str] = &["true", "false"];
fn user_input(criteria: &AllocationCriteria, assignee: &Assignee) -> NotificationMessage<'static> {
let assignee = bs58::encode(assignee).into_string();
let notification_json = serde_json::json!({
"user": assignee,
"token": {
"tier": criteria.frequency,
"max_age": format!("{} seconds", criteria.max_age.as_secs())
}
});
NotificationMessage::try_from(¬ification_json).unwrap()
}
fn allocate_token(
context: &mut Context,
app: ContractInstanceId,
RequestNewToken {
request_id,
component_id,
criteria,
mut records,
assignee,
assignment_hash,
}: RequestNewToken,
) -> Result<Vec<OutboundComponentMsg>, ComponentError> {
let request = context
.waiting_for_user_input
.iter()
.find(|p| **p == request_id)
.copied();
let response = context.user_response.get(&request_id);
match (&context.key_pair, request, response) {
(None, _, _) => {
// lacks keys; ask for keys
let context: ComponentContext = (&*context).try_into()?;
let request_secret = {
GetSecretRequest {
key: component_id.clone(),
context: context.clone(),
processed: false,
}
.into()
};
let req_allocation = {
let msg = TokenComponentMessage::RequestNewToken(RequestNewToken {
request_id,
component_id,
criteria,
records,
assignee,
assignment_hash,
});
OutboundComponentMsg::ApplicationMessage(
ApplicationMessage::new(app, msg.serialize()?, false).with_context(context),
)
};
Ok(vec![request_secret, req_allocation])
}
(Some(_), None, None) => {
// request user input and add to waiting queue
context.waiting_for_user_input.insert(request_id);
let context: ComponentContext = (&*context).try_into()?;
let message = user_input(&criteria, &assignee);
let req_allocation = {
let msg = TokenComponentMessage::RequestNewToken(RequestNewToken {
request_id,
component_id,
criteria,
records,
assignee,
assignment_hash,
});
OutboundComponentMsg::ApplicationMessage(
ApplicationMessage::new(app, msg.serialize()?, false).with_context(context),
)
};
let request_user_input = OutboundComponentMsg::RequestUserInput(UserInputRequest {
request_id,
responses: RESPONSES
.iter()
.map(|s| ClientResponse::new(s.as_bytes().to_vec()))
.collect(),
message,
});
Ok(vec![request_user_input, req_allocation])
}
(Some(_), Some(_), _) => {
// waiting for response
let context: ComponentContext = (&*context).try_into()?;
let req_allocation = {
let msg = TokenComponentMessage::RequestNewToken(RequestNewToken {
request_id,
component_id,
criteria,
records,
assignee,
assignment_hash,
});
OutboundComponentMsg::ApplicationMessage(
ApplicationMessage::new(app, msg.serialize()?, false).with_context(context),
)
};
Ok(vec![req_allocation])
}
(Some(keypair), _, Some(response)) => {
// got response, check if allocation is allowed and return to application
let application_response = match response {
Response::Allowed => {
let context: ComponentContext = (&*context).try_into()?;
let Some(assignment) = records.assign(assignee, &criteria, keypair, assignment_hash) else {
let msg = TokenComponentMessage::Failure(FailureReason::NoFreeSlot { component_id, criteria } );
return Ok(vec![OutboundComponentMsg::ApplicationMessage(
ApplicationMessage::new(app, msg.serialize()?, true).with_context(context),
)]);
};
let msg = TokenComponentMessage::AllocatedToken {
component_id,
assignment,
records,
};
OutboundComponentMsg::ApplicationMessage(
ApplicationMessage::new(app, msg.serialize()?, true).with_context(context),
)
}
Response::NotAllowed => {
let context: ComponentContext = (&*context).try_into()?;
let msg = TokenComponentMessage::Failure(FailureReason::UserPermissionDenied);
OutboundComponentMsg::ApplicationMessage(
ApplicationMessage::new(app, msg.serialize()?, true).with_context(context),
)
}
};
context.user_response.remove(&request_id);
Ok(vec![application_response])
}
}
}
#[derive(Debug, Serialize, Deserialize)]
enum TokenComponentMessage {
RequestNewToken(RequestNewToken),
AllocatedToken {
component_id: SecretsId,
assignment: TokenAssignment,
/// An updated version of the record with the newly allocated token included
records: TokenAllocationRecord,
},
Failure(FailureReason),
}
impl TokenComponentMessage {
fn serialize(self) -> Result<Vec<u8>, ComponentError> {
bincode::serialize(&self).map_err(|err| ComponentError::Deser(format!("{err}")))
}
}
#[derive(Debug, Serialize, Deserialize)]
enum FailureReason {
/// The user didn't accept to allocate the tokens.
UserPermissionDenied,
/// No free slot to allocate with the requested criteria
NoFreeSlot {
component_id: SecretsId,
criteria: AllocationCriteria,
},
}
#[derive(Debug, Serialize, Deserialize)]
struct RequestNewToken {
request_id: u32,
component_id: SecretsId,
criteria: AllocationCriteria,
records: TokenAllocationRecord,
assignee: Assignee,
assignment_hash: AssignmentHash,
}
#[derive(Debug, Serialize, Deserialize, Default)]
struct Context {
waiting_for_user_input: HashSet<u32>,
user_response: HashMap<u32, Response>,
/// The token generator instance key pair (pub + secret key).
key_pair: Option<Keypair>,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum Response {
Allowed,
NotAllowed,
}
impl TryFrom<ComponentContext> for Context {
type Error = ComponentError;
fn try_from(value: ComponentContext) -> Result<Self, Self::Error> {
bincode::deserialize(&value.0).map_err(|err| ComponentError::Deser(format!("{err}")))
}
}
impl TryFrom<&Context> for ComponentContext {
type Error = ComponentError;
fn try_from(value: &Context) -> Result<Self, Self::Error> {
bincode::serialize(value)
.map(ComponentContext::new)
.map_err(|err| ComponentError::Deser(format!("{err}")))
}
}
impl TryFrom<&[u8]> for TokenComponentMessage {
type Error = ComponentError;
fn try_from(payload: &[u8]) -> Result<Self, Self::Error> {
bincode::deserialize(payload).map_err(|err| ComponentError::Deser(format!("{err}")))
}
}
/// This is used internally by the component to allocate new tokens on behave of the requesting client app.
///
/// Conflicting assignments for the same time slot are not permitted and indicate that the generator is broken or malicious.
trait TokenAssignmentInternal {
fn assign(
&mut self,
assignee: Assignee,
criteria: &AllocationCriteria,
private_key: &Keypair,
assignment_hash: AssignmentHash,
) -> Option<TokenAssignment>;
/// Given a datetime, get the newest free slot for this criteria.
fn next_free_assignment(
&self,
criteria: &AllocationCriteria,
current: DateTime<Utc>,
) -> Option<DateTime<Utc>>;
fn append_unchecked(&mut self, assignment: TokenAssignment);
}
impl TokenAssignmentInternal for TokenAllocationRecord {
/// Assigns the next theoretical free slot. This could be out of sync due to other concurrent requests so it may fail
/// to validate at the node. In that case the application should retry again, after refreshing the ledger version.
fn assign(
&mut self,
assignee: Assignee,
criteria: &AllocationCriteria,
keys: &Keypair,
assignment_hash: AssignmentHash,
) -> Option<TokenAssignment> {
let current = time::now();
let time_slot = self.next_free_assignment(criteria, current)?;
let assignment = {
let msg = TokenAssignment::to_be_signed(&time_slot, &assignee, criteria.frequency);
let signature = keys.sign(&msg);
TokenAssignment {
tier: criteria.frequency,
time_slot,
assignee,
signature,
assignment_hash,
token_record: criteria.contract,
}
};
self.append_unchecked(assignment.clone());
Some(assignment)
}
fn next_free_assignment(
&self,
criteria: &AllocationCriteria,
current: DateTime<Utc>,
) -> Option<DateTime<Utc>> {
let normalized = criteria.frequency.normalize_to_next(current);
let max_age = Duration::from_std(criteria.max_age).unwrap();
// todo: add optimization branch where we check if all slots have been allocated upfront
match self.get_tier(&criteria.frequency) {
Some(currently_assigned) => {
let mut oldest_valid_observed = None;
let mut first_valid = None;
for (_idx, assignment) in currently_assigned.iter().enumerate() {
// dbg!(
// oldest_valid_observed.map(|a: &TokenAssignment| a.time_slot),
// first_valid,
// assignment.time_slot
// );
if assignment.time_slot >= (normalized - max_age) {
match (oldest_valid_observed, first_valid) {
(None, None) => {
oldest_valid_observed = Some(assignment);
let oldest_valid = normalized - max_age;
if assignment.time_slot != oldest_valid {
first_valid = Some(oldest_valid);
} else {
let prev = assignment.previous_slot();
if prev > oldest_valid {
first_valid = Some(prev);
} else {
first_valid = Some(assignment.next_slot());
}
}
}
(Some(_), Some(first_valid_date)) => {
if first_valid_date == assignment.time_slot {
let next = assignment.next_slot();
if next > normalized {
first_valid = None;
} else {
first_valid = Some(next);
}
}
}
_ => unreachable!(),
}
}
}
if oldest_valid_observed.is_none() {
// first slot for the tier free
return Some(normalized - max_age);
}
if first_valid.is_some() {
return first_valid;
}
None
}
None => Some(normalized - max_age),
}
}
fn append_unchecked(&mut self, assignment: TokenAssignment) {
match self.get_mut_tier(&assignment.tier) {
Some(list) => {
list.push(assignment);
list.sort_unstable();
}
None => {
self.insert(assignment.tier, vec![assignment]);
}
}
}
}