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

Change ContractManifest serialization #2254

Merged
merged 7 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/neo/SmartContract/ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void IInteroperable.FromStackItem(StackItem stackItem)
UpdateCounter = (ushort)array[1].GetInteger();
Hash = new UInt160(array[2].GetSpan());
Nef = array[3].GetSpan().AsSerializable<NefFile>();
Manifest = ContractManifest.Parse(array[4].GetSpan());
Manifest = array[4].ToInteroperable<ContractManifest>();
}

/// <summary>
Expand Down Expand Up @@ -53,7 +53,7 @@ public JObject ToJson()

public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new Array(referenceCounter, new StackItem[] { Id, (int)UpdateCounter, Hash.ToArray(), Nef.ToArray(), Manifest.ToString() });
return new Array(referenceCounter, new StackItem[] { Id, (int)UpdateCounter, Hash.ToArray(), Nef.ToArray(), Manifest.ToStackItem(referenceCounter) });
}
}
}
8 changes: 8 additions & 0 deletions src/neo/SmartContract/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
Expand Down Expand Up @@ -143,6 +144,13 @@ public static bool IsStandardContract(this byte[] script)
return script.IsSignatureContract() || script.IsMultiSigContract();
}

public static T ToInteroperable<T>(this StackItem item) where T : IInteroperable, new()
{
T t = new T();
t.FromStackItem(item);
return t;
}

