From d75189290c2ed557d35ad6ba49d18a853d10eb0d Mon Sep 17 00:00:00 2001 From: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:10:21 -0600 Subject: [PATCH 1/6] replace pub instance with access(all) --- scripts/xorshift128plus/next_uint64.cdc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/xorshift128plus/next_uint64.cdc b/scripts/xorshift128plus/next_uint64.cdc index 0a5c69d..0bdff02 100644 --- a/scripts/xorshift128plus/next_uint64.cdc +++ b/scripts/xorshift128plus/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() } From 44709cc0d770b5b5affce204c379a6e1a1f78156 Mon Sep 17 00:00:00 2001 From: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:43:52 -0600 Subject: [PATCH 2/6] restructure project directories --- contracts/{ => example}/CoinToss.cdc | 0 contracts/{ => example}/Xorshift128plus.cdc | 0 contracts/{utility => standard}/FlowToken.cdc | 0 .../{utility => standard}/FungibleToken.cdc | 0 .../{utility => standard}/MetadataViews.cdc | 0 .../NonFungibleToken.cdc | 0 .../{ => standard}/RandomBeaconHistory.cdc | 0 .../{utility => standard}/ViewResolver.cdc | 0 .../{utility => test}/RandomResultStorage.cdc | 0 flow.json | 18 ++++++------ go.mod | 2 +- prg_test_helpers.go | 10 +++---- .../get_results.cdc | 0 .../get_results_in_range.cdc | 0 .../{xorshift128plus => test}/next_uint64.cdc | 0 .../xorshift128plus/get_array_next_uint64.cdc | 25 ---------------- .../generate_results.cdc | 0 .../initialize_prg.cdc | 0 .../xorshift128plus/iterative_next_uint64.cdc | 29 ------------------- 19 files changed, 15 insertions(+), 69 deletions(-) rename contracts/{ => example}/CoinToss.cdc (100%) rename contracts/{ => example}/Xorshift128plus.cdc (100%) rename contracts/{utility => standard}/FlowToken.cdc (100%) rename contracts/{utility => standard}/FungibleToken.cdc (100%) rename contracts/{utility => standard}/MetadataViews.cdc (100%) rename contracts/{utility => standard}/NonFungibleToken.cdc (100%) rename contracts/{ => standard}/RandomBeaconHistory.cdc (100%) rename contracts/{utility => standard}/ViewResolver.cdc (100%) rename contracts/{utility => test}/RandomResultStorage.cdc (100%) rename scripts/{random-result-storage => test}/get_results.cdc (100%) rename scripts/{random-result-storage => test}/get_results_in_range.cdc (100%) rename scripts/{xorshift128plus => test}/next_uint64.cdc (100%) delete mode 100644 scripts/xorshift128plus/get_array_next_uint64.cdc rename transactions/{random-result-storage => test}/generate_results.cdc (100%) rename transactions/{random-result-storage => test}/initialize_prg.cdc (100%) delete mode 100644 transactions/xorshift128plus/iterative_next_uint64.cdc diff --git a/contracts/CoinToss.cdc b/contracts/example/CoinToss.cdc similarity index 100% rename from contracts/CoinToss.cdc rename to contracts/example/CoinToss.cdc diff --git a/contracts/Xorshift128plus.cdc b/contracts/example/Xorshift128plus.cdc similarity index 100% rename from contracts/Xorshift128plus.cdc rename to contracts/example/Xorshift128plus.cdc 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..b7e63ef 100644 --- a/flow.json +++ b/flow.json @@ -1,14 +1,14 @@ { "contracts": { "CoinToss": { - "source": "./contracts/CoinToss.cdc", + "source": "./contracts/example/CoinToss.cdc", "aliases": { "emulator": "f8d6e0586b0a20c7", "testnet": "d1299e755e8be5e7" } }, "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", @@ -57,7 +57,7 @@ } }, "Xorshift128plus": { - "source": "./contracts/Xorshift128plus.cdc", + "source": "./contracts/example/Xorshift128plus.cdc", "aliases": { "emulator": "f8d6e0586b0a20c7", "testnet": "ed24dbe901028c5c" 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..737b679 100644 --- a/prg_test_helpers.go +++ b/prg_test_helpers.go @@ -36,7 +36,7 @@ func GetNextUInt64NewPRGWithSalt( var value uint64 err := o.Script( - "xorshift128plus/next_uint64", + "test/next_uint64", WithArg("sourceOfRandomness", seed), WithArg("salt", salt), ).MarshalAs(value) @@ -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,7 +70,7 @@ 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) @@ -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 100% rename from scripts/xorshift128plus/next_uint64.cdc rename to scripts/test/next_uint64.cdc 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... - } -} From 7f9ebdcbd9252550095a8e8b35b645a171e8ae28 Mon Sep 17 00:00:00 2001 From: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:58:30 -0600 Subject: [PATCH 3/6] update ci workflow to run on prg test updates --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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: From efdce3a0fe0e435ebec01d18621524a59c3a8cc4 Mon Sep 17 00:00:00 2001 From: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:58:51 -0600 Subject: [PATCH 4/6] fix test helper bug when marshalling value --- prg_test_helpers.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prg_test_helpers.go b/prg_test_helpers.go index 737b679..dd89e20 100644 --- a/prg_test_helpers.go +++ b/prg_test_helpers.go @@ -39,7 +39,7 @@ func GetNextUInt64NewPRGWithSalt( "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("test/get_results").MarshalAs(uint64Array) + err := o.Script("test/get_results").MarshalAs(&uint64Array) require.NoError(t, err) return uint64Array } @@ -73,7 +73,7 @@ func GetResultsInRangeFromRandomResultStorage( "test/get_results_in_range", WithArg("from", from), WithArg("upTo", upTo), - ).MarshalAs(uint64Array) + ).MarshalAs(&uint64Array) require.NoError(t, err) return uint64Array From c7405120ec84d41e05067497106573f2c1283b86 Mon Sep 17 00:00:00 2001 From: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> Date: Thu, 16 Nov 2023 14:02:17 -0600 Subject: [PATCH 5/6] move CoinToss & Xorshift.. to ./contracts --- contracts/{example => }/CoinToss.cdc | 0 contracts/{example => }/Xorshift128plus.cdc | 0 flow.json | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename contracts/{example => }/CoinToss.cdc (100%) rename contracts/{example => }/Xorshift128plus.cdc (100%) diff --git a/contracts/example/CoinToss.cdc b/contracts/CoinToss.cdc similarity index 100% rename from contracts/example/CoinToss.cdc rename to contracts/CoinToss.cdc diff --git a/contracts/example/Xorshift128plus.cdc b/contracts/Xorshift128plus.cdc similarity index 100% rename from contracts/example/Xorshift128plus.cdc rename to contracts/Xorshift128plus.cdc diff --git a/flow.json b/flow.json index b7e63ef..88bbad1 100644 --- a/flow.json +++ b/flow.json @@ -1,7 +1,7 @@ { "contracts": { "CoinToss": { - "source": "./contracts/example/CoinToss.cdc", + "source": "./contracts/CoinToss.cdc", "aliases": { "emulator": "f8d6e0586b0a20c7", "testnet": "d1299e755e8be5e7" @@ -57,7 +57,7 @@ } }, "Xorshift128plus": { - "source": "./contracts/example/Xorshift128plus.cdc", + "source": "./contracts/Xorshift128plus.cdc", "aliases": { "emulator": "f8d6e0586b0a20c7", "testnet": "ed24dbe901028c5c" From 46f4632d2694d2173f681727ebe3d9e04043044f Mon Sep 17 00:00:00 2001 From: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> Date: Thu, 16 Nov 2023 14:40:23 -0600 Subject: [PATCH 6/6] comment edit in CoinToss contract --- contracts/CoinToss.cdc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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