Skip to content

Commit

Permalink
Merge branch 'master' into nep10-to-abi
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Jul 8, 2020
2 parents 09f1b63 + 5952ad2 commit 1a68824
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
23 changes: 22 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 Down Expand Up @@ -164,7 +165,27 @@ private void CallContractInternal(UInt160 contractHash, string method, Array arg
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
50 changes: 25 additions & 25 deletions src/neo/SmartContract/NefFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Neo.SmartContract
/// | Version | 16 bytes | Compiler version (Mayor, Minor, Build, Version) |
/// | ScriptHash | 20 bytes | ScriptHash for the script |
/// +------------+-----------+------------------------------------------------------------+
/// | Checksum | 4 bytes | Sha256 of the header (CRC) |
/// | Checksum | 4 bytes | First four bytes of double SHA256 hash |
/// +------------+-----------+------------------------------------------------------------+
/// | Script | Var bytes | Var bytes for the payload |
/// +------------+-----------+------------------------------------------------------------+
Expand Down Expand Up @@ -52,17 +52,24 @@ public class NefFile : ISerializable
public byte[] Script { get; set; }

private const int HeaderSize =
sizeof(uint) + // Magic
32 + // Compiler
(sizeof(int) * 4) + // Version
UInt160.Length + // ScriptHash
sizeof(uint); // Checksum
sizeof(uint) + // Magic
32 + // Compiler
(sizeof(int) * 4) + // Version
UInt160.Length; // ScriptHash

public int Size =>
HeaderSize + // Header
Script.GetVarSize(); // Script
HeaderSize + // Header
sizeof(uint) + // Checksum
Script.GetVarSize();// Script

public void Serialize(BinaryWriter writer)
{
SerializeHeader(writer);
writer.Write(CheckSum);
writer.WriteVarBytes(Script ?? Array.Empty<byte>());
}

private void SerializeHeader(BinaryWriter writer)
{
writer.Write(Magic);
writer.WriteFixedString(Compiler, 32);
Expand All @@ -74,8 +81,6 @@ public void Serialize(BinaryWriter writer)
writer.Write(Version.Revision);

writer.Write(ScriptHash);
writer.Write(CheckSum);
writer.WriteVarBytes(Script ?? Array.Empty<byte>());
}

public void Deserialize(BinaryReader reader)
Expand Down Expand Up @@ -108,22 +113,17 @@ public void Deserialize(BinaryReader reader)
/// </summary>
/// <param name="file">File</param>
/// <returns>Return checksum</returns>
public static uint ComputeChecksum(NefFile file)
unsafe public static uint ComputeChecksum(NefFile file)
{
using (var ms = new MemoryStream())
using (var wr = new BinaryWriter(ms))
{
file.Serialize(wr);
wr.Flush();

// Read header without CRC

Span<byte> buffer = stackalloc byte[HeaderSize - sizeof(uint)];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer);

return BitConverter.ToUInt32(buffer.Sha256(), 0);
}
Span<byte> header = stackalloc byte[HeaderSize];
fixed (byte* p = header)
using (UnmanagedMemoryStream ms = new UnmanagedMemoryStream(p, HeaderSize, HeaderSize, FileAccess.Write))
using (BinaryWriter wr = new BinaryWriter(ms, Utility.StrictUTF8, false))
{
file.SerializeHeader(wr);
wr.Flush();
}
return BitConverter.ToUInt32(Crypto.Hash256(header), 0);
}
}
}
7 changes: 6 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,19 @@ 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]
Expand Down

0 comments on commit 1a68824

Please sign in to comment.