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

[v0.42] Port adding new revertibleRandom function #2910

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion runtime/common/computationkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const (
//
ComputationKindSTDLIBPanic
ComputationKindSTDLIBAssert
ComputationKindSTDLIBUnsafeRandom
ComputationKindSTDLIBRevertibleRandom
_
_
_
Expand Down
6 changes: 3 additions & 3 deletions runtime/common/computationkind_string.go

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

2 changes: 1 addition & 1 deletion runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type interpreterEnvironment struct {

var _ Environment = &interpreterEnvironment{}
var _ stdlib.Logger = &interpreterEnvironment{}
var _ stdlib.UnsafeRandomGenerator = &interpreterEnvironment{}
var _ stdlib.RandomGenerator = &interpreterEnvironment{}
var _ stdlib.BlockAtHeightProvider = &interpreterEnvironment{}
var _ stdlib.CurrentBlockProvider = &interpreterEnvironment{}
var _ stdlib.PublicAccountHandler = &interpreterEnvironment{}
Expand Down
9 changes: 6 additions & 3 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4734,7 +4734,7 @@ func TestRuntimeBlock(t *testing.T) {
)
}

func TestRuntimeUnsafeRandom(t *testing.T) {
func TestRuntimeRandom(t *testing.T) {

t.Parallel()

Expand All @@ -4743,8 +4743,10 @@ func TestRuntimeUnsafeRandom(t *testing.T) {
script := []byte(`
transaction {
prepare() {
let rand = unsafeRandom()
log(rand)
let rand1 = revertibleRandom()
log(rand1)
let rand2 = unsafeRandom()
log(rand2)
}
}
`)
Expand Down Expand Up @@ -4777,6 +4779,7 @@ func TestRuntimeUnsafeRandom(t *testing.T) {
assert.Equal(t,
[]string{
"7558174677681708339",
"7558174677681708339",
},
loggedMessages,
)
Expand Down
3 changes: 2 additions & 1 deletion runtime/stdlib/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package stdlib

type StandardLibraryHandler interface {
Logger
UnsafeRandomGenerator
RandomGenerator
BlockAtHeightProvider
CurrentBlockProvider
PublicAccountHandler
Expand All @@ -40,6 +40,7 @@ func DefaultStandardLibraryValues(handler StandardLibraryHandler) []StandardLibr
SignatureAlgorithmConstructor,
RLPContract,
NewLogFunction(handler),
NewRevertibleRandomFunction(handler),
NewUnsafeRandomFunction(handler),
NewGetBlockFunction(handler),
NewGetCurrentBlockFunction(handler),
Expand Down
46 changes: 42 additions & 4 deletions runtime/stdlib/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,64 @@
"github.com/onflow/cadence/runtime/sema"
)

const unsafeRandomFunctionDocString = `
const revertibleRandomFunctionDocString = `
Returns a pseudo-random number.

NOTE: The use of this function is unsafe if not used correctly.

Follow best practices to prevent security issues when using this function
`

var unsafeRandomFunctionType = &sema.FunctionType{
var revertibleRandomFunctionType = &sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(
sema.UInt64Type,
),
}

type UnsafeRandomGenerator interface {
type RandomGenerator interface {
// ReadRandom reads pseudo-random bytes into the input slice, using distributed randomness.
ReadRandom([]byte) error
}

func NewUnsafeRandomFunction(generator UnsafeRandomGenerator) StandardLibraryValue {
func NewRevertibleRandomFunction(generator RandomGenerator) StandardLibraryValue {
return NewStandardLibraryFunction(
"revertibleRandom",
revertibleRandomFunctionType,
revertibleRandomFunctionDocString,
func(invocation interpreter.Invocation) interpreter.Value {
return interpreter.NewUInt64Value(
invocation.Interpreter,
func() uint64 {
var buffer [8]byte
var err error
errors.WrapPanic(func() {
err = generator.ReadRandom(buffer[:])
})
if err != nil {
panic(interpreter.WrappedExternalError(err))

Check warning on line 63 in runtime/stdlib/random.go

View check run for this annotation

Codecov / codecov/patch

runtime/stdlib/random.go#L63

Added line #L63 was not covered by tests
}
return binary.LittleEndian.Uint64(buffer[:])
},
)
},
)
}

// `unsafeRandom` related constants and functions will be deleted
// when the function is deprecated
const unsafeRandomFunctionDocString = `
Deprecated: Use revertibleRandom instead.

Returns a pseudo-random number.

NOTE: The use of this function is unsafe if not used correctly.

Follow best practices to prevent security issues when using this function
`

var unsafeRandomFunctionType = revertibleRandomFunctionType

func NewUnsafeRandomFunction(generator RandomGenerator) StandardLibraryValue {
return NewStandardLibraryFunction(
"unsafeRandom",
unsafeRandomFunctionType,
Expand Down
Loading