Skip to content

Commit

Permalink
add checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y committed Oct 18, 2024
1 parent acceb92 commit 9fa8a33
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/Neo.CLI/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
"seed3.neo.org:10333",
"seed4.neo.org:10333",
"seed5.neo.org:10333"
],
"CheckPoint": [
"0x7339b8a32e6ff7e8affbbb7a7be62d95f09c7a9426f2bea582d910f2228fded0",
"0xbcb5d0b779c80b0339266cceaf33aebe9d6eb20e8d0cfcc8c8a5d3718d1247ea",
"0x153bc34532f29cd729a9015cf5b69950f9577787789f5f1bc26c4647273426fc",
"0xcad42b6bcbcbff165f862df442280c07f73ad263bd28b6fd60722a8ae7a9318b",
"0xa56a4bf59a7cec3df965c77d943a2c74a94b8117598fea6b35004f394c918e32",
"0x0e23f98c7e9eb2bac066f759009d06daf61c82ede43ae8d9d80e49bd7bdf9b57"
]
}
}
8 changes: 8 additions & 0 deletions src/Neo.CLI/config.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
"seed3.neo.org:10333",
"seed4.neo.org:10333",
"seed5.neo.org:10333"
],
"CheckPoint": [
"0x7339b8a32e6ff7e8affbbb7a7be62d95f09c7a9426f2bea582d910f2228fded0",
"0xbcb5d0b779c80b0339266cceaf33aebe9d6eb20e8d0cfcc8c8a5d3718d1247ea",
"0x153bc34532f29cd729a9015cf5b69950f9577787789f5f1bc26c4647273426fc",
"0xcad42b6bcbcbff165f862df442280c07f73ad263bd28b6fd60722a8ae7a9318b",
"0xa56a4bf59a7cec3df965c77d943a2c74a94b8117598fea6b35004f394c918e32",
"0x0e23f98c7e9eb2bac066f759009d06daf61c82ede43ae8d9d80e49bd7bdf9b57"
]
}
}
22 changes: 13 additions & 9 deletions src/Neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,8 +102,6 @@ public class Reverify
public IReadOnlyList<IInventory> Inventories { get; init; }
}

private UInt160 _currentConsensus = null;

/// <summary>
/// Sent by the <see cref="Blockchain"/> when an <see cref="IInventory"/> is relayed.
/// </summary>
Expand Down Expand Up @@ -165,18 +164,23 @@ private void OnImport(IEnumerable<Block> 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;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Neo/ProtocolSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public record ProtocolSettings
/// </summary>
public IReadOnlyList<ECPoint> StandbyValidators => _standbyValidators ??= StandbyCommittee.Take(ValidatorsCount).ToArray();

public List<string> CheckPoint { get; private init; }

/// <summary>
/// The default protocol settings for NEO MainNet.
/// </summary>
Expand All @@ -118,7 +120,8 @@ public record ProtocolSettings
MemoryPoolMaxTransactions = 50_000,
MaxTraceableBlocks = 2_102_400,
InitialGasDistribution = 52_000_000_00000000,
Hardforks = EnsureOmmitedHardforks(new Dictionary<Hardfork, uint>()).ToImmutableDictionary()
Hardforks = EnsureOmmitedHardforks(new Dictionary<Hardfork, uint>()).ToImmutableDictionary(),
CheckPoint = []
};

public static ProtocolSettings Custom { get; set; }
Expand Down Expand Up @@ -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<Hardfork>(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;
}
Expand Down

0 comments on commit 9fa8a33

Please sign in to comment.