Skip to content

Commit

Permalink
fix implementation detail in prg_tests & update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Oct 28, 2023
1 parent e156b1b commit 6adf6f5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/go/templates/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion lib/go/test/prg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ func TestNextUInt64NewPRG(t *testing.T) {
n := 1 << (1 + mrand.Intn(10))
classWidth := (math.MaxUint64 / uint64(n)) + 1

// using the same seed, the salt varies in getNextUInt64()
seed := GetRandomSeed(t)

uintf := func() (uint64, error) {
return getNextUInt64(t, b, adapter, prgAddress)
return GetNextUInt64NewPRG(t, b, adapter, prgAddress, seed)
}

random.BasicDistributionTest(t, uint64(n), uint64(classWidth), uintf)
Expand Down
11 changes: 8 additions & 3 deletions lib/go/test/prg_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@ func deployPRGContract(
return prgAddress
}

func getNextUInt64(
// calls a script which creates a new PRG resource from the given seed and
// random salt, gets the next uint64 and destroys the prg resource before
// returning the next uint64
func GetNextUInt64NewPRG(
t *testing.T,
b emulator.Emulator,
adapter *adapters.SDKAdapter,
prgAddress flow.Address,
seed []byte,
) (uint64, error) {

script := templates.GenerateNextUInt64NewPRG(prgAddress)
result, err := b.ExecuteScript(
script,
[][]byte{
jsoncdc.MustEncode(bytesToCadenceArray(GetRandomSeed(t))),
jsoncdc.MustEncode(bytesToCadenceArray(seed)),
jsoncdc.MustEncode(cadence.NewUInt64(GetRandomSalt(t))),
},
)
Expand All @@ -48,10 +52,10 @@ func getNextUInt64(
t.Log(result.Error.Error())
}

// return uint64(result.Value.String()), err
return CadenceUInt64ToUInt64(result.Value), err
}

// gets a random 32 byte array using crypto/rand
func GetRandomSeed(t *testing.T) []byte {
buf := make([]byte, 32)

Expand All @@ -61,6 +65,7 @@ func GetRandomSeed(t *testing.T) []byte {
return buf
}

// gets a random uint64 using crypto/rand
func GetRandomSalt(t *testing.T) uint64 {
var b [8]byte

Expand Down
3 changes: 2 additions & 1 deletion transactions/pseudo-random-generator/next_uint64.cdc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "PseudoRandomGenerator"

/// Saves and links a .PRG resource in the signer's storage and public namespace
/// For demonstration - generates the given number of random numbers
/// These values might be passed to a consuming contract or resource
///
transaction(generationLength: Int) {
prepare(signer: AuthAccount) {
Expand Down

0 comments on commit 6adf6f5

Please sign in to comment.