forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtocolSettings.cs
More file actions
195 lines (172 loc) · 8.4 KB
/
Copy pathProtocolSettings.cs
File metadata and controls
195 lines (172 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (C) 2015-2024 The Neo Project.
//
// ProtocolSettings.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using Microsoft.Extensions.Configuration;
using Neo.Cryptography.ECC;
using Neo.Network.P2P.Payloads;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
namespace Neo
{
/// <summary>
/// Represents the protocol settings of the NEO system.
/// </summary>
public record ProtocolSettings
{
/// <summary>
/// The magic number of the NEO network.
/// </summary>
public uint Network { get; init; }
/// <summary>
/// The address version of the NEO system.
/// </summary>
public byte AddressVersion { get; init; }
/// <summary>
/// The public keys of the standby committee members.
/// </summary>
public IReadOnlyList<ECPoint> StandbyCommittee { get; init; }
/// <summary>
/// The number of members of the committee in NEO system.
/// </summary>
public int CommitteeMembersCount => StandbyCommittee.Count;
/// <summary>
/// The number of the validators in NEO system.
/// </summary>
public int ValidatorsCount { get; init; }
/// <summary>
/// The default seed nodes list.
/// </summary>
public string[] SeedList { get; init; }
/// <summary>
/// Indicates the time in milliseconds between two blocks.
/// </summary>
public uint MillisecondsPerBlock { get; init; }
/// <summary>
/// Indicates the time between two blocks.
/// </summary>
public TimeSpan TimePerBlock => TimeSpan.FromMilliseconds(MillisecondsPerBlock);
/// <summary>
/// The maximum increment of the <see cref="Transaction.ValidUntilBlock"/> field.
/// </summary>
public uint MaxValidUntilBlockIncrement => 86400000 / MillisecondsPerBlock;
/// <summary>
/// Indicates the maximum number of transactions that can be contained in a block.
/// </summary>
public uint MaxTransactionsPerBlock { get; init; }
/// <summary>
/// Indicates the maximum number of transactions that can be contained in the memory pool.
/// </summary>
public int MemoryPoolMaxTransactions { get; init; }
/// <summary>
/// Indicates the maximum number of blocks that can be traced in the smart contract.
/// </summary>
public uint MaxTraceableBlocks { get; init; }
/// <summary>
/// Sets the block height from which a hardfork is activated.
/// </summary>
public ImmutableDictionary<Hardfork, uint> Hardforks { get; init; }
/// <summary>
/// Indicates the amount of gas to distribute during initialization.
/// </summary>
public ulong InitialGasDistribution { get; init; }
private IReadOnlyList<ECPoint> _standbyValidators;
/// <summary>
/// The public keys of the standby validators.
/// </summary>
public IReadOnlyList<ECPoint> StandbyValidators => _standbyValidators ??= StandbyCommittee.Take(ValidatorsCount).ToArray();
/// <summary>
/// The default protocol settings for NEO MainNet.
/// </summary>
public static ProtocolSettings Default { get; } = Custom ?? new ProtocolSettings
{
Network = 0u,
AddressVersion = 0x35,
StandbyCommittee = Array.Empty<ECPoint>(),
ValidatorsCount = 0,
SeedList = Array.Empty<string>(),
MillisecondsPerBlock = 15000,
MaxTransactionsPerBlock = 512,
MemoryPoolMaxTransactions = 50_000,
MaxTraceableBlocks = 2_102_400,
InitialGasDistribution = 52_000_000_00000000,
Hardforks = ImmutableDictionary<Hardfork, uint>.Empty
};
public static ProtocolSettings? Custom { get; set; }
/// <summary>
/// Loads the <see cref="ProtocolSettings"/> at the specified path.
/// </summary>
/// <param name="path">The path of the settings file.</param>
/// <param name="optional">Indicates whether the file is optional.</param>
/// <returns>The loaded <see cref="ProtocolSettings"/>.</returns>
public static ProtocolSettings Load(string path, bool optional = true)
{
IConfigurationRoot config = new ConfigurationBuilder().AddJsonFile(path, optional).Build();
IConfigurationSection section = config.GetSection("ProtocolConfiguration");
var settings = Load(section);
CheckingHardfork(settings);
return settings;
}
/// <summary>
/// Loads the <see cref="ProtocolSettings"/> with the specified <see cref="IConfigurationSection"/>.
/// </summary>
/// <param name="section">The <see cref="IConfigurationSection"/> to be loaded.</param>
/// <returns>The loaded <see cref="ProtocolSettings"/>.</returns>
public static ProtocolSettings Load(IConfigurationSection section)
{
return new ProtocolSettings
{
Network = section.GetValue("Network", Default.Network),
AddressVersion = section.GetValue("AddressVersion", Default.AddressVersion),
StandbyCommittee = section.GetSection("StandbyCommittee").Exists()
? section.GetSection("StandbyCommittee").GetChildren().Select(p => ECPoint.Parse(p.Get<string>(), ECCurve.Secp256r1)).ToArray()
: Default.StandbyCommittee,
ValidatorsCount = section.GetValue("ValidatorsCount", Default.ValidatorsCount),
SeedList = section.GetSection("SeedList").Exists()
? section.GetSection("SeedList").GetChildren().Select(p => p.Get<string>()).ToArray()
: Default.SeedList,
MillisecondsPerBlock = section.GetValue("MillisecondsPerBlock", Default.MillisecondsPerBlock),
MaxTransactionsPerBlock = section.GetValue("MaxTransactionsPerBlock", Default.MaxTransactionsPerBlock),
MemoryPoolMaxTransactions = section.GetValue("MemoryPoolMaxTransactions", Default.MemoryPoolMaxTransactions),
MaxTraceableBlocks = section.GetValue("MaxTraceableBlocks", Default.MaxTraceableBlocks),
InitialGasDistribution = section.GetValue("InitialGasDistribution", Default.InitialGasDistribution),
Hardforks = section.GetSection("Hardforks").Exists()
? section.GetSection("Hardforks").GetChildren().ToImmutableDictionary(p => Enum.Parse<Hardfork>(p.Key), p => uint.Parse(p.Value))
: Default.Hardforks
};
}
private static void CheckingHardfork(ProtocolSettings settings)
{
var allHardforks = Enum.GetValues(typeof(Hardfork)).Cast<Hardfork>().ToList();
// Check for continuity in configured hardforks
var sortedHardforks = settings.Hardforks.Keys
.OrderBy(h => allHardforks.IndexOf(h))
.ToList();
for (int i = 0; i < sortedHardforks.Count - 1; i++)
{
int currentIndex = allHardforks.IndexOf(sortedHardforks[i]);
int nextIndex = allHardforks.IndexOf(sortedHardforks[i + 1]);
// If they aren't consecutive, return false.
if (nextIndex - currentIndex > 1)
throw new Exception("Hardfork configuration is not continuous.");
}
// Check that block numbers are not higher in earlier hardforks than in later ones
for (int i = 0; i < sortedHardforks.Count - 1; i++)
{
if (settings.Hardforks[sortedHardforks[i]] > settings.Hardforks[sortedHardforks[i + 1]])
{
// This means the block number for the current hardfork is greater than the next one, which should not be allowed.
throw new Exception($"The Hardfork configuration for {sortedHardforks[i]} is greater than for {sortedHardforks[i + 1]}");
}
}
}
}
}