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

Script header #903

Merged
merged 30 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d46b46e
Script header
shargon Jul 9, 2019
b1d381a
Merge branch 'master' into script-header
igormcoelho Jul 11, 2019
1b62609
Script header draft
shargon Jul 11, 2019
01ba36b
Clean files
shargon Jul 11, 2019
99b995c
Typo
shargon Jul 11, 2019
6d66fab
Rename class and magic header
shargon Jul 11, 2019
5bcdbad
Clean hash
shargon Jul 11, 2019
4ac9ad4
Merge branch 'master' into script-header
shargon Jul 12, 2019
d9a1561
UT
shargon Jul 12, 2019
d90a2b7
Merge branch 'master' into script-header
shargon Jul 14, 2019
e7c9a95
Erik's suggestions
shargon Jul 14, 2019
1b92114
Change name
shargon Jul 14, 2019
88ecdf0
Typo
shargon Jul 14, 2019
ad656f6
Check magic
shargon Jul 14, 2019
bcad1de
64 to 32
shargon Jul 17, 2019
b08a3b3
Nef file only for neo-cli
shargon Jul 17, 2019
dcd878d
Merge branch 'master' into script-header
shargon Jul 17, 2019
176bfbc
Merge branch 'master' into script-header
shargon Jul 18, 2019
827bf8b
Merge branch 'master' into script-header
shargon Jul 19, 2019
8ae4f85
Real Checksum
shargon Jul 19, 2019
6f04657
Clean
shargon Jul 19, 2019
15be163
Clean again
shargon Jul 19, 2019
d0e2bc6
Remove code
shargon Jul 19, 2019
a75e6f4
Update neo/SmartContract/NefFile.cs
erikzhang Jul 22, 2019
75d1b64
Enum to const
shargon Jul 22, 2019
e06e8e1
Merge branch 'master' into script-header
shargon Jul 26, 2019
13254b0
Merge branch 'master' into script-header
shargon Jul 27, 2019
1cd02d6
Remove unnecessary change
erikzhang Jul 28, 2019
2ae2875
Apply Erik's suggestions
shargon Jul 28, 2019
0d7f22e
Merge remote-tracking branch 'shargon/script-header' into script-header
shargon Jul 28, 2019
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
78 changes: 78 additions & 0 deletions neo.UnitTests/UT_NefFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;
using Neo.IO;
using Neo.SmartContract;
using System;

namespace Neo.UnitTests
{
[TestClass]
public class UT_NefFile
{
[TestMethod]
public void ParseTest()
{
var file = new NefFile()
{
Magic = NefFile.NefMagic.NEF3,
Compiler = "".PadLeft(32, ' '),
Version = new Version(1, 2, 3, 4),
Script = new byte[] { 0x01, 0x02, 0x03 }
};

file.ScriptHash = file.Script.ToScriptHash();
file.CheckSum = NefFile.ComputeChecksum(file);

var data = file.ToArray();
file = data.AsSerializable<NefFile>();

Assert.AreEqual(NefFile.NefMagic.NEF3, file.Magic);
Assert.AreEqual("".PadLeft(32, ' '), file.Compiler);
Assert.AreEqual(new Version(1, 2, 3, 4), file.Version);
Assert.AreEqual(file.Script.ToScriptHash(), file.ScriptHash);
CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03 }, file.Script);
}

