-
Notifications
You must be signed in to change notification settings - Fork 1k
NEP-25 #4043
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
base: dev
Are you sure you want to change the base?
NEP-25 #4043
Changes from all commits
b5b0a26
9fa095d
ddba39e
46ea96f
18a250c
368cfdc
418a8ab
d0f8d21
d392f50
55ec5e8
7ab0c64
b36370a
2e0152f
5e47abc
8929079
1f8d488
d21d906
7c073f2
a1248b2
b0cd9b1
d3c8564
0e85e4a
65f27ec
cfd07b4
f6aa6aa
890dcd4
5ffeb94
de230cc
a7967e5
c3759a8
8c6aa4b
3b20380
d093162
2fa6247
761ca46
2ac2da1
d2e9871
14a9176
7cd74e6
35229d4
8718ed4
3b56be4
e2ac006
ac21144
242dbb5
7c3a377
3fb806a
20965c4
b9cfdb6
c0fd6f2
b6a80d1
c45eecf
51adba8
3b0ab01
4ca1f65
0118b4c
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
|
||
|
@@ -28,6 +29,11 @@ public class ContractMethodDescriptor : ContractEventDescriptor, IEquatable<Cont | |
/// </summary> | ||
public ContractParameterType ReturnType { get; set; } | ||
|
||
/// <summary> | ||
/// NEP-25 extended return type | ||
/// </summary> | ||
public ExtendedType ExtendedReturnType { get; set; } | ||
|
||
/// <summary> | ||
/// The position of the method in the contract script. | ||
/// </summary> | ||
|
@@ -42,46 +48,63 @@ public class ContractMethodDescriptor : ContractEventDescriptor, IEquatable<Cont | |
public override void FromStackItem(StackItem stackItem) | ||
{ | ||
base.FromStackItem(stackItem); | ||
Struct @struct = (Struct)stackItem; | ||
ReturnType = (ContractParameterType)(byte)@struct[2].GetInteger(); | ||
Offset = (int)@struct[3].GetInteger(); | ||
Safe = @struct[4].GetBoolean(); | ||
var item = (Struct)stackItem; | ||
ReturnType = (ContractParameterType)(byte)item[2].GetInteger(); | ||
Offset = (int)item[3].GetInteger(); | ||
Safe = item[4].GetBoolean(); | ||
|
||
if (item.Count >= 6) | ||
{ | ||
ExtendedReturnType = new ExtendedType(); | ||
ExtendedReturnType.FromStackItem(item[5]); | ||
ExtendedReturnType.ValidateForParameterOrReturn(ReturnType, null); | ||
} | ||
else | ||
{ | ||
ExtendedReturnType = null; | ||
} | ||
shargon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public override StackItem ToStackItem(IReferenceCounter referenceCounter) | ||
{ | ||
Struct @struct = (Struct)base.ToStackItem(referenceCounter); | ||
@struct.Add((byte)ReturnType); | ||
@struct.Add(Offset); | ||
@struct.Add(Safe); | ||
return @struct; | ||
var item = (Struct)base.ToStackItem(referenceCounter); | ||
item.Add((byte)ReturnType); | ||
item.Add(Offset); | ||
item.Add(Safe); | ||
if (ExtendedReturnType != null) | ||
{ | ||
item.Add(ExtendedReturnType.ToStackItem(referenceCounter)); | ||
} | ||
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. Looks like a bit inconsistent encoding, we add NULL stackitem for missing 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 it's solved now |
||
return item; | ||
} | ||
|
||
/// <summary> | ||
/// Converts the method from a JSON object. | ||
/// </summary> | ||
/// <param name="json">The method represented by a JSON object.</param> | ||
/// <param name="knownNamedTypes">Set of named type identifiers declared in the manifest, if any.</param> | ||
/// <returns>The converted method.</returns> | ||
public new static ContractMethodDescriptor FromJson(JObject json) | ||
public new static ContractMethodDescriptor FromJson(JObject json, ISet<string> knownNamedTypes = null) | ||
{ | ||
ContractMethodDescriptor descriptor = new() | ||
{ | ||
Name = json["name"].GetString(), | ||
Parameters = ((JArray)json["parameters"]).Select(u => ContractParameterDefinition.FromJson((JObject)u)).ToArray(), | ||
Parameters = ((JArray)json["parameters"]).Select(u => ContractParameterDefinition.FromJson((JObject)u, knownNamedTypes)).ToArray(), | ||
ReturnType = Enum.Parse<ContractParameterType>(json["returntype"].GetString()), | ||
Offset = json["offset"].GetInt32(), | ||
Safe = json["safe"].GetBoolean() | ||
Safe = json["safe"].GetBoolean(), | ||
ExtendedReturnType = json["extendedreturntype"] != null ? ExtendedType.FromJson((JObject)json["extendedreturntype"]) : null | ||
}; | ||
|
||
if (string.IsNullOrEmpty(descriptor.Name)) | ||
throw new FormatException("Name in ContractMethodDescriptor is empty"); | ||
throw new FormatException("Name in ContractMethodDescriptor are empty"); | ||
|
||
_ = descriptor.Parameters.ToDictionary(p => p.Name); | ||
|
||
if (!Enum.IsDefined(typeof(ContractParameterType), descriptor.ReturnType)) | ||
throw new FormatException($"ReturnType({descriptor.ReturnType}) in ContractMethodDescriptor is not valid"); | ||
throw new FormatException($"ReturnType({descriptor.ReturnType}) in ContractMethodDescriptor are not valid"); | ||
if (descriptor.Offset < 0) | ||
throw new FormatException($"Offset({descriptor.Offset}) in ContractMethodDescriptor is not valid"); | ||
throw new FormatException($"Offset({descriptor.Offset}) in ContractMethodDescriptor are not valid"); | ||
descriptor.ExtendedReturnType?.ValidateForParameterOrReturn(descriptor.ReturnType, knownNamedTypes); | ||
return descriptor; | ||
} | ||
|
||
|
@@ -95,6 +118,10 @@ public override JObject ToJson() | |
json["returntype"] = ReturnType.ToString(); | ||
json["offset"] = Offset; | ||
json["safe"] = Safe; | ||
if (ExtendedReturnType != null) | ||
{ | ||
json["extendedreturntype"] = ExtendedReturnType.ToJson(); | ||
} | ||
return json; | ||
} | ||
|
||
|
@@ -106,7 +133,8 @@ public bool Equals(ContractMethodDescriptor other) | |
base.Equals(other) && // Already check null | ||
ReturnType == other.ReturnType | ||
&& Offset == other.Offset | ||
&& Safe == other.Safe; | ||
&& Safe == other.Safe | ||
&& Equals(ExtendedReturnType, other.ExtendedReturnType); | ||
} | ||
|
||
public override bool Equals(object other) | ||
|
@@ -119,7 +147,7 @@ public override bool Equals(object other) | |
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(ReturnType, Offset, Safe, base.GetHashCode()); | ||
return HashCode.Combine(ReturnType, Offset, Safe, ExtendedReturnType?.GetHashCode() ?? -1, base.GetHashCode()); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
|
Uh oh!
There was an error while loading. Please reload this page.