-
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 #30 from onflow/gio/expose-prg
Expose PRG as fulfillment option supporting many random values per Request
- Loading branch information
Showing
7 changed files
with
158 additions
and
9 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,65 @@ | ||
import Test | ||
import BlockchainHelpers | ||
import "test_helpers.cdc" | ||
|
||
import "RandomConsumer" | ||
|
||
access(all) let serviceAccount = Test.serviceAccount() | ||
access(all) let randomConsumer = Test.getAccount(0x0000000000000007) | ||
|
||
access(all) | ||
fun setup() { | ||
var err = Test.deployContract( | ||
name: "Xorshift128plus", | ||
path: "../contracts/Xorshift128plus.cdc", | ||
arguments: [] | ||
) | ||
Test.expect(err, Test.beNil()) | ||
err = Test.deployContract( | ||
name: "RandomConsumer", | ||
path: "../contracts/RandomConsumer.cdc", | ||
arguments: [] | ||
) | ||
Test.expect(err, Test.beNil()) | ||
} | ||
|
||
access(all) | ||
fun testRequestRandomnessSucceeds() { | ||
let signer = Test.createAccount() | ||
|
||
let consumerSetup = executeTransaction("./transactions/create_consumer.cdc", [], signer) | ||
Test.expect(consumerSetup, Test.beSucceeded()) | ||
|
||
let requestStoragePath = /storage/RandomConsumerRequest | ||
let requestRes = executeTransaction("./transactions/request_randomness.cdc", [requestStoragePath], signer) | ||
Test.expect(requestRes, Test.beSucceeded()) | ||
|
||
let expectedHeight = getCurrentBlockHeight() | ||
|
||
let requestHeightRes = executeScript("./scripts/get_request_blockheight.cdc", [signer.address, requestStoragePath]) | ||
let requestCanFulfillRes = executeScript("./scripts/request_can_fulfill.cdc", [signer.address, requestStoragePath]) | ||
Test.expect(requestHeightRes, Test.beSucceeded()) | ||
Test.expect(requestCanFulfillRes, Test.beSucceeded()) | ||
let requestHeight = requestHeightRes.returnValue! as! UInt64 | ||
let requestCanFulfill = requestCanFulfillRes.returnValue! as! Bool | ||
|
||
|
||
Test.assertEqual(expectedHeight, requestHeight) | ||
Test.assertEqual(false, requestCanFulfill) | ||
|
||
} | ||
|
||
access(all) | ||
fun testFulfillRandomnessSucceeds() { | ||
let signer = Test.createAccount() | ||
|
||
let consumerSetup = executeTransaction("./transactions/create_consumer.cdc", [], signer) | ||
Test.expect(consumerSetup, Test.beSucceeded()) | ||
|
||
let requestStoragePath = /storage/RandomConsumerRequest | ||
let requestRes = executeTransaction("./transactions/request_randomness.cdc", [requestStoragePath], signer) | ||
Test.expect(requestRes, Test.beSucceeded()) | ||
|
||
let fulfillRes = executeTransaction("./transactions/fulfill_random_request.cdc", [requestStoragePath], signer) | ||
Test.expect(fulfillRes, Test.beSucceeded()) | ||
} |
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,10 @@ | ||
import "RandomConsumer" | ||
|
||
access(all) | ||
fun main(address: Address, storagePath: StoragePath): UInt64 { | ||
return getAuthAccount<auth(BorrowValue) &Account>(address).storage | ||
.borrow<&RandomConsumer.Request>( | ||
from: storagePath | ||
)?.block | ||
?? panic("No Request found") | ||
} |
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,10 @@ | ||
import "RandomConsumer" | ||
|
||
access(all) | ||
fun main(address: Address, storagePath: StoragePath): Bool { | ||
return getAuthAccount<auth(BorrowValue) &Account>(address).storage | ||
.borrow<&RandomConsumer.Request>( | ||
from: storagePath | ||
)?.canFullfill() | ||
?? panic("No Request found") | ||
} |
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,10 @@ | ||
import "RandomConsumer" | ||
|
||
transaction { | ||
prepare (signer: auth(BorrowValue, SaveValue) &Account) { | ||
if signer.storage.type(at: RandomConsumer.ConsumerStoragePath) != nil { | ||
panic("Consumer already stored") | ||
} | ||
signer.storage.save(<-RandomConsumer.createConsumer(), to: RandomConsumer.ConsumerStoragePath) | ||
} | ||
} |
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,18 @@ | ||
import "RandomConsumer" | ||
|
||
transaction(storagePath: StoragePath) { | ||
let consumer: auth(RandomConsumer.Reveal) &RandomConsumer.Consumer | ||
let request: @RandomConsumer.Request | ||
|
||
prepare (signer: auth(BorrowValue, LoadValue) &Account) { | ||
self.consumer = signer.storage.borrow<auth(RandomConsumer.Reveal) &RandomConsumer.Consumer>( | ||
from: RandomConsumer.ConsumerStoragePath | ||
) ?? panic("Consumer not found in storage") | ||
self.request <- signer.storage.load<@RandomConsumer.Request>(from: storagePath) | ||
?? panic("No Request found at provided storage path") | ||
} | ||
|
||
execute { | ||
let rand = self.consumer.fulfillRandomRequest(<-self.request) | ||
} | ||
} |
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,13 @@ | ||
import "RandomConsumer" | ||
|
||
transaction(storagePath: StoragePath) { | ||
prepare (signer: auth(BorrowValue, SaveValue) &Account) { | ||
if signer.storage.type(at: storagePath) != nil { | ||
panic("Object already stored in provided storage path") | ||
} | ||
let consumer = signer.storage.borrow<auth(RandomConsumer.Commit) &RandomConsumer.Consumer>( | ||
from: RandomConsumer.ConsumerStoragePath | ||
) ?? panic("Consumer not found in storage") | ||
signer.storage.save(<-consumer.requestRandomness(), to: storagePath) | ||
} | ||
} |