From 9e1716b6c81738b76f0682f7f34411e3ad206aac Mon Sep 17 00:00:00 2001 From: Tarak Ben Youssef Date: Tue, 24 Oct 2023 14:57:23 -0500 Subject: [PATCH 1/7] rename the generator interface to RandomGenerator --- runtime/environment.go | 2 +- runtime/stdlib/builtin.go | 2 +- runtime/stdlib/random.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/runtime/environment.go b/runtime/environment.go index 426f38dcfe..2d837c386d 100644 --- a/runtime/environment.go +++ b/runtime/environment.go @@ -126,7 +126,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{} diff --git a/runtime/stdlib/builtin.go b/runtime/stdlib/builtin.go index 60e0111fc2..329ca2229d 100644 --- a/runtime/stdlib/builtin.go +++ b/runtime/stdlib/builtin.go @@ -20,7 +20,7 @@ package stdlib type StandardLibraryHandler interface { Logger - UnsafeRandomGenerator + RandomGenerator BlockAtHeightProvider CurrentBlockProvider PublicAccountHandler diff --git a/runtime/stdlib/random.go b/runtime/stdlib/random.go index 558d87f023..8c0a97b623 100644 --- a/runtime/stdlib/random.go +++ b/runtime/stdlib/random.go @@ -40,12 +40,12 @@ var unsafeRandomFunctionType = &sema.FunctionType{ ), } -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 NewUnsafeRandomFunction(generator RandomGenerator) StandardLibraryValue { return NewStandardLibraryFunction( "unsafeRandom", unsafeRandomFunctionType, From 86de31ba284c9afe536914fa0deb98545e930dd3 Mon Sep 17 00:00:00 2001 From: Tarak Ben Youssef Date: Tue, 24 Oct 2023 15:40:34 -0500 Subject: [PATCH 2/7] add new standard library value for revertibleRandom --- runtime/stdlib/builtin.go | 1 + runtime/stdlib/random.go | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/runtime/stdlib/builtin.go b/runtime/stdlib/builtin.go index 329ca2229d..3ec0e2474a 100644 --- a/runtime/stdlib/builtin.go +++ b/runtime/stdlib/builtin.go @@ -40,6 +40,7 @@ func DefaultStandardLibraryValues(handler StandardLibraryHandler) []StandardLibr SignatureAlgorithmConstructor, RLPContract, NewLogFunction(handler), + NewRevertibleRandomFunction(handler), NewUnsafeRandomFunction(handler), NewGetBlockFunction(handler), NewGetCurrentBlockFunction(handler), diff --git a/runtime/stdlib/random.go b/runtime/stdlib/random.go index 8c0a97b623..3016002be7 100644 --- a/runtime/stdlib/random.go +++ b/runtime/stdlib/random.go @@ -26,7 +26,7 @@ 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. @@ -34,7 +34,7 @@ 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, ), @@ -45,6 +45,36 @@ type RandomGenerator interface { ReadRandom([]byte) error } +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 = revertibleRandomFunctionDocString + +var unsafeRandomFunctionType = revertibleRandomFunctionType + func NewUnsafeRandomFunction(generator RandomGenerator) StandardLibraryValue { return NewStandardLibraryFunction( "unsafeRandom", From 611ad45eb063ba6b0621882725e66a57cc343053 Mon Sep 17 00:00:00 2001 From: Tarak Ben Youssef Date: Tue, 24 Oct 2023 15:45:07 -0500 Subject: [PATCH 3/7] add revertibleRandom test --- runtime/runtime_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/runtime/runtime_test.go b/runtime/runtime_test.go index 8f1af02fb3..ae99cfc67a 100644 --- a/runtime/runtime_test.go +++ b/runtime/runtime_test.go @@ -4734,7 +4734,7 @@ func TestRuntimeBlock(t *testing.T) { ) } -func TestRuntimeUnsafeRandom(t *testing.T) { +func TestRuntimeRandom(t *testing.T) { t.Parallel() @@ -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) } } `) @@ -4777,6 +4779,7 @@ func TestRuntimeUnsafeRandom(t *testing.T) { assert.Equal(t, []string{ "7558174677681708339", + "7558174677681708339", }, loggedMessages, ) From 217c359a4e9786e2f506665fa963e80cc4955a51 Mon Sep 17 00:00:00 2001 From: Tarak Ben Youssef Date: Tue, 24 Oct 2023 16:04:42 -0500 Subject: [PATCH 4/7] fix indent --- runtime/runtime_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/runtime_test.go b/runtime/runtime_test.go index ae99cfc67a..f19f19b04d 100644 --- a/runtime/runtime_test.go +++ b/runtime/runtime_test.go @@ -4743,8 +4743,8 @@ func TestRuntimeRandom(t *testing.T) { script := []byte(` transaction { prepare() { - let rand1 = revertibleRandom() - log(rand1) + let rand1 = revertibleRandom() + log(rand1) let rand2 = unsafeRandom() log(rand2) } From 1d90ebe5e0b37c2e87d232237822c2715a129135 Mon Sep 17 00:00:00 2001 From: Tarak Ben Youssef Date: Thu, 26 Oct 2023 12:46:05 -0600 Subject: [PATCH 5/7] update computation constant name from revertible to unsafe --- runtime/common/computationkind.go | 2 +- runtime/common/computationkind_string.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/runtime/common/computationkind.go b/runtime/common/computationkind.go index 90b96e858e..68be5b6f9b 100644 --- a/runtime/common/computationkind.go +++ b/runtime/common/computationkind.go @@ -133,7 +133,7 @@ const ( // ComputationKindSTDLIBPanic ComputationKindSTDLIBAssert - ComputationKindSTDLIBUnsafeRandom + ComputationKindSTDLIBRevertibleRandom _ _ _ diff --git a/runtime/common/computationkind_string.go b/runtime/common/computationkind_string.go index 9ed621d72f..302fdd11bb 100644 --- a/runtime/common/computationkind_string.go +++ b/runtime/common/computationkind_string.go @@ -24,7 +24,7 @@ func _() { _ = x[ComputationKindEncodeValue-1080] _ = x[ComputationKindSTDLIBPanic-1100] _ = x[ComputationKindSTDLIBAssert-1101] - _ = x[ComputationKindSTDLIBUnsafeRandom-1102] + _ = x[ComputationKindSTDLIBRevertibleRandom-1102] _ = x[ComputationKindSTDLIBRLPDecodeString-1108] _ = x[ComputationKindSTDLIBRLPDecodeList-1109] } @@ -36,7 +36,7 @@ const ( _ComputationKind_name_3 = "CreateArrayValueTransferArrayValueDestroyArrayValue" _ComputationKind_name_4 = "CreateDictionaryValueTransferDictionaryValueDestroyDictionaryValue" _ComputationKind_name_5 = "EncodeValue" - _ComputationKind_name_6 = "STDLIBPanicSTDLIBAssertSTDLIBUnsafeRandom" + _ComputationKind_name_6 = "STDLIBPanicSTDLIBAssertSTDLIBRevertibleRandom" _ComputationKind_name_7 = "STDLIBRLPDecodeStringSTDLIBRLPDecodeList" ) @@ -45,7 +45,7 @@ var ( _ComputationKind_index_2 = [...]uint8{0, 20, 42, 63} _ComputationKind_index_3 = [...]uint8{0, 16, 34, 51} _ComputationKind_index_4 = [...]uint8{0, 21, 44, 66} - _ComputationKind_index_6 = [...]uint8{0, 11, 23, 41} + _ComputationKind_index_6 = [...]uint8{0, 11, 23, 45} _ComputationKind_index_7 = [...]uint8{0, 21, 40} ) From c6bbd52d1b11487025e8c85c50e5a59a64775ddf Mon Sep 17 00:00:00 2001 From: Tarak Ben Youssef Date: Thu, 26 Oct 2023 23:27:49 -0600 Subject: [PATCH 6/7] add deprecation warning to doc string --- runtime/stdlib/random.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/runtime/stdlib/random.go b/runtime/stdlib/random.go index 3016002be7..72eefcb293 100644 --- a/runtime/stdlib/random.go +++ b/runtime/stdlib/random.go @@ -71,7 +71,15 @@ func NewRevertibleRandomFunction(generator RandomGenerator) StandardLibraryValue // `unsafeRandom` related constants and functions will be deleted // when the function is deprecated -const unsafeRandomFunctionDocString = revertibleRandomFunctionDocString +const unsafeRandomFunctionDocString = ` +Warning: this function will be deprecated, use revertibleRandom function 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 From 7bade59a820791e0f129886165f2ae31921ff15d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Thu, 26 Oct 2023 23:19:14 -0700 Subject: [PATCH 7/7] Improve docstring, use "deprecated" keyword --- runtime/stdlib/random.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/stdlib/random.go b/runtime/stdlib/random.go index 72eefcb293..ffc28172fd 100644 --- a/runtime/stdlib/random.go +++ b/runtime/stdlib/random.go @@ -72,7 +72,7 @@ func NewRevertibleRandomFunction(generator RandomGenerator) StandardLibraryValue // `unsafeRandom` related constants and functions will be deleted // when the function is deprecated const unsafeRandomFunctionDocString = ` -Warning: this function will be deprecated, use revertibleRandom function instead. +Deprecated: Use revertibleRandom instead. Returns a pseudo-random number.