Skip to content

Commit

Permalink
Fixed check function for scripts version before V3. (#1039)
Browse files Browse the repository at this point in the history
Check fucntions implemented as separate functions in evironment.go and used in tests.
Old test check functions removed.
  • Loading branch information
alexeykiselev authored Mar 3, 2023
1 parent f22d664 commit 39a4229
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 54 deletions.
14 changes: 10 additions & 4 deletions pkg/ride/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ride/functions_bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 36 additions & 42 deletions pkg/ride/functions_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions pkg/ride/test_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ride/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
Expand Down

0 comments on commit 39a4229

Please sign in to comment.