From b5b89881b7461f4088b093074667b9618901f502 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 18 Oct 2024 17:44:25 +0300 Subject: [PATCH] core: prove contract notifications count is not restricted A part of #3490. Signed-off-by: Anna Shaleva --- pkg/core/blockchain_neotest_test.go | 63 +++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/pkg/core/blockchain_neotest_test.go b/pkg/core/blockchain_neotest_test.go index 22523882b3..dd08846def 100644 --- a/pkg/core/blockchain_neotest_test.go +++ b/pkg/core/blockchain_neotest_test.go @@ -43,6 +43,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm/emit" "github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" @@ -2697,3 +2698,65 @@ func TestEngineLimits(t *testing.T) { // ProduceLargeObject: hit the limit. cInv.InvokeFail(t, "stack is too big", "produceLargeObject", 500) } + +// TestRuntimeNotifyRefcounting tries to emit more than MaxStackSize notifications. +func TestRuntimeNotifyRefcounting(t *testing.T) { + const eArgsCount = 500 + + bc, acc := chain.NewSingle(t) + e := neotest.NewExecutor(t, bc, acc, acc) + + args, _ := strings.CutSuffix(strings.Repeat(`"", `, eArgsCount), `, `) + src := fmt.Sprintf(`package test + import ( + "github.com/nspcc-dev/neo-go/pkg/interop/runtime" + ) + // args is an array of LargeEvent parameters containing 500 empty strings. + var args = []any{%s}; + func ProduceNumerousNotifications(count int) { + for i := 0; i < count; i++ { + runtime.Notify("LargeEvent", args...) + } + }`, args) + + eParams := make([]compiler.HybridParameter, eArgsCount) + for i := range eParams { + eParams[i].Name = fmt.Sprintf("str%d", i) + eParams[i].Type = smartcontract.ByteArrayType + } + c := neotest.CompileSource(t, acc.ScriptHash(), strings.NewReader(src), &compiler.Options{ + Name: "test_contract", + ContractEvents: []compiler.HybridEvent{ + { + Name: "LargeEvent", + Parameters: eParams, + }, + }, + }) + e.DeployContract(t, c, nil) + + var params = make([]stackitem.Item, eArgsCount) + for i := range params { + params[i] = stackitem.Make("") + } + cInv := e.NewInvoker(c.Hash, acc) + expected := state.NotificationEvent{ + ScriptHash: c.Hash, + Name: "LargeEvent", + Item: stackitem.NewArray(params), + } + + // ProduceNumerousNotifications: 1 iteration, no limits are hit. + h := cInv.Invoke(t, stackitem.Null{}, "produceNumerousNotifications", 1) + cInv.CheckTxNotificationEvent(t, h, 0, expected) + + // ProduceNumerousNotifications: vm.MaxStackSize + 1 iterations. + count := vm.MaxStackSize + 1 + h = cInv.Invoke(t, stackitem.Null{}, "produceNumerousNotifications", count) + aer, err := e.Chain.GetAppExecResults(h, trigger.Application) + require.NoError(t, err) + require.Equal(t, count, len(aer[0].Events)) + for i := range count { + require.Equal(t, expected, aer[0].Events[i]) + } +}