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

SmartContract: restrict the number of allowed notifications #3548

Open
wants to merge 7 commits into
base: HF_Echidna
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
14 changes: 13 additions & 1 deletion src/Neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ partial class ApplicationEngine
/// </summary>
public const int MaxNotificationSize = 1024;

/// <summary>
/// The maximum number of notifications per application execution.
/// </summary>
public const int MaxNotificationCount = 512;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ExecutionEngineLimits Class engine.Limits.MaxNotificationCount

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notifications is a syscall, outside from VM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its still an Engine Limitation, just cause the VM is splitted into two libraries shouldn't make it be to separate things.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have MaxNotificationSize and MaxEventName declared in the same class as MaxNotificationCount, these three constants must be kept together.


private uint random_times = 0;

/// <summary>
Expand Down Expand Up @@ -394,9 +399,16 @@ protected internal void RuntimeNotifyV1(byte[] eventName, Array state)
/// <param name="state">The arguments of the event.</param>
protected internal void SendNotification(UInt160 hash, string eventName, Array state)
{
notifications ??= new List<NotifyEventArgs>();
// Restrict the number of notifications for Application executions. Do not check
shargon marked this conversation as resolved.
Show resolved Hide resolved
// persisting triggers to avoid native persist failure. Do not check verification
// trigger since verification context is loaded with ReadOnly flag.
if (IsHardforkEnabled(Hardfork.HF_Echidna) && Trigger == TriggerType.Application && notifications.Count >= MaxNotificationCount)
shargon marked this conversation as resolved.
Show resolved Hide resolved
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
throw new InvalidOperationException($"Maximum number of notifications `{MaxNotificationCount}` is reached.");
}
NotifyEventArgs notification = new(ScriptContainer, hash, eventName, (Array)state.DeepCopy(asImmutable: true));
Notify?.Invoke(this, notification);
notifications ??= new List<NotifyEventArgs>();
notifications.Add(notification);
CurrentContext.GetState<ExecutionContextState>().NotificationCount++;
}
Expand Down
1 change: 1 addition & 0 deletions src/Neo/SmartContract/Native/RoleManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
list.AddRange(nodes);
list.Sort();
engine.SnapshotCache.Add(key, new StorageItem(list));

Check warning on line 82 in src/Neo/SmartContract/Native/RoleManagement.cs

View workflow job for this annotation

GitHub Actions / Format

Fix formatting
cschuchardt88 marked this conversation as resolved.
Show resolved Hide resolved
if (engine.IsHardforkEnabled(Hardfork.HF_Echidna))
{
var oldNodes = new VM.Types.Array(engine.ReferenceCounter, GetDesignatedByRole(engine.Snapshot, role, index - 1).Select(u => (ByteString)u.EncodePoint(true)));
Expand Down
Loading