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

Fix storage snapshot sync #898

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 13 additions & 6 deletions neo-modules.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,24 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SQLiteWallet", "src\SQLiteW
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorageDumper", "src\StorageDumper\StorageDumper.csproj", "{938D86EA-0F48-436B-9255-4AD9A8E6B9AC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo", "neo\src\Neo\Neo.csproj", "{A00FC746-1351-4275-B2D9-489477B409C0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo", "neo\src\Neo\Neo.csproj", "{A00FC746-1351-4275-B2D9-489477B409C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.IO", "neo\src\Neo.IO\Neo.IO.csproj", "{FEC96A32-38AB-426B-A93E-D37583BEE901}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.IO", "neo\src\Neo.IO\Neo.IO.csproj", "{FEC96A32-38AB-426B-A93E-D37583BEE901}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Json", "neo\src\Neo.Json\Neo.Json.csproj", "{188D043E-393D-4E5B-A216-4274BF9AFB23}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Json", "neo\src\Neo.Json\Neo.Json.csproj", "{188D043E-393D-4E5B-A216-4274BF9AFB23}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.VM", "neo\src\Neo.VM\Neo.VM.csproj", "{EDDE78BC-2064-4517-B9B2-25BA012A93C1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.VM", "neo\src\Neo.VM\Neo.VM.csproj", "{EDDE78BC-2064-4517-B9B2-25BA012A93C1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Extensions", "neo\src\Neo.Extensions\Neo.Extensions.csproj", "{9FF233D7-84C2-4947-96F6-88EE3594C66A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Extensions", "neo\src\Neo.Extensions\Neo.Extensions.csproj", "{9FF233D7-84C2-4947-96F6-88EE3594C66A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Cryptography.BLS12_381", "neo\src\Neo.Cryptography.BLS12_381\Neo.Cryptography.BLS12_381.csproj", "{7437514A-290D-4F84-B315-32ED95F710C1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Cryptography.BLS12_381", "neo\src\Neo.Cryptography.BLS12_381\Neo.Cryptography.BLS12_381.csproj", "{7437514A-290D-4F84-B315-32ED95F710C1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dependency", "Dependency", "{997874E0-C2A7-4EB2-85AA-180AF592DC6D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.ConsoleService", "neo\src\Neo.ConsoleService\Neo.ConsoleService.csproj", "{919EC990-C586-4B46-900E-8A7496004030}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.CLI", "neo\src\Neo.CLI\Neo.CLI.csproj", "{F9A412E0-5FAA-438B-9E2A-886EB4AA0C66}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -165,6 +167,10 @@ Global
{919EC990-C586-4B46-900E-8A7496004030}.Debug|Any CPU.Build.0 = Debug|Any CPU
{919EC990-C586-4B46-900E-8A7496004030}.Release|Any CPU.ActiveCfg = Release|Any CPU
{919EC990-C586-4B46-900E-8A7496004030}.Release|Any CPU.Build.0 = Release|Any CPU
{F9A412E0-5FAA-438B-9E2A-886EB4AA0C66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9A412E0-5FAA-438B-9E2A-886EB4AA0C66}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9A412E0-5FAA-438B-9E2A-886EB4AA0C66}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9A412E0-5FAA-438B-9E2A-886EB4AA0C66}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -195,6 +201,7 @@ Global
{9FF233D7-84C2-4947-96F6-88EE3594C66A} = {997874E0-C2A7-4EB2-85AA-180AF592DC6D}
{7437514A-290D-4F84-B315-32ED95F710C1} = {997874E0-C2A7-4EB2-85AA-180AF592DC6D}
{919EC990-C586-4B46-900E-8A7496004030} = {997874E0-C2A7-4EB2-85AA-180AF592DC6D}
{F9A412E0-5FAA-438B-9E2A-886EB4AA0C66} = {997874E0-C2A7-4EB2-85AA-180AF592DC6D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61D3ADE6-BBFC-402D-AB42-1C71C9F9EDE3}
Expand Down
14 changes: 7 additions & 7 deletions src/ApplicationLogs/Store/LogStorageStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public bool TryGetEngineState(Guid engineStateId, out EngineLogState state)
.Add(engineStateId.ToByteArray())
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<EngineLogState>();
state = data?.AsSerializable<EngineLogState>()!;
return data != null && data.Length > 0;
}

Expand All @@ -298,7 +298,7 @@ public bool TryGetTransactionEngineState(UInt256 hash, out TransactionEngineLogS
.Add(hash)
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<TransactionEngineLogState>();
state = data?.AsSerializable<TransactionEngineLogState>()!;
return data != null && data.Length > 0;
}

Expand All @@ -309,7 +309,7 @@ public bool TryGetBlockState(UInt256 hash, TriggerType trigger, out BlockLogStat
.Add((byte)trigger)
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<BlockLogState>();
state = data?.AsSerializable<BlockLogState>()!;
return data != null && data.Length > 0;
}

Expand All @@ -319,7 +319,7 @@ public bool TryGetNotifyState(Guid notifyStateId, out NotifyLogState state)
.Add(notifyStateId.ToByteArray())
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<NotifyLogState>();
state = data?.AsSerializable<NotifyLogState>()!;
return data != null && data.Length > 0;
}

Expand All @@ -331,7 +331,7 @@ public bool TryGetContractState(UInt160 scriptHash, ulong timestamp, uint iterIn
.AddBigEndian(iterIndex)
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<ContractLogState>();
state = data?.AsSerializable<ContractLogState>()!;
return data != null && data.Length > 0;
}

Expand All @@ -341,7 +341,7 @@ public bool TryGetExecutionState(Guid executionStateId, out ExecutionLogState st
.Add(executionStateId.ToByteArray())
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<ExecutionLogState>();
state = data?.AsSerializable<ExecutionLogState>()!;
return data != null && data.Length > 0;
}

Expand Down Expand Up @@ -388,7 +388,7 @@ public bool TryGetTransactionState(UInt256 hash, out TransactionLogState state)
.Add(hash)
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<TransactionLogState>();
state = data?.AsSerializable<TransactionLogState>()!;
return data != null && data.Length > 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Neo.Plugins.Store.Models
{
public class ApplicationEngineLogModel
{
public UInt160 ScriptHash { get; private init; } = UInt160.Zero;
public UInt160 ScriptHash { get; private init; } = new();
public string Message { get; private init; } = string.Empty;

public static ApplicationEngineLogModel Create(EngineLogState logEventState) =>
Expand Down
5 changes: 2 additions & 3 deletions src/ApplicationLogs/Store/Models/BlockchainEventModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
using ApplicationLogs.Store.States;
using Neo;
using Neo.VM.Types;
using Array = System.Array;

namespace ApplicationLogs.Store.Models
{
public class BlockchainEventModel
{
public UInt160 ScriptHash { get; private init; } = UInt160.Zero;
public UInt160 ScriptHash { get; private init; } = new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why create a new empty instance if we have one in memory?

Copy link
Member Author

@cschuchardt88 cschuchardt88 Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a memory leak. That's the reason why we had this problem with different addresses in the state. It uses the class instance of UInt160.Zero when doing ScriptHash.Deserialize(). It will keep writing to the same buffer; overwriting the previous value. So i need a new instance either way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we reuse the same instance, how can be a memory leak?

Copy link
Member Author

@cschuchardt88 cschuchardt88 May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shargon we are using the same instance. That was the problem. The instance being UInt160.Zero; overwriting it for each method call.

public string EventName { get; private init; } = string.Empty;
public StackItem[] State { get; private init; } = Array.Empty<StackItem>();
public StackItem[] State { get; private init; } = [];

public static BlockchainEventModel Create(UInt160 scriptHash, string eventName, StackItem[] state) =>
new()
Expand Down
6 changes: 3 additions & 3 deletions src/ApplicationLogs/Store/Models/BlockchainExecutionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class BlockchainExecutionModel
public VMState VmState { get; private init; } = VMState.NONE;
public string Exception { get; private init; } = string.Empty;
public long GasConsumed { get; private init; } = 0L;
public StackItem[] Stack { get; private init; } = System.Array.Empty<StackItem>();
public BlockchainEventModel[] Notifications { get; set; } = System.Array.Empty<BlockchainEventModel>();
public ApplicationEngineLogModel[] Logs { get; set; } = System.Array.Empty<ApplicationEngineLogModel>();
public StackItem[] Stack { get; private init; } = [];
public BlockchainEventModel[] Notifications { get; set; } = [];
public ApplicationEngineLogModel[] Logs { get; set; } = [];

public static BlockchainExecutionModel Create(TriggerType trigger, ExecutionLogState executionLogState, StackItem[] stack) =>
new()
Expand Down
2 changes: 1 addition & 1 deletion src/ApplicationLogs/Store/States/BlockLogState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ApplicationLogs.Store.States
{
public class BlockLogState : ISerializable, IEquatable<BlockLogState>
{
public Guid[] NotifyLogIds { get; private set; } = Array.Empty<Guid>();
public Guid[] NotifyLogIds { get; private set; } = [];

public static BlockLogState Create(Guid[] notifyLogIds) =>
new()
Expand Down
2 changes: 1 addition & 1 deletion src/ApplicationLogs/Store/States/ContractLogState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace ApplicationLogs.Store.States
{
public class ContractLogState : NotifyLogState, IEquatable<ContractLogState>
{
public UInt256 TransactionHash { get; private set; } = UInt256.Zero;
public UInt256 TransactionHash { get; private set; } = new();
public TriggerType Trigger { get; private set; } = TriggerType.All;

public static ContractLogState Create(Blockchain.ApplicationExecuted applicationExecuted, NotifyEventArgs notifyEventArgs, Guid[] stackItemIds) =>
Expand Down
2 changes: 1 addition & 1 deletion src/ApplicationLogs/Store/States/EngineLogState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Neo.Plugins.Store.States
{
public class EngineLogState : ISerializable, IEquatable<EngineLogState>
{
public UInt160 ScriptHash { get; private set; } = UInt160.Zero;
public UInt160 ScriptHash { get; private set; } = new();
public string Message { get; private set; } = string.Empty;

public static EngineLogState Create(UInt160 scriptHash, string message) =>
Expand Down
4 changes: 2 additions & 2 deletions src/ApplicationLogs/Store/States/ExecutionLogState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public class ExecutionLogState : ISerializable, IEquatable<ExecutionLogState>
public VMState VmState { get; private set; } = VMState.NONE;
public string Exception { get; private set; } = string.Empty;
public long GasConsumed { get; private set; } = 0L;
public Guid[] StackItemIds { get; private set; } = Array.Empty<Guid>();
public Guid[] StackItemIds { get; private set; } = [];

public static ExecutionLogState Create(Blockchain.ApplicationExecuted appExecution, Guid[] stackItemIds) =>
new()
{
VmState = appExecution.VMState,
Exception = appExecution.Exception?.InnerException?.Message ?? appExecution.Exception?.Message,
Exception = appExecution.Exception?.InnerException?.Message ?? appExecution.Exception?.Message!,
GasConsumed = appExecution.GasConsumed,
StackItemIds = stackItemIds,
};
Expand Down
8 changes: 4 additions & 4 deletions src/ApplicationLogs/Store/States/NotifyLogState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace ApplicationLogs.Store.States
{
public class NotifyLogState : ISerializable, IEquatable<NotifyLogState>
{
public UInt160 ScriptHash { get; protected set; } = UInt160.Zero;
public UInt160 ScriptHash { get; protected set; } = new();
public string EventName { get; protected set; } = string.Empty;
public Guid[] StackItemIds { get; protected set; } = Array.Empty<Guid>();
public Guid[] StackItemIds { get; protected set; } = [];

public static NotifyLogState Create(NotifyEventArgs notifyItem, Guid[] stackItemsIds) =>
new()
Expand All @@ -44,7 +44,7 @@ public virtual void Deserialize(ref MemoryReader reader)
// It should be safe because it filled from a transaction's notifications.
uint aLen = reader.ReadUInt32();
StackItemIds = new Guid[aLen];
for (int i = 0; i < aLen; i++)
for (var i = 0; i < aLen; i++)
StackItemIds[i] = new Guid(reader.ReadVarMemory().Span);
}

Expand All @@ -54,7 +54,7 @@ public virtual void Serialize(BinaryWriter writer)
writer.WriteVarString(EventName ?? string.Empty);

writer.Write((uint)StackItemIds.Length);
for (int i = 0; i < StackItemIds.Length; i++)
for (var i = 0; i < StackItemIds.Length; i++)
writer.WriteVarBytes(StackItemIds[i].ToByteArray());
}

Expand Down
Loading