Skip to content

Commit

Permalink
switch back to using the Randomness beacon instead of revertible random
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkline committed Dec 21, 2023
1 parent 664e98b commit 000662b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
30 changes: 14 additions & 16 deletions contracts/FlowtyRaffles.cdc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "MetadataViews"
import "RandomBeaconHistory"
// import "Xorshift128plus"
import "Xorshift128plus"

pub contract FlowtyRaffles {
pub let ManagerStoragePath: StoragePath
Expand Down Expand Up @@ -343,22 +343,20 @@ pub contract FlowtyRaffles {
// taken from
// https://github.com/onflow/random-coin-toss/blob/4271cd571b7761af36b0f1037767171aeca18387/contracts/CoinToss.cdc#L95
pub fun randUInt64(atBlockHeight: UInt64, salt: UInt64): UInt64 {
// // query the Random Beacon history core-contract - if `blockHeight` <= current block height, panic & revert
// let sourceOfRandomness = RandomBeaconHistory.sourceOfRandomness(atBlockHeight: atBlockHeight)
// assert(sourceOfRandomness.blockHeight == atBlockHeight, message: "RandomSource block height mismatch")
// // instantiate a PRG object, seeding a source of randomness with `salt` and returns a pseudo-random
// // generator object.
// let prg = Xorshift128plus.PRG(
// sourceOfRandomness: sourceOfRandomness.value,
// salt: salt.toBigEndianBytes()
// )
// return prg.nextUInt64()
// TODO: use commented-out implementation once we can test using the randomness beacon in the cadence testing framework
return revertibleRandom()
// query the Random Beacon history core-contract - if `blockHeight` <= current block height, panic & revert
let sourceOfRandomness = RandomBeaconHistory.sourceOfRandomnessAtBlockHeight(blockHeight: atBlockHeight)
assert(sourceOfRandomness.blockHeight == atBlockHeight, message: "RandomSource block height mismatch")

// instantiate a PRG object, seeding a source of randomness with `salt` and returns a pseudo-random
// generator object.
let prg = Xorshift128plus.PRG(
sourceOfRandomness: sourceOfRandomness.value,
salt: salt.toBigEndianBytes()
)

return prg.nextUInt64()
}

pub fun extractString(_ value: AnyStruct?): String? {
if value == nil {
return nil
Expand Down
17 changes: 9 additions & 8 deletions contracts/standard/RandomBeaconHistory.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,22 @@ access(all) contract RandomBeaconHistory {
/// precedes or exceeds the recorded history. Note that a source of randomness for block n will not be accessible
/// until block n+1.
///
/// @param atBlockHeight The block height at which to retrieve the source of randomness
/// @param blockHeight The block height at which to retrieve the source of randomness
///
/// @return The source of randomness at the given block height as RandomSource struct
///
access(all) fun sourceOfRandomness(atBlockHeight: UInt64): RandomSource {
access(all) fun sourceOfRandomnessAtBlockHeight(blockHeight: UInt64): RandomSource {
pre {
self.lowestHeight != nil: "History has not yet been initialized"
atBlockHeight >= self.lowestHeight!: "Requested block height precedes recorded history"
atBlockHeight < getCurrentBlock().height: "Source of randomness not yet recorded"
blockHeight >= self.lowestHeight!: "Requested block height precedes recorded history"
blockHeight < getCurrentBlock().height: "Source of randomness not yet recorded"
}
let index: UInt64 = atBlockHeight - self.lowestHeight!
let index = blockHeight - self.lowestHeight!
assert(
index >= 0 && index < UInt64(self.randomSourceHistory.length),
message: "Problem finding random source history index"
)
return RandomSource(blockHeight: atBlockHeight, value: self.randomSourceHistory[index])
return RandomSource(blockHeight: blockHeight, value: self.randomSourceHistory[index])
}

/// Retrieves a page from the history of random sources, ordered chronologically
Expand All @@ -111,7 +111,7 @@ access(all) contract RandomBeaconHistory {
/// @return A RandomSourceHistoryPage containing RandomSource values in choronological order according to
/// associated block height
///
access(all) view fun getRandomSourceHistoryPage(page: UInt64, perPage: UInt64): RandomSourceHistoryPage {
access(all) view fun getRandomSourceHistoryPage(_ page: UInt64, perPage: UInt64): RandomSourceHistoryPage {
pre {
self.lowestHeight != nil: "History has not yet been initialized"
}
Expand All @@ -132,10 +132,11 @@ access(all) contract RandomBeaconHistory {
}

// Iterate over history and construct page RandomSource values
let lowestHeight = self.lowestHeight!
for i, block in self.randomSourceHistory.slice(from: Int(startIndex), upTo: Int(endIndex)) {
values.append(
RandomSource(
blockHeight: self.lowestHeight! + startIndex + UInt64(i),
blockHeight: lowestHeight + startIndex + UInt64(i),
value: self.randomSourceHistory[startIndex + UInt64(i)]
)
)
Expand Down
7 changes: 4 additions & 3 deletions test/FlowtyRaffles_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ pub let Xorshift128plusContractAddress = Address(0x0000000000000008)
pub let GenericRaffleSourceIdentifier = "A.0000000000000008.FlowtyRaffleSource.AnyStructRaffleSource"

pub fun setup() {
var err = Test.deployContract(name: "FlowtyRaffles", path: "../contracts/FlowtyRaffles.cdc", arguments: [])
var err = Test.deployContract(name: "Xorshift128plus", path: "../contracts/standard/Xorshift128plus.cdc", arguments: [])
Test.expect(err, Test.beNil())

err = Test.deployContract(name: "FlowtyRaffles", path: "../contracts/FlowtyRaffles.cdc", arguments: [])
Test.expect(err, Test.beNil())

err = Test.deployContract(name: "FlowtyRaffleSource", path: "../contracts/FlowtyRaffleSource.cdc", arguments: [])
Test.expect(err, Test.beNil())

// err = Test.deployContract(name: "Xorshift128plus", path: "../contracts/standard/Xorshift128plus.cdc", arguments: [])
// Test.expect(err, Test.beNil())
}

pub fun testSetupManager() {
Expand Down

0 comments on commit 000662b

Please sign in to comment.