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

Check witnesses on isStandard #1754

Merged
merged 8 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
36 changes: 35 additions & 1 deletion src/neo/SmartContract/ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
using Neo.VM;
Expand All @@ -20,6 +21,8 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Contract_Call = Register("System.Contract.Call", nameof(CallContract), 0_01000000, TriggerType.System | TriggerType.Application, CallFlags.AllowCall, false);
public static readonly InteropDescriptor System_Contract_CallEx = Register("System.Contract.CallEx", nameof(CallContractEx), 0_01000000, TriggerType.System | TriggerType.Application, CallFlags.AllowCall, false);
public static readonly InteropDescriptor System_Contract_IsStandard = Register("System.Contract.IsStandard", nameof(IsStandardContract), 0_00030000, TriggerType.All, CallFlags.None, true);
public static readonly InteropDescriptor System_Contract_IsDeployed = Register("System.Contract.IsDeployed", nameof(IsDeployedContract), 0_00030000, TriggerType.All, CallFlags.None, true);
public static readonly InteropDescriptor System_Contract_IsPayable = Register("System.Contract.IsPayable", nameof(IsPayableContract), 0_00030000, TriggerType.All, CallFlags.None, true);
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
public static readonly InteropDescriptor System_Contract_GetCallFlags = Register("System.Contract.GetCallFlags", nameof(GetCallFlags), 0_00030000, TriggerType.All, CallFlags.None, false);
/// <summary>
/// Calculate corresponding account scripthash for given public key
Expand Down Expand Up @@ -161,10 +164,41 @@ private void CallContractInternal(UInt160 contractHash, string method, Array arg
if (md != null) LoadClonedContext(md.Offset);
}

internal bool IsDeployedContract(UInt160 hash)
{
return Snapshot.Contracts.TryGet(hash) != null;
}

internal bool IsPayableContract(UInt160 hash)
{
ContractState contract = Snapshot.Contracts.TryGet(hash);
return contract != null && contract.Payable;
}

internal bool IsStandardContract(UInt160 hash)
{
ContractState contract = Snapshot.Contracts.TryGet(hash);
return contract is null || contract.Script.IsStandardContract();

// It's a stored contract

if (contract != null) return contract.Script.IsStandardContract();

// Try to find it in the transaction

if (ScriptContainer is Transaction tx)
{
foreach (var witness in tx.Witnesses)
{
if (witness.ScriptHash == hash)
{
return witness.VerificationScript.IsStandardContract();
}
}
}

// It's not possible to determine if it's standard

return false;
}

internal CallFlags GetCallFlags()
Expand Down
43 changes: 42 additions & 1 deletion tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,55 @@ public void TestAccount_IsStandard()
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01 };
engine.IsStandardContract(new UInt160(hash)).Should().BeTrue();
engine.IsStandardContract(new UInt160(hash)).Should().BeFalse();

var snapshot = Blockchain.Singleton.GetSnapshot();
var state = TestUtils.GetContract();
snapshot.Contracts.Add(state.ScriptHash, state);
engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
engine.LoadScript(new byte[] { 0x01 });
engine.IsStandardContract(state.ScriptHash).Should().BeFalse();

state.Script = Contract.CreateSignatureRedeemScript(Blockchain.StandbyValidators[0]);
engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
engine.LoadScript(new byte[] { 0x01 });
engine.IsStandardContract(state.ScriptHash).Should().BeTrue();
}

[TestMethod]
public void TestAccount_IsDeployed()
{
var engine = GetEngine(false, true);
var hash = new byte[] { 0x01, 0x01, 0x01 ,0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01 };
engine.IsDeployedContract(new UInt160(hash)).Should().BeFalse();

var snapshot = Blockchain.Singleton.GetSnapshot();
var state = TestUtils.GetContract();
snapshot.Contracts.Add(state.ScriptHash, state);
engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
engine.LoadScript(new byte[] { 0x01 });
engine.IsDeployedContract(state.ScriptHash).Should().BeTrue();
}

[TestMethod]
public void TestAccount_IsPayable()
{
var engine = GetEngine(false, true);
var hash = new byte[] { 0x01, 0x01, 0x01 ,0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01 };
engine.IsPayableContract(new UInt160(hash)).Should().BeFalse();

var snapshot = Blockchain.Singleton.GetSnapshot();
var state = TestUtils.GetContract();
snapshot.Contracts.Add(state.ScriptHash, state);
engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
engine.LoadScript(new byte[] { 0x01 });
engine.IsPayableContract(state.ScriptHash).Should().BeFalse();
}

[TestMethod]
Expand Down