-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathProgram.cs
More file actions
276 lines (247 loc) · 10.5 KB
/
Program.cs
File metadata and controls
276 lines (247 loc) · 10.5 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nethereum.DevChain;
using Nethereum.DevChain.Accounts;
using Nethereum.DevChain.Configuration;
using Nethereum.DevChain.Hosting;
if (args.Any(a => a == "--help" || a == "-h" || a == "-?"))
{
PrintHelp();
return;
}
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("appsettings.json", optional: true);
builder.Configuration.AddCommandLine(args);
var config = new DevChainServerConfig();
builder.Configuration.GetSection("DevChain").Bind(config);
ApplyCommandLineOverrides(config, args);
builder.AddDevChainServer(config);
builder.WebHost.ConfigureKestrel(options =>
options.Limits.MaxRequestBodySize = 10 * 1024 * 1024);
builder.Logging.SetMinimumLevel(LogLevel.Warning);
builder.Logging.AddFilter("Nethereum", config.Verbose ? LogLevel.Debug : LogLevel.Information);
var app = builder.Build();
var node = app.Services.GetRequiredService<DevChainNode>();
var accountManager = app.Services.GetRequiredService<DevAccountManager>();
await node.StartAsync(accountManager.Accounts.Select(a => a.Address));
PrintBanner(config, accountManager, app);
await app.MapDevChainEndpointsAsync();
app.Run($"http://{config.Host}:{config.Port}");
// ──────────────────────────────────────────────────────────────────
// CLI
// ──────────────────────────────────────────────────────────────────
void PrintHelp()
{
var version = typeof(Program).Assembly.GetName().Version;
Console.WriteLine($"Nethereum DevChain Server v{version?.Major}.{version?.Minor}.{version?.Build}");
Console.WriteLine("Local Ethereum development node with JSON-RPC server");
Console.WriteLine();
Console.WriteLine("USAGE: nethereum-devchain [OPTIONS]");
Console.WriteLine();
Console.WriteLine("SERVER:");
Console.WriteLine(" -p, --port <PORT> Port to listen on (default: 8545)");
Console.WriteLine(" --host <HOST> Host to bind to (default: 127.0.0.1)");
Console.WriteLine(" -v, --verbose Enable verbose RPC logging");
Console.WriteLine();
Console.WriteLine("ACCOUNTS:");
Console.WriteLine(" -a, --accounts <NUM> Number of accounts to generate (default: 10)");
Console.WriteLine(" -m, --mnemonic <MNEMONIC> HD wallet mnemonic phrase");
Console.WriteLine(" -e, --balance <ETH> Account balance in ETH (default: 10000)");
Console.WriteLine();
Console.WriteLine("CHAIN:");
Console.WriteLine(" -c, --chain-id <ID> Chain ID (default: 31337)");
Console.WriteLine(" -b, --block-time <MS> Block time in ms, 0 = auto-mine (default: 0)");
Console.WriteLine(" --gas-limit <GAS> Block gas limit (default: 30000000)");
Console.WriteLine();
Console.WriteLine("FORK:");
Console.WriteLine(" -f, --fork <URL> Fork from a remote RPC endpoint");
Console.WriteLine(" --fork-block <NUMBER> Fork at a specific block number");
Console.WriteLine();
Console.WriteLine("STORAGE:");
Console.WriteLine(" --persist [DIR] Persist chain data to disk (default: ./chaindata)");
Console.WriteLine(" --in-memory Use in-memory storage instead of SQLite");
Console.WriteLine();
Console.WriteLine(" Default storage is SQLite with auto-cleanup on exit.");
Console.WriteLine(" Use --persist to keep data between restarts.");
Console.WriteLine();
Console.WriteLine("EXAMPLES:");
Console.WriteLine(" nethereum-devchain");
Console.WriteLine(" nethereum-devchain -p 8546 -a 20 -e 100");
Console.WriteLine(" nethereum-devchain -f https://eth.llamarpc.com --fork-block 19000000");
Console.WriteLine(" nethereum-devchain --persist ./mychain");
Console.WriteLine(" nethereum-devchain -b 1000 -c 1234");
}
void ApplyCommandLineOverrides(DevChainServerConfig config, string[] args)
{
for (int i = 0; i < args.Length; i++)
{
var arg = args[i].ToLowerInvariant();
// Server
if ((arg == "--port" || arg == "-p") && i + 1 < args.Length)
{
if (int.TryParse(args[++i], out var port))
config.Port = port;
}
else if (arg == "--host" && i + 1 < args.Length)
{
config.Host = args[++i];
}
else if (arg == "--verbose" || arg == "-v")
{
config.Verbose = true;
}
// Accounts
else if ((arg == "--accounts" || arg == "-a") && i + 1 < args.Length)
{
if (int.TryParse(args[++i], out var count))
config.AccountCount = count;
}
else if ((arg == "--mnemonic" || arg == "-m") && i + 1 < args.Length)
{
config.Mnemonic = args[++i];
}
else if ((arg == "--balance" || arg == "-e") && i + 1 < args.Length)
{
config.SetAccountBalanceEth(args[++i]);
}
// Chain
else if ((arg == "--chain-id" || arg == "-c") && i + 1 < args.Length)
{
if (int.TryParse(args[++i], out var chainId))
config.ChainId = chainId;
}
else if ((arg == "--block-time" || arg == "-b") && i + 1 < args.Length)
{
if (long.TryParse(args[++i], out var blockTime))
{
config.BlockTime = blockTime;
config.AutoMine = blockTime == 0;
}
}
else if (arg == "--gas-limit" && i + 1 < args.Length)
{
if (long.TryParse(args[++i], out var gasLimit))
config.BlockGasLimit = gasLimit;
}
// Fork
else if ((arg == "--fork" || arg == "-f") && i + 1 < args.Length)
{
config.Fork ??= new ForkConfig();
config.Fork.Url = args[++i];
}
else if (arg == "--fork-block" && i + 1 < args.Length)
{
config.Fork ??= new ForkConfig();
if (long.TryParse(args[++i], out var block))
config.Fork.BlockNumber = block;
}
// Storage
else if (arg == "--persist")
{
config.Persist = true;
config.Storage = "sqlite";
if (i + 1 < args.Length && !args[i + 1].StartsWith("-"))
{
config.DataDir = args[++i];
}
}
else if (arg == "--in-memory")
{
config.Storage = "memory";
}
// Legacy aliases (kept for backwards compat, hidden from help)
else if (arg == "--storage" && i + 1 < args.Length)
{
config.Storage = args[++i];
}
else if (arg == "--data-dir" && i + 1 < args.Length)
{
config.DataDir = args[++i];
}
else if (arg == "--batch-size" && i + 1 < args.Length)
{
if (int.TryParse(args[++i], out var batchSize))
config.AutoMineBatchSize = batchSize;
}
else if (arg == "--batch-timeout" && i + 1 < args.Length)
{
if (int.TryParse(args[++i], out var timeoutMs))
config.AutoMineBatchTimeoutMs = timeoutMs;
}
else if (arg == "--max-tx-per-block" && i + 1 < args.Length)
{
if (int.TryParse(args[++i], out var maxTx))
config.MaxTransactionsPerBlock = maxTx;
}
}
}
void PrintBanner(DevChainServerConfig config, DevAccountManager accounts, WebApplication app)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(@" _ _ _ _");
Console.WriteLine(@"| \ | | ___| |_| |__ ___ _ __ ___ _ _ _ __ ___");
Console.WriteLine(@"| \| |/ _ \ __| '_ \ / _ \ '__/ _ \ | | | '_ ` _ \");
Console.WriteLine(@"| |\ | __/ |_| | | | __/ | | __/ |_| | | | | | |");
Console.WriteLine(@"|_| \_|\___|\__|_| |_|\___|_| \___|\__,_|_| |_| |_|");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
var version = typeof(Program).Assembly.GetName().Version;
Console.WriteLine($" DevChain Server v{version?.Major}.{version?.Minor}.{version?.Build}");
Console.ResetColor();
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($" RPC: http://{config.Host}:{config.Port}");
Console.ResetColor();
Console.WriteLine($" Chain ID: {config.ChainId}");
Console.WriteLine($" Gas Limit: {config.BlockGasLimit:N0}");
if (config.BlockTime > 0)
Console.WriteLine($" Block Time: {config.BlockTime}ms (interval mining)");
else
Console.WriteLine($" Mining: auto-mine (instant)");
var storageMode = config.Storage?.ToLowerInvariant() ?? "sqlite";
if (storageMode == "memory")
{
Console.WriteLine($" Storage: in-memory");
}
else
{
var sqliteMgr = app.Services.GetService<Nethereum.DevChain.Storage.Sqlite.SqliteStorageManager>();
if (config.Persist)
Console.WriteLine($" Storage: SQLite (persist: {sqliteMgr?.DbPath ?? config.DataDir})");
else
Console.WriteLine($" Storage: SQLite (auto-cleanup on exit)");
}
if (config.Fork?.Url != null)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write($" Fork: {config.Fork.Url}");
if (config.Fork.BlockNumber.HasValue)
Console.Write($" @ block {config.Fork.BlockNumber}");
Console.WriteLine();
Console.ResetColor();
}
Console.WriteLine();
Console.WriteLine(" Available Accounts");
Console.WriteLine(" ==================");
foreach (var account in accounts.Accounts)
{
var balanceEth = account.Balance / System.Numerics.BigInteger.Parse("1000000000000000000");
Console.WriteLine($" ({account.Index}) {account.Address} ({balanceEth} ETH)");
}
Console.WriteLine();
Console.WriteLine(" Private Keys");
Console.WriteLine(" ============");
foreach (var account in accounts.Accounts)
{
Console.WriteLine($" ({account.Index}) {account.GetPrivateKeyHex()}");
}
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine(" HD Wallet");
Console.WriteLine(" =========");
Console.WriteLine($" Mnemonic: {config.Mnemonic}");
Console.WriteLine(" Derivation Path: m/44'/60'/0'/0/x");
Console.ResetColor();
Console.WriteLine();
}