From acceb923e77af472020821e4a5cded566d85bdae Mon Sep 17 00:00:00 2001 From: Jimmy Date: Fri, 18 Oct 2024 11:32:40 +0800 Subject: [PATCH 1/2] optimize the resync process --- src/Neo/Ledger/Blockchain.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Neo/Ledger/Blockchain.cs b/src/Neo/Ledger/Blockchain.cs index f675226303..7da4b8f530 100644 --- a/src/Neo/Ledger/Blockchain.cs +++ b/src/Neo/Ledger/Blockchain.cs @@ -101,6 +101,8 @@ public class Reverify public IReadOnlyList Inventories { get; init; } } + private UInt160 _currentConsensus = null; + /// /// Sent by the when an is relayed. /// @@ -160,8 +162,21 @@ private void OnImport(IEnumerable blocks, bool verify) if (block.Index <= currentHeight) continue; if (block.Index != currentHeight + 1) throw new InvalidOperationException(); - if (verify && !block.Verify(system.Settings, system.StoreView)) - throw new InvalidOperationException(); + + if (verify) + { + var nextConsensus = block.NextConsensus; + if (block.Index == 0) + { + _currentConsensus = nextConsensus; + } + else if (nextConsensus != _currentConsensus) + { + if (!block.Verify(system.Settings, system.StoreView)) + throw new InvalidOperationException(); + _currentConsensus = nextConsensus; + } + } Persist(block); ++currentHeight; } @@ -475,6 +490,7 @@ private void Persist(Block block) } InvokeCommitted(system, block); system.MemPool.UpdatePoolForBlockPersisted(block, system.StoreView); + extensibleWitnessWhiteList = null; block_cache.Remove(block.PrevHash); Context.System.EventStream.Publish(new PersistCompleted { Block = block }); From 9fa8a339604d7a623aecf47b611cd0b3aa43590f Mon Sep 17 00:00:00 2001 From: Jimmy Date: Fri, 18 Oct 2024 14:28:15 +0800 Subject: [PATCH 2/2] add checkpoint --- src/Neo.CLI/config.json | 8 ++++++++ src/Neo.CLI/config.mainnet.json | 8 ++++++++ src/Neo/Ledger/Blockchain.cs | 22 +++++++++++++--------- src/Neo/ProtocolSettings.cs | 8 ++++++-- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/Neo.CLI/config.json b/src/Neo.CLI/config.json index 772a221714..1ee0df58dd 100644 --- a/src/Neo.CLI/config.json +++ b/src/Neo.CLI/config.json @@ -71,6 +71,14 @@ "seed3.neo.org:10333", "seed4.neo.org:10333", "seed5.neo.org:10333" + ], + "CheckPoint": [ + "0x7339b8a32e6ff7e8affbbb7a7be62d95f09c7a9426f2bea582d910f2228fded0", + "0xbcb5d0b779c80b0339266cceaf33aebe9d6eb20e8d0cfcc8c8a5d3718d1247ea", + "0x153bc34532f29cd729a9015cf5b69950f9577787789f5f1bc26c4647273426fc", + "0xcad42b6bcbcbff165f862df442280c07f73ad263bd28b6fd60722a8ae7a9318b", + "0xa56a4bf59a7cec3df965c77d943a2c74a94b8117598fea6b35004f394c918e32", + "0x0e23f98c7e9eb2bac066f759009d06daf61c82ede43ae8d9d80e49bd7bdf9b57" ] } } diff --git a/src/Neo.CLI/config.mainnet.json b/src/Neo.CLI/config.mainnet.json index 772a221714..1ee0df58dd 100644 --- a/src/Neo.CLI/config.mainnet.json +++ b/src/Neo.CLI/config.mainnet.json @@ -71,6 +71,14 @@ "seed3.neo.org:10333", "seed4.neo.org:10333", "seed5.neo.org:10333" + ], + "CheckPoint": [ + "0x7339b8a32e6ff7e8affbbb7a7be62d95f09c7a9426f2bea582d910f2228fded0", + "0xbcb5d0b779c80b0339266cceaf33aebe9d6eb20e8d0cfcc8c8a5d3718d1247ea", + "0x153bc34532f29cd729a9015cf5b69950f9577787789f5f1bc26c4647273426fc", + "0xcad42b6bcbcbff165f862df442280c07f73ad263bd28b6fd60722a8ae7a9318b", + "0xa56a4bf59a7cec3df965c77d943a2c74a94b8117598fea6b35004f394c918e32", + "0x0e23f98c7e9eb2bac066f759009d06daf61c82ede43ae8d9d80e49bd7bdf9b57" ] } } diff --git a/src/Neo/Ledger/Blockchain.cs b/src/Neo/Ledger/Blockchain.cs index 7da4b8f530..7d89eaf9e2 100644 --- a/src/Neo/Ledger/Blockchain.cs +++ b/src/Neo/Ledger/Blockchain.cs @@ -20,6 +20,7 @@ using Neo.SmartContract; using Neo.SmartContract.Native; using Neo.VM; +using Neo.Wallets; using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -101,8 +102,6 @@ public class Reverify public IReadOnlyList Inventories { get; init; } } - private UInt160 _currentConsensus = null; - /// /// Sent by the when an is relayed. /// @@ -165,18 +164,23 @@ private void OnImport(IEnumerable blocks, bool verify) if (verify) { - var nextConsensus = block.NextConsensus; - if (block.Index == 0) + const int CheckpointInterval = 1_000_000; + var maxCheckpoint = system.Settings.CheckPoint.Count * CheckpointInterval; + if (block.Index % CheckpointInterval == 0 && block.Index >= CheckpointInterval && block.Index <= maxCheckpoint) { - _currentConsensus = nextConsensus; + var checkpointIndex = (int)(block.Index / CheckpointInterval - 1); + if (system.Settings.CheckPoint[checkpointIndex] != block.Hash.ToString()) + { + throw new InvalidOperationException("Invalid block hash for checkpoint."); + } } - else if (nextConsensus != _currentConsensus) + + if (!block.Verify(system.Settings, system.StoreView)) { - if (!block.Verify(system.Settings, system.StoreView)) - throw new InvalidOperationException(); - _currentConsensus = nextConsensus; + throw new InvalidOperationException("Block verification failed."); } } + Persist(block); ++currentHeight; } diff --git a/src/Neo/ProtocolSettings.cs b/src/Neo/ProtocolSettings.cs index 19011dc24c..73d797aaa8 100644 --- a/src/Neo/ProtocolSettings.cs +++ b/src/Neo/ProtocolSettings.cs @@ -103,6 +103,8 @@ public record ProtocolSettings /// public IReadOnlyList StandbyValidators => _standbyValidators ??= StandbyCommittee.Take(ValidatorsCount).ToArray(); + public List CheckPoint { get; private init; } + /// /// The default protocol settings for NEO MainNet. /// @@ -118,7 +120,8 @@ public record ProtocolSettings MemoryPoolMaxTransactions = 50_000, MaxTraceableBlocks = 2_102_400, InitialGasDistribution = 52_000_000_00000000, - Hardforks = EnsureOmmitedHardforks(new Dictionary()).ToImmutableDictionary() + Hardforks = EnsureOmmitedHardforks(new Dictionary()).ToImmutableDictionary(), + CheckPoint = [] }; public static ProtocolSettings Custom { get; set; } @@ -163,7 +166,8 @@ public static ProtocolSettings Load(IConfigurationSection section) InitialGasDistribution = section.GetValue("InitialGasDistribution", Default.InitialGasDistribution), Hardforks = section.GetSection("Hardforks").Exists() ? EnsureOmmitedHardforks(section.GetSection("Hardforks").GetChildren().ToDictionary(p => Enum.Parse(p.Key, true), p => uint.Parse(p.Value))).ToImmutableDictionary() - : Default.Hardforks + : Default.Hardforks, + CheckPoint = section.GetSection("CheckPoint").GetChildren().Select(p => p.Value).ToList() }; return Custom; }