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 2 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 @@ -395,8 +400,15 @@ protected internal void RuntimeNotifyV1(byte[] eventName, Array state)
protected internal void SendNotification(UInt160 hash, string eventName, Array state)
{
NotifyEventArgs notification = new(ScriptContainer, hash, eventName, (Array)state.DeepCopy(asImmutable: true));
Notify?.Invoke(this, notification);
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)
Copy link
Member

Choose a reason for hiding this comment

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

Looks good to me, we just need to ensure .Count is correct calculated at this step.

Copy link
Member

@cschuchardt88 cschuchardt88 Oct 23, 2024

Choose a reason for hiding this comment

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

Is trigger really needed? Limits shouldn't have a trigger.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it's needed, because we can't allow OnPersist and PostPersist failures (imagine that in future MaxNotificationCount may be reduced to some lower value). And for Verification trigger this check is useless by design, no notifications are possible with this trigger.

Copy link
Member Author

Choose a reason for hiding this comment

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

shargon marked this conversation as resolved.
Show resolved Hide resolved
{
throw new InvalidOperationException($"Maximum number of notifications `{MaxNotificationCount}` is reached.");
}
Notify?.Invoke(this, notification);
notifications.Add(notification);
CurrentContext.GetState<ExecutionContextState>().NotificationCount++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/Native/RoleManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@
list.AddRange(nodes);
list.Sort();
engine.SnapshotCache.Add(key, new StorageItem(list));
Copy link
Member

Choose a reason for hiding this comment

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

revert

Copy link
Member Author

@AnnaShaleva AnnaShaleva Oct 23, 2024

Choose a reason for hiding this comment

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

Formatting job is failing without this change.


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)));

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

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'ApplicationEngine.Snapshot' is obsolete: 'This property is deprecated. Use SnapshotCache instead.'

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

View workflow job for this annotation

GitHub Actions / Test-Everything

'ApplicationEngine.Snapshot' is obsolete: 'This property is deprecated. Use SnapshotCache instead.'

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

View workflow job for this annotation

GitHub Actions / Test-Everything

'ApplicationEngine.Snapshot' is obsolete: 'This property is deprecated. Use SnapshotCache instead.'

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

View workflow job for this annotation

GitHub Actions / Test-Everything

'ApplicationEngine.Snapshot' is obsolete: 'This property is deprecated. Use SnapshotCache instead.'

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

View workflow job for this annotation

GitHub Actions / Test-Everything

'ApplicationEngine.Snapshot' is obsolete: 'This property is deprecated. Use SnapshotCache instead.'

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

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'ApplicationEngine.Snapshot' is obsolete: 'This property is deprecated. Use SnapshotCache instead.'

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

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'ApplicationEngine.Snapshot' is obsolete: 'This property is deprecated. Use SnapshotCache instead.'

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

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'ApplicationEngine.Snapshot' is obsolete: 'This property is deprecated. Use SnapshotCache instead.'

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

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

'ApplicationEngine.Snapshot' is obsolete: 'This property is deprecated. Use SnapshotCache instead.'
var newNodes = new VM.Types.Array(engine.ReferenceCounter, nodes.Select(u => (ByteString)u.EncodePoint(true)));

engine.SendNotification(Hash, "Designation", new VM.Types.Array(engine.ReferenceCounter, [(int)role, engine.PersistingBlock.Index, oldNodes, newNodes]));
Expand Down
Loading