Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust actions per block rate limit #389

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contracts/game/src/game/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions contracts/game/src/tests/test_game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions contracts/game_entropy/src/constants.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const RATE_LIMIT_ACTIONS_PER_HOUR: u16 = 180;
31 changes: 22 additions & 9 deletions contracts/game_entropy/src/game_entropy.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use debug::PrintTrait;
use poseidon::poseidon_hash_span;
use starknet::{StorePacking};
use super::constants;

#[derive(Drop, Copy, Serde)]
struct GameEntropy {
Expand Down Expand Up @@ -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::<felt252>::new();
hash_span.append(last_updated_block.into());
hash_span.append(last_updated_time.into());
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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');
Expand All @@ -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]
Expand Down Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion contracts/game_entropy/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod game_entropy;
mod game_entropy;
mod constants;