public static UInt160 ToScriptHash(this byte[] script)
{
return new UInt160(Crypto.Hash160(script));
Expand Down
20 changes: 15 additions & 5 deletions src/neo/SmartContract/Manifest/ContractAbi.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using Neo.IO.Json;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using Array = Neo.VM.Types.Array;

namespace Neo.SmartContract.Manifest
{
/// <summary>
/// NeoContract ABI
/// </summary>
public class ContractAbi
public class ContractAbi : IInteroperable
{
private IReadOnlyDictionary<(string, int), ContractMethodDescriptor> methodDictionary;

Expand All @@ -22,12 +25,19 @@ public class ContractAbi
/// </summary>
public ContractEventDescriptor[] Events { get; set; }

public ContractAbi Clone()
void IInteroperable.FromStackItem(StackItem stackItem)
{
return new ContractAbi
Struct @struct = (Struct)stackItem;
Methods = ((Array)@struct[0]).Select(p => p.ToInteroperable<ContractMethodDescriptor>()).ToArray();
Events = ((Array)@struct[1]).Select(p => p.ToInteroperable<ContractEventDescriptor>()).ToArray();
}

public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new Struct(referenceCounter)
{
Methods = Methods.Select(p => p.Clone()).ToArray(),
Events = Events.Select(p => p.Clone()).ToArray()
new Array(referenceCounter, Methods.Select(p => p.ToStackItem(referenceCounter))),
new Array(referenceCounter, Events.Select(p => p.ToStackItem(referenceCounter))),
};
}

Expand Down
19 changes: 14 additions & 5 deletions src/neo/SmartContract/Manifest/ContractEventDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Neo.IO.Json;
using Neo.VM;
using Neo.VM.Types;
using System.Linq;

namespace Neo.SmartContract.Manifest
{
public class ContractEventDescriptor
public class ContractEventDescriptor : IInteroperable
{
/// <summary>
/// Name is the name of the method, which can be any valid identifier.
Expand All @@ -15,12 +17,19 @@ public class ContractEventDescriptor
/// </summary>
public ContractParameterDefinition[] Parameters { get; set; }

public ContractEventDescriptor Clone()
public virtual void FromStackItem(StackItem stackItem)
{
return new ContractEventDescriptor
Struct @struct = (Struct)stackItem;
Name = @struct[0].GetString();
Parameters = ((Array)@struct[1]).Select(p => p.ToInteroperable<ContractParameterDefinition>()).ToArray();
}

public virtual StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new Struct(referenceCounter)
{
Name = Name,
Parameters = Parameters.Select(p => p.Clone()).ToArray()
Name,
new Array(referenceCounter, Parameters.Select(p => p.ToStackItem(referenceCounter)))
};
}

Expand Down
19 changes: 12 additions & 7 deletions src/neo/SmartContract/Manifest/ContractGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.IO.Json;
using Neo.VM;
using Neo.VM.Types;
using System;

namespace Neo.SmartContract.Manifest
Expand All @@ -10,7 +12,7 @@ namespace Neo.SmartContract.Manifest
/// A group represents a set of mutually trusted contracts. A contract will trust and allow any contract in the same group to invoke it, and the user interface will not give any warnings.
/// A group is identified by a public key and must be accompanied by a signature for the contract hash to prove that the contract is indeed included in the group.
/// </summary>
public class ContractGroup
public class ContractGroup : IInteroperable
{
/// <summary>
/// Pubkey represents the public key of the group.
Expand All @@ -22,13 +24,16 @@ public class ContractGroup
/// </summary>
public byte[] Signature { get; set; }

public ContractGroup Clone()
void IInteroperable.FromStackItem(StackItem stackItem)
{
return new ContractGroup
{
PubKey = PubKey,
Signature = Signature
};
Struct @struct = (Struct)stackItem;
PubKey = @struct[0].GetSpan().AsSerializable<ECPoint>();
Signature = @struct[1].GetSpan().ToArray();
}

public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new Struct(referenceCounter) { PubKey.ToArray(), Signature };
}

/// <summary>
Expand Down
107 changes: 45 additions & 62 deletions src/neo/SmartContract/Manifest/ContractManifest.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;
using System.Linq;
using Array = Neo.VM.Types.Array;

namespace Neo.SmartContract.Manifest
{
/// <summary>
/// When a smart contract is deployed, it must explicitly declare the features and permissions it will use.
/// When it is running, it will be limited by its declared list of features and permissions, and cannot make any behavior beyond the scope of the list.
/// </summary>
public class ContractManifest : ISerializable
public class ContractManifest : IInteroperable
{
/// <summary>
/// Max length for a valid Contract Manifest
/// </summary>
public const int MaxLength = ushort.MaxValue;

/// <summary>
/// Serialized size
/// </summary>
public int Size
{
get
{
int size = Utility.StrictUTF8.GetByteCount(ToString());
return IO.Helper.GetVarSize(size) + size;
}
}

/// <summary>
/// Contract name
/// </summary>
Expand Down Expand Up @@ -65,16 +55,54 @@ public int Size
/// </summary>
public JObject Extra { get; set; }

void IInteroperable.FromStackItem(StackItem stackItem)
{
Struct @struct = (Struct)stackItem;
Name = @struct[0].GetString();
Groups = ((Array)@struct[1]).Select(p => p.ToInteroperable<ContractGroup>()).ToArray();
SupportedStandards = ((Array)@struct[2]).Select(p => p.GetString()).ToArray();
Abi = @struct[3].ToInteroperable<ContractAbi>();
Permissions = ((Array)@struct[4]).Select(p => p.ToInteroperable<ContractPermission>()).ToArray();
Trusts = @struct[5] switch
{
Null => WildcardContainer<UInt160>.CreateWildcard(),
Array array => WildcardContainer<UInt160>.Create(array.Select(p => new UInt160(p.GetSpan())).ToArray()),
_ => throw new ArgumentException(null, nameof(stackItem))
};
Extra = JObject.Parse(@struct[6].GetSpan());
}

public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new Struct(referenceCounter)
{
Name,
new Array(referenceCounter, Groups.Select(p => p.ToStackItem(referenceCounter))),
new Array(referenceCounter, SupportedStandards.Select(p => (StackItem)p)),
Abi.ToStackItem(referenceCounter),
new Array(referenceCounter, Permissions.Select(p => p.ToStackItem(referenceCounter))),
Trusts.IsWildcard ? StackItem.Null : new Array(referenceCounter, Trusts.Select(p => (StackItem)p.ToArray())),
Extra is null ? "null" : Extra.ToByteArray(false)
};
}

/// <summary>
/// Parse ContractManifest from json
/// </summary>
/// <param name="json">Json</param>
/// <returns>Return ContractManifest</returns>
public static ContractManifest FromJson(JObject json)
{
var manifest = new ContractManifest();
manifest.DeserializeFromJson(json);
return manifest;
return new ContractManifest
{
Name = json["name"].AsString(),
Groups = ((JArray)json["groups"]).Select(u => ContractGroup.FromJson(u)).ToArray(),
SupportedStandards = ((JArray)json["supportedstandards"]).Select(u => u.AsString()).ToArray(),
Abi = ContractAbi.FromJson(json["abi"]),
Permissions = ((JArray)json["permissions"]).Select(u => ContractPermission.FromJson(u)).ToArray(),
Trusts = WildcardContainer<UInt160>.FromJson(json["trusts"], u => UInt160.Parse(u.AsString())),
Extra = json["extra"]
};
}

/// <summary>
Expand Down Expand Up @@ -103,51 +131,6 @@ public JObject ToJson()
};
}

/// <summary>
/// Clone
/// </summary>
/// <returns>Return a copy of this object</returns>
public ContractManifest Clone()
{
return new ContractManifest
{
Name = Name,
Groups = Groups.Select(p => p.Clone()).ToArray(),
SupportedStandards = SupportedStandards[..],
Abi = Abi.Clone(),
Permissions = Permissions.Select(p => p.Clone()).ToArray(),
Trusts = Trusts,
Extra = Extra?.Clone()
};
}

/// <summary>
/// String representation
/// </summary>
/// <returns>Return json string</returns>
public override string ToString() => ToJson().ToString();

public void Serialize(BinaryWriter writer)
{
writer.WriteVarString(ToString());
}

public void Deserialize(BinaryReader reader)
{
DeserializeFromJson(JObject.Parse(reader.ReadVarString(MaxLength)));
}

private void DeserializeFromJson(JObject json)
{
Name = json["name"].AsString();
Groups = ((JArray)json["groups"]).Select(u => ContractGroup.FromJson(u)).ToArray();
SupportedStandards = ((JArray)json["supportedstandards"]).Select(u => u.AsString()).ToArray();
Abi = ContractAbi.FromJson(json["abi"]);
Permissions = ((JArray)json["permissions"]).Select(u => ContractPermission.FromJson(u)).ToArray();
Trusts = WildcardContainer<UInt160>.FromJson(json["trusts"], u => UInt160.Parse(u.AsString()));
Extra = json["extra"];
}

/// <summary>
/// Return true if is valid
/// </summary>
Expand Down
43 changes: 25 additions & 18 deletions src/neo/SmartContract/Manifest/ContractMethodDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
using Neo.IO.Json;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Linq;

namespace Neo.SmartContract.Manifest
{
public class ContractMethodDescriptor : ContractEventDescriptor
{
private int _offset;
/// <summary>
/// Returntype indicates the return type of the method. It can be one of the following values:
/// Any, Signature, Boolean, Integer, Hash160, Hash256, ByteArray, PublicKey, String, Array, Map, InteropInterface, Void.
/// </summary>
public ContractParameterType ReturnType { get; set; }

private int _offset;
public int Offset
{
get => _offset;
set => _offset = value >= 0 ? value : throw new FormatException();
}

/// <summary>
/// Returntype indicates the return type of the method. It can be one of the following values:
/// Any, Signature, Boolean, Integer, Hash160, Hash256, ByteArray, PublicKey, String, Array, Map, InteropInterface, Void.
/// </summary>
public ContractParameterType ReturnType { get; set; }

/// <summary>
/// Determine if it's safe to call this method
/// If a method is marked as safe, the user interface will not give any warnings when it is called by any other contract.
/// </summary>
public bool Safe { get; set; }

public new ContractMethodDescriptor Clone()
public override void FromStackItem(StackItem stackItem)
{
return new ContractMethodDescriptor
{
Name = Name,
Parameters = Parameters.Select(p => p.Clone()).ToArray(),
Offset = Offset,
ReturnType = ReturnType,
Safe = Safe
};
base.FromStackItem(stackItem);
Struct @struct = (Struct)stackItem;
ReturnType = (ContractParameterType)(byte)@struct[2].GetInteger();
Offset = (int)@struct[3].GetInteger();
Safe = @struct[4].GetBoolean();
}

public override StackItem ToStackItem(ReferenceCounter referenceCounter)
{
Struct @struct = (Struct)base.ToStackItem(referenceCounter);
@struct.Add((byte)ReturnType);
@struct.Add(Offset);
@struct.Add(Safe);
return @struct;
}

/// <summary>
Expand All @@ -49,17 +56,17 @@ public int Offset
{
Name = json["name"].AsString(),
Parameters = ((JArray)json["parameters"]).Select(u => ContractParameterDefinition.FromJson(u)).ToArray(),
Offset = (int)json["offset"].AsNumber(),
ReturnType = (ContractParameterType)Enum.Parse(typeof(ContractParameterType), json["returntype"].AsString()),
Offset = (int)json["offset"].AsNumber(),
Safe = json["safe"].AsBoolean(),
};
}

public override JObject ToJson()
{
var json = base.ToJson();
json["offset"] = Offset;
json["returntype"] = ReturnType.ToString();
json["offset"] = Offset;
json["safe"] = Safe;
return json;
}
Expand Down
Loading