From 82155bb09148726b609fe80641104466a660f86e Mon Sep 17 00:00:00 2001 From: loothero <100039621+loothero@users.noreply.github.com> Date: Fri, 6 Oct 2023 20:59:16 -0400 Subject: [PATCH] adjust actions per block rate limit (#389) - previous rate limit 120 actions per hour - new rate limit 180 actions per hour --- contracts/game/src/game/constants.cairo | 1 + contracts/game/src/tests/test_game.cairo | 1 + contracts/game_entropy/src/constants.cairo | 1 + contracts/game_entropy/src/game_entropy.cairo | 31 +++++++++++++------ contracts/game_entropy/src/lib.cairo | 3 +- 5 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 contracts/game_entropy/src/constants.cairo diff --git a/contracts/game/src/game/constants.cairo b/contracts/game/src/game/constants.cairo index 84a484b1b..1f5fd5e31 100644 --- a/contracts/game/src/game/constants.cairo +++ b/contracts/game/src/game/constants.cairo @@ -33,6 +33,7 @@ const COST_TO_PLAY: u8 = 25; const NUM_STARTING_STATS: u8 = 9; const STARTING_GAME_ENTROPY_ROTATION_INTERVAL: u8 = 6; const MINIMUM_DAMAGE_FROM_BEASTS: u8 = 2; +const RATE_LIMIT: u16 = 180; const U64_MAX: u64 = 18446744073709551615; const U128_MAX: u128 = 340282366920938463463374607431768211455; diff --git a/contracts/game/src/tests/test_game.cairo b/contracts/game/src/tests/test_game.cairo index 36823b4e6..8fa5c40e6 100644 --- a/contracts/game/src/tests/test_game.cairo +++ b/contracts/game/src/tests/test_game.cairo @@ -1829,6 +1829,7 @@ mod tests { game.explore(ADVENTURER_ID, false); game.attack(ADVENTURER_ID, false); game.attack(ADVENTURER_ID, false); + game.attack(ADVENTURER_ID, false); } #[test] diff --git a/contracts/game_entropy/src/constants.cairo b/contracts/game_entropy/src/constants.cairo new file mode 100644 index 000000000..ef9083c5f --- /dev/null +++ b/contracts/game_entropy/src/constants.cairo @@ -0,0 +1 @@ +const RATE_LIMIT_ACTIONS_PER_HOUR: u16 = 180; \ No newline at end of file diff --git a/contracts/game_entropy/src/game_entropy.cairo b/contracts/game_entropy/src/game_entropy.cairo index 0a1705ad2..50a88b040 100644 --- a/contracts/game_entropy/src/game_entropy.cairo +++ b/contracts/game_entropy/src/game_entropy.cairo @@ -1,6 +1,6 @@ -use debug::PrintTrait; use poseidon::poseidon_hash_span; use starknet::{StorePacking}; +use super::constants; #[derive(Drop, Copy, Serde)] struct GameEntropy { @@ -52,13 +52,17 @@ impl ImplGameEntropy of IGameEntropy { /// @param next_update_block The block number for the next scheduled update. /// @return A new instance of GameEntropy. fn new(last_updated_block: u64, last_updated_time: u64, next_update_block: u64) -> GameEntropy { - let hash = ImplGameEntropy::get_hash(last_updated_block, last_updated_time, next_update_block); + let hash = ImplGameEntropy::get_hash( + last_updated_block, last_updated_time, next_update_block + ); GameEntropy { hash, last_updated_block, last_updated_time, next_update_block } } /// @notice Calculate a hash based on the properties of the GameEntropy struct /// @return A 252-bit hash value - fn get_hash(last_updated_block: u64, last_updated_time: u64, next_update_block: u64) -> felt252 { + fn get_hash( + last_updated_block: u64, last_updated_time: u64, next_update_block: u64 + ) -> felt252 { let mut hash_span = ArrayTrait::::new(); hash_span.append(last_updated_block.into()); hash_span.append(last_updated_time.into()); @@ -106,10 +110,12 @@ impl ImplGameEntropy of IGameEntropy { #[inline(always)] fn get_rate_limit(self: GameEntropy) -> u64 { let blocks_per_hour = self.current_blocks_per_hour(); - if blocks_per_hour < 120 { - return (120 / blocks_per_hour); + if blocks_per_hour < constants::RATE_LIMIT_ACTIONS_PER_HOUR.into() { + constants::RATE_LIMIT_ACTIONS_PER_HOUR.into() / blocks_per_hour } else { - return 1; + // if the current blocks per hour exceeds the rate limit, we return 1 + // which means 1 action per block + 1 } } @@ -148,7 +154,9 @@ mod tests { let last_updated_time = 1696209920; let next_update_block = 282364; - let game_entropy = ImplGameEntropy::new(last_updated_block, last_updated_time, next_update_block); + let game_entropy = ImplGameEntropy::new( + last_updated_block, last_updated_time, next_update_block + ); assert(game_entropy.hash != 0, 'hash should be set'); assert(game_entropy.last_updated_block == last_updated_block, 'wrong last_updated_block'); assert(game_entropy.last_updated_time == last_updated_time, 'wrong last_updated_time'); @@ -162,7 +170,9 @@ mod tests { let last_updated_block = 0; let last_updated_time = 0; let next_update_block = 0; - let hash = ImplGameEntropy::get_hash(last_updated_block, last_updated_time, next_update_block); + let hash = ImplGameEntropy::get_hash( + last_updated_block, last_updated_time, next_update_block + ); } #[test] @@ -215,7 +225,10 @@ mod tests { #[available_gas(6350)] fn test_current_blocks_per_hour() { let game_entropy = GameEntropy { - hash: 0x123, last_updated_block: 282465, last_updated_time: 1696214108, next_update_block: 282481, + hash: 0x123, + last_updated_block: 282465, + last_updated_time: 1696214108, + next_update_block: 282481, }; let blocks_per_hour = game_entropy.current_blocks_per_hour(); assert(blocks_per_hour == 96, 'wrong blocks per hour') diff --git a/contracts/game_entropy/src/lib.cairo b/contracts/game_entropy/src/lib.cairo index 43dce041a..f16d9cb7e 100644 --- a/contracts/game_entropy/src/lib.cairo +++ b/contracts/game_entropy/src/lib.cairo @@ -1 +1,2 @@ -mod game_entropy; \ No newline at end of file +mod game_entropy; +mod constants; \ No newline at end of file