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

Optimize contract calls #3170

Merged
merged 6 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void IInteroperable.FromStackItem(StackItem stackItem)
Id = (int)array[0].GetInteger();
UpdateCounter = (ushort)array[1].GetInteger();
Hash = new UInt160(array[2].GetSpan());
Nef = ((ByteString)array[3]).Memory.AsSerializable<NefFile>();
Nef = NefFile.Parse(((ByteString)array[3]).Memory, false);
shargon marked this conversation as resolved.
Show resolved Hide resolved
Manifest = array[4].ToInteroperable<ContractManifest>();
}

Expand Down
25 changes: 22 additions & 3 deletions src/Neo/SmartContract/NefFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ public class NefFile : ISerializable
Script.GetVarSize() + // Script
sizeof(uint); // Checksum

/// <summary>
/// Parse NefFile from memory
/// </summary>
/// <param name="memory">Memory</param>
/// <param name="ensureValid">Do checksum and MaxItemSize checks</param>
/// <returns>NefFile</returns>
public static NefFile Parse(ReadOnlyMemory<byte> memory, bool ensureValid = true)
{
var reader = new MemoryReader(memory);
var nef = new NefFile();
nef.Deserialize(ref reader, ensureValid);
return nef;
}

public void Serialize(BinaryWriter writer)
{
SerializeHeader(writer);
Expand All @@ -103,7 +117,9 @@ private void SerializeHeader(BinaryWriter writer)
writer.WriteFixedString(Compiler, 64);
}

public void Deserialize(ref MemoryReader reader)
public void Deserialize(ref MemoryReader reader) => Deserialize(ref reader, true);

public void Deserialize(ref MemoryReader reader, bool ensureValid)
{
long startPosition = reader.Position;
if (reader.ReadUInt32() != Magic) throw new FormatException("Wrong magic");
Expand All @@ -115,8 +131,11 @@ public void Deserialize(ref MemoryReader reader)
Script = reader.ReadVarMemory((int)ExecutionEngineLimits.Default.MaxItemSize);
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");
if (reader.Position - startPosition > ExecutionEngineLimits.Default.MaxItemSize) throw new FormatException("Max vm item size exceed");
if (ensureValid)
{
if (CheckSum != ComputeChecksum(this)) throw new FormatException("CRC verification fail");
if (reader.Position - startPosition > ExecutionEngineLimits.Default.MaxItemSize) throw new FormatException("Max vm item size exceed");
}
}

/// <summary>
Expand Down
Loading