From b50ece509f888eadea0c5c0f9533463c234fb7cf Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Tue, 27 Feb 2024 22:09:40 +1100 Subject: [PATCH] Revert "updates" This reverts commit c8f3c250139fe756397e4b47fdfa465027793272. --- contracts/game/src/game/constants.cairo | 3 - contracts/game/src/game/interfaces.cairo | 11 +-- contracts/game/src/lib.cairo | 100 +---------------------- contracts/game/src/tests/test_game.cairo | 50 ++++-------- 4 files changed, 18 insertions(+), 146 deletions(-) diff --git a/contracts/game/src/game/constants.cairo b/contracts/game/src/game/constants.cairo index 04a68c952..eb73c7ec8 100644 --- a/contracts/game/src/game/constants.cairo +++ b/contracts/game/src/game/constants.cairo @@ -31,9 +31,6 @@ mod messages { const NOT_OWNER_OF_TOKEN: felt252 = 'Not owner of token'; const MA_PERIOD_LESS_THAN_WEEK: felt252 = 'MA period too small'; const TERMINAL_TIME_REACHED: felt252 = 'terminal time reached'; - const STARTING_HASH_NOT_ZERO: felt252 = 'Starting hash must be zero'; - const NO_OPTIMISTIC_START: felt252 = 'No optimistic start'; - const NOT_ENOUGH_BLOCKS: felt252 = 'Not enough blocks'; } // TODO: Update for mainnet diff --git a/contracts/game/src/game/interfaces.cairo b/contracts/game/src/game/interfaces.cairo index 975a04021..8ae13d872 100644 --- a/contracts/game/src/game/interfaces.cairo +++ b/contracts/game/src/game/interfaces.cairo @@ -14,12 +14,7 @@ use game_snapshot::GamesPlayedSnapshot; trait IGame { // ------ Game Actions ------ fn new_game( - ref self: TContractState, - client_reward_address: ContractAddress, - weapon: u8, - name: u128, - golden_token_id: u256, - interface_camel: bool + ref self: TContractState, client_reward_address: ContractAddress, weapon: u8, name: u128, golden_token_id: u256, interface_camel: bool ); fn explore(ref self: TContractState, adventurer_id: felt252, till_beast: bool); fn attack(ref self: TContractState, adventurer_id: felt252, to_the_death: bool); @@ -37,8 +32,6 @@ trait IGame { fn rotate_game_entropy(ref self: TContractState); fn update_cost_to_play(ref self: TContractState); fn initiate_price_change(ref self: TContractState); - fn optimistic_start(ref self: TContractState, adventurer_id: felt252, block_hash: felt252); - fn slay_invalid_adventurer(ref self: TContractState, adventurer_id: felt252); // ------ View Functions ------ @@ -161,4 +154,4 @@ trait IGame { fn get_cost_to_play(self: @TContractState) -> u128; fn get_games_played_snapshot(self: @TContractState) -> GamesPlayedSnapshot; fn can_play(self: @TContractState, golden_token_id: u256) -> bool; -} +} \ No newline at end of file diff --git a/contracts/game/src/lib.cairo b/contracts/game/src/lib.cairo index 73568f03e..203fb87e5 100644 --- a/contracts/game/src/lib.cairo +++ b/contracts/game/src/lib.cairo @@ -117,7 +117,6 @@ mod Game { _cost_to_play: u128, _games_played_snapshot: GamesPlayedSnapshot, _terminal_timestamp: u64, - _starting_entropy: LegacyMap::, } #[event] @@ -225,45 +224,6 @@ mod Game { _start_game(ref self, weapon, name, interface_camel); } - fn optimistic_start(ref self: ContractState, adventurer_id: felt252, block_hash: felt252) { - // assert the starting entropy has not been set - assert( - self._starting_entropy.read(adventurer_id) == 0, messages::STARTING_HASH_NOT_ZERO - ); - - self._starting_entropy.write(adventurer_id, block_hash); - - // then attack the starting beast - self.attack(adventurer_id, false); - } - - - fn slay_invalid_adventurer(ref self: ContractState, adventurer_id: felt252) { - // get starting blockhash - let optimistic_hash = self._starting_entropy.read(adventurer_id); - - // must be optimistic start - assert(optimistic_hash != 0, messages::NO_OPTIMISTIC_START); - - // get adventurer meta data - let mut adventurer_meta = _load_adventurer_metadata(@self, adventurer_id); - - // at least 11 blocks must have passed - assert( - starknet::get_block_info().unbox().block_number > adventurer_meta.start_block + 11, - messages::NOT_ENOUGH_BLOCKS - ); - - // get the blockhash - let block_hash = starknet::get_block_hash_syscall(adventurer_meta.start_block + 1) - .unwrap_syscall(); - - // slay - if (optimistic_hash != block_hash) { - _slay_invalid_adventurer(ref self, adventurer_id); - } - } - /// @title Explore Function /// /// @notice Allows an adventurer to explore @@ -346,12 +306,7 @@ mod Game { // ensure player is not exceeding the rate limit let block_number = starknet::get_block_info().unbox().block_number; - - // we check if optimistic start and has not started - let is_starting = self._starting_entropy.read(adventurer_id) == 0 - || adventurer.get_level() == 1; - - if !is_starting || !adventurer.block_changed_since_last_action(block_number) { + if !adventurer.block_changed_since_last_action(block_number) { _assert_rate_limit(adventurer.actions_per_block, game_entropy.get_rate_limit()); } @@ -365,12 +320,10 @@ mod Game { // if the adventurer is on level 1, this is their first action of the game if adventurer.get_level() == 1 { + // so we reveal their starting stats and store them in Adventurer Meta let adventurer_meta = _handle_stat_reveal(@self, ref adventurer, adventurer_id); // update adventurer meta data (this is the last time this storage slot is updated) _save_adventurer_metadata(ref self, adventurer_id, adventurer_meta); - - // at least one block must have passed - assert(block_number > adventurer_meta.start_block, 'CANNOT START IN SAME BLOCK'); } // get number of blocks between actions @@ -1130,27 +1083,6 @@ mod Game { _save_adventurer_no_boosts(ref self, adventurer, adventurer_id); } - fn _slay_invalid_adventurer(ref self: ContractState, adventurer_id: felt252) { - // unpack adventurer from storage (no need for stat boosts) - let mut adventurer = _load_adventurer_no_boosts(@self, adventurer_id); - - // assert adventurer is not already dead - _assert_not_dead(adventurer); - - // assert adventurer is idle - _assert_is_idle(@self, adventurer, adventurer_id); - - // slay adventurer by setting health to 0 and 0 the xp because of invalid start - adventurer.health = 0; - adventurer.xp = 0; - - // handle adventurer death - _process_adventurer_death(ref self, adventurer, adventurer_id, 0, 0,); - - // save adventurer (gg) - _save_adventurer_no_boosts(ref self, adventurer, adventurer_id); - } - fn _process_beast_death( ref self: ContractState, ref adventurer: Adventurer, @@ -1278,31 +1210,6 @@ mod Game { } } - fn _check_and_kill_invalid_adventurer(ref self: ContractState, adventurer_id: felt252) { - let optimistic_hash = self._starting_entropy.read(adventurer_id); - - if (optimistic_hash != 0) { - // get adventurer meta data - let mut adventurer_meta = _load_adventurer_metadata(@self, adventurer_id); - - // get the blockhash - let block_hash = starknet::get_block_hash_syscall(adventurer_meta.start_block + 1) - .unwrap_syscall(); - - if (optimistic_hash != block_hash) { // zero out - let mut adventurer = _load_adventurer_no_boosts(@self, adventurer_id); - - // zero out adventurer stats - adventurer.xp = 0; - adventurer.health = 0; - - // save adventurer - _save_adventurer_no_boosts(ref self, adventurer, adventurer_id); - } - } - } - - fn _process_adventurer_death( ref self: ContractState, adventurer: Adventurer, @@ -1310,9 +1217,6 @@ mod Game { beast_id: u8, obstacle_id: u8 ) { - // optimistic check - _check_and_kill_invalid_adventurer(ref self, adventurer_id); - let adventurer_state = AdventurerState { owner: _owner_of(@self, adventurer_id), adventurer_id, adventurer }; diff --git a/contracts/game/src/tests/test_game.cairo b/contracts/game/src/tests/test_game.cairo index d24877188..5869e485f 100644 --- a/contracts/game/src/tests/test_game.cairo +++ b/contracts/game/src/tests/test_game.cairo @@ -258,8 +258,6 @@ mod tests { 'wrong starter beast health ' ); - testing::set_block_number(starting_block + 1); - game } @@ -267,7 +265,7 @@ mod tests { // start game on block number 1 let mut game = new_adventurer(1000, 1696201757); - // fast forward chain to block number 1002 + // fast forward chain to block number 400 testing::set_block_number(1002); // double attack beast @@ -622,7 +620,6 @@ mod tests { // is annotated in the test game.explore(ADVENTURER_ID, true); } - #[test] #[should_panic] #[available_gas(90000000)] @@ -1326,7 +1323,7 @@ mod tests { // verify last action block number is correct assert( - adventurer.last_action_block == STARTING_BLOCK_NUMBER.try_into().unwrap() + 1, + adventurer.last_action_block == STARTING_BLOCK_NUMBER.try_into().unwrap(), 'unexpected last action block' ); @@ -1441,8 +1438,6 @@ mod tests { add_adventurer_to_game(ref game, 0); add_adventurer_to_game(ref game, 0); - testing::set_block_number(STARTING_BLOCK_NUMBER + 2); - // attack starter beast, resulting in adventurer last action block number being 1 game.attack(ADVENTURER_ID, false); game.attack(ADVENTURER2_ID, false); @@ -2243,7 +2238,9 @@ mod tests { let starting_block = 364063; let starting_timestamp = 1698678554; let terminal_timestamp = 0; - let (mut game, _, _, _) = setup(starting_block, starting_timestamp, terminal_timestamp); + let (mut game, _, _, _) = setup( + starting_block, starting_timestamp, terminal_timestamp + ); add_adventurer_to_game(ref game, 1); testing::set_block_timestamp(starting_timestamp + DAY); add_adventurer_to_game(ref game, 1); @@ -2256,7 +2253,9 @@ mod tests { let starting_block = 364063; let starting_timestamp = 1698678554; let terminal_timestamp = 0; - let (mut game, _, _, _) = setup(starting_block, starting_timestamp, terminal_timestamp); + let (mut game, _, _, _) = setup( + starting_block, starting_timestamp, terminal_timestamp + ); assert(game.can_play(1), 'should be able to play'); add_adventurer_to_game(ref game, golden_token_id); assert(!game.can_play(1), 'should not be able to play'); @@ -2274,7 +2273,9 @@ mod tests { let starting_block = 364063; let starting_timestamp = 1698678554; let terminal_timestamp = 0; - let (mut game, _, _, _) = setup(starting_block, starting_timestamp, terminal_timestamp); + let (mut game, _, _, _) = setup( + starting_block, starting_timestamp, terminal_timestamp + ); add_adventurer_to_game(ref game, golden_token_id); } @@ -2286,7 +2287,9 @@ mod tests { let starting_block = 364063; let starting_timestamp = 1698678554; let terminal_timestamp = 0; - let (mut game, _, _, _) = setup(starting_block, starting_timestamp, terminal_timestamp); + let (mut game, _, _, _) = setup( + starting_block, starting_timestamp, terminal_timestamp + ); add_adventurer_to_game(ref game, golden_token_id); // roll blockchain forward 1 second less than a day @@ -2320,29 +2323,4 @@ mod tests { let (is_idle, _) = game.is_idle(ADVENTURER_ID); assert(is_idle, 'should be idle'); } - - const TEST_BLOCKHASH: felt252 = 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab; - - fn optimistic_start() -> IGameDispatcher { - // start new game - let mut game = new_adventurer(1000, 1696201757); - - game.optimistic_start(ADVENTURER_ID, TEST_BLOCKHASH); - - game - } - - #[test] - #[available_gas(2300000000)] - fn test_optimistic_start() { - // start new game - optimistic_start(); - } - - #[test] - #[available_gas(2300000000)] - fn test_optimistic_start_slay_invalid() { - // start new game - let mut game = optimistic_start(); - } }