Skip to content

Commit

Permalink
Add NefFile.Source (#2605)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Sep 23, 2021
1 parent 778feed commit b3b2207
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ protected NativeContract()
this.Nef = new NefFile
{
Compiler = "neo-core-v3.0",
Source = string.Empty,
Tokens = Array.Empty<MethodToken>(),
Script = script
};
Expand Down
24 changes: 14 additions & 10 deletions src/neo/SmartContract/NefFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace Neo.SmartContract
│ Magic │ uint32 │ Magic header │
│ Compiler │ byte[64] │ Compiler name and version │
├──────────┼───────────────┼────────────────────────────────────────────┤
│ Reserve │ byte[2] │ Reserved for future extensions. Must be 0. │
│ Source │ byte[] │ The url of the source files │
│ Reserve │ byte │ Reserved for future extensions. Must be 0. │
│ Tokens │ MethodToken[] │ Method tokens. │
│ Reserve │ byte[2] │ Reserved for future extensions. Must be 0. │
│ Script │ byte[] │ Var bytes for the payload │
Expand All @@ -50,6 +51,11 @@ public class NefFile : ISerializable
/// </summary>
public string Compiler { get; set; }

/// <summary>
/// The url of the source files.
/// </summary>
public string Source { get; set; }

/// <summary>
/// The methods that to be called statically.
/// </summary>
Expand All @@ -76,7 +82,8 @@ public class NefFile : ISerializable

public int Size =>
HeaderSize + // Header
2 + // Reserve
Source.GetVarSize() + // Source
1 + // Reserve
Tokens.GetVarSize() + // Tokens
2 + // Reserve
Script.GetVarSize() + // Script
Expand All @@ -85,7 +92,8 @@ public class NefFile : ISerializable
public void Serialize(BinaryWriter writer)
{
SerializeHeader(writer);
writer.Write((short)0);
writer.WriteVarString(Source);
writer.Write((byte)0);
writer.Write(Tokens);
writer.Write((short)0);
writer.WriteVarBytes(Script ?? Array.Empty<byte>());
Expand All @@ -101,18 +109,13 @@ private void SerializeHeader(BinaryWriter writer)
public void Deserialize(BinaryReader reader)
{
if (reader.ReadUInt32() != Magic) throw new FormatException("Wrong magic");

Compiler = reader.ReadFixedString(64);

if (reader.ReadUInt16() != 0) throw new FormatException("Reserved bytes must be 0");

Source = reader.ReadVarString(256);
if (reader.ReadByte() != 0) throw new FormatException("Reserved bytes must be 0");
Tokens = reader.ReadSerializableArray<MethodToken>(128);

if (reader.ReadUInt16() != 0) throw new FormatException("Reserved bytes must be 0");

Script = reader.ReadVarBytes(MaxScriptLength);
if (Script.Length == 0) throw new ArgumentException($"Script can't be empty");

CheckSum = reader.ReadUInt32();
if (CheckSum != ComputeChecksum(this)) throw new FormatException("CRC verification fail");
}
Expand All @@ -137,6 +140,7 @@ public JObject ToJson()
{
["magic"] = Magic,
["compiler"] = Compiler,
["source"] = Source,
["tokens"] = new JArray(Tokens.Select(p => p.ToJson())),
["script"] = Convert.ToBase64String(Script),
["checksum"] = CheckSum
Expand Down
1 change: 1 addition & 0 deletions tests/neo.UnitTests/SmartContract/UT_ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void TestSetup()
Nef = new NefFile
{
Compiler = nameof(ScriptBuilder),
Source = string.Empty,
Tokens = Array.Empty<MethodToken>(),
Script = script
},
Expand Down
1 change: 1 addition & 0 deletions tests/neo.UnitTests/SmartContract/UT_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void TestGetContractHash()
var nef = new NefFile()
{
Compiler = "test",
Source = string.Empty,
Tokens = Array.Empty<MethodToken>(),
Script = new byte[] { 1, 2, 3 }
};
Expand Down
5 changes: 5 additions & 0 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void TestContract_Create()
var nef = new NefFile()
{
Script = Enumerable.Repeat((byte)OpCode.RET, byte.MaxValue).ToArray(),
Source = string.Empty,
Compiler = "",
Tokens = System.Array.Empty<MethodToken>()
};
Expand All @@ -121,6 +122,7 @@ public void TestContract_Create()
var script_exceedMaxLength = new NefFile()
{
Script = new byte[NefFile.MaxScriptLength - 1],
Source = string.Empty,
Compiler = "",
Tokens = System.Array.Empty<MethodToken>()
};
Expand Down Expand Up @@ -151,6 +153,7 @@ public void TestContract_Update()
var nef = new NefFile()
{
Script = new[] { (byte)OpCode.RET },
Source = string.Empty,
Compiler = "",
Tokens = System.Array.Empty<MethodToken>()
};
Expand Down Expand Up @@ -201,6 +204,7 @@ public void TestContract_Update_Invalid()
var nefFile = new NefFile()
{
Script = new byte[] { 0x01 },
Source = string.Empty,
Compiler = "",
Tokens = System.Array.Empty<MethodToken>()
};
Expand All @@ -215,6 +219,7 @@ public void TestContract_Update_Invalid()
nefFile = new NefFile()
{
Script = new byte[0],
Source = string.Empty,
Compiler = "",
Tokens = System.Array.Empty<MethodToken>()
};
Expand Down
3 changes: 3 additions & 0 deletions tests/neo.UnitTests/SmartContract/UT_NefFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class UT_NefFile
public NefFile file = new()
{
Compiler = "".PadLeft(32, ' '),
Source = string.Empty,
Tokens = Array.Empty<MethodToken>(),
Script = new byte[] { 0x01, 0x02, 0x03 }
};
Expand Down Expand Up @@ -86,6 +87,7 @@ public void ParseTest()
var file = new NefFile()
{
Compiler = "".PadLeft(32, ' '),
Source = string.Empty,
Tokens = Array.Empty<MethodToken>(),
Script = new byte[] { 0x01, 0x02, 0x03 }
};
Expand All @@ -105,6 +107,7 @@ public void LimitTest()
var file = new NefFile()
{
Compiler = "".PadLeft(byte.MaxValue, ' '),
Source = string.Empty,
Tokens = Array.Empty<MethodToken>(),
Script = new byte[1024 * 1024],
CheckSum = 0
Expand Down

0 comments on commit b3b2207

Please sign in to comment.