Skip to content

Commit

Permalink
Add new WitnessScope: Rules (#2622)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Nov 9, 2021
1 parent 6e8497f commit 01f77d8
Show file tree
Hide file tree
Showing 19 changed files with 891 additions and 27 deletions.
57 changes: 57 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/AndCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (C) 2015-2021 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using System;
using System.IO;
using System.Linq;

namespace Neo.Network.P2P.Payloads.Conditions
{
/// <summary>
/// Represents the condition that all conditions must be met.
/// </summary>
public class AndCondition : WitnessCondition
{
/// <summary>
/// The expressions of the condition.
/// </summary>
public WitnessCondition[] Expressions;

public override int Size => base.Size + Expressions.GetVarSize();
public override WitnessConditionType Type => WitnessConditionType.And;

protected override void DeserializeWithoutType(BinaryReader reader, int maxNestDepth)
{
if (maxNestDepth <= 0) throw new FormatException();
Expressions = DeserializeConditions(reader, maxNestDepth - 1);
if (Expressions.Length == 0) throw new FormatException();
}

public override bool Match(ApplicationEngine engine)
{
return Expressions.All(p => p.Match(engine));
}

protected override void SerializeWithoutType(BinaryWriter writer)
{
writer.Write(Expressions);
}

public override JObject ToJson()
{
JObject json = base.ToJson();
json["expressions"] = Expressions.Select(p => p.ToJson()).ToArray();
return json;
}
}
}
49 changes: 49 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/BooleanCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2015-2021 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.IO.Json;
using Neo.SmartContract;
using System.IO;

namespace Neo.Network.P2P.Payloads.Conditions
{
public class BooleanCondition : WitnessCondition
{
/// <summary>
/// The expression of the <see cref="BooleanCondition"/>.
/// </summary>
public bool Expression;

public override int Size => base.Size + sizeof(bool);
public override WitnessConditionType Type => WitnessConditionType.Boolean;

protected override void DeserializeWithoutType(BinaryReader reader, int maxNestDepth)
{
Expression = reader.ReadBoolean();
}

public override bool Match(ApplicationEngine engine)
{
return Expression;
}

protected override void SerializeWithoutType(BinaryWriter writer)
{
writer.Write(Expression);
}

public override JObject ToJson()
{
JObject json = base.ToJson();
json["expression"] = Expression;
return json;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (C) 2015-2021 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using System.IO;

namespace Neo.Network.P2P.Payloads.Conditions
{
public class CalledByContractCondition : WitnessCondition
{
/// <summary>
/// The script hash to be checked.
/// </summary>
public UInt160 Hash;

public override int Size => base.Size + UInt160.Length;
public override WitnessConditionType Type => WitnessConditionType.CalledByContract;

protected override void DeserializeWithoutType(BinaryReader reader, int maxNestDepth)
{
Hash = reader.ReadSerializable<UInt160>();
}

public override bool Match(ApplicationEngine engine)
{
return engine.CallingScriptHash == Hash;
}

protected override void SerializeWithoutType(BinaryWriter writer)
{
writer.Write(Hash);
}

public override JObject ToJson()
{
JObject json = base.ToJson();
json["hash"] = Hash.ToString();
return json;
}
}
}
33 changes: 33 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/CalledByEntryCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2015-2021 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.SmartContract;
using System.IO;

namespace Neo.Network.P2P.Payloads.Conditions
{
public class CalledByEntryCondition : WitnessCondition
{
public override WitnessConditionType Type => WitnessConditionType.CalledByEntry;

protected override void DeserializeWithoutType(BinaryReader reader, int maxNestDepth)
{
}

public override bool Match(ApplicationEngine engine)
{
return engine.CallingScriptHash is null || engine.CallingScriptHash == engine.EntryScriptHash;
}

protected override void SerializeWithoutType(BinaryWriter writer)
{
}
}
}
55 changes: 55 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/CalledByGroupCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) 2015-2021 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using System.IO;
using System.Linq;

namespace Neo.Network.P2P.Payloads.Conditions
{
public class CalledByGroupCondition : WitnessCondition
{
/// <summary>
/// The group to be checked.
/// </summary>
public ECPoint Group;

public override int Size => base.Size + Group.Size;
public override WitnessConditionType Type => WitnessConditionType.CalledByGroup;

protected override void DeserializeWithoutType(BinaryReader reader, int maxNestDepth)
{
Group = reader.ReadSerializable<ECPoint>();
}

public override bool Match(ApplicationEngine engine)
{
engine.ValidateCallFlags(CallFlags.ReadStates);
ContractState contract = NativeContract.ContractManagement.GetContract(engine.Snapshot, engine.CallingScriptHash);
return contract is not null && contract.Manifest.Groups.Any(p => p.PubKey.Equals(Group));
}

protected override void SerializeWithoutType(BinaryWriter writer)
{
writer.Write(Group);
}

public override JObject ToJson()
{
JObject json = base.ToJson();
json["group"] = Group.ToString();
return json;
}
}
}
55 changes: 55 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/GroupCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) 2015-2021 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using System.IO;
using System.Linq;

namespace Neo.Network.P2P.Payloads.Conditions
{
public class GroupCondition : WitnessCondition
{
/// <summary>
/// The group to be checked.
/// </summary>
public ECPoint Group;

public override int Size => base.Size + Group.Size;
public override WitnessConditionType Type => WitnessConditionType.Group;

protected override void DeserializeWithoutType(BinaryReader reader, int maxNestDepth)
{
Group = reader.ReadSerializable<ECPoint>();
}

public override bool Match(ApplicationEngine engine)
{
engine.ValidateCallFlags(CallFlags.ReadStates);
ContractState contract = NativeContract.ContractManagement.GetContract(engine.Snapshot, engine.CurrentScriptHash);
return contract is not null && contract.Manifest.Groups.Any(p => p.PubKey.Equals(Group));
}

protected override void SerializeWithoutType(BinaryWriter writer)
{
writer.Write(Group);
}

public override JObject ToJson()
{
JObject json = base.ToJson();
json["group"] = Group.ToString();
return json;
}
}
}
55 changes: 55 additions & 0 deletions src/neo/Network/P2P/Payloads/Conditions/NotCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) 2015-2021 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using System;
using System.IO;

namespace Neo.Network.P2P.Payloads.Conditions
{
/// <summary>
/// Reverse another condition.
/// </summary>
public class NotCondition : WitnessCondition
{
/// <summary>
/// The expression of the condition to be reversed.
/// </summary>
public WitnessCondition Expression;

public override int Size => base.Size + Expression.Size;
public override WitnessConditionType Type => WitnessConditionType.Not;

protected override void DeserializeWithoutType(BinaryReader reader, int maxNestDepth)
{
if (maxNestDepth <= 0) throw new FormatException();
Expression = DeserializeFrom(reader, maxNestDepth - 1);
}

public override bool Match(ApplicationEngine engine)
{
return !Expression.Match(engine);
}

protected override void SerializeWithoutType(BinaryWriter writer)
{
writer.Write(Expression);
}

public override JObject ToJson()
{
JObject json = base.ToJson();
json["expression"] = Expression.ToJson();
return json;
}
}
}
Loading

0 comments on commit 01f77d8

Please sign in to comment.