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

Lightweight blockstate #2565

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 11 additions & 0 deletions src/neo/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,16 @@ internal static IPEndPoint Unmap(this IPEndPoint endPoint)
return endPoint;
return new IPEndPoint(endPoint.Address.Unmap(), endPoint.Port);
}

internal static byte[] XOR(this byte[] value, byte[] other)
{
if (value.Length != other.Length) return null;

for (int i = 0; i < value.Length; i++)
{
value[i] ^= other[i];
}
return value;
}
}
}
9 changes: 8 additions & 1 deletion src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;

namespace Neo.Ledger
{
Expand Down Expand Up @@ -390,14 +391,16 @@ private void Persist(Block block)
all_application_executed.Add(application_executed);
}
DataCache clonedSnapshot = snapshot.CreateSnapshot();
using var sha256 = SHA256.Create();
byte[] state = null;
// Warning: Do not write into variable snapshot directly. Write into variable clonedSnapshot and commit instead.
foreach (Transaction tx in block.Transactions)
{
using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, tx, clonedSnapshot, block, system.Settings, tx.SystemFee);
engine.LoadScript(tx.Script);
if (engine.Execute() == VMState.HALT)
{
clonedSnapshot.Commit();
state = sha256.ComputeHash(clonedSnapshot.Commit()).XOR(state);
}
else
{
Expand All @@ -417,6 +420,10 @@ private void Persist(Block block)
}
foreach (IPersistencePlugin plugin in Plugin.PersistencePlugins)
plugin.OnPersist(system, block, snapshot, all_application_executed);

// TODO: this should be moved to a native contract or somewhere to make is more reasonable
// The key should contain the block height
snapshot.Add(new StorageKey(), new StorageItem(state));
snapshot.Commit();
List<Exception> commitExceptions = null;
foreach (IPersistencePlugin plugin in Plugin.PersistencePlugins)
Expand Down
22 changes: 21 additions & 1 deletion src/neo/Persistence/DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
using Neo.SmartContract;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using Neo.Cryptography;

namespace Neo.Persistence
{
Expand Down Expand Up @@ -30,6 +33,16 @@ public class Trackable
/// The state of the entry.
/// </summary>
public TrackState State;

public byte[] GetHash()
{
using MemoryStream ms = new();
using BinaryWriter writer = new(ms);
writer.Write(Key);
writer.Write(Item);
writer.Write((byte)State);
return ms.ToArray().Sha256();
}
}

private readonly Dictionary<StorageKey, Trackable> dictionary = new();
Expand Down Expand Up @@ -100,10 +113,14 @@ public void Add(StorageKey key, StorageItem value)
/// <summary>
/// Commits all changes in the cache to the underlying storage.
/// </summary>
public virtual void Commit()
public virtual byte[] Commit()
{
using var sha256 = SHA256.Create();
byte[] state = null;
LinkedList<StorageKey> deletedItem = new();
foreach (Trackable trackable in GetChangeSet())
{
state = sha256.ComputeHash(trackable.GetHash()).XOR(state);
switch (trackable.State)
{
case TrackState.Added:
Expand All @@ -119,11 +136,14 @@ public virtual void Commit()
deletedItem.AddFirst(trackable.Key);
break;
}
}

foreach (StorageKey key in deletedItem)
{
dictionary.Remove(key);
}
changeSet.Clear();
return state;
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/neo/Persistence/SnapshotCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ protected override void DeleteInternal(StorageKey key)
snapshot?.Delete(key.ToArray());
}

public override void Commit()
public override byte[] Commit()
{
base.Commit();
snapshot.Commit();
return null;
}

protected override bool ContainsInternal(StorageKey key)
Expand Down