forked from AleoHQ/aleo-setup
/
rest.rs
422 lines (362 loc) · 16.1 KB
/
rest.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
//! REST API endpoints exposed by the [Coordinator](`crate::Coordinator`).
use std::{
collections::{HashMap, HashSet},
io::{Cursor, Read, Write},
};
use tracing::warn;
use crate::{
objects::{ContributionInfo, LockedLocators},
rest_utils::{
self, ContributorStatus, Coordinator, CurrentContributor, LazyJson, NewParticipant, PostChunkRequest,
ResponseError, Result, Secret, ServerAuth, HEALTH_PATH, TOKENS_PATH, TOKENS_ZIP_FILE,
},
s3::S3Ctx,
storage::{Locator, Object},
CoordinatorState, Participant,
};
use rocket::{
get, post,
serde::json::Json,
tokio::{fs, task},
Shutdown, State,
};
use url::Url;
/// Add the incoming contributor to the queue of contributors.
#[post("/contributor/join_queue", format = "json", data = "<token>")]
pub async fn join_queue(
coordinator: &State<Coordinator>,
new_participant: NewParticipant,
token: LazyJson<String>,
) -> Result<Json<u64>> {
// NOTE: check on the token happens only here meaning that a contributor can join the ceremony at the very last moment of a cohort and
// contribute effectively in the following cohort. Forcing the contribution to happen in the correct cohort would take more complicated checks
// and could lower the amount of contributions received
let cohort = rest_utils::token_check((*coordinator).clone(), token.as_str()).await?;
let mut write_lock = (*coordinator).clone().write_owned().await;
task::spawn_blocking(move || {
write_lock.add_to_queue(
new_participant.participant,
new_participant.ip_address,
token.clone(),
10,
)
})
.await?
.map_err(|e| ResponseError::CoordinatorError(e))?;
Ok(Json(cohort))
}
/// Lock a [Chunk](`crate::objects::Chunk`) in the ceremony. This should be the first function called when attempting to contribute to a chunk. Once the chunk is locked, it is ready to be downloaded.
#[get("/contributor/lock_chunk", format = "json")]
pub async fn lock_chunk(
coordinator: &State<Coordinator>,
participant: CurrentContributor,
) -> Result<Json<LockedLocators>> {
let mut write_lock = (*coordinator).clone().write_owned().await;
match task::spawn_blocking(move || write_lock.try_lock(&participant)).await? {
Ok((_, locked_locators)) => Ok(Json(locked_locators)),
Err(e) => Err(ResponseError::CoordinatorError(e)),
}
}
/// Get the challenge key on Amazon S3 from the [Coordinator](`crate::Coordinator`).
#[post("/contributor/challenge", format = "json", data = "<round_height>")]
pub async fn get_challenge_url(
coordinator: &State<Coordinator>,
_participant: CurrentContributor,
round_height: LazyJson<u64>,
) -> Result<Json<String>> {
let s3_ctx = S3Ctx::new().await?;
let key = format!("round_{}/chunk_0/contribution_0.verified", *round_height);
// If challenge is already on S3 (round rollback) immediately return the key
if let Some(url) = s3_ctx.get_challenge_url(key.clone()).await {
return Ok(Json(url));
}
// Since we don't chunk the parameters, we have one chunk and one allowed contributor per round. Thus the challenge will always be located at round_{i}/chunk_0/contribution_0.verified
// For example, the 1st challenge (after the initialization) is located at round_1/chunk_0/contribution_0.verified
let read_lock = (*coordinator).clone().read_owned().await;
let challenge = match task::spawn_blocking(move || read_lock.get_challenge(*round_height, 0, 0, true)).await? {
Ok(challenge) => challenge,
Err(e) => return Err(ResponseError::CoordinatorError(e)),
};
// Upload challenge to S3 and return url
let url = s3_ctx.upload_challenge(key, challenge).await?;
Ok(Json(url))
}
/// Request the urls where to upload a [Chunk](`crate::objects::Chunk`) contribution and the ContributionFileSignature.
#[post("/upload/chunk", format = "json", data = "<round_height>")]
pub async fn get_contribution_url(
_participant: CurrentContributor,
round_height: LazyJson<u64>,
) -> Result<Json<(String, String)>> {
let contrib_key = format!("round_{}/chunk_0/contribution_1.unverified", *round_height);
let contrib_sig_key = format!("round_{}/chunk_0/contribution_1.unverified.signature", *round_height);
// Prepare urls for the upload
let s3_ctx = S3Ctx::new().await?;
let urls = s3_ctx.get_contribution_urls(contrib_key, contrib_sig_key);
Ok(Json(urls))
}
/// Notify the [Coordinator](`crate::Coordinator`) of a finished and uploaded [Contribution](`crate::objects::Contribution`). This will unlock the given [Chunk](`crate::objects::Chunk`).
#[post(
"/contributor/contribute_chunk",
format = "json",
data = "<contribute_chunk_request>"
)]
pub async fn contribute_chunk(
coordinator: &State<Coordinator>,
participant: CurrentContributor,
contribute_chunk_request: LazyJson<PostChunkRequest>,
) -> Result<()> {
// Download contribution and its signature from S3 to local disk from the provided Urls
let s3_ctx = S3Ctx::new().await?;
let (contribution, contribution_sig) = s3_ctx.get_contribution(contribute_chunk_request.round_height).await?;
let mut write_lock = (*coordinator).clone().write_owned().await;
task::spawn_blocking(move || {
write_lock.write_contribution(contribute_chunk_request.contribution_locator, contribution)?;
write_lock.write_contribution_file_signature(
contribute_chunk_request.contribution_signature_locator,
serde_json::from_slice(&contribution_sig)?,
)?;
write_lock.try_contribute(&participant, 0) // Only 1 chunk per round, chunk_id is always 0
})
.await?
.map_or_else(|e| Err(ResponseError::CoordinatorError(e)), |_| Ok(()))
}
/// Update the [Coordinator](`crate::Coordinator`) state. This endpoint is accessible only by the coordinator itself.
#[cfg(debug_assertions)]
#[get("/update")]
pub async fn update_coordinator(coordinator: &State<Coordinator>, _auth: ServerAuth) -> Result<()> {
rest_utils::perform_coordinator_update((*coordinator).clone()).await
}
/// Let the [Coordinator](`crate::Coordinator`) know that the participant is still alive and participating (or waiting to participate) in the ceremony.
#[post("/contributor/heartbeat")]
pub async fn heartbeat(coordinator: &State<Coordinator>, participant: Participant) -> Result<()> {
coordinator
.write()
.await
.heartbeat(&participant)
.map_err(|e| ResponseError::CoordinatorError(e))
}
/// Stop the [Coordinator](`crate::Coordinator`) and shuts the rest server down. This endpoint is accessible only by the coordinator itself.
#[get("/stop")]
pub async fn stop_coordinator(_auth: ServerAuth, shutdown: Shutdown) {
// Shut Rocket server down
shutdown.notify();
}
/// Verify all the pending contributions. This endpoint is accessible only by the coordinator itself.
#[cfg(debug_assertions)]
#[get("/verify")]
pub async fn verify_chunks(coordinator: &State<Coordinator>, _auth: ServerAuth) -> Result<()> {
rest_utils::perform_verify_chunks((*coordinator).clone(), &S3Ctx::new().await?).await
}
/// Load new tokens to update the future cohorts. The `tokens` parameter is the serialized zip folder
#[post("/update_cohorts", format = "json", data = "<tokens>")]
pub async fn update_cohorts(
coordinator: &State<Coordinator>,
_auth: ServerAuth,
tokens: LazyJson<Vec<u8>>,
) -> Result<()> {
let reader = Cursor::new(tokens.clone());
let mut zip = zip::ZipArchive::new(reader).map_err(|e| ResponseError::IoError(e.to_string()))?;
let mut zip_clone = zip.clone();
let new_tokens = task::spawn_blocking(move || -> Result<Vec<HashSet<String>>> {
let mut cohorts: HashMap<String, Vec<u8>> = HashMap::new();
let file_names: Vec<String> = zip_clone.file_names().map(|name| name.to_owned()).collect();
for file in file_names {
let mut buffer = Vec::new();
zip_clone
.by_name(file.as_str())
.map_err(|e| ResponseError::IoError(e.to_string()))?
.read_to_end(&mut buffer)
.map_err(|e| ResponseError::IoError(e.to_string()))?;
cohorts.insert(file, buffer);
}
// NOTE: tokens currently in use are still in memory, no need to take care of those
Ok(CoordinatorState::load_tokens_from_bytes(&cohorts))
})
.await
.unwrap()?;
// Check that the new tokens for the current cohort match the old ones (to prevent inconsistencies during contributions in the current cohort)
let read_lock = coordinator.read().await;
let cohort = read_lock.state().get_current_cohort_index();
let old_tokens = match read_lock.state().tokens(cohort) {
Some(t) => t,
None => return Err(ResponseError::CeremonyIsOver),
};
match new_tokens.get(cohort) {
Some(new_tokens) => {
if new_tokens != old_tokens {
return Err(ResponseError::InvalidNewTokens);
}
}
_ => return Err(ResponseError::InvalidNewTokens),
}
drop(read_lock);
// Persist new tokens to disk
// New tokens MUST be written to file in case of a coordinator restart
task::spawn_blocking(move || -> Result<()> {
let mut zip_file = std::fs::File::options()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(TOKENS_ZIP_FILE)
.map_err(|e| ResponseError::IoError(e.to_string()))?;
zip_file
.write_all(&tokens)
.map_err(|e| ResponseError::IoError(e.to_string()))?;
if let Err(e) = std::fs::remove_dir_all(&*TOKENS_PATH) {
// Log the error and continue
warn!("Error while removing old tokens folder: {}", e);
}
zip.extract(&*TOKENS_PATH)
.map_err(|e| ResponseError::IoError(e.to_string()))?;
Ok(())
})
.await
.unwrap()?;
// Update cohorts in coordinator's state
coordinator.write().await.update_tokens(new_tokens);
Ok(())
}
/// Get the queue status of the contributor.
#[get("/contributor/queue_status", format = "json")]
pub async fn get_contributor_queue_status(
coordinator: &State<Coordinator>,
participant: Participant,
) -> Json<ContributorStatus> {
let contributor = participant.clone();
let read_lock = (*coordinator).clone().read_owned().await;
// Check that the contributor is authorized to lock a chunk in the current round.
if task::spawn_blocking(move || read_lock.is_current_contributor(&contributor))
.await
.unwrap()
{
return Json(ContributorStatus::Round);
}
let read_lock = coordinator.read().await;
if read_lock.is_queue_contributor(&participant) {
let queue_size = read_lock.number_of_queue_contributors() as u64;
let queue_position = match read_lock.state().queue_contributor_info(&participant) {
Some((_, Some(round), _, _)) => round - read_lock.state().current_round_height(),
Some((_, None, _, _)) => queue_size,
None => return Json(ContributorStatus::Other),
};
return Json(ContributorStatus::Queue(queue_position, queue_size));
}
if read_lock.is_finished_contributor(&participant) {
return Json(ContributorStatus::Finished);
}
if read_lock.is_banned_participant(&participant) {
return Json(ContributorStatus::Banned);
}
// Not in the queue, not finished, nor in the current round
Json(ContributorStatus::Other)
}
/// Write [`ContributionInfo`] to disk
#[post("/contributor/contribution_info", format = "json", data = "<request>")]
pub async fn post_contribution_info(
coordinator: &State<Coordinator>,
participant: CurrentContributor,
request: LazyJson<ContributionInfo>,
) -> Result<()> {
// Validate info
if request.public_key != participant.address() {
return Err(ResponseError::InvalidContributionInfo(format!(
"Public key in info {} doesnt' match the participant one {}",
request.public_key,
participant.address()
)));
}
let current_round_height = match coordinator.read().await.current_round_height() {
Ok(r) => r,
Err(e) => return Err(ResponseError::CoordinatorError(e)),
};
if current_round_height != request.ceremony_round {
// NOTE: validation of round_height matters in case of a round rollback
return Err(ResponseError::InvalidContributionInfo(format!(
"Round height in info {} doesnt' match the current round height {}",
request.ceremony_round, current_round_height
)));
}
// Write contribution info and summary to file
let mut write_lock = (*coordinator).clone().write_owned().await;
task::spawn_blocking(move || {
write_lock.write_contribution_info(request.clone())?;
write_lock.update_contribution_summary(request.0.into())
})
.await?
.map_err(|e| ResponseError::CoordinatorError(e))
}
/// Uploads the attestation for a contribution
#[post("/contributor/attestation", format = "json", data = "<request>")]
pub async fn post_attestation(
coordinator: &State<Coordinator>,
participant: Participant,
request: LazyJson<(u64, String)>,
) -> Result<()> {
let (round, attestation) = request.0;
// Check url format
if let Err(e) = Url::parse(attestation.as_str()) {
return Err(ResponseError::IoError(e.to_string()));
}
let read_lock = (*coordinator).clone().read_owned().await;
task::spawn_blocking(move || {
if !read_lock.is_finished_contributor_at_round(&participant, round) {
// Only finished contributors are allowed to query this endpoint
return Err(ResponseError::UnauthorizedParticipant(
participant,
"/contributor/attestation".to_string(),
"Not a current nor finished contributor".to_string(),
));
}
// Check the provided round height matches the signing participant
match read_lock
.storage()
.get(&Locator::ContributionInfoFile { round_height: round })
.map_err(|e| ResponseError::CoordinatorError(e))?
{
Object::ContributionInfoFile(f) => {
if f.public_key == participant.address() {
Ok(())
} else {
Err(ResponseError::CoordinatorError(
crate::CoordinatorError::ParticipantRoundHeightInvalid,
))
}
}
_ => Err(ResponseError::CoordinatorError(crate::CoordinatorError::StorageFailed)),
}
})
.await??;
// Update the contribution info and the summary with the attestation
let mut write_lock = (*coordinator).clone().write_owned().await;
task::spawn_blocking(move || write_lock.update_contribution_info_attestation(round, attestation))
.await?
.map_err(|e| ResponseError::CoordinatorError(e))
}
/// Retrieve the contributions' info. This endpoint is accessible by anyone and does not require a signed request.
#[cfg(debug_assertions)]
#[get("/contribution_info")]
pub async fn get_contributions_info(coordinator: &State<Coordinator>) -> Result<Vec<u8>> {
let read_lock = (*coordinator).clone().read_owned().await;
let summary = task::spawn_blocking(move || read_lock.storage().get_contributions_summary())
.await?
.map_err(|e| ResponseError::CoordinatorError(e))?;
Ok(summary)
}
/// Retrieve the coordinator.json status file
#[get("/coordinator_status")]
pub async fn get_coordinator_state(coordinator: &State<Coordinator>, _auth: Secret) -> Result<Vec<u8>> {
let read_lock = (*coordinator).clone().read_owned().await;
let state = task::spawn_blocking(move || read_lock.storage().get_coordinator_state())
.await?
.map_err(|e| ResponseError::CoordinatorError(e))?;
Ok(state)
}
/// Retrieve healthcheck info. This endpoint is accessible by anyone and does not require a signed request.
#[get("/healthcheck", format = "json")]
pub async fn get_healthcheck() -> Result<String> {
let content = fs::read_to_string(HEALTH_PATH.as_str())
.await
.map_err(|e| ResponseError::IoError(e.to_string()))?;
Ok(content)
}