Skip to content

Commit

Permalink
Limit notifications (#2747)
Browse files Browse the repository at this point in the history
Notifications are deep copied as immutable.
  • Loading branch information
erikzhang authored May 24, 2022
1 parent 44b95cc commit 3b58adf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ protected internal void RuntimeNotify(byte[] eventName, Array state)
/// <param name="state">The arguments of the event.</param>
protected internal void SendNotification(UInt160 hash, string eventName, Array state)
{
NotifyEventArgs notification = new(ScriptContainer, hash, eventName, (Array)state.DeepCopy());
NotifyEventArgs notification = new(ScriptContainer, hash, eventName, (Array)state.DeepCopy(asImmutable: true));
Notify?.Invoke(this, notification);
notifications ??= new List<NotifyEventArgs>();
notifications.Add(notification);
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/NotifyEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
ScriptHash.ToArray(),
EventName,
State.DeepCopy()
State
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/neo/neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.10" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.2.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.4" />
<PackageReference Include="Neo.VM" Version="3.2.0-CI00307" />
<PackageReference Include="Neo.VM" Version="3.2.0-CI00312" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions tests/neo.UnitTests/SmartContract/UT_ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,42 @@ public void TestGetNetworkAndAddressVersion()
engine.GetAddressVersion().Should().Be(TestBlockchain.TheNeoSystem.Settings.AddressVersion);
}

[TestMethod]
public void TestNotSupportedNotification()
{
using var engine = ApplicationEngine.Create(TriggerType.Application, null, null, TestBlockchain.TheNeoSystem.GenesisBlock, settings: TestBlockchain.TheNeoSystem.Settings, gas: 1100_00000000);
engine.LoadScript(Array.Empty<byte>());

// circular

VM.Types.Array array = new();
array.Add(array);

Assert.ThrowsException<NotSupportedException>(() => engine.RuntimeNotify(new byte[] { 0x01 }, array));

// Buffer

array.Clear();
array.Add(new VM.Types.Buffer(1));

engine.RuntimeNotify(new byte[] { 0x01 }, array);
engine.Notifications[0].State[0].Type.Should().Be(VM.Types.StackItemType.ByteString);

// Pointer

array.Clear();
array.Add(new VM.Types.Pointer(Array.Empty<byte>(), 1));

Assert.ThrowsException<NotSupportedException>(() => engine.RuntimeNotify(new byte[] { 0x01 }, array));

// InteropInterface

array.Clear();
array.Add(new VM.Types.InteropInterface(new object()));

Assert.ThrowsException<NotSupportedException>(() => engine.RuntimeNotify(new byte[] { 0x01 }, array));
}

[TestMethod]
public void TestGetRandomSameBlock()
{
Expand Down

0 comments on commit 3b58adf

Please sign in to comment.