Skip to content

Commit

Permalink
Optimize ContractManifest.Clone() (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Dec 5, 2019
1 parent afec99a commit 75b1586
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/neo/SmartContract/Manifest/ContractAbi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public class ContractAbi
/// </summary>
public ContractEventDescriptor[] Events { get; set; }

public ContractAbi Clone()
{
return new ContractAbi
{
Hash = Hash,
EntryPoint = EntryPoint.Clone(),
Methods = Methods.Select(p => p.Clone()).ToArray(),
Events = Events.Select(p => p.Clone()).ToArray()
};
}

/// <summary>
/// Parse ContractAbi from json
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/neo/SmartContract/Manifest/ContractEventDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public class ContractEventDescriptor
/// </summary>
public ContractParameterDefinition[] Parameters { get; set; }

public ContractEventDescriptor Clone()
{
return new ContractEventDescriptor
{
Name = Name,
Parameters = Parameters.Select(p => p.Clone()).ToArray()
};
}

/// <summary>
/// Parse ContractMethodDescription from json
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/neo/SmartContract/Manifest/ContractGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public class ContractGroup
/// </summary>
public byte[] Signature { get; set; }

public ContractGroup Clone()
{
return new ContractGroup
{
PubKey = PubKey,
Signature = Signature
};
}

/// <summary>
/// Parse ContractManifestGroup from json
/// </summary>
Expand Down
13 changes: 12 additions & 1 deletion src/neo/SmartContract/Manifest/ContractManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,18 @@ public JObject ToJson()
/// Clone
/// </summary>
/// <returns>Return a copy of this object</returns>
public ContractManifest Clone() => FromJson(ToJson());
public ContractManifest Clone()
{
return new ContractManifest
{
Groups = Groups.Select(p => p.Clone()).ToArray(),
Features = Features,
Abi = Abi.Clone(),
Permissions = Permissions.Select(p => p.Clone()).ToArray(),
Trusts = Trusts,
SafeMethods = SafeMethods
};
}

/// <summary>
/// String representation
Expand Down
10 changes: 10 additions & 0 deletions src/neo/SmartContract/Manifest/ContractMethodDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public class ContractMethodDescriptor : ContractEventDescriptor
/// </summary>
public ContractParameterType ReturnType { get; set; }

public new ContractMethodDescriptor Clone()
{
return new ContractMethodDescriptor
{
Name = Name,
Parameters = Parameters.Select(p => p.Clone()).ToArray(),
ReturnType = ReturnType
};
}

/// <summary>
/// Parse ContractMethodDescription from json
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/neo/SmartContract/Manifest/ContractParameterDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ public class ContractParameterDefinition
/// </summary>
public ContractParameterType Type { get; set; }

public ContractParameterDefinition Clone()
{
return new ContractParameterDefinition
{
Name = Name,
Type = Type
};
}

/// <summary>
/// Parse ContractParameterDefinition from json
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/neo/SmartContract/Manifest/ContractPermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public class ContractPermission
Methods = WildCardContainer<string>.CreateWildcard()
};

public ContractPermission Clone()
{
return new ContractPermission
{
Contract = Contract,
Methods = Methods
};
}

/// <summary>
/// Parse ContractPermission from json
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void TestClone()
var expected = ContractManifest.CreateDefault(UInt160.Zero);
expected.SafeMethods = WildCardContainer<string>.Create(new string[] { "AAA" });
var actual = expected.Clone();
Assert.AreEqual(actual.SafeMethods.ToString(), expected.SafeMethods.ToString());
Assert.AreEqual(actual.ToString(), expected.ToString());
}
}
}

0 comments on commit 75b1586

Please sign in to comment.