-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathlib.rs
More file actions
548 lines (493 loc) · 18.6 KB
/
Copy pathlib.rs
File metadata and controls
548 lines (493 loc) · 18.6 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#![no_std]
//! # Sub Rosa — Round
//!
//! A reusable Soroban primitive for confidential commit → verifiable-reveal →
//! on-chain-settle coordination rounds. Bids are sealed with Drand timelock
//! encryption until a future round R that nobody controls; round R's threshold
//! signature is verified on-chain (BLS12-381) to force a simultaneous reveal.
//! The protocol — not the operator — owns fairness.
//!
//! No mocks, no fallbacks: every gate is a real on-chain check.
mod drand;
mod storage;
mod types;
use soroban_sdk::{
contract, contractimpl, symbol_short, token, Address, Bytes, BytesN, Env, Vec,
};
use storage::*;
use types::*;
const MAX_CIPHERTEXT: u32 = 4096;
const MAX_AUDITOR_BLOB: u32 = 2048;
const MAX_AUDITOR_PUBKEY: u32 = 1024;
/// Cap on distinct bidders per round so the persisted bidder index stays well
/// within the contract data-entry size ceiling (PRD §8).
const MAX_BIDDERS: u32 = 500;
/// Grace window (seconds) after the reveal deadline before a stuck round
/// (e.g. Drand never produced R) can be voided and all escrow refunded.
const VOID_GRACE: u64 = 3600;
/// Maximum page size for paginated getters. Prevents resource exhaustion.
const MAX_PAGE_SIZE: u32 = 100;
#[contract]
pub struct SubRosaRound;
#[contractimpl]
impl SubRosaRound {
/// One-time deploy configuration. All Drand parameters are supplied by the
/// deployer from values validated against a live quicknet round.
pub fn __constructor(
env: Env,
drand_pubkey: BytesN<192>,
g2_neg_generator: BytesN<192>,
dst: Bytes,
drand_genesis: u64,
drand_period: u64,
usdc: Address,
) {
if is_initialized(&env) {
panic_with(&env, Error::AlreadyInitialized);
}
let config = GlobalConfig {
drand_pubkey,
g2_neg_generator,
dst,
drand_genesis,
drand_period,
usdc,
};
set_config(&env, &config);
bump_instance(&env);
}
/// Open a new sealed round. Permissionless: anyone can be an operator, and
/// the operator gets no special read power — that is the point.
pub fn create_round(
env: Env,
operator: Address,
item_ref: BytesN<32>,
reveal_round: u64,
clearing_rule: ClearingRule,
commit_deadline: u64,
reveal_deadline: u64,
auditor_pubkey: Bytes,
) -> Result<u64, Error> {
operator.require_auth();
let config = get_config(&env)?;
bump_instance(&env);
if reveal_round == 0 {
return Err(Error::InvalidAmount);
}
if auditor_pubkey.len() > MAX_AUDITOR_PUBKEY {
return Err(Error::PayloadTooLarge);
}
let now = env.ledger().timestamp();
let t_reveal = drand::time_of_round(&config, reveal_round);
// Commit must close strictly before R is published, otherwise a bidder
// could decrypt others' sealed bids before committing.
if commit_deadline >= t_reveal {
return Err(Error::CommitDeadlineAfterReveal);
}
if reveal_deadline <= t_reveal {
return Err(Error::CommitDeadlineAfterReveal);
}
if commit_deadline <= now {
return Err(Error::DeadlineInPast);
}
let round_id = next_round_id(&env);
let round = Round {
operator: operator.clone(),
item_ref,
reveal_round,
clearing_rule,
commit_deadline,
reveal_deadline,
auditor_pubkey,
status: Status::Open,
bidders: Vec::new(&env),
winner: None,
winning_bid: 0,
};
set_round(&env, round_id, &round);
env.events().publish(
(symbol_short!("created"), round_id),
(operator, reveal_round, commit_deadline),
);
Ok(round_id)
}
/// Submit (or overwrite, before the deadline) a sealed bid and lock escrow.
///
/// - `commitment` H binds the bid; checked at reveal.
/// - `ciphertext` C is the timelock seal; guarantees forced reveal.
/// - `escrow` is a public USDC budget and an upper bound on the sealed bid;
/// locked now so the winner can always pay.
/// - `auditor_blob` is the bidder identity encrypted to the auditor key.
pub fn commit(
env: Env,
round_id: u64,
bidder: Address,
commitment: BytesN<32>,
ciphertext: Bytes,
escrow: i128,
auditor_blob: Bytes,
) -> Result<(), Error> {
bidder.require_auth();
let config = get_config(&env)?;
let mut round = get_round(&env, round_id)?;
if round.status == Status::Voided {
return Err(Error::RoundVoided);
}
if round.status == Status::Settled {
return Err(Error::AlreadySettled);
}
if round.status != Status::Open {
return Err(Error::WrongStatus);
}
if env.ledger().timestamp() > round.commit_deadline {
return Err(Error::CommitClosed);
}
if escrow <= 0 {
return Err(Error::InvalidAmount);
}
if ciphertext.len() > MAX_CIPHERTEXT || auditor_blob.len() > MAX_AUDITOR_BLOB {
return Err(Error::PayloadTooLarge);
}
let usdc = token::Client::new(&env, &config.usdc);
let contract = env.current_contract_address();
// Overwrite-before-close: refund the prior escrow, then re-lock the new
// amount. This keeps "one effective bid per bidder" while allowing edits.
match try_get_state(&env, round_id, &bidder) {
Some(prev) => {
if prev.escrow > 0 {
usdc.transfer(&contract, &bidder, &prev.escrow);
}
}
None => {
if round.bidders.len() >= MAX_BIDDERS {
return Err(Error::RoundFull);
}
round.bidders.push_back(bidder.clone());
}
}
usdc.transfer(&bidder, &contract, &escrow);
let state = BidState {
commitment,
escrow,
revealed_value: None,
revealed_nonce: None,
valid: false,
settled: false,
};
set_state(&env, round_id, &bidder, &state);
set_seal(
&env,
round_id,
&bidder,
&Seal {
ciphertext,
auditor_blob,
},
round.reveal_deadline,
);
set_round(&env, round_id, &round);
env.events()
.publish((symbol_short!("commit"), round_id), (bidder, escrow));
Ok(())
}
/// Open the reveal window by proving Drand round R has been produced.
///
/// The supplied signature is verified on-chain via BLS12-381. This is the
/// only way to move a round into `Revealing`; there is no operator override.
pub fn open_reveal(
env: Env,
round_id: u64,
drand_signature: BytesN<96>,
) -> Result<(), Error> {
let config = get_config(&env)?;
let mut round = get_round(&env, round_id)?;
if round.status == Status::Settled {
return Err(Error::AlreadySettled);
}
if round.status == Status::Voided {
return Err(Error::RoundVoided);
}
if round.status == Status::Cleared {
return Err(Error::AlreadyCleared);
}
if round.status != Status::Open {
return Err(Error::RevealAlreadyOpen);
}
if env.ledger().timestamp() <= round.commit_deadline {
return Err(Error::CommitNotClosed);
}
if !drand::verify_round(&env, &config, round.reveal_round, &drand_signature) {
return Err(Error::InvalidDrandSignature);
}
round.status = Status::Revealing;
extend_round_seals(&env, round_id, &round.bidders, round.reveal_deadline);
set_round(&env, round_id, &round);
env.events()
.publish((symbol_short!("revealing"), round_id), round.reveal_round);
Ok(())
}
/// Reveal a bid. Permissionless: once R's signature is public, anyone can
/// decrypt any ciphertext and submit the reveal — so no bidder can abort.
/// The contract checks `sha256(be16(value) ‖ nonce) == H`.
pub fn reveal(
env: Env,
round_id: u64,
bidder: Address,
value: i128,
nonce: BytesN<32>,
) -> Result<(), Error> {
let round = get_round(&env, round_id)?;
if round.status == Status::Settled {
return Err(Error::AlreadySettled);
}
if round.status == Status::Voided {
return Err(Error::RoundVoided);
}
if round.status != Status::Revealing {
return Err(Error::RevealNotOpen);
}
if env.ledger().timestamp() > round.reveal_deadline {
return Err(Error::RevealWindowClosed);
}
let mut state = get_state(&env, round_id, &bidder)?;
if state.revealed_value.is_some() {
return Err(Error::AlreadyRevealed);
}
let mut preimage = Bytes::new(&env);
preimage.extend_from_array(&value.to_be_bytes());
preimage.extend_from_array(&nonce.to_array());
let computed = env.crypto().sha256(&preimage).to_bytes();
// A reveal MUST match the commitment, or it is rejected outright with no
// state change. Reveal is permissionless, so without this a third party
// could grief an honest bidder by front-running their reveal with a
// garbage value — locking them out (AlreadyRevealed) and invalidating
// their bid. Since the canonical value is recoverable by anyone from the
// ciphertext after R, only the value that hashes to H is ever recorded.
if computed != state.commitment {
return Err(Error::HashMismatch);
}
// The committed value is canonical, but a reveal above escrow is rejected
// outright so integrators see BidExceedsEscrow instead of a silent invalid bid.
if value > state.escrow {
return Err(Error::BidExceedsEscrow);
}
state.revealed_value = Some(value);
state.revealed_nonce = Some(nonce.clone());
state.valid = value > 0;
set_state(&env, round_id, &bidder, &state);
env.events().publish(
(symbol_short!("reveal"), round_id),
(bidder, value, state.valid),
);
Ok(())
}
/// Deterministically compute the winner after the reveal deadline. If no
/// valid bid was revealed, the round is voided and all escrow becomes
/// refundable.
pub fn clear(env: Env, round_id: u64) -> Result<Option<Address>, Error> {
let mut round = get_round(&env, round_id)?;
if round.status == Status::Cleared {
return Err(Error::AlreadyCleared);
}
if round.status == Status::Settled {
return Err(Error::AlreadySettled);
}
if round.status == Status::Voided {
return Err(Error::RoundVoided);
}
if round.status != Status::Revealing {
return Err(Error::RevealNotOpen);
}
if env.ledger().timestamp() <= round.reveal_deadline {
return Err(Error::RevealStillOpen);
}
let mut winner: Option<Address> = None;
let mut best: i128 = 0;
let mut found = false;
for bidder in round.bidders.iter() {
let state = match try_get_state(&env, round_id, &bidder) {
Some(s) => s,
None => continue,
};
if !state.valid {
continue;
}
let value = match state.revealed_value {
Some(v) => v,
None => continue,
};
let better = if !found {
true
} else {
match round.clearing_rule {
ClearingRule::HighestBid => value > best,
ClearingRule::LowestBid => value < best,
}
};
if better {
best = value;
winner = Some(bidder.clone());
found = true;
}
}
if !found {
round.status = Status::Voided;
set_round(&env, round_id, &round);
refund_all(&env, &round, round_id);
env.events().publish((symbol_short!("voided"), round_id), 0u32);
return Ok(None);
}
round.winner = winner.clone();
round.winning_bid = best;
round.status = Status::Cleared;
set_round(&env, round_id, &round);
env.events()
.publish((symbol_short!("cleared"), round_id), (winner.clone(), best));
Ok(winner)
}
/// Settle a cleared round. The winner pays their bid from escrow to the
/// operator; the winner's surplus and every loser's escrow are refunded.
/// Cannot fail for lack of funds — everything was escrowed at commit.
pub fn settle(env: Env, round_id: u64) -> Result<(), Error> {
let config = get_config(&env)?;
let mut round = get_round(&env, round_id)?;
if round.status == Status::Settled {
return Err(Error::AlreadySettled);
}
if round.status == Status::Voided {
return Err(Error::RoundVoided);
}
if round.status != Status::Cleared {
return Err(Error::NotCleared);
}
let winner = round.winner.clone().ok_or(Error::NoValidBids)?;
let usdc = token::Client::new(&env, &config.usdc);
let contract = env.current_contract_address();
for bidder in round.bidders.iter() {
let mut state = match try_get_state(&env, round_id, &bidder) {
Some(s) => s,
None => continue,
};
if state.settled {
continue;
}
if bidder == winner {
usdc.transfer(&contract, &round.operator, &round.winning_bid);
let surplus = state.escrow - round.winning_bid;
if surplus > 0 {
usdc.transfer(&contract, &bidder, &surplus);
}
} else if state.escrow > 0 {
usdc.transfer(&contract, &bidder, &state.escrow);
}
state.settled = true;
set_state(&env, round_id, &bidder, &state);
}
round.status = Status::Settled;
set_round(&env, round_id, &round);
env.events().publish(
(symbol_short!("settled"), round_id),
(winner, round.winning_bid),
);
Ok(())
}
/// Liveness safety valve: if Drand round R is never produced (network stall)
/// and the grace window after the reveal deadline has passed without the
/// round opening, anyone can void it and all escrow is refunded.
pub fn void(env: Env, round_id: u64) -> Result<(), Error> {
let mut round = get_round(&env, round_id)?;
if round.status == Status::Voided {
return Err(Error::RoundVoided);
}
if round.status == Status::Settled {
return Err(Error::AlreadySettled);
}
if round.status != Status::Open {
return Err(Error::NotVoidable);
}
if env.ledger().timestamp() <= round.reveal_deadline + VOID_GRACE {
return Err(Error::NotVoidable);
}
round.status = Status::Voided;
set_round(&env, round_id, &round);
refund_all(&env, &round, round_id);
env.events().publish((symbol_short!("voided"), round_id), 1u32);
Ok(())
}
// ---- Views ----
pub fn get_round(env: Env, round_id: u64) -> Result<Round, Error> {
storage::get_round(&env, round_id)
}
pub fn get_bid_state(env: Env, round_id: u64, bidder: Address) -> Result<BidState, Error> {
storage::get_state(&env, round_id, &bidder)
}
/// Keeper view: the deterministic, ordered bidder index for a round. The
/// keeper reads this to learn exactly which seals must be opened and
/// revealed — the reveal set is on-chain state, so no event scraping or
/// indexer is required and nothing can be missed.
pub fn get_bidders(env: Env, round_id: u64) -> Result<Vec<Address>, Error> {
Ok(storage::get_round(&env, round_id)?.bidders)
}
/// Paginated bidder index for a round. Returns a page of bidders starting
/// at `cursor` (zero-based), with continuation metadata.
///
/// `limit` must be 1–100. `next_cursor` in the response is 0 when there
/// are no more pages.
pub fn get_bidders_page(
env: Env,
round_id: u64,
cursor: u32,
limit: u32,
) -> Result<BiddersPage, Error> {
if limit == 0 || limit > MAX_PAGE_SIZE {
return Err(Error::InvalidLimit);
}
let bidders = storage::get_round(&env, round_id)?.bidders;
let total = bidders.len();
let start = cursor.min(total);
let end = (start + limit).min(total);
let mut data: Vec<Address> = Vec::new(&env);
for i in start..end {
data.push_back(bidders.get(i).unwrap());
}
let next_cursor = if end < total { end } else { 0 };
Ok(BiddersPage {
data,
next_cursor,
total,
})
}
/// Observer view: the sealed ciphertext + auditor blob while still in
/// Temporary storage. Returns `None` once the seal TTL has expired (by design
/// after the reveal window). Persistent bid state remains for settlement.
pub fn get_seal(env: Env, round_id: u64, bidder: Address) -> Option<Seal> {
let round = storage::get_round(&env, round_id).ok()?;
storage::get_seal(&env, round_id, &bidder, round.reveal_deadline)
}
pub fn get_config(env: Env) -> Result<GlobalConfig, Error> {
storage::get_config(&env)
}
}
/// Refund every locked escrow for a voided round.
fn refund_all(env: &Env, round: &Round, round_id: u64) {
let config = match storage::get_config(env) {
Ok(c) => c,
Err(_) => return,
};
let usdc = token::Client::new(env, &config.usdc);
let contract = env.current_contract_address();
for bidder in round.bidders.iter() {
if let Some(mut state) = try_get_state(env, round_id, &bidder) {
if !state.settled && state.escrow > 0 {
usdc.transfer(&contract, &bidder, &state.escrow);
state.settled = true;
set_state(env, round_id, &bidder, &state);
}
}
}
}
fn panic_with(env: &Env, error: Error) -> ! {
soroban_sdk::panic_with_error!(env, error)
}
#[cfg(test)]
mod test;