From 2c05c8177a745e603e63575d3dcd5ca14d967f54 Mon Sep 17 00:00:00 2001 From: Shargon Date: Mon, 26 Aug 2019 09:15:41 +0200 Subject: [PATCH 1/2] Small update --- neo/Ledger/Blockchain.cs | 6 ++---- neo/Ledger/HashIndexState.cs | 7 +++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/neo/Ledger/Blockchain.cs b/neo/Ledger/Blockchain.cs index db879c0732..8ac68477de 100644 --- a/neo/Ledger/Blockchain.cs +++ b/neo/Ledger/Blockchain.cs @@ -455,13 +455,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); diff --git a/neo/Ledger/HashIndexState.cs b/neo/Ledger/HashIndexState.cs index 04f7b0bd4a..fb7439a252 100644 --- a/neo/Ledger/HashIndexState.cs +++ b/neo/Ledger/HashIndexState.cs @@ -1,4 +1,5 @@ using Neo.IO; +using Neo.Network.P2P.Payloads; using System.IO; namespace Neo.Ledger @@ -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; + } } } From 8be333cbf44c5787e56a6fd07e7fba26c91fd488 Mon Sep 17 00:00:00 2001 From: Shargon Date: Mon, 26 Aug 2019 09:19:13 +0200 Subject: [PATCH 2/2] Cache native persist script --- neo/Ledger/Blockchain.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/neo/Ledger/Blockchain.cs b/neo/Ledger/Blockchain.cs index 8ac68477de..a74bfa981c 100644 --- a/neo/Ledger/Blockchain.cs +++ b/neo/Ledger/Blockchain.cs @@ -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; @@ -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) @@ -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);