diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fa26866..e846c0b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,9 @@ name: CI on: pull_request: paths: - contract/Xorshift128plus.cdc + - contract/Xorshift128plus.cdc + - prg_test.go + - prg_test_helpers.go jobs: build: diff --git a/contracts/CoinToss.cdc b/contracts/CoinToss.cdc index fb2b663..d1f2d65 100644 --- a/contracts/CoinToss.cdc +++ b/contracts/CoinToss.cdc @@ -69,7 +69,7 @@ access(all) contract CoinToss { let receiptID = receipt.uuid // self.randomCoin() errors if commitBlock <= current block height in call to - // RandomBeaconHistory.sourceOfRandomness() + // RandomBeaconHistory.sourceOfRandomness() let coin = self.randomCoin(atBlockHeight: receipt.commitBlock, salt: receipt.uuid) destroy receipt diff --git a/contracts/utility/FlowToken.cdc b/contracts/standard/FlowToken.cdc similarity index 100% rename from contracts/utility/FlowToken.cdc rename to contracts/standard/FlowToken.cdc diff --git a/contracts/utility/FungibleToken.cdc b/contracts/standard/FungibleToken.cdc similarity index 100% rename from contracts/utility/FungibleToken.cdc rename to contracts/standard/FungibleToken.cdc diff --git a/contracts/utility/MetadataViews.cdc b/contracts/standard/MetadataViews.cdc similarity index 100% rename from contracts/utility/MetadataViews.cdc rename to contracts/standard/MetadataViews.cdc diff --git a/contracts/utility/NonFungibleToken.cdc b/contracts/standard/NonFungibleToken.cdc similarity index 100% rename from contracts/utility/NonFungibleToken.cdc rename to contracts/standard/NonFungibleToken.cdc diff --git a/contracts/RandomBeaconHistory.cdc b/contracts/standard/RandomBeaconHistory.cdc similarity index 100% rename from contracts/RandomBeaconHistory.cdc rename to contracts/standard/RandomBeaconHistory.cdc diff --git a/contracts/utility/ViewResolver.cdc b/contracts/standard/ViewResolver.cdc similarity index 100% rename from contracts/utility/ViewResolver.cdc rename to contracts/standard/ViewResolver.cdc diff --git a/contracts/utility/RandomResultStorage.cdc b/contracts/test/RandomResultStorage.cdc similarity index 100% rename from contracts/utility/RandomResultStorage.cdc rename to contracts/test/RandomResultStorage.cdc diff --git a/flow.json b/flow.json index 00b04bb..88bbad1 100644 --- a/flow.json +++ b/flow.json @@ -8,7 +8,7 @@ } }, "FlowToken": { - "source": "./contracts/utility/FlowToken.cdc", + "source": "./contracts/standard/FlowToken.cdc", "aliases": { "emulator": "0ae53cb6e3f42a79", "mainnet": "1654653399040a61", @@ -16,7 +16,7 @@ } }, "FungibleToken": { - "source": "./contracts/utility/FungibleToken.cdc", + "source": "./contracts/standard/FungibleToken.cdc", "aliases": { "emulator": "ee82856bf20e2aa6", "mainnet": "f233dcee88fe0abe", @@ -24,7 +24,7 @@ } }, "MetadataViews": { - "source": "./contracts/utility/MetadataViews.cdc", + "source": "./contracts/standard/MetadataViews.cdc", "aliases": { "emulator": "f8d6e0586b0a20c7", "mainnet": "1d7e57aa55817448", @@ -32,7 +32,7 @@ } }, "NonFungibleToken": { - "source": "./contracts/utility/NonFungibleToken.cdc", + "source": "./contracts/standard/NonFungibleToken.cdc", "aliases": { "emulator": "f8d6e0586b0a20c7", "mainnet": "1d7e57aa55817448", @@ -40,16 +40,16 @@ } }, "RandomBeaconHistory": { - "source": "./contracts/RandomBeaconHistory.cdc", + "source": "./contracts/standard/RandomBeaconHistory.cdc", "aliases": { "emulator": "f8d6e0586b0a20c7", "mainnet": "e467b9dd11fa00df", "testnet": "8c5303eaa26202d6" } }, - "RandomResultStorage": "./contracts/utility/RandomResultStorage.cdc", + "RandomResultStorage": "./contracts/test/RandomResultStorage.cdc", "ViewResolver": { - "source": "./contracts/utility/ViewResolver.cdc", + "source": "./contracts/standard/ViewResolver.cdc", "aliases": { "emulator": "f8d6e0586b0a20c7", "mainnet": "1d7e57aa55817448", diff --git a/go.mod b/go.mod index 2a7f7b6..aeb3b41 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.19 require ( github.com/bjartek/overflow v1.14.1 - github.com/onflow/cadence v0.42.2 github.com/onflow/flow-go/crypto v0.24.10 github.com/stretchr/testify v1.8.4 ) @@ -108,6 +107,7 @@ require ( github.com/multiformats/go-varint v0.0.7 // indirect github.com/nightlyone/lockfile v1.0.0 // indirect github.com/onflow/atree v0.6.0 // indirect + github.com/onflow/cadence v0.42.2 // indirect github.com/onflow/flow-cli/flowkit v1.4.5 // indirect github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20230703193002-53362441b57d // indirect github.com/onflow/flow-core-contracts/lib/go/templates v1.2.3 // indirect diff --git a/prg_test_helpers.go b/prg_test_helpers.go index ed5dc94..dd89e20 100644 --- a/prg_test_helpers.go +++ b/prg_test_helpers.go @@ -36,10 +36,10 @@ func GetNextUInt64NewPRGWithSalt( var value uint64 err := o.Script( - "xorshift128plus/next_uint64", + "test/next_uint64", WithArg("sourceOfRandomness", seed), WithArg("salt", salt), - ).MarshalAs(value) + ).MarshalAs(&value) require.NoError(t, err) @@ -54,7 +54,7 @@ func GetResultsFromRandomResultStorage( ) []uint64 { t.Helper() var uint64Array []uint64 - err := o.Script("random-result-storage/get_results").MarshalAs(uint64Array) + err := o.Script("test/get_results").MarshalAs(&uint64Array) require.NoError(t, err) return uint64Array } @@ -70,10 +70,10 @@ func GetResultsInRangeFromRandomResultStorage( var uint64Array []uint64 err := o.Script( - "random-result-storage/get_results_in_range", + "test/get_results_in_range", WithArg("from", from), WithArg("upTo", upTo), - ).MarshalAs(uint64Array) + ).MarshalAs(&uint64Array) require.NoError(t, err) return uint64Array @@ -89,7 +89,7 @@ func GenerateResultsAndStore( ) { t.Helper() o.Tx( - "random-result-storage/generate_results", + "test/generate_results", WithSignerServiceAccount(), WithArg("generationLength", length), ).AssertSuccess(t) @@ -104,7 +104,7 @@ func InitializePRG( ) { t.Helper() o.Tx( - "random-result-storage/initialize_prg", + "test/initialize_prg", WithSignerServiceAccount(), WithArg("sourceOfRandomness", seed), WithArg("salt", salt), diff --git a/scripts/random-result-storage/get_results.cdc b/scripts/test/get_results.cdc similarity index 100% rename from scripts/random-result-storage/get_results.cdc rename to scripts/test/get_results.cdc diff --git a/scripts/random-result-storage/get_results_in_range.cdc b/scripts/test/get_results_in_range.cdc similarity index 100% rename from scripts/random-result-storage/get_results_in_range.cdc rename to scripts/test/get_results_in_range.cdc diff --git a/scripts/xorshift128plus/next_uint64.cdc b/scripts/test/next_uint64.cdc similarity index 81% rename from scripts/xorshift128plus/next_uint64.cdc rename to scripts/test/next_uint64.cdc index 0a5c69d..0bdff02 100644 --- a/scripts/xorshift128plus/next_uint64.cdc +++ b/scripts/test/next_uint64.cdc @@ -4,6 +4,6 @@ import "Xorshift128plus" /// /// Note that if the PRG were stored onchain, this script would not advance the state of the PRG /// -pub fun main(sourceOfRandomness: [UInt8], salt: [UInt8]): UInt64 { +access(all) fun main(sourceOfRandomness: [UInt8], salt: [UInt8]): UInt64 { return Xorshift128plus.PRG(sourceOfRandomness: sourceOfRandomness, salt: salt).nextUInt64() } diff --git a/scripts/xorshift128plus/get_array_next_uint64.cdc b/scripts/xorshift128plus/get_array_next_uint64.cdc deleted file mode 100644 index a1adae7..0000000 --- a/scripts/xorshift128plus/get_array_next_uint64.cdc +++ /dev/null @@ -1,25 +0,0 @@ -import "Xorshift128plus" - -/// Generates an arbitrary number of random numbers using the Xorshift128plus.PRG struct. -/// This script demonstrates how one would go about generating any number of random numbers using the -/// example PRG implemented in Xorshift128plus. -/// -/// Note that if the PRG were stored onchain, this script would not advance the state of the PRG -/// -access(all) fun main(sourceOfRandomness: [UInt8], salt: [UInt8], generationLength: Int): [UInt64] { - let prg = Xorshift128plus.PRG( - sourceOfRandomness: sourceOfRandomness, - salt: salt - ) - - var i: Int = 0 - let randomNumbers: [UInt64] = [] - // Generate the desired number of random numbers - while i < generationLength { - prg.nextUInt64() - i = i + 1 - randomNumbers.append(prg.nextUInt64()) - } - - return randomNumbers -} diff --git a/transactions/random-result-storage/generate_results.cdc b/transactions/test/generate_results.cdc similarity index 100% rename from transactions/random-result-storage/generate_results.cdc rename to transactions/test/generate_results.cdc diff --git a/transactions/random-result-storage/initialize_prg.cdc b/transactions/test/initialize_prg.cdc similarity index 100% rename from transactions/random-result-storage/initialize_prg.cdc rename to transactions/test/initialize_prg.cdc diff --git a/transactions/xorshift128plus/iterative_next_uint64.cdc b/transactions/xorshift128plus/iterative_next_uint64.cdc deleted file mode 100644 index 2c6b657..0000000 --- a/transactions/xorshift128plus/iterative_next_uint64.cdc +++ /dev/null @@ -1,29 +0,0 @@ -import "Xorshift128plus" - -/// Generates an arbitrary number of random numbers using the Xorshift128plus.PRG struct. -/// While the values generated in this transaction are not used or stored, this transaction demonstrates how one would -/// go about generating any number of random numbers using the Xorshift128plus.PRG. If desired, the generated numbers -/// could be stored in some resource or used in a transaction. -/// -/// Note that since transactions are mutating, calls to `nextUInt64` will advance the PRG state. -/// -transaction(sourceOfRandomness: [UInt8], salt: [UInt8], generationLength: Int) { - - prepare(signer: AuthAccount) { - let prg = Xorshift128plus.PRG( - sourceOfRandomness: sourceOfRandomness, - salt: salt - ) - - var i: Int = 0 - let randomNumbers: [UInt64] = [] - // Generate the desired number of random numbers - while i < generationLength { - prg.nextUInt64() - i = i + 1 - randomNumbers.append(prg.nextUInt64()) - } - - // Can then continue using array of random numbers for your use case... - } -}