Skip to content

Commit

Permalink
Adding contact information to manifest (neo-project#1246)
Browse files Browse the repository at this point in the history
* Adding neo 2 metadata

* Double check

* Adding website and description

* Solution tests

* Dotnet format

* Using generic extra object

* Fix clone

* Removing manifest to test push

* Removing wrong file, renaming test

* Revert "Removing manifest to test push"

This reverts commit 4f57da4.

* Update src/neo/SmartContract/Manifest/ContractManifest.cs

Co-Authored-By: Erik Zhang <erik@neo.org>

* Update src/neo/SmartContract/Manifest/ContractManifest.cs

Co-Authored-By: Erik Zhang <erik@neo.org>

* Fixing tests

* JObject clone

* Dotnet format

* Null check

* Removing unecessary instantiation of primitive types

* Removing refenrece check, returning this
  • Loading branch information
lock9 authored and Luchuan committed Jan 10, 2020
1 parent f36d521 commit 53fd01f
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 43 deletions.
12 changes: 12 additions & 0 deletions src/neo/IO/Json/JArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ internal override void Write(Utf8JsonWriter writer)
writer.WriteEndArray();
}

public override JObject Clone()
{
var cloned = new JArray();

foreach (JObject item in items)
{
cloned.Add(item.Clone());
}

return cloned;
}

public static implicit operator JArray(JObject[] value)
{
return new JArray(value);
Expand Down
5 changes: 5 additions & 0 deletions src/neo/IO/Json/JBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ internal override void Write(Utf8JsonWriter writer)
writer.WriteBooleanValue(Value);
}

public override JObject Clone()
{
return this;
}

public static implicit operator JBoolean(bool value)
{
return new JBoolean(value);
Expand Down
5 changes: 5 additions & 0 deletions src/neo/IO/Json/JNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ internal override void Write(Utf8JsonWriter writer)
writer.WriteNumberValue(Value);
}

public override JObject Clone()
{
return this;
}

public static implicit operator JNumber(double value)
{
return new JNumber(value);
Expand Down
12 changes: 12 additions & 0 deletions src/neo/IO/Json/JObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,17 @@ public static implicit operator JObject(string value)
{
return (JString)value;
}

public virtual JObject Clone()
{
var cloned = new JObject();

foreach (KeyValuePair<string, JObject> pair in Properties)
{
cloned[pair.Key] = pair.Value != null ? pair.Value.Clone() : Null;
}

return cloned;
}
}
}
5 changes: 5 additions & 0 deletions src/neo/IO/Json/JString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ internal override void Write(Utf8JsonWriter writer)
writer.WriteStringValue(Value);
}

public override JObject Clone()
{
return this;
}

public static implicit operator JString(Enum value)
{
return new JString(value.ToString());
Expand Down
25 changes: 17 additions & 8 deletions src/neo/SmartContract/Manifest/ContractManifest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Neo.IO;
using Neo.IO.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -50,13 +51,18 @@ public class ContractManifest : ISerializable
/// The trusts field is an array containing a set of contract hashes or group public keys. It can also be assigned with a wildcard *. If it is a wildcard *, then it means that it trusts any contract.
/// If a contract is trusted, the user interface will not give any warnings when called by the contract.
/// </summary>
public WildCardContainer<UInt160> Trusts { get; set; }
public WildcardContainer<UInt160> Trusts { get; set; }

/// <summary>
/// The safemethods field is an array containing a set of method names. It can also be assigned with a wildcard *. If it is a wildcard *, then it means that all methods of the contract are safe.
/// 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 WildCardContainer<string> SafeMethods { get; set; }
public WildcardContainer<string> SafeMethods { get; set; }

/// <summary>
/// Custom user data
/// </summary>
public JObject Extra { get; set; }

/// <summary>
/// Create Default Contract manifest
Expand All @@ -77,8 +83,9 @@ public static ContractManifest CreateDefault(UInt160 hash)
},
Features = ContractFeatures.NoProperty,
Groups = new ContractGroup[0],
SafeMethods = WildCardContainer<string>.Create(),
Trusts = WildCardContainer<UInt160>.Create()
SafeMethods = WildcardContainer<string>.Create(),
Trusts = WildcardContainer<UInt160>.Create(),
Extra = null,
};
}

Expand Down Expand Up @@ -128,6 +135,7 @@ public JObject ToJson()
json["permissions"] = Permissions.Select(p => p.ToJson()).ToArray();
json["trusts"] = Trusts.ToJson();
json["safeMethods"] = SafeMethods.ToJson();
json["extra"] = Extra;

return json;
}
Expand All @@ -145,7 +153,8 @@ public ContractManifest Clone()
Abi = Abi.Clone(),
Permissions = Permissions.Select(p => p.Clone()).ToArray(),
Trusts = Trusts,
SafeMethods = SafeMethods
SafeMethods = SafeMethods,
Extra = Extra?.Clone()
};
}

