|
56 | 56 | return; |
57 | 57 | } |
58 | 58 |
|
59 | | - var jsonOptions = new JsonSerializerOptions |
60 | | - { |
61 | | - PropertyNameCaseInsensitive = true |
62 | | - }; |
| 59 | + httpContext.Response.ContentType = "application/json"; |
63 | 60 |
|
64 | 61 | if (json.TrimStart().StartsWith('[')) |
65 | 62 | { |
66 | | - var requests = JsonSerializer.Deserialize<RpcRequestMessage[]>(json, jsonOptions); |
| 63 | + var requests = JsonSerializer.Deserialize(json, CoreChainJsonContext.Default.JsonRpcRequestArray); |
67 | 64 | if (requests != null) |
68 | 65 | { |
69 | | - var responses = await dispatcher.DispatchBatchAsync(requests); |
70 | | - httpContext.Response.ContentType = "application/json"; |
71 | | - await httpContext.Response.WriteAsync(JsonSerializer.Serialize(responses)); |
| 66 | + var rpcRequests = requests.Select(ToRpcRequestMessage).ToArray(); |
| 67 | + var responses = await dispatcher.DispatchBatchAsync(rpcRequests); |
| 68 | + var jsonResponses = responses.Select(ToJsonRpcResponse).ToArray(); |
| 69 | + await httpContext.Response.WriteAsync(JsonSerializer.Serialize(jsonResponses, CoreChainJsonContext.Default.JsonRpcResponseArray)); |
72 | 70 | return; |
73 | 71 | } |
74 | 72 | } |
75 | 73 |
|
76 | | - var request = JsonSerializer.Deserialize<RpcRequestMessage>(json, jsonOptions); |
| 74 | + var request = JsonSerializer.Deserialize(json, CoreChainJsonContext.Default.JsonRpcRequest); |
77 | 75 | if (request == null) |
78 | 76 | { |
79 | 77 | httpContext.Response.StatusCode = 400; |
80 | | - await httpContext.Response.WriteAsJsonAsync(new { error = "Invalid JSON-RPC request" }); |
| 78 | + await httpContext.Response.WriteAsync("{\"error\":\"Invalid JSON-RPC request\"}"); |
81 | 79 | return; |
82 | 80 | } |
83 | 81 |
|
84 | | - var response = await dispatcher.DispatchAsync(request); |
85 | | - httpContext.Response.ContentType = "application/json"; |
86 | | - await httpContext.Response.WriteAsync(JsonSerializer.Serialize(response)); |
| 82 | + var rpcRequest = ToRpcRequestMessage(request); |
| 83 | + var response = await dispatcher.DispatchAsync(rpcRequest); |
| 84 | + var jsonResponse = ToJsonRpcResponse(response); |
| 85 | + await httpContext.Response.WriteAsync(JsonSerializer.Serialize(jsonResponse, CoreChainJsonContext.Default.JsonRpcResponse)); |
87 | 86 | } |
88 | 87 | catch (JsonException ex) |
89 | 88 | { |
90 | 89 | logger.LogError(ex, "JSON parsing error"); |
91 | 90 | httpContext.Response.StatusCode = 400; |
92 | | - await httpContext.Response.WriteAsJsonAsync(new RpcResponseMessage(null, new RpcError |
| 91 | + httpContext.Response.ContentType = "application/json"; |
| 92 | + var errorResponse = new JsonRpcResponse |
93 | 93 | { |
94 | | - Code = -32700, |
95 | | - Message = "Parse error: " + ex.Message |
96 | | - })); |
| 94 | + Id = null, |
| 95 | + Error = new JsonRpcError { Code = -32700, Message = "Parse error: " + ex.Message } |
| 96 | + }; |
| 97 | + await httpContext.Response.WriteAsync(JsonSerializer.Serialize(errorResponse, CoreChainJsonContext.Default.JsonRpcResponse)); |
97 | 98 | } |
98 | 99 | catch (Exception ex) |
99 | 100 | { |
100 | 101 | logger.LogError(ex, "Unexpected error"); |
101 | 102 | httpContext.Response.StatusCode = 500; |
102 | | - await httpContext.Response.WriteAsJsonAsync(new RpcResponseMessage(null, new RpcError |
| 103 | + httpContext.Response.ContentType = "application/json"; |
| 104 | + var errorResponse = new JsonRpcResponse |
103 | 105 | { |
104 | | - Code = -32603, |
105 | | - Message = "Internal error: " + ex.Message |
106 | | - })); |
| 106 | + Id = null, |
| 107 | + Error = new JsonRpcError { Code = -32603, Message = "Internal error: " + ex.Message } |
| 108 | + }; |
| 109 | + await httpContext.Response.WriteAsync(JsonSerializer.Serialize(errorResponse, CoreChainJsonContext.Default.JsonRpcResponse)); |
107 | 110 | } |
108 | 111 | }); |
109 | 112 |
|
@@ -133,6 +136,20 @@ void PrintBanner(DevChainServerConfig config, DevAccountManager accounts) |
133 | 136 | Console.ResetColor(); |
134 | 137 | Console.WriteLine($"Chain ID: {config.ChainId}"); |
135 | 138 |
|
| 139 | + if (config.Storage?.ToLowerInvariant() == "rocksdb") |
| 140 | + { |
| 141 | + Console.ForegroundColor = ConsoleColor.Blue; |
| 142 | + Console.WriteLine($"Storage: RocksDB ({config.DataDir})"); |
| 143 | + Console.ResetColor(); |
| 144 | + } |
| 145 | + |
| 146 | + if (config.AutoMineBatchSize > 1) |
| 147 | + { |
| 148 | + Console.ForegroundColor = ConsoleColor.Magenta; |
| 149 | + Console.WriteLine($"Batch Mining: {config.AutoMineBatchSize} txs / {config.AutoMineBatchTimeoutMs}ms timeout"); |
| 150 | + Console.ResetColor(); |
| 151 | + } |
| 152 | + |
136 | 153 | if (config.Fork?.Url != null) |
137 | 154 | { |
138 | 155 | Console.WriteLine($"Forking from: {config.Fork.Url}"); |
@@ -213,5 +230,57 @@ void ApplyCommandLineOverrides(DevChainServerConfig config, string[] args) |
213 | 230 | { |
214 | 231 | config.Verbose = true; |
215 | 232 | } |
| 233 | + else if (arg == "--batch-size" && i + 1 < args.Length) |
| 234 | + { |
| 235 | + if (int.TryParse(args[++i], out var batchSize)) |
| 236 | + config.AutoMineBatchSize = batchSize; |
| 237 | + } |
| 238 | + else if (arg == "--batch-timeout" && i + 1 < args.Length) |
| 239 | + { |
| 240 | + if (int.TryParse(args[++i], out var timeoutMs)) |
| 241 | + config.AutoMineBatchTimeoutMs = timeoutMs; |
| 242 | + } |
| 243 | + else if (arg == "--storage" && i + 1 < args.Length) |
| 244 | + { |
| 245 | + config.Storage = args[++i]; |
| 246 | + } |
| 247 | + else if (arg == "--data-dir" && i + 1 < args.Length) |
| 248 | + { |
| 249 | + config.DataDir = args[++i]; |
| 250 | + } |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +RpcRequestMessage ToRpcRequestMessage(JsonRpcRequest request) |
| 255 | +{ |
| 256 | + return new RpcRequestMessage |
| 257 | + { |
| 258 | + Id = request.Id, |
| 259 | + Method = request.Method, |
| 260 | + JsonRpcVersion = request.Jsonrpc, |
| 261 | + RawParameters = request.Params.HasValue ? request.Params.Value : null |
| 262 | + }; |
| 263 | +} |
| 264 | + |
| 265 | +JsonRpcResponse ToJsonRpcResponse(RpcResponseMessage response) |
| 266 | +{ |
| 267 | + if (response.HasError) |
| 268 | + { |
| 269 | + return new JsonRpcResponse |
| 270 | + { |
| 271 | + Id = response.Id, |
| 272 | + Error = new JsonRpcError |
| 273 | + { |
| 274 | + Code = response.Error.Code, |
| 275 | + Message = response.Error.Message, |
| 276 | + Data = response.Error.Data |
| 277 | + } |
| 278 | + }; |
216 | 279 | } |
| 280 | + |
| 281 | + return new JsonRpcResponse |
| 282 | + { |
| 283 | + Id = response.Id, |
| 284 | + Result = response.Result |
| 285 | + }; |
217 | 286 | } |
0 commit comments