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

Create only one snapshot for persist #1602

Merged
Merged
Show file tree
Hide file tree
Changes from 9 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
6 changes: 6 additions & 0 deletions src/neo/IO/Caching/DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ public TValue TryGet(TKey key)
}
}

public void AddAndCommit(TKey key, TValue value)
{
Add(key, value);
Commit();
}
erikzhang marked this conversation as resolved.
Show resolved Hide resolved

protected abstract TValue TryGetInternal(TKey key);

protected abstract void UpdateInternal(TKey key, TValue value);
Expand Down
12 changes: 9 additions & 3 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ private void Persist(Block block)
}
}
snapshot.Blocks.Add(block.Hash, block.Trim());
StoreView clonedSnapshot = snapshot.Clone();
// Warning: Do not write into variable snapshot directly. Write into variable clonedSnapshot and commit instead.
foreach (Transaction tx in block.Transactions)
{
var state = new TransactionState
Expand All @@ -496,15 +498,19 @@ private void Persist(Block block)
Transaction = tx
};

snapshot.Transactions.Add(tx.Hash, state);
clonedSnapshot.Transactions.AddAndCommit(tx.Hash, state);

using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, tx, snapshot.Clone(), tx.SystemFee))
using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, tx, clonedSnapshot, tx.SystemFee))
{
engine.LoadScript(tx.Script);
state.VMState = engine.Execute();
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (state.VMState == VMState.HALT)
{
engine.Snapshot.Commit();
clonedSnapshot.Commit();
shargon marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
clonedSnapshot = snapshot.Clone();
}
ApplicationExecuted application_executed = new ApplicationExecuted(engine);
Context.System.EventStream.Publish(application_executed);
Expand Down
7 changes: 7 additions & 0 deletions tests/neo.UnitTests/IO/Caching/UT_DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,5 +343,12 @@ public void TestTryGet()
myDataCache.TryGet(new MyKey("key2")).Should().Be(new MyValue("value2"));
myDataCache.TryGet(new MyKey("key3")).Should().BeNull();
}

[TestMethod]
public void TestAddAndCommit()
{
myDataCache.AddAndCommit(new MyKey("key1"), new MyValue("value1"));
myDataCache.InnerDict[new MyKey("key1")].Should().Be(new MyValue("value1"));
}
}
}