Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade cadence and change to ReadRandom #4679

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions fvm/environment/facade_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,3 @@ func (env *facadeEnvironment) SetInterpreterSharedState(state *interpreter.Share
func (env *facadeEnvironment) GetInterpreterSharedState() *interpreter.SharedState {
return nil
}

func (env *facadeEnvironment) ReadRandom(buffer []byte) error {
// NO-OP for now, to unblock certain downstream dependencies.
// E.g. cadence-tools/test
return nil
}
38 changes: 14 additions & 24 deletions fvm/environment/mock/environment.go

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

26 changes: 8 additions & 18 deletions fvm/environment/mock/random_generator.go

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

33 changes: 14 additions & 19 deletions fvm/environment/random_generator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package environment

import (
"encoding/binary"
"fmt"

"github.com/onflow/flow-go/crypto/random"
Expand All @@ -23,9 +22,9 @@ type EntropyProvider interface {
}

type RandomGenerator interface {
// UnsafeRandom returns a random uint64
// ReadRandom reads pseudo-random bytes into the input slice, using distributed randomness.
// The name follows Cadence interface
UnsafeRandom() (uint64, error)
ReadRandom([]byte) error
}

var _ RandomGenerator = (*randomGenerator)(nil)
Expand Down Expand Up @@ -55,14 +54,12 @@ func NewParseRestrictedRandomGenerator(
}
}

func (gen ParseRestrictedRandomGenerator) UnsafeRandom() (
uint64,
error,
) {
return parseRestrict1Ret(
func (gen ParseRestrictedRandomGenerator) ReadRandom(buf []byte) error {
return parseRestrict1Arg(
gen.txnState,
trace.FVMEnvRandom,
gen.impl.UnsafeRandom)
gen.impl.ReadRandom,
buf)
}

func NewRandomGenerator(
Expand Down Expand Up @@ -104,28 +101,27 @@ func (gen *randomGenerator) createPRG() (random.Rand, error) {
return csprg, nil
}

// UnsafeRandom returns a random uint64 using the underlying PRG (currently
// ReadRandom reads pseudo-random bytes into the input slice using the underlying PRG (currently
// using a crypto-secure one). This function is not thread safe, due to the gen.prg
// instance currently used. This is fine because a
// single transaction has a single RandomGenerator and is run in a single
// thread.
func (gen *randomGenerator) UnsafeRandom() (uint64, error) {
func (gen *randomGenerator) ReadRandom(buf []byte) error {
defer gen.tracer.StartExtensiveTracingChildSpan(
trace.FVMEnvRandom).End()

// PRG creation is only done once.
if !gen.isPRGCreated {
newPRG, err := gen.createPRG()
if err != nil {
return 0, err
return err
}
gen.prg = newPRG
gen.isPRGCreated = true
}

buf := make([]byte, 8)
gen.prg.Read(buf) // Note: prg.Read does not return error
return binary.LittleEndian.Uint64(buf), nil
gen.prg.Read(buf)
return nil
}

var _ RandomGenerator = (*dummyRandomGenerator)(nil)
Expand All @@ -138,8 +134,7 @@ func NewDummyRandomGenerator() RandomGenerator {
return &dummyRandomGenerator{}
}

// UnsafeRandom() returns an error because executing scripts
// does not support randomness APIs.
func (gen *dummyRandomGenerator) UnsafeRandom() (uint64, error) {
return 0, nil
// ReadRandom does nothing, because scripts do not support randomness APIs.
func (gen *dummyRandomGenerator) ReadRandom(buf []byte) error {
return nil
}
15 changes: 12 additions & 3 deletions fvm/environment/random_generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package environment_test

import (
"encoding/binary"
"math"
mrand "math/rand"
"testing"
Expand All @@ -27,9 +28,10 @@ func TestRandomGenerator(t *testing.T) {
txId)
numbers := make([]uint64, N)
for i := 0; i < N; i++ {
u, err := urg.UnsafeRandom()
var buffer [8]byte
err := urg.ReadRandom(buffer[:])
require.NoError(t, err)
numbers[i] = u
numbers[i] = binary.LittleEndian.Uint64(buffer[:])
}
return numbers
}
Expand All @@ -48,7 +50,14 @@ func TestRandomGenerator(t *testing.T) {
// n is a random power of 2 (from 2 to 2^10)
n := 1 << (1 + mrand.Intn(10))
classWidth := (math.MaxUint64 / uint64(n)) + 1
random.BasicDistributionTest(t, uint64(n), uint64(classWidth), urg.UnsafeRandom)
random.BasicDistributionTest(t, uint64(n), uint64(classWidth), func() (uint64, error) {
var buffer [8]byte
err := urg.ReadRandom(buffer[:])
if err != nil {
return 0, err
}
return binary.LittleEndian.Uint64(buffer[:]), nil
})
}
})

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ require (
github.com/multiformats/go-multiaddr-dns v0.3.1
github.com/multiformats/go-multihash v0.2.3
github.com/onflow/atree v0.6.0
github.com/onflow/cadence v0.40.0
github.com/onflow/cadence v0.40.1-0.20230830181337-52549529e96b
github.com/onflow/flow v0.3.4
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20230703193002-53362441b57d
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1240,8 +1240,8 @@ github.com/onflow/atree v0.1.0-beta1.0.20211027184039-559ee654ece9/go.mod h1:+6x
github.com/onflow/atree v0.6.0 h1:j7nQ2r8npznx4NX39zPpBYHmdy45f4xwoi+dm37Jk7c=
github.com/onflow/atree v0.6.0/go.mod h1:gBHU0M05qCbv9NN0kijLWMgC47gHVNBIp4KmsVFi0tc=
github.com/onflow/cadence v0.20.1/go.mod h1:7mzUvPZUIJztIbr9eTvs+fQjWWHTF8veC+yk4ihcNIA=
github.com/onflow/cadence v0.40.0 h1:3pTdkyVTjMx2U5+YZYvIpyw74CSxabjk9PdAZUkJ1GU=
github.com/onflow/cadence v0.40.0/go.mod h1:OIJLyVBPa339DCBQXBfGaorT4tBjQh9gSKe+ZAIyyh0=
github.com/onflow/cadence v0.40.1-0.20230830181337-52549529e96b h1:n9/WMBooqLRXERLYMCa/23qrULdyfBvio2OlGTuo/iM=
github.com/onflow/cadence v0.40.1-0.20230830181337-52549529e96b/go.mod h1:2ggmhENvPPZXRnv9SmqN2xiYvluu4wx/96GCpeRh8lU=
github.com/onflow/flow v0.3.4 h1:FXUWVdYB90f/rjNcY0Owo30gL790tiYff9Pb/sycXYE=
github.com/onflow/flow v0.3.4/go.mod h1:lzyAYmbu1HfkZ9cfnL5/sjrrsnJiUU8fRL26CqLP7+c=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20230703193002-53362441b57d h1:B7PdhdUNkve5MVrekWDuQf84XsGBxNZ/D3x+QQ8XeVs=
Expand Down
2 changes: 1 addition & 1 deletion insecure/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ require (
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onflow/atree v0.6.0 // indirect
github.com/onflow/cadence v0.40.0 // indirect
github.com/onflow/cadence v0.40.1-0.20230830181337-52549529e96b // 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
github.com/onflow/flow-ft/lib/go/contracts v0.7.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions insecure/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1216,8 +1216,8 @@ github.com/onflow/atree v0.1.0-beta1.0.20211027184039-559ee654ece9/go.mod h1:+6x
github.com/onflow/atree v0.6.0 h1:j7nQ2r8npznx4NX39zPpBYHmdy45f4xwoi+dm37Jk7c=
github.com/onflow/atree v0.6.0/go.mod h1:gBHU0M05qCbv9NN0kijLWMgC47gHVNBIp4KmsVFi0tc=
github.com/onflow/cadence v0.20.1/go.mod h1:7mzUvPZUIJztIbr9eTvs+fQjWWHTF8veC+yk4ihcNIA=
github.com/onflow/cadence v0.40.0 h1:3pTdkyVTjMx2U5+YZYvIpyw74CSxabjk9PdAZUkJ1GU=
github.com/onflow/cadence v0.40.0/go.mod h1:OIJLyVBPa339DCBQXBfGaorT4tBjQh9gSKe+ZAIyyh0=
github.com/onflow/cadence v0.40.1-0.20230830181337-52549529e96b h1:n9/WMBooqLRXERLYMCa/23qrULdyfBvio2OlGTuo/iM=
github.com/onflow/cadence v0.40.1-0.20230830181337-52549529e96b/go.mod h1:2ggmhENvPPZXRnv9SmqN2xiYvluu4wx/96GCpeRh8lU=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20230703193002-53362441b57d h1:B7PdhdUNkve5MVrekWDuQf84XsGBxNZ/D3x+QQ8XeVs=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20230703193002-53362441b57d/go.mod h1:xAiV/7TKhw863r6iO3CS5RnQ4F+pBY1TxD272BsILlo=
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.3 h1:X25A1dNajNUtE+KoV76wQ6BR6qI7G65vuuRXxDDqX7E=
Expand Down
2 changes: 1 addition & 1 deletion integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ds-badger2 v0.1.3
github.com/ipfs/go-ipfs-blockstore v1.3.0
github.com/onflow/cadence v0.40.0
github.com/onflow/cadence v0.40.1-0.20230830181337-52549529e96b
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20230703193002-53362441b57d
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.3
github.com/onflow/flow-emulator v0.53.0
Expand Down
4 changes: 2 additions & 2 deletions integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,8 @@ github.com/onflow/atree v0.1.0-beta1.0.20211027184039-559ee654ece9/go.mod h1:+6x
github.com/onflow/atree v0.6.0 h1:j7nQ2r8npznx4NX39zPpBYHmdy45f4xwoi+dm37Jk7c=
github.com/onflow/atree v0.6.0/go.mod h1:gBHU0M05qCbv9NN0kijLWMgC47gHVNBIp4KmsVFi0tc=
github.com/onflow/cadence v0.20.1/go.mod h1:7mzUvPZUIJztIbr9eTvs+fQjWWHTF8veC+yk4ihcNIA=
github.com/onflow/cadence v0.40.0 h1:3pTdkyVTjMx2U5+YZYvIpyw74CSxabjk9PdAZUkJ1GU=
github.com/onflow/cadence v0.40.0/go.mod h1:OIJLyVBPa339DCBQXBfGaorT4tBjQh9gSKe+ZAIyyh0=
github.com/onflow/cadence v0.40.1-0.20230830181337-52549529e96b h1:n9/WMBooqLRXERLYMCa/23qrULdyfBvio2OlGTuo/iM=
github.com/onflow/cadence v0.40.1-0.20230830181337-52549529e96b/go.mod h1:2ggmhENvPPZXRnv9SmqN2xiYvluu4wx/96GCpeRh8lU=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20230703193002-53362441b57d h1:B7PdhdUNkve5MVrekWDuQf84XsGBxNZ/D3x+QQ8XeVs=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20230703193002-53362441b57d/go.mod h1:xAiV/7TKhw863r6iO3CS5RnQ4F+pBY1TxD272BsILlo=
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.3 h1:X25A1dNajNUtE+KoV76wQ6BR6qI7G65vuuRXxDDqX7E=
Expand Down
Loading