Skip to content

Commit 21b138a

Browse files
committed
SmartContract: restrict the number of allowed notifications
Fix the problem described in nspcc-dev/neo-go#3490. The MaxNotificationsCount constraint is chosen based on the Mainnet notifications data, ref. nspcc-dev/neo-go#3490 (comment). Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
1 parent 620d938 commit 21b138a

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/Neo/SmartContract/ApplicationEngine.Runtime.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ partial class ApplicationEngine
3636
/// </summary>
3737
public const int MaxNotificationSize = 1024;
3838

39+
/// <summary>
40+
/// The maximum number of notifications per application execution.
41+
/// </summary>
42+
public const int MaxNotificationCount = 512;
43+
3944
private uint random_times = 0;
4045

4146
/// <summary>
@@ -395,8 +400,15 @@ protected internal void RuntimeNotifyV1(byte[] eventName, Array state)
395400
protected internal void SendNotification(UInt160 hash, string eventName, Array state)
396401
{
397402
NotifyEventArgs notification = new(ScriptContainer, hash, eventName, (Array)state.DeepCopy(asImmutable: true));
398-
Notify?.Invoke(this, notification);
399403
notifications ??= new List<NotifyEventArgs>();
404+
// Restrict the number of notifications for Application executions. Do not check
405+
// persisting triggers to avoid native persist failure. Do not check verification
406+
// trigger since verification context is loaded with ReadOnly flag.
407+
if (IsHardforkEnabled(Hardfork.HF_Echidna) && Trigger == TriggerType.Application && notifications.Count == MaxNotificationCount)
408+
{
409+
throw new InvalidOperationException($"Maximum number of notifications `{MaxNotificationCount}` is reached.");
410+
}
411+
Notify?.Invoke(this, notification);
400412
notifications.Add(notification);
401413
CurrentContext.GetState<ExecutionContextState>().NotificationCount++;
402414
}

0 commit comments

Comments
 (0)