|
| 1 | +// Copyright (C) 2015-2025 The Neo Project. |
| 2 | +// |
| 3 | +// BenchmarkProtocolSettings.cs file belongs to the neo project and is free |
| 4 | +// software distributed under the MIT software license, see the |
| 5 | +// accompanying file LICENSE in the main directory of the |
| 6 | +// repository or http://www.opensource.org/licenses/mit-license.php |
| 7 | +// for more details. |
| 8 | +// |
| 9 | +// Redistribution and use in source and binary forms with or without |
| 10 | +// modifications are permitted. |
| 11 | + |
| 12 | +using Neo; |
| 13 | +using Neo.Cryptography.ECC; |
| 14 | +using Neo.SmartContract; |
| 15 | +using System; |
| 16 | +using System.Buffers.Binary; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
| 19 | + |
| 20 | +namespace Neo.VM.Benchmark.Infrastructure |
| 21 | +{ |
| 22 | + internal static class BenchmarkProtocolSettings |
| 23 | + { |
| 24 | + private static readonly IReadOnlyList<ECPoint> s_fallbackValidators = CreateFallbackValidators(); |
| 25 | + private static readonly IReadOnlyList<ECPoint> s_fallbackCommittee = s_fallbackValidators; |
| 26 | + |
| 27 | + public static IReadOnlyList<ECPoint> StandbyValidators => ProtocolSettings.Default.StandbyValidators.Count > 0 |
| 28 | + ? ProtocolSettings.Default.StandbyValidators |
| 29 | + : s_fallbackValidators; |
| 30 | + |
| 31 | + public static IReadOnlyList<ECPoint> StandbyCommittee => ProtocolSettings.Default.StandbyCommittee.Count > 0 |
| 32 | + ? ProtocolSettings.Default.StandbyCommittee |
| 33 | + : s_fallbackCommittee; |
| 34 | + |
| 35 | + public static int ValidatorsCount => ProtocolSettings.Default.ValidatorsCount > 0 |
| 36 | + ? ProtocolSettings.Default.ValidatorsCount |
| 37 | + : StandbyValidators.Count; |
| 38 | + |
| 39 | + public static UInt160 GetCommitteeAddress() |
| 40 | + { |
| 41 | + return Contract.GetBFTAddress(StandbyValidators); |
| 42 | + } |
| 43 | + |
| 44 | + public static ProtocolSettings ResolveSettings(ProtocolSettings? candidate = null) |
| 45 | + { |
| 46 | + var baseSettings = candidate ?? ProtocolSettings.Default; |
| 47 | + if (baseSettings.StandbyCommittee.Count > 0 && baseSettings.ValidatorsCount > 0) |
| 48 | + return baseSettings; |
| 49 | + |
| 50 | + var committee = s_fallbackCommittee.Select(static p => (ECPoint)p).ToArray(); |
| 51 | + var validatorsCount = committee.Length; |
| 52 | + |
| 53 | + return new ProtocolSettings |
| 54 | + { |
| 55 | + Network = baseSettings.Network, |
| 56 | + AddressVersion = baseSettings.AddressVersion, |
| 57 | + StandbyCommittee = committee, |
| 58 | + ValidatorsCount = validatorsCount, |
| 59 | + SeedList = baseSettings.SeedList.ToArray(), |
| 60 | + MillisecondsPerBlock = baseSettings.MillisecondsPerBlock, |
| 61 | + MaxValidUntilBlockIncrement = baseSettings.MaxValidUntilBlockIncrement, |
| 62 | + MaxTransactionsPerBlock = baseSettings.MaxTransactionsPerBlock, |
| 63 | + MemoryPoolMaxTransactions = baseSettings.MemoryPoolMaxTransactions, |
| 64 | + MaxTraceableBlocks = baseSettings.MaxTraceableBlocks, |
| 65 | + InitialGasDistribution = baseSettings.InitialGasDistribution, |
| 66 | + Hardforks = baseSettings.Hardforks |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + private static IReadOnlyList<ECPoint> CreateFallbackValidators() |
| 71 | + { |
| 72 | + var curve = ECCurve.Secp256r1; |
| 73 | + return new[] |
| 74 | + { |
| 75 | + Multiply(curve.G, 1u), |
| 76 | + Multiply(curve.G, 2u), |
| 77 | + Multiply(curve.G, 3u), |
| 78 | + Multiply(curve.G, 5u), |
| 79 | + Multiply(curve.G, 7u), |
| 80 | + Multiply(curve.G, 11u), |
| 81 | + Multiply(curve.G, 13u) |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + private static ECPoint Multiply(ECPoint point, uint scalar) |
| 86 | + { |
| 87 | + var buffer = new byte[32]; |
| 88 | + BinaryPrimitives.WriteUInt32BigEndian(buffer.AsSpan(28), scalar); |
| 89 | + return point * buffer; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments