Skip to content

Commit

Permalink
Small OnPersist optimization (#1058)
Browse files Browse the repository at this point in the history
* Small update

* Cache native persist script
  • Loading branch information
shargon committed Aug 26, 2019
1 parent 98173cf commit 25666ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
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;
}
}
}

0 comments on commit 25666ba

Please sign in to comment.