From 772f9a2d3eb105c955757d99af1632a41ef4a516 Mon Sep 17 00:00:00 2001 From: Jin Qiao Date: Mon, 20 Apr 2020 10:43:05 +0800 Subject: [PATCH 1/8] Cache most recent block --- src/neo/Ledger/Blockchain.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index fd76d1ebca..4bd3b26ab3 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -36,6 +36,7 @@ public class RelayResult { public IInventory Inventory; public VerifyResult Resu public static readonly byte ValidatorsCount = ProtocolSettings.Default.ValidatorsCount; public static readonly ECPoint[] StandbyCommittee = ProtocolSettings.Default.StandbyCommittee.Select(p => ECPoint.DecodePoint(p.HexToBytes(), ECCurve.Secp256r1)).ToArray(); public static readonly ECPoint[] StandbyValidators = StandbyCommittee[..ValidatorsCount]; + private static UInt256 lastBlockhash = null; public static readonly Block GenesisBlock = new Block { @@ -367,6 +368,7 @@ private VerifyResult OnNewBlock(Block block) Self.Tell(unverifiedBlock, ActorRefs.NoSender); block_cache_unverified.Remove(Height + 1); } + block_cache.Add(block.Hash, block); } else { @@ -430,7 +432,8 @@ private VerifyResult OnNewTransaction(Transaction transaction) private void OnPersistCompleted(Block block) { - block_cache.Remove(block.Hash); + if (lastBlockhash != null) block_cache.Remove(lastBlockhash); + lastBlockhash = block.Hash; MemPool.UpdatePoolForBlockPersisted(block, currentSnapshot); Context.System.EventStream.Publish(new PersistCompleted { Block = block }); } From f1a14e1a078838425fa11039b5f427545cf80393 Mon Sep 17 00:00:00 2001 From: Jin Qiao Date: Mon, 20 Apr 2020 15:38:09 +0800 Subject: [PATCH 2/8] Code optimization --- src/neo/Ledger/Blockchain.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 4bd3b26ab3..45330293fd 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -36,7 +36,6 @@ public class RelayResult { public IInventory Inventory; public VerifyResult Resu public static readonly byte ValidatorsCount = ProtocolSettings.Default.ValidatorsCount; public static readonly ECPoint[] StandbyCommittee = ProtocolSettings.Default.StandbyCommittee.Select(p => ECPoint.DecodePoint(p.HexToBytes(), ECCurve.Secp256r1)).ToArray(); public static readonly ECPoint[] StandbyValidators = StandbyCommittee[..ValidatorsCount]; - private static UInt256 lastBlockhash = null; public static readonly Block GenesisBlock = new Block { @@ -67,6 +66,7 @@ public class RelayResult { public IInventory Inventory; public VerifyResult Resu private readonly Dictionary> block_cache_unverified = new Dictionary>(); internal readonly RelayCache ConsensusRelayCache = new RelayCache(100); private SnapshotView currentSnapshot; + private static UInt256 lastBlockhash = null; public IStore Store { get; } public ReadOnlyView View { get; } From 140d11eb0a113d08d0ed05937a8e6ca01f721659 Mon Sep 17 00:00:00 2001 From: Jin Qiao Date: Mon, 20 Apr 2020 18:35:52 +0800 Subject: [PATCH 3/8] Code optimization --- src/neo/Ledger/Blockchain.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 45330293fd..4c774bf087 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -66,7 +66,7 @@ public class RelayResult { public IInventory Inventory; public VerifyResult Resu private readonly Dictionary> block_cache_unverified = new Dictionary>(); internal readonly RelayCache ConsensusRelayCache = new RelayCache(100); private SnapshotView currentSnapshot; - private static UInt256 lastBlockhash = null; + private UInt256 lastBlockhash = null; public IStore Store { get; } public ReadOnlyView View { get; } From fcfafe658473157e83f389641a9c14e68cfbea21 Mon Sep 17 00:00:00 2001 From: Jin Qiao Date: Tue, 21 Apr 2020 11:11:53 +0800 Subject: [PATCH 4/8] Code optimization --- src/neo/Ledger/Blockchain.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 4c774bf087..75bced18ef 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -66,7 +66,6 @@ public class RelayResult { public IInventory Inventory; public VerifyResult Resu private readonly Dictionary> block_cache_unverified = new Dictionary>(); internal readonly RelayCache ConsensusRelayCache = new RelayCache(100); private SnapshotView currentSnapshot; - private UInt256 lastBlockhash = null; public IStore Store { get; } public ReadOnlyView View { get; } @@ -432,8 +431,7 @@ private VerifyResult OnNewTransaction(Transaction transaction) private void OnPersistCompleted(Block block) { - if (lastBlockhash != null) block_cache.Remove(lastBlockhash); - lastBlockhash = block.Hash; + block_cache.Remove(block.PrevHash); MemPool.UpdatePoolForBlockPersisted(block, currentSnapshot); Context.System.EventStream.Publish(new PersistCompleted { Block = block }); } From 4a5e41dd6b313689f91f5e8559637d345f7a435e Mon Sep 17 00:00:00 2001 From: Jin Qiao Date: Tue, 21 Apr 2020 16:33:25 +0800 Subject: [PATCH 5/8] Code optimization --- src/neo/Ledger/Blockchain.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 75bced18ef..887262e022 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -367,11 +367,11 @@ private VerifyResult OnNewBlock(Block block) Self.Tell(unverifiedBlock, ActorRefs.NoSender); block_cache_unverified.Remove(Height + 1); } - block_cache.Add(block.Hash, block); + block_cache[block.Hash] = block; } else { - block_cache.Add(block.Hash, block); + block_cache[block.Hash] = block; if (block.Index + 100 >= header_index.Count) system.LocalNode.Tell(new LocalNode.RelayDirectly { Inventory = block }); if (block.Index == header_index.Count) From 2ff8f13b549c1cdd925f175df2c197dd68ad1c66 Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 21 Apr 2020 11:34:29 +0200 Subject: [PATCH 6/8] Optimize --- src/neo/Ledger/Blockchain.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 887262e022..cca8181b59 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -367,11 +367,9 @@ private VerifyResult OnNewBlock(Block block) Self.Tell(unverifiedBlock, ActorRefs.NoSender); block_cache_unverified.Remove(Height + 1); } - block_cache[block.Hash] = block; } else { - block_cache[block.Hash] = block; if (block.Index + 100 >= header_index.Count) system.LocalNode.Tell(new LocalNode.RelayDirectly { Inventory = block }); if (block.Index == header_index.Count) @@ -387,6 +385,7 @@ private VerifyResult OnNewBlock(Block block) UpdateCurrentSnapshot(); } } + block_cache[block.Hash] = block; return VerifyResult.Succeed; } From 902e2587f365823f818f10696bd3b8b3aec1fc1f Mon Sep 17 00:00:00 2001 From: Jin Qiao Date: Tue, 21 Apr 2020 18:20:02 +0800 Subject: [PATCH 7/8] Code optimization --- src/neo/Ledger/Blockchain.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index cca8181b59..5e5a5f8a4e 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -331,6 +331,7 @@ private VerifyResult OnNewBlock(Block block) if (!block.Hash.Equals(header_index[(int)block.Index])) return VerifyResult.Invalid; } + block_cache[block.Hash] = block; if (block.Index == Height + 1) { Block block_persist = block; @@ -385,7 +386,6 @@ private VerifyResult OnNewBlock(Block block) UpdateCurrentSnapshot(); } } - block_cache[block.Hash] = block; return VerifyResult.Succeed; } From 427ea145939debb3497e2de5ef2825c91674f67b Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 21 Apr 2020 17:13:47 +0200 Subject: [PATCH 8/8] Change to TryAdd --- src/neo/Ledger/Blockchain.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 5e5a5f8a4e..0a535f9a5b 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -331,7 +331,7 @@ private VerifyResult OnNewBlock(Block block) if (!block.Hash.Equals(header_index[(int)block.Index])) return VerifyResult.Invalid; } - block_cache[block.Hash] = block; + block_cache.TryAdd(block.Hash, block); if (block.Index == Height + 1) { Block block_persist = block;