Expand All @@ -171,9 +180,9 @@ private void DeserializeFromJson(JObject json)
Groups = ((JArray)json["groups"]).Select(u => ContractGroup.FromJson(u)).ToArray();
Features = ContractFeatures.NoProperty;
Permissions = ((JArray)json["permissions"]).Select(u => ContractPermission.FromJson(u)).ToArray();
Trusts = WildCardContainer<UInt160>.FromJson(json["trusts"], u => UInt160.Parse(u.AsString()));
SafeMethods = WildCardContainer<string>.FromJson(json["safeMethods"], u => u.AsString());

Trusts = WildcardContainer<UInt160>.FromJson(json["trusts"], u => UInt160.Parse(u.AsString()));
SafeMethods = WildcardContainer<string>.FromJson(json["safeMethods"], u => u.AsString());
Extra = json["extra"];
if (json["features"]["storage"].AsBoolean()) Features |= ContractFeatures.HasStorage;
if (json["features"]["payable"].AsBoolean()) Features |= ContractFeatures.Payable;
}
Expand Down
6 changes: 3 additions & 3 deletions src/neo/SmartContract/Manifest/ContractPermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class ContractPermission
/// The methods field is an array containing a set of methods to be called. It can also be assigned with a wildcard *. If it is a wildcard *, then it means that any method can be called.
/// If a contract invokes a contract or method that is not declared in the manifest at runtime, the invocation will fail.
/// </summary>
public WildCardContainer<string> Methods { get; set; }
public WildcardContainer<string> Methods { get; set; }

public static readonly ContractPermission DefaultPermission = new ContractPermission
{
Contract = ContractPermissionDescriptor.CreateWildcard(),
Methods = WildCardContainer<string>.CreateWildcard()
Methods = WildcardContainer<string>.CreateWildcard()
};

public ContractPermission Clone()
Expand All @@ -46,7 +46,7 @@ public static ContractPermission FromJson(JObject json)
return new ContractPermission
{
Contract = ContractPermissionDescriptor.FromJson(json["contract"]),
Methods = WildCardContainer<string>.FromJson(json["methods"], u => u.AsString()),
Methods = WildcardContainer<string>.FromJson(json["methods"], u => u.AsString()),
};
}

Expand Down
10 changes: 5 additions & 5 deletions src/neo/SmartContract/Manifest/WildCardContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Neo.SmartContract.Manifest
{
public class WildCardContainer<T> : IReadOnlyList<T>
public class WildcardContainer<T> : IReadOnlyList<T>
{
private readonly T[] _data;

Expand All @@ -26,7 +26,7 @@ public class WildCardContainer<T> : IReadOnlyList<T>
/// Constructor
/// </summary>
/// <param name="data">Data</param>
private WildCardContainer(T[] data)
private WildcardContainer(T[] data)
{
_data = data;
}
Expand All @@ -36,15 +36,15 @@ private WildCardContainer(T[] data)
/// </summary>
/// <param name="data">Data</param>
/// <returns>WildCardContainer</returns>
public static WildCardContainer<T> Create(params T[] data) => new WildCardContainer<T>(data);
public static WildcardContainer<T> Create(params T[] data) => new WildcardContainer<T>(data);

/// <summary>
/// Create a wildcard
/// </summary>
/// <returns>WildCardContainer</returns>
public static WildCardContainer<T> CreateWildcard() => new WildCardContainer<T>(null);
public static WildcardContainer<T> CreateWildcard() => new WildcardContainer<T>(null);

public static WildCardContainer<T> FromJson(JObject json, Func<JObject, T> elementSelector)
public static WildcardContainer<T> FromJson(JObject json, Func<JObject, T> elementSelector)
{
switch (json)
{
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected NativeContract()
});
}
this.Manifest.Abi.Methods = descriptors.ToArray();
this.Manifest.SafeMethods = WildCardContainer<string>.Create(safeMethods.ToArray());
this.Manifest.SafeMethods = WildcardContainer<string>.Create(safeMethods.ToArray());
contracts.Add(this);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/neo.UnitTests/IO/Json/UT_JObject.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Json;
using Neo.SmartContract.Manifest;
using System;

namespace Neo.UnitTests.IO.Json
Expand Down Expand Up @@ -106,5 +107,16 @@ public void TestGetNull()
{
JObject.Null.Should().BeNull();
}

[TestMethod]
public void TestClone()
{
var bobClone = bob.Clone();
bobClone.Should().NotBeSameAs(bob);
foreach (var key in bobClone.Properties.Keys)
{
bobClone[key].Should().BeEquivalentTo(bob[key]);
}
}
}
}
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/Ledger/UT_ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void TestDeserialize()
public void TestGetSize()
{
ISerializable newContract = contract;
newContract.Size.Should().Be(355);
newContract.Size.Should().Be(368);
}

[TestMethod]
Expand Down
Loading

0 comments on commit 53fd01f

Please sign in to comment.