-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from onflow/gio/fix-get-number-in-range
Fix `RandomConsumer.getNumberInRange()`
- Loading branch information
Showing
4 changed files
with
51 additions
and
10 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
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,30 @@ | ||
import "RandomBeaconHistory" | ||
import "RandomConsumer" | ||
import "Xorshift128plus" | ||
|
||
/// Test script to ensure PRG state advances across calls to | ||
access(all) | ||
fun main(): Bool { | ||
// Instantiate PRG | ||
let sor = RandomBeaconHistory.sourceOfRandomness(atBlockHeight: getCurrentBlock().height - 1) | ||
let salt = UInt64.max | ||
let prg = Xorshift128plus.PRG(sourceOfRandomness: sor.value, salt: salt.toBigEndianBytes()) | ||
// Reference new PRG | ||
let prgRef: &Xorshift128plus.PRG = &prg | ||
|
||
// Call params | ||
let min: UInt64 = 0 | ||
let max: UInt64 = 100 | ||
|
||
// Ensure PRG state changes between calls | ||
let preState0 = prg.state0 | ||
let preState1 = prg.state1 | ||
|
||
RandomConsumer.getNumberInRange(prg: prgRef, min: min, max: max) | ||
|
||
let postState0 = prg.state0 | ||
let postState1 = prg.state1 | ||
|
||
// Ensure state on the PRG struct changed as a result of the getNumberInRange() call | ||
return preState0 != postState0 && preState1 != postState1 | ||
} |