From 39a4229d9e71c6719b2e326f316dfa2f7be6b128 Mon Sep 17 00:00:00 2001 From: Alexey Kiselev Date: Fri, 3 Mar 2023 14:30:52 +0400 Subject: [PATCH] Fixed check function for scripts version before V3. (#1039) Check fucntions implemented as separate functions in evironment.go and used in tests. Old test check functions removed. --- pkg/ride/environment.go | 14 ++++-- pkg/ride/functions_bytes_test.go | 2 +- pkg/ride/functions_proto_test.go | 78 +++++++++++++++----------------- pkg/ride/test_helpers_test.go | 8 +--- pkg/ride/vm_test.go | 2 +- 5 files changed, 50 insertions(+), 54 deletions(-) diff --git a/pkg/ride/environment.go b/pkg/ride/environment.go index 8c05c3098..7323b13ee 100644 --- a/pkg/ride/environment.go +++ b/pkg/ride/environment.go @@ -1004,6 +1004,14 @@ type EvaluationEnvironment struct { cc complexityCalculator } +func bytesSizeCheckV1V2(l int) bool { + return l <= math.MaxInt32 +} + +func bytesSizeCheckV3V6(l int) bool { + return l <= proto.MaxDataWithProofsBytes +} + func NewEnvironment(scheme proto.Scheme, state types.SmartState, internalPaymentsValidationHeight uint64, blockV5, rideV6, consensusImprovements, invokeExpression bool, ) (*EvaluationEnvironment, error) { @@ -1015,7 +1023,7 @@ func NewEnvironment(scheme proto.Scheme, state types.SmartState, internalPayment sch: scheme, st: state, h: rideInt(height), - check: func(l int) bool { return l > math.MaxInt32 }, // By default almost unlimited + check: bytesSizeCheckV1V2, // By default almost unlimited takeStr: func(s string, n int) rideString { panic("function 'takeStr' was not initialized") }, validatePaymentsAfter: internalPaymentsValidationHeight, isBlockV5Activated: blockV5, @@ -1106,9 +1114,7 @@ func (e *EvaluationEnvironment) ChooseTakeString(isRideV5 bool) { func (e *EvaluationEnvironment) ChooseSizeCheck(v ast.LibraryVersion) { e.setLibVersion(v) if v > ast.LibV2 { - e.check = func(l int) bool { - return l <= proto.MaxDataWithProofsBytes - } + e.check = bytesSizeCheckV3V6 } } diff --git a/pkg/ride/functions_bytes_test.go b/pkg/ride/functions_bytes_test.go index 19e2b7112..361ac75d7 100644 --- a/pkg/ride/functions_bytes_test.go +++ b/pkg/ride/functions_bytes_test.go @@ -93,7 +93,7 @@ func TestDropBytes(t *testing.T) { } func TestConcatBytes(t *testing.T) { - te := &mockRideEnvironment{checkMessageLengthFunc: v2check} + te := &mockRideEnvironment{checkMessageLengthFunc: bytesSizeCheckV1V2} for _, test := range []struct { args []rideType fail bool diff --git a/pkg/ride/functions_proto_test.go b/pkg/ride/functions_proto_test.go index 7546304c6..6968bd0ed 100644 --- a/pkg/ride/functions_proto_test.go +++ b/pkg/ride/functions_proto_test.go @@ -19,12 +19,6 @@ import ( ) var ( - v2check = func(int) bool { - return true - } - v3check = func(size int) bool { - return size <= proto.MaxDataWithProofsBytes - } v5takeString = takeRideString noRideV6 = func() bool { return false @@ -587,18 +581,18 @@ func TestSigVerify(t *testing.T) { fail bool r rideType }{ - {[]rideType{rideBytes(msg), rideBytes(sig), rideBytes(pk)}, v2check, false, rideBoolean(true)}, - {[]rideType{rideBytes(msg), rideBytes(bad), rideBytes(pk)}, v2check, false, rideBoolean(false)}, - {[]rideType{rideBytes(msg), rideBytes(sig), rideBytes(pk[:10])}, v2check, false, rideBoolean(false)}, - {[]rideType{rideString("MESSAGE"), rideBytes(sig), rideBytes(pk)}, v2check, true, nil}, - {[]rideType{rideBytes(big), rideBytes(sig), rideBytes(pk)}, v2check, false, rideBoolean(false)}, - {[]rideType{rideBytes(big), rideBytes(sig), rideBytes(pk)}, v3check, true, nil}, - {[]rideType{rideBytes(msg), rideString("SIGNATURE"), rideBytes(pk)}, v2check, true, nil}, - {[]rideType{rideBytes(msg), rideBytes(sig), rideString("PUBLIC KEY")}, v2check, true, nil}, - {[]rideType{rideUnit{}}, v2check, true, nil}, - {[]rideType{}, v2check, true, nil}, - {[]rideType{rideInt(12345)}, v2check, true, nil}, - {[]rideType{rideString("dsfjsadfl"), rideInt(12345)}, v2check, true, nil}, + {[]rideType{rideBytes(msg), rideBytes(sig), rideBytes(pk)}, bytesSizeCheckV1V2, false, rideBoolean(true)}, + {[]rideType{rideBytes(msg), rideBytes(bad), rideBytes(pk)}, bytesSizeCheckV1V2, false, rideBoolean(false)}, + {[]rideType{rideBytes(msg), rideBytes(sig), rideBytes(pk[:10])}, bytesSizeCheckV1V2, false, rideBoolean(false)}, + {[]rideType{rideString("MESSAGE"), rideBytes(sig), rideBytes(pk)}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideBytes(big), rideBytes(sig), rideBytes(pk)}, bytesSizeCheckV1V2, false, rideBoolean(false)}, + {[]rideType{rideBytes(big), rideBytes(sig), rideBytes(pk)}, bytesSizeCheckV3V6, true, nil}, + {[]rideType{rideBytes(msg), rideString("SIGNATURE"), rideBytes(pk)}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideBytes(msg), rideBytes(sig), rideString("PUBLIC KEY")}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideUnit{}}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideInt(12345)}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideString("dsfjsadfl"), rideInt(12345)}, bytesSizeCheckV1V2, true, nil}, } { te := &mockRideEnvironment{ checkMessageLengthFunc: test.check, @@ -632,14 +626,14 @@ func TestKeccak256(t *testing.T) { fail bool r rideType }{ - {[]rideType{rideBytes(data)}, v2check, false, rideBytes(digest1)}, - {[]rideType{rideString("123")}, v2check, false, rideBytes(digest2)}, - {[]rideType{rideBytes(big)}, v2check, false, rideBytes(digest3)}, - {[]rideType{rideBytes(big)}, v3check, true, nil}, - {[]rideType{rideUnit{}}, v2check, true, nil}, - {[]rideType{}, v2check, true, nil}, - {[]rideType{rideInt(12345)}, v2check, true, nil}, - {[]rideType{rideString("dsfjsadfl"), rideInt(12345)}, v2check, true, nil}, + {[]rideType{rideBytes(data)}, bytesSizeCheckV1V2, false, rideBytes(digest1)}, + {[]rideType{rideString("123")}, bytesSizeCheckV1V2, false, rideBytes(digest2)}, + {[]rideType{rideBytes(big)}, bytesSizeCheckV1V2, false, rideBytes(digest3)}, + {[]rideType{rideBytes(big)}, bytesSizeCheckV3V6, true, nil}, + {[]rideType{rideUnit{}}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideInt(12345)}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideString("dsfjsadfl"), rideInt(12345)}, bytesSizeCheckV1V2, true, nil}, } { r, err := keccak256(&mockRideEnvironment{checkMessageLengthFunc: test.check}, test.args...) if test.fail { @@ -667,14 +661,14 @@ func TestBlake2b256(t *testing.T) { fail bool r rideType }{ - {[]rideType{rideBytes(data)}, v2check, false, rideBytes(digest1)}, - {[]rideType{rideString("123")}, v2check, false, rideBytes(digest2)}, - {[]rideType{rideBytes(big)}, v2check, false, rideBytes(digest3)}, - {[]rideType{rideBytes(big)}, v3check, true, nil}, - {[]rideType{rideUnit{}}, v2check, true, nil}, - {[]rideType{}, v2check, true, nil}, - {[]rideType{rideInt(12345)}, v2check, true, nil}, - {[]rideType{rideString("dsfjsadfl"), rideInt(12345)}, v2check, true, nil}, + {[]rideType{rideBytes(data)}, bytesSizeCheckV1V2, false, rideBytes(digest1)}, + {[]rideType{rideString("123")}, bytesSizeCheckV1V2, false, rideBytes(digest2)}, + {[]rideType{rideBytes(big)}, bytesSizeCheckV1V2, false, rideBytes(digest3)}, + {[]rideType{rideBytes(big)}, bytesSizeCheckV3V6, true, nil}, + {[]rideType{rideUnit{}}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideInt(12345)}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideString("dsfjsadfl"), rideInt(12345)}, bytesSizeCheckV1V2, true, nil}, } { r, err := blake2b256(&mockRideEnvironment{checkMessageLengthFunc: test.check}, test.args...) if test.fail { @@ -702,14 +696,14 @@ func TestSha256(t *testing.T) { fail bool r rideType }{ - {[]rideType{rideBytes(data1)}, v2check, false, rideBytes(digest1)}, - {[]rideType{rideString("123")}, v2check, false, rideBytes(digest2)}, - {[]rideType{rideBytes(big)}, v2check, false, rideBytes(digest3)}, - {[]rideType{rideBytes(big)}, v3check, true, nil}, - {[]rideType{rideUnit{}}, v2check, true, nil}, - {[]rideType{}, v2check, true, nil}, - {[]rideType{rideInt(12345)}, v2check, true, nil}, - {[]rideType{rideString("dsfjsadfl"), rideInt(12345)}, v2check, true, nil}, + {[]rideType{rideBytes(data1)}, bytesSizeCheckV1V2, false, rideBytes(digest1)}, + {[]rideType{rideString("123")}, bytesSizeCheckV1V2, false, rideBytes(digest2)}, + {[]rideType{rideBytes(big)}, bytesSizeCheckV1V2, false, rideBytes(digest3)}, + {[]rideType{rideBytes(big)}, bytesSizeCheckV3V6, true, nil}, + {[]rideType{rideUnit{}}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideInt(12345)}, bytesSizeCheckV1V2, true, nil}, + {[]rideType{rideString("dsfjsadfl"), rideInt(12345)}, bytesSizeCheckV1V2, true, nil}, } { r, err := sha256(&mockRideEnvironment{checkMessageLengthFunc: test.check}, test.args...) if test.fail { diff --git a/pkg/ride/test_helpers_test.go b/pkg/ride/test_helpers_test.go index 7785587a6..4c4bbcc14 100644 --- a/pkg/ride/test_helpers_test.go +++ b/pkg/ride/test_helpers_test.go @@ -129,9 +129,7 @@ func newTestEnv(t *testing.T) *testEnv { maxDataEntriesSizeFunc: func() int { return proto.MaxDataEntriesScriptActionsSizeInBytesV1 // V1 by default }, - checkMessageLengthFunc: func(n int) bool { - return true // V2 by default - }, + checkMessageLengthFunc: bytesSizeCheckV1V2, validateInternalPaymentsFunc: func() bool { return false }, @@ -402,9 +400,7 @@ func (e *testEnv) withDataEntriesSizeV2() *testEnv { } func (e *testEnv) withMessageLengthV3() *testEnv { - e.me.checkMessageLengthFunc = func(n int) bool { - return n <= maxMessageLength - } + e.me.checkMessageLengthFunc = bytesSizeCheckV3V6 return e } diff --git a/pkg/ride/vm_test.go b/pkg/ride/vm_test.go index d06d93b74..ea73b5438 100644 --- a/pkg/ride/vm_test.go +++ b/pkg/ride/vm_test.go @@ -125,7 +125,7 @@ func TestFunctions(t *testing.T) { data := newDataTransaction() require.NoError(t, err) env := &mockRideEnvironment{ - checkMessageLengthFunc: v3check, + checkMessageLengthFunc: bytesSizeCheckV3V6, schemeFunc: func() byte { return 'W' },