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

Small OnPersist optimization #1058

Merged
merged 2 commits into from
Aug 26, 2019
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
24 changes: 13 additions & 11 deletions neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class FillCompleted { }
Transactions = new[] { DeployNativeContracts() }
};

private readonly static byte[] onPersistNativeContractScript;
private const int MaxTxToReverifyPerIdle = 10;
private static readonly object lockObj = new object();
private readonly NeoSystem system;
Expand Down Expand Up @@ -83,6 +84,15 @@ public static Blockchain Singleton
static Blockchain()
{
GenesisBlock.RebuildMerkleRoot();

NativeContract[] contracts = { NativeContract.GAS, NativeContract.NEO };
using (ScriptBuilder sb = new ScriptBuilder())
{
foreach (NativeContract contract in contracts)
sb.EmitAppCall(contract.Hash, "onPersist");

onPersistNativeContractScript = sb.ToArray();
}
}

public Blockchain(NeoSystem system, Store store)
Expand Down Expand Up @@ -416,15 +426,9 @@ private void Persist(Block block)
snapshot.PersistingBlock = block;
if (block.Index > 0)
{
NativeContract[] contracts = { NativeContract.GAS, NativeContract.NEO };
using (ApplicationEngine engine = new ApplicationEngine(TriggerType.System, null, snapshot, 0, true))
{
using (ScriptBuilder sb = new ScriptBuilder())
{
foreach (NativeContract contract in contracts)
sb.EmitAppCall(contract.Hash, "onPersist");
engine.LoadScript(sb.ToArray());
}
engine.LoadScript(onPersistNativeContractScript);
if (engine.Execute() != VMState.HALT) throw new InvalidOperationException();
ApplicationExecuted application_executed = new ApplicationExecuted(engine);
Context.System.EventStream.Publish(application_executed);
Expand Down Expand Up @@ -455,13 +459,11 @@ private void Persist(Block block)
all_application_executed.Add(application_executed);
}
}
snapshot.BlockHashIndex.GetAndChange().Hash = block.Hash;
snapshot.BlockHashIndex.GetAndChange().Index = block.Index;
snapshot.BlockHashIndex.GetAndChange().Set(block);
if (block.Index == header_index.Count)
{
header_index.Add(block.Hash);
snapshot.HeaderHashIndex.GetAndChange().Hash = block.Hash;
snapshot.HeaderHashIndex.GetAndChange().Index = block.Index;
snapshot.HeaderHashIndex.GetAndChange().Set(block);
}
foreach (IPersistencePlugin plugin in Plugin.PersistencePlugins)
plugin.OnPersist(snapshot, all_application_executed);
Expand Down
7 changes: 7 additions & 0 deletions neo/Ledger/HashIndexState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Neo.IO;
using Neo.Network.P2P.Payloads;
using System.IO;

namespace Neo.Ledger
Expand Down Expand Up @@ -36,5 +37,11 @@ void ISerializable.Serialize(BinaryWriter writer)
writer.Write(Hash);
writer.Write(Index);
}

internal void Set(BlockBase block)
{
Hash = block.Hash;
Index = block.Index;
}
}
}