-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduces game entropy from 2 storage updates to 1
* previously the entropy and last updated block were stored in two storage slots * now entropy and last updated block are packed into single storage slot * this reduces cost of rotating game entropy by ~50% which will be a common call
- Loading branch information
Showing
6 changed files
with
146 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use pack::{constants::pow, pack::{Packing, rshift_split}}; | ||
|
||
#[derive(Drop, Copy, Serde)] | ||
struct GameEntropy { | ||
entropy: u128, | ||
last_updated: u64, | ||
} | ||
|
||
impl GameEntropyPacking of Packing<GameEntropy> { | ||
fn pack(self: GameEntropy) -> felt252 { | ||
(self.entropy.into() | ||
+ self.last_updated.into() * pow::TWO_POW_128) | ||
.try_into() | ||
.expect('pack GameEntropy') | ||
} | ||
|
||
fn unpack(packed: felt252) -> GameEntropy { | ||
let packed = packed.into(); | ||
let (packed, entropy) = rshift_split(packed, pow::TWO_POW_128); | ||
let (_, last_updated) = rshift_split(packed, pow::TWO_POW_128); | ||
|
||
GameEntropy { | ||
entropy: entropy.try_into().unwrap(), last_updated: last_updated.try_into().unwrap(), | ||
} | ||
} | ||
// Not used for game entropy | ||
fn overflow_pack_protection(self: GameEntropy) -> GameEntropy { | ||
self | ||
} | ||
} | ||
|
||
// --------------------------- | ||
// ---------- Tests ---------- | ||
// --------------------------- | ||
#[cfg(test)] | ||
mod tests { | ||
use game::game::game_entropy::{GameEntropy, GameEntropyPacking}; | ||
use pack::{pack::{Packing}}; | ||
|
||
#[test] | ||
#[available_gas(3000000)] | ||
fn test_packing_and_unpacking_game_entropy() { | ||
let game_entropy = GameEntropy { entropy: 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, last_updated: 0xFFFFFFFFFFFFFFFF }; | ||
let unpacked: GameEntropy = Packing::unpack(game_entropy.pack()); | ||
|
||
assert(unpacked.entropy == game_entropy.entropy, 'wrong entropy max value'); | ||
assert(unpacked.last_updated == game_entropy.last_updated, 'wrong last_updated max value'); | ||
|
||
let game_entropy = GameEntropy { entropy: 0, last_updated: 0 }; | ||
let unpacked: GameEntropy = Packing::unpack(game_entropy.pack()); | ||
|
||
assert(unpacked.entropy == game_entropy.entropy, 'wrong entropy zero'); | ||
assert(unpacked.last_updated == game_entropy.last_updated, 'wrong last_updated zero'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod game; | ||
mod constants; | ||
mod interfaces; | ||
mod game_entropy; |
Oops, something went wrong.
7af07c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
loot-survivor – ./
loot-survivor-git-main-loot-bibliotheca.vercel.app
loot-survivor-loot-bibliotheca.vercel.app
loot-survivor.vercel.app
beta-survivor.realms.world
survivor.realms.world