Skip to content

Commit

Permalink
Merge pull request #2910 from onflow/supun/port-revertible-random
Browse files Browse the repository at this point in the history
[v0.42] Port adding new `revertibleRandom` function
  • Loading branch information
SupunS authored Oct 30, 2023
2 parents 50c54a9 + 81309d4 commit 529b34d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
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 @@ import (
"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))
}
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

0 comments on commit 529b34d

Please sign in to comment.