Skip to content

Commit

Permalink
fix trusts conflicts (#2892)
Browse files Browse the repository at this point in the history
* fix trusts conflicts

* update to process wildcard while converting to stackitem

* code optimization

* clean the ut test

* Add unit test to check manifest Trusts serialization to stack item

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>

---------

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
3 people authored Sep 1, 2023
1 parent 4a2a8cf commit 5210728
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/Neo/SmartContract/Manifest/ContractManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ void IInteroperable.FromStackItem(StackItem stackItem)
Permissions = ((Array)@struct[5]).Select(p => p.ToInteroperable<ContractPermission>()).ToArray();
Trusts = @struct[6] switch
{
Null => WildcardContainer<ContractPermissionDescriptor>.CreateWildcard(),
Null _ => WildcardContainer<ContractPermissionDescriptor>.CreateWildcard(),
// Array array when array.Any(p => ((ByteString)p).Size == 0) => WildcardContainer<ContractPermissionDescriptor>.CreateWildcard(),
Array array => WildcardContainer<ContractPermissionDescriptor>.Create(array.Select(p => new ContractPermissionDescriptor(p.GetSpan())).ToArray()),
_ => throw new ArgumentException(null, nameof(stackItem))
};
Expand All @@ -96,7 +97,7 @@ public StackItem ToStackItem(ReferenceCounter 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())),
Trusts.IsWildcard ? StackItem.Null : new Array(referenceCounter, Trusts.Select(p => p.ToArray()?? StackItem.Null)),
Extra is null ? "null" : Extra.ToByteArray(false)
};
}
Expand Down
24 changes: 21 additions & 3 deletions tests/Neo.UnitTests/SmartContract/Manifest/UT_ContractManifest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Json;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.VM;

namespace Neo.UnitTests.SmartContract.Manifest
{
Expand Down Expand Up @@ -53,12 +56,12 @@ public void ParseFromJson_SafeMethods()
[TestMethod]
public void ParseFromJson_Trust()
{
var json = @"{""name"":""testManifest"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""testMethod"",""parameters"":[],""returntype"":""Void"",""offset"":0,""safe"":true}],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""extra"":null}";
ReferenceCounter referenceCounter = new ReferenceCounter();
var json = @"{""name"":""testManifest"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""testMethod"",""parameters"":[],""returntype"":""Void"",""offset"":0,""safe"":true}],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001"",""*""],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToJson().ToString(), json);

var check = TestUtils.CreateDefaultManifest();
check.Trusts = WildcardContainer<ContractPermissionDescriptor>.Create(ContractPermissionDescriptor.Create(UInt160.Parse("0x0000000000000000000000000000000000000001")));
check.Trusts = WildcardContainer<ContractPermissionDescriptor>.Create(ContractPermissionDescriptor.Create(UInt160.Parse("0x0000000000000000000000000000000000000001")), ContractPermissionDescriptor.CreateWildcard());
Assert.AreEqual(manifest.ToJson().ToString(), check.ToJson().ToString());
}

Expand Down Expand Up @@ -103,6 +106,21 @@ public void TestDeserializeAndSerialize()
Assert.AreEqual(expected.ToString(), clone.ToString());
}

[TestMethod]
public void TestSerializeTrusts()
{
var check = TestUtils.CreateDefaultManifest();
check.Trusts = WildcardContainer<ContractPermissionDescriptor>.Create(ContractPermissionDescriptor.Create(UInt160.Parse("0x0000000000000000000000000000000000000001")), ContractPermissionDescriptor.CreateWildcard());
var si = check.ToStackItem(null);

var actualTrusts = ((VM.Types.Array)si)[6];

Assert.AreEqual(((VM.Types.Array)actualTrusts).Count, 2);
Assert.AreEqual(((VM.Types.Array)actualTrusts)[0], new VM.Types.ByteString(UInt160.Parse("0x0000000000000000000000000000000000000001").ToArray()));
// Wildcard trust should be represented as Null stackitem (not as zero-length ByteString):
Assert.AreEqual(((VM.Types.Array)actualTrusts)[1], VM.Types.StackItem.Null);
}

[TestMethod]
public void TestGenerator()
{
Expand Down

0 comments on commit 5210728

Please sign in to comment.