-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Check ABI for notifications #2810
Changes from 13 commits
e88062f
f0b3071
c7e9ced
95ee47e
a426a29
e3db758
95b9983
8cb741b
75829a9
080695d
b812394
93b4126
be2ef40
780103a
3f10c96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,17 +8,17 @@ | |
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Numerics; | ||
using Neo.Cryptography.ECC; | ||
using Neo.IO; | ||
using Neo.Network.P2P.Payloads; | ||
using Neo.SmartContract.Native; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Numerics; | ||
using Array = Neo.VM.Types.Array; | ||
|
||
namespace Neo.SmartContract | ||
|
@@ -332,6 +332,35 @@ protected internal void RuntimeLog(byte[] state) | |
/// <param name="eventName">The name of the event.</param> | ||
/// <param name="state">The arguments of the event.</param> | ||
protected internal void RuntimeNotify(byte[] eventName, Array state) | ||
{ | ||
if (!IsHardforkEnabled(Hardfork.HF_Basilisk)) | ||
{ | ||
RuntimeNotifyV1(eventName, state); | ||
return; | ||
} | ||
if (eventName.Length > MaxEventName) throw new ArgumentException(null, nameof(eventName)); | ||
string name = Utility.StrictUTF8.GetString(eventName); | ||
ContractState contract = CurrentContext.GetState<ExecutionContextState>().Contract; | ||
if (contract is null) | ||
throw new InvalidOperationException("Notifications are not allowed in dynamic scripts."); | ||
var @event = contract.Manifest.Abi.Events.FirstOrDefault(p => string.Equals(p.Name, name, StringComparison.Ordinal)); | ||
if (@event is null) | ||
throw new InvalidOperationException($"Event `{name}` does not exist."); | ||
if (@event.Parameters.Length != state.Count) | ||
throw new InvalidOperationException("The number of the arguments does not match the formal parameters of the event."); | ||
for (int i = 0; i < @event.Parameters.Length; i++) | ||
{ | ||
var p = @event.Parameters[i]; | ||
if (!CheckItemType(state[i], p.Type)) | ||
throw new InvalidOperationException($"The type of the argument `{p.Name}` does not match the formal parameter."); | ||
} | ||
using MemoryStream ms = new(MaxNotificationSize); | ||
using BinaryWriter writer = new(ms, Utility.StrictUTF8, true); | ||
BinarySerializer.Serialize(writer, state, MaxNotificationSize); | ||
SendNotification(CurrentScriptHash, name, state); | ||
} | ||
|
||
protected internal void RuntimeNotifyV1(byte[] eventName, Array state) | ||
{ | ||
if (eventName.Length > MaxEventName) throw new ArgumentException(null, nameof(eventName)); | ||
if (CurrentContext.GetState<ExecutionContextState>().Contract is null) | ||
|
@@ -384,5 +413,58 @@ protected internal void BurnGas(long gas) | |
throw new InvalidOperationException("GAS must be positive."); | ||
AddGas(gas); | ||
} | ||
|
||
private static bool CheckItemType(StackItem item, ContractParameterType type) | ||
{ | ||
StackItemType aType = item.Type; | ||
if (aType == StackItemType.Pointer) return false; | ||
switch (type) | ||
{ | ||
case ContractParameterType.Any: | ||
return true; | ||
case ContractParameterType.Boolean: | ||
return aType == StackItemType.Boolean; | ||
case ContractParameterType.Integer: | ||
return aType == StackItemType.Integer; | ||
case ContractParameterType.ByteArray: | ||
case ContractParameterType.String: | ||
{ | ||
if (aType is StackItemType.Any or StackItemType.ByteString or StackItemType.Buffer) | ||
Jim8y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
try | ||
{ | ||
_ = Utility.StrictUTF8.GetString(item.GetSpan()); // Prevent any non-UTF8 string | ||
return true; | ||
} | ||
catch { } | ||
} | ||
return false; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And the last question from my side: if we have @shargon, @roman-khimov, what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AnnaShaleva, you're programming in Go, Jokes aside, I don't think I have any strong preference here. I'm slightly more in favor of making strings always have some contents (even if it's "", basically the way it is now), but if there are useful cases where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's then adjust a bit the current check so that it'll be more explicit:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that should be empty, not null There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shargon, good, then see the #2810 (comment), please. |
||
case ContractParameterType.Hash160: | ||
if (aType == StackItemType.Any) return true; | ||
if (aType != StackItemType.ByteString && aType != StackItemType.Buffer) return false; | ||
return item.GetSpan().Length == UInt160.Length; | ||
case ContractParameterType.Hash256: | ||
if (aType == StackItemType.Any) return true; | ||
if (aType != StackItemType.ByteString && aType != StackItemType.Buffer) return false; | ||
return item.GetSpan().Length == UInt256.Length; | ||
case ContractParameterType.PublicKey: | ||
if (aType == StackItemType.Any) return true; | ||
if (aType != StackItemType.ByteString && aType != StackItemType.Buffer) return false; | ||
return item.GetSpan().Length == 33; | ||
case ContractParameterType.Signature: | ||
if (aType == StackItemType.Any) return true; | ||
if (aType != StackItemType.ByteString && aType != StackItemType.Buffer) return false; | ||
return item.GetSpan().Length == 64; | ||
case ContractParameterType.Array: | ||
return aType is StackItemType.Any or StackItemType.Array or StackItemType.Struct; | ||
case ContractParameterType.Map: | ||
return aType is StackItemType.Any or StackItemType.Map; | ||
case ContractParameterType.InteropInterface: | ||
return aType is StackItemType.Any or StackItemType.InteropInterface; | ||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want to have some additional checks for the
String
type? The way it is now it's absolutely the same asByteArray
, maybe some differentiation can be useful like ifString
could only contain valid UTF-8 byte sequence.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shargon, how about this additional check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AnnaShaleva Done