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 17 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
13 changes: 10 additions & 3 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,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 @@ -495,15 +497,20 @@ private void Persist(Block block)
Transaction = tx
};

snapshot.Transactions.Add(tx.Hash, state);
clonedSnapshot.Transactions.Add(tx.Hash, state);
clonedSnapshot.Transactions.Commit();
erikzhang marked this conversation as resolved.
Show resolved Hide resolved

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
61 changes: 61 additions & 0 deletions tests/neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Akka.Actor;
using Akka.TestKit.Xunit2;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -8,8 +9,10 @@
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.SmartContract.Native.Tokens;
using Neo.VM;
using Neo.Wallets;
using Neo.Wallets.NEP6;
using System;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -135,6 +138,64 @@ public void TestValidTransaction()
}
}

[TestMethod]
public void TestInvalidTransactionInPersist()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
//var validators = NativeContract.NEO.GetValidators(snapshot).Reverse().ToArray();
var tx = new Transaction()
{
Attributes = Array.Empty<TransactionAttribute>(),
NetworkFee = 0,
Nonce = (uint)Environment.TickCount,
Script = new byte[] { 1 },
Sender = UInt160.Zero,
SystemFee = 0,
ValidUntilBlock = Blockchain.GenesisBlock.Index + 1,
Version = 0,
Witnesses = new Witness[0],
};
snapshot.Transactions.TryGet(tx.Hash).Should().BeNull();
var block = new TestBlock
{
Version = 0,
PrevHash = UInt256.Zero,
MerkleRoot = UInt256.Zero,
Timestamp = 0,
Index = 1,
NextConsensus = UInt160.Zero,
Transactions = new Transaction[] { tx },
ConsensusData = new ConsensusData
{
PrimaryIndex = 0
},
Witness = new Witness()
};
system.Blockchain.Ask(block).Wait();
snapshot = Blockchain.Singleton.GetSnapshot();
snapshot.Transactions.TryGet(tx.Hash).Should().NotBeNull();
snapshot.Transactions.TryGet(tx.Hash).VMState.Should().Be(VMState.FAULT);
snapshot.Transactions.Delete(tx.Hash);
snapshot.Blocks.Delete(block.Hash);
snapshot.PersistingBlock = null;
snapshot.BlockHashIndex.GetAndChange().Set(Blockchain.GenesisBlock);
snapshot.HeaderHashIndex.GetAndChange().Set(Blockchain.GenesisBlock);
snapshot.Storages.Delete(CreateStorageKey(14));
snapshot.Commit();
}

internal static StorageKey CreateStorageKey(byte prefix, byte[] key = null)
{
StorageKey storageKey = new StorageKey
{
Id = NativeContract.NEO.Id,
Key = new byte[sizeof(byte) + (key?.Length ?? 0)]
};
storageKey.Key[0] = prefix;
key?.CopyTo(storageKey.Key.AsSpan(1));
return storageKey;
}

private Transaction CreateValidTx(NEP6Wallet wallet, UInt160 account, uint nonce)
{
var tx = wallet.MakeTransaction(new TransferOutput[]
Expand Down