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

Ignore last two invoke complexities ride v5 #783

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
0deb6ca
WIP: ignore last two complexities.
nickeskov Jun 14, 2022
162b56d
Reproduce scala's node buggy behaviour in complexity calculations aft…
nickeskov Jun 14, 2022
22cd254
Return an error in case when total block complexity exceeds limit.
nickeskov Jun 14, 2022
ac129f2
Fixed 'TestInvokeFailedBalanceValidationV6' test.
nickeskov Jun 14, 2022
22c52a5
Edit comments in 'complexityInCaseOfEvaluationError' function.
nickeskov Jun 17, 2022
26d4092
Fixed 'TestFailedInvokeApplicationComplexity' test.
nickeskov Jun 17, 2022
3027513
Fixed tests in 'ride' package. Refactored a bit.
nickeskov Jun 17, 2022
55c37a1
Refactored a bit tests in 'state' package.
nickeskov Jun 17, 2022
8b4e48f
Nullify failed invoke complexity in case of 'InternalInvocationError'…
nickeskov Jun 17, 2022
c2445cf
Tests from scala implementation added
alexeykiselev Jun 22, 2022
ab781e3
Fix linter issue in 'makeInvokeTransactionTestObjects' func.
nickeskov Jun 27, 2022
afcb9c7
Change method receivers for types which implement 'proto.Argument' in…
nickeskov Jun 27, 2022
16e78ab
Changed dapp balances in 'evaluation_complexity_test.go' to 9.99 WAVE…
nickeskov Jun 30, 2022
6de4dcd
Create reverse complexities list in 'evaluationError' type.
nickeskov Jul 1, 2022
043ac07
Create 'invokeApplier.handleInvokeScriptInvocationError' method.
nickeskov Jul 1, 2022
1bf1b3c
Refactor 'invokeApplier.handleInvokeScriptInvocationError' method.
nickeskov Jul 1, 2022
17eaca0
Fix error message in 'handleInvokeScriptInvocationError'.
nickeskov Jul 4, 2022
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
12 changes: 6 additions & 6 deletions pkg/proto/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5983,7 +5983,7 @@ func TestInvokeScriptWithProofsValidations(t *testing.T) {
repeat := func(arg StringArgument, n int) Arguments {
r := make([]Argument, n)
for i := 0; i < n; i++ {
r = append(r, arg)
r = append(r, &arg)
}
return r
}
Expand All @@ -6000,13 +6000,13 @@ func TestInvokeScriptWithProofsValidations(t *testing.T) {
err string
version byte
}{
{ScriptPayments{}, "foo", Arguments{IntegerArgument{Value: 1234567890}}, 0, "fee should be positive", 1},
{ScriptPayments{{12345, *a1}}, "foo", Arguments{StringArgument{Value: "some value should be ok"}}, math.MaxInt64 + 1, "fee is too big", 1},
{ScriptPayments{}, "foo", Arguments{&IntegerArgument{Value: 1234567890}}, 0, "fee should be positive", 1},
{ScriptPayments{{12345, *a1}}, "foo", Arguments{&StringArgument{Value: "some value should be ok"}}, math.MaxInt64 + 1, "fee is too big", 1},
{ScriptPayments{{12345, *a1}}, strings.Repeat("foo", 100), Arguments{}, 13245, "function name is too big", 1},
{ScriptPayments{{12345, *a1}}, "foo", repeat(StringArgument{Value: "some value should be ok"}, 100), 13245, "too many arguments", 1},
{ScriptPayments{{0, *a1}}, "foo", Arguments{StringArgument{Value: "some value should be ok"}}, 1234, "at least one payment has a non-positive amount", 1},
{ScriptPayments{{math.MaxInt64 + 123, *a1}}, "foo", Arguments{StringArgument{Value: "some value should be ok"}}, 12345, "at least one payment has a too big amount", 1},
{ScriptPayments{}, "foo", Arguments{IntegerArgument{Value: 1234567890}}, 1, "unexpected version 128 for InvokeScriptWithProofs", 128},
{ScriptPayments{{0, *a1}}, "foo", Arguments{&StringArgument{Value: "some value should be ok"}}, 1234, "at least one payment has a non-positive amount", 1},
{ScriptPayments{{math.MaxInt64 + 123, *a1}}, "foo", Arguments{&StringArgument{Value: "some value should be ok"}}, 12345, "at least one payment has a too big amount", 1},
{ScriptPayments{}, "foo", Arguments{&IntegerArgument{Value: 1234567890}}, 1, "unexpected version 128 for InvokeScriptWithProofs", 128},
//TODO: add test on arguments evaluation
}
for _, tc := range tests {
Expand Down
40 changes: 20 additions & 20 deletions pkg/proto/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3189,16 +3189,16 @@ func NewIntegerArgument(i int64) *IntegerArgument {
}

//GetValueType returns the value type of the entry.
func (a IntegerArgument) GetValueType() ArgumentValueType {
func (a *IntegerArgument) GetValueType() ArgumentValueType {
return ArgumentInteger
}

func (a IntegerArgument) BinarySize() int {
func (a *IntegerArgument) BinarySize() int {
return integerArgumentLen
}

//MarshalBinary marshals the integer argument in its bytes representation.
func (a IntegerArgument) MarshalBinary() ([]byte, error) {
func (a *IntegerArgument) MarshalBinary() ([]byte, error) {
buf := make([]byte, a.BinarySize())
pos := 0
buf[pos] = byte(ArgumentInteger)
Expand All @@ -3208,7 +3208,7 @@ func (a IntegerArgument) MarshalBinary() ([]byte, error) {
}

//Serialize the integer argument in its bytes representation.
func (a IntegerArgument) Serialize(s *serializer.Serializer) error {
func (a *IntegerArgument) Serialize(s *serializer.Serializer) error {
err := s.Byte(byte(ArgumentInteger))
if err != nil {
return err
Expand Down Expand Up @@ -3255,16 +3255,16 @@ type BooleanArgument struct {
}

//GetValueType returns the data type (Boolean) of the argument.
func (a BooleanArgument) GetValueType() ArgumentValueType {
func (a *BooleanArgument) GetValueType() ArgumentValueType {
return ArgumentBoolean
}

func (a BooleanArgument) BinarySize() int {
func (a *BooleanArgument) BinarySize() int {
return booleanArgumentLen
}

//MarshalBinary writes a byte representation of the boolean data entry.
func (a BooleanArgument) MarshalBinary() ([]byte, error) {
func (a *BooleanArgument) MarshalBinary() ([]byte, error) {
buf := make([]byte, a.BinarySize())
if a.Value {
buf[0] = byte(ArgumentValueTrue)
Expand All @@ -3275,7 +3275,7 @@ func (a BooleanArgument) MarshalBinary() ([]byte, error) {
}

//Serialize argument to its byte representation.
func (a BooleanArgument) Serialize(s *serializer.Serializer) error {
func (a *BooleanArgument) Serialize(s *serializer.Serializer) error {
buf := byte(0)
if a.Value {
buf = byte(ArgumentValueTrue)
Expand Down Expand Up @@ -3328,16 +3328,16 @@ type BinaryArgument struct {
}

//GetValueType returns the type of value (Binary) stored in an argument.
func (a BinaryArgument) GetValueType() ArgumentValueType {
func (a *BinaryArgument) GetValueType() ArgumentValueType {
return ArgumentBinary
}

func (a BinaryArgument) BinarySize() int {
func (a *BinaryArgument) BinarySize() int {
return binaryArgumentMinLen + len(a.Value)
}

//MarshalBinary writes an argument to its byte representation.
func (a BinaryArgument) MarshalBinary() ([]byte, error) {
func (a *BinaryArgument) MarshalBinary() ([]byte, error) {
buf := make([]byte, a.BinarySize())
pos := 0
buf[pos] = byte(ArgumentBinary)
Expand All @@ -3347,7 +3347,7 @@ func (a BinaryArgument) MarshalBinary() ([]byte, error) {
}

//Serialize argument to its byte representation.
func (a BinaryArgument) Serialize(s *serializer.Serializer) error {
func (a *BinaryArgument) Serialize(s *serializer.Serializer) error {
err := s.Byte(byte(ArgumentBinary))
if err != nil {
return err
Expand Down Expand Up @@ -3402,16 +3402,16 @@ func NewStringArgument(s string) *StringArgument {
}

//GetValueType returns the type of value of the argument.
func (a StringArgument) GetValueType() ArgumentValueType {
func (a *StringArgument) GetValueType() ArgumentValueType {
return ArgumentString
}

func (a StringArgument) BinarySize() int {
func (a *StringArgument) BinarySize() int {
return stringArgumentMinLen + len(a.Value)
}

//MarshalBinary converts the argument to its byte representation.
func (a StringArgument) MarshalBinary() ([]byte, error) {
func (a *StringArgument) MarshalBinary() ([]byte, error) {
buf := make([]byte, a.BinarySize())
pos := 0
buf[pos] = byte(ArgumentString)
Expand All @@ -3421,7 +3421,7 @@ func (a StringArgument) MarshalBinary() ([]byte, error) {
}

//Serialize argument to its byte representation.
func (a StringArgument) Serialize(s *serializer.Serializer) error {
func (a *StringArgument) Serialize(s *serializer.Serializer) error {
err := s.Byte(byte(ArgumentString))
if err != nil {
return err
Expand Down Expand Up @@ -3471,16 +3471,16 @@ type ListArgument struct {
}

//GetValueType returns the type of value of the argument.
func (a ListArgument) GetValueType() ArgumentValueType {
func (a *ListArgument) GetValueType() ArgumentValueType {
return ArgumentList
}

func (a ListArgument) BinarySize() int {
func (a *ListArgument) BinarySize() int {
return 1 + a.Items.BinarySize()
}

//MarshalBinary converts the argument to its byte representation.
func (a ListArgument) MarshalBinary() ([]byte, error) {
func (a *ListArgument) MarshalBinary() ([]byte, error) {
buf := make([]byte, a.BinarySize())
pos := 0
buf[pos] = byte(ArgumentList)
Expand All @@ -3494,7 +3494,7 @@ func (a ListArgument) MarshalBinary() ([]byte, error) {
}

//Serialize argument to its byte representation.
func (a ListArgument) Serialize(s *serializer.Serializer) error {
func (a *ListArgument) Serialize(s *serializer.Serializer) error {
err := s.Byte(byte(ArgumentList))
if err != nil {
return err
Expand Down
20 changes: 16 additions & 4 deletions pkg/ride/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ var (
errDeletedEntry = errors.New("entry has been deleted")
)

var (
libV2CheckMessageLength = func(int) bool { return true }
libV3CheckMessageLength = func(l int) bool {
return l <= maxMessageLength
}
)

type WrappedState struct {
diff diffState
cle rideAddress
Expand Down Expand Up @@ -943,6 +950,7 @@ type EvaluationEnvironment struct {
ver ast.LibraryVersion
validatePaymentsAfter uint64
isBlockV5Activated bool
isRiveV5Activated bool
isRiveV6Activated bool
isProtobufTransaction bool
mds int
Expand All @@ -957,7 +965,7 @@ func NewEnvironment(scheme proto.Scheme, state types.SmartState, internalPayment
sch: scheme,
st: state,
h: rideInt(height),
check: func(int) bool { return true }, // By default, for versions below 2 there was no check, always ok.
check: libV2CheckMessageLength, // By default, for versions below 2 there was no check, always ok.
takeStr: func(s string, n int) rideString { panic("function 'takeStr' was not initialized") },
validatePaymentsAfter: internalPaymentsValidationHeight,
}, nil
Expand All @@ -968,6 +976,7 @@ func NewEnvironmentWithWrappedState(
payments proto.ScriptPayments,
sender proto.WavesAddress,
isBlockV5Activated bool,
isRideV5Activated bool,
isRideV6Activated bool,
isProtobufTransaction bool,
rootScriptLibVersion ast.LibraryVersion,
Expand Down Expand Up @@ -1040,6 +1049,7 @@ func NewEnvironmentWithWrappedState(
validatePaymentsAfter: env.validatePaymentsAfter,
mds: env.mds,
isBlockV5Activated: isBlockV5Activated,
isRiveV5Activated: isRideV5Activated,
isRiveV6Activated: isRideV6Activated,
isProtobufTransaction: isProtobufTransaction,
}, nil
Expand All @@ -1049,6 +1059,10 @@ func (e *EvaluationEnvironment) rideV6Activated() bool {
return e.isRiveV6Activated
}

func (e *EvaluationEnvironment) rideV5Activated() bool {
return e.isRiveV5Activated
}

func (e *EvaluationEnvironment) blockV5Activated() bool {
return e.isBlockV5Activated
}
Expand All @@ -1063,9 +1077,7 @@ func (e *EvaluationEnvironment) ChooseTakeString(isRideV5 bool) {
func (e *EvaluationEnvironment) ChooseSizeCheck(v ast.LibraryVersion) {
e.ver = v
if v > ast.LibV2 {
e.check = func(l int) bool {
return l <= maxMessageLength
}
e.check = libV3CheckMessageLength
}
}

Expand Down
30 changes: 24 additions & 6 deletions pkg/ride/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ type evaluationError struct {
originalError error
callStack []string
spentComplexity int
complexities []int
}

func (e evaluationError) Error() string {
return e.originalError.Error()
}

func (e evaluationError) AddComplexity(complexity int) error {
e.spentComplexity += complexity
return e
}

func (e EvaluationError) New(msg string) error {
return evaluationError{errorType: e, originalError: errors.New(msg)}
}
Expand Down Expand Up @@ -62,6 +58,13 @@ func EvaluationErrorCallStack(err error) []string {
return nil
}

func EvaluationErrorReverseComplexitiesList(err error) []int {
if ee, ok := err.(evaluationError); ok {
return ee.complexities
}
return nil
}

func EvaluationErrorSpentComplexity(err error) int {
if ee, ok := err.(evaluationError); ok {
return ee.spentComplexity
Expand All @@ -71,12 +74,27 @@ func EvaluationErrorSpentComplexity(err error) int {

func EvaluationErrorPush(err error, format string, args ...interface{}) error {
if ee, ok := err.(evaluationError); ok {
ee.callStack = append([]string{fmt.Sprintf(format, args...)}, ee.callStack...)
elem := fmt.Sprintf(format, args...)
if cap(ee.callStack) > len(ee.callStack) { // reusing the same memory area
ee.callStack = append(ee.callStack[:1], ee.callStack...)
ee.callStack[0] = elem
} else { // allocating memory
ee.callStack = append([]string{elem}, ee.callStack...)
}
return ee
}
return errors.Wrapf(err, format, args...)
}

func EvaluationErrorPushComplexity(err error, complexity int) error {
if ee, ok := err.(evaluationError); ok {
ee.complexities = append(ee.complexities, complexity)
ee.spentComplexity += complexity
return ee
}
return err
}

func EvaluationErrorAddComplexity(err error, complexity int) error {
if ee, ok := err.(evaluationError); ok {
ee.spentComplexity += complexity
Expand Down
Loading