[TestMethod]
public void LimitTest()
{
var file = new NefFile()
{
Magic = NefFile.NefMagic.NEF3,
Compiler = "".PadLeft(byte.MaxValue, ' '),
Version = new Version(1, 2, 3, 4),
Script = new byte[1024 * 1024],
ScriptHash = new byte[1024 * 1024].ToScriptHash(),
CheckSum = 0
};

// Wrong compiler

Assert.ThrowsException<ArgumentException>(() => file.ToArray());

// Wrong script

file.Compiler = "";
file.Script = new byte[(1024 * 1024) + 1];
file.ScriptHash = file.Script.ToScriptHash();
var data = file.ToArray();

Assert.ThrowsException<FormatException>(() => data.AsSerializable<NefFile>());

// Wrong script hash

file.Script = new byte[1024 * 1024];
data = file.ToArray();

Assert.ThrowsException<FormatException>(() => data.AsSerializable<NefFile>());

// Wrong checksum

file.Script = new byte[1024];
data = file.ToArray();
file.CheckSum = NefFile.ComputeChecksum(file) + 1;

Assert.ThrowsException<FormatException>(() => data.AsSerializable<NefFile>());
}
}
}
2 changes: 1 addition & 1 deletion neo/SmartContract/InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,4 @@ private static bool Json_Serialize(ApplicationEngine engine)
return true;
}
}
}
}
133 changes: 133 additions & 0 deletions neo/SmartContract/NefFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using Neo.Cryptography;
using Neo.IO;
using System;
using System.IO;

namespace Neo.SmartContract
{
/// <summary>
///
/// +------------+-----------+------------------------------------------------------------+
/// | Field | Length | Comment |
/// +------------+-----------+------------------------------------------------------------+
/// | Magic | 4 bytes | Magic header |
/// | Compiler | 32 bytes | Compiler used |
/// | Version | 16 bytes | Compiler version (Mayor, Minor, Build, Version) |
/// | ScriptHash | 20 bytes | ScriptHash for the script |
/// | Script | Var bytes | Var bytes for the payload |
/// | Checksum | 4 bytes | Sha256 of the whole file whithout the last for bytes(CRC) |
/// +------------+-----------+------------------------------------------------------------+
///
/// </summary>
public class NefFile : ISerializable
{
public enum NefMagic : int
{
/// <summary>
/// NEO Executable Format 3
/// </summary>
NEF3 = 0x3346454E
}

/// <summary>
/// Magic
/// </summary>
public NefMagic Magic { get; set; }

/// <summary>
/// Compiler
/// </summary>
public string Compiler { get; set; }

/// <summary>
/// Version
/// </summary>
public Version Version { get; set; }

/// <summary>
/// Script Hash
/// </summary>
public UInt160 ScriptHash { get; set; }

/// <summary>
/// Script
/// </summary>
public byte[] Script { get; set; }

/// <summary>
/// Checksum
/// </summary>
public uint CheckSum { get; set; }

public int Size =>
sizeof(NefMagic) + // Magic
Compiler.GetVarSize() + // Compiler
(sizeof(int) * 4) + // Version
ScriptHash.Size + // ScriptHash
Script.GetVarSize() + // Script
sizeof(uint); // Checksum

public void Serialize(BinaryWriter writer)
{
writer.Write((int)Magic);
writer.WriteFixedString(Compiler, 32);

// Version
writer.Write(Version.Major);
writer.Write(Version.Minor);
writer.Write(Version.Build);
writer.Write(Version.Revision);

writer.Write(ScriptHash);
writer.WriteVarBytes(Script);
writer.Write(CheckSum);
}

public void Deserialize(BinaryReader reader)
{
Magic = (NefMagic)reader.ReadInt32();

if (Magic != NefMagic.NEF3)
{
throw new FormatException("Wrong magic");
}

Compiler = reader.ReadFixedString(32);
Version = new Version(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
ScriptHash = reader.ReadSerializable<UInt160>();

Script = reader.ReadVarBytes(1024 * 1024);
if (Script.ToScriptHash() != ScriptHash)
{
throw new FormatException("ScriptHash is different");
}

CheckSum = reader.ReadUInt32();
if (CheckSum != ComputeChecksum(this))
{
throw new FormatException("CRC verification fail");
}
}

/// <summary>
/// Compute checksum for a file
/// </summary>
/// <param name="file">File</param>
/// <returns>Return checksum</returns>
public static uint ComputeChecksum(NefFile file)
{
using (var ms = new MemoryStream())
using (var wr = new BinaryWriter(ms))
{
file.Serialize(wr);
wr.Flush();

var buffer = new byte[ms.Length - sizeof(uint)];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer, 0, buffer.Length);

return BitConverter.ToUInt32(buffer.Sha256(), 0);
}
}
}
}