From 817695a9d1adb012829586645e18c917ceb9f4b3 Mon Sep 17 00:00:00 2001 From: benr-ml Date: Wed, 13 Mar 2024 11:23:13 +0200 Subject: [PATCH] update comments --- .../packages/sui-framework/sources/random.move | 7 +++---- .../examples/games/sources/raffles.move | 11 +++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/sui-framework/packages/sui-framework/sources/random.move b/crates/sui-framework/packages/sui-framework/sources/random.move index 7b22e92303d42..671d35baa468a 100644 --- a/crates/sui-framework/packages/sui-framework/sources/random.move +++ b/crates/sui-framework/packages/sui-framework/sources/random.move @@ -105,7 +105,7 @@ module sui::random { let epoch = tx_context::epoch(ctx); let inner = load_inner_mut(self); if (inner.randomness_round == 0 && inner.epoch == 0 && - vector::is_empty(&inner.random_bytes)) { + vector::is_empty(&inner.random_bytes)) { // First update should be for round zero. assert!(new_round == 0, EInvalidRandomnessUpdate); } else { @@ -272,7 +272,7 @@ module sui::random { (u128_in_range(g, (min as u128), (max as u128), 9) as u8) } - /// Shuffle a vector using the random generator. + /// Shuffle a vector using the random generator (Fisher–Yates/Knuth shuffle). public fun shuffle(g: &mut RandomGenerator, v: &mut vector) { let n = (vector::length(v) as u32); if (n == 0) { @@ -280,7 +280,7 @@ module sui::random { }; let i: u32 = 0; while (i < (n - 1)) { - let j = generate_u32_in_range(g, i, n-1); + let j = generate_u32_in_range(g, i, n - 1); vector::swap(v, (i as u64), (j as u64)); i = i + 1; }; @@ -300,5 +300,4 @@ module sui::random { public fun generator_buffer(r: &RandomGenerator): &vector { &r.buffer } - } diff --git a/sui_programmability/examples/games/sources/raffles.move b/sui_programmability/examples/games/sources/raffles.move index 3da2ca24cd62c..ab609b35d4286 100644 --- a/sui_programmability/examples/games/sources/raffles.move +++ b/sui_programmability/examples/games/sources/raffles.move @@ -3,7 +3,7 @@ /// Basic raffles games that depends on Sui randomness. /// -/// Anyone can create a new lottery game with an end time and a price. After the end time, anyone can trigger +/// Anyone can create a new raffle game with an end time and a price. After the end time, anyone can trigger /// a function to determine the winner, and the winner gets the entire balance of the game. /// /// - raffle_with_tickets uses tickets which could be transferred to other accounts, used as NFTs, etc. @@ -104,7 +104,7 @@ module games::raffle_with_tickets { } public fun destroy_ticket(ticket: Ticket) { - let Ticket { id, game_id: _, participant_index: _} = ticket; + let Ticket { id, game_id: _, participant_index: _ } = ticket; object::delete(id); } @@ -150,6 +150,9 @@ module games::small_raffle { const EGameInProgress: u64 = 0; const EGameAlreadyCompleted: u64 = 1; const EInvalidAmount: u64 = 2; + const EReachedMaxParticipants: u64 = 3; + + const MaxParticipants: u32 = 500; /// Game represents a set of parameters of a single game. struct Game has key { @@ -181,7 +184,7 @@ module games::small_raffle { /// Gas based attacks are not possible since the gas cost of this function is independent of the winner. entry fun close(game: Game, r: &Random, clock: &Clock, ctx: &mut TxContext) { assert!(game.end_time <= clock::timestamp_ms(clock), EGameInProgress); - let Game { id, cost_in_sui: _, participants, end_time: _, balance , participants_table } = game; + let Game { id, cost_in_sui: _, participants, end_time: _, balance, participants_table } = game; if (participants > 0) { let generator = new_generator(r, ctx); let winner = random::generate_u32_in_range(&mut generator, 1, participants); @@ -192,7 +195,6 @@ module games::small_raffle { balance::destroy_zero(balance); }; - // TODO: will this work with 10K objects? let i = 1; while (i <= participants) { table::remove(&mut participants_table, i); @@ -206,6 +208,7 @@ module games::small_raffle { public fun play(game: &mut Game, coin: Coin, clock: &Clock, ctx: &mut TxContext) { assert!(game.end_time > clock::timestamp_ms(clock), EGameAlreadyCompleted); assert!(coin::value(&coin) == game.cost_in_sui, EInvalidAmount); + assert!(game.participants < MaxParticipants, EReachedMaxParticipants); game.participants = game.participants + 1; coin::put(&mut game.balance, coin);