-
Notifications
You must be signed in to change notification settings - Fork 100
/
RpcServer.Blockchain.cs
335 lines (306 loc) · 13.1 KB
/
RpcServer.Blockchain.cs
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (C) 2015-2023 The Neo Project.
//
// The Neo.Network.RPC is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project 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 Neo.IO;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Neo.Plugins
{
partial class RpcServer
{
[RpcMethod]
protected virtual JToken GetBestBlockHash(JArray _params)
{
return NativeContract.Ledger.CurrentHash(system.StoreView).ToString();
}
[RpcMethod]
protected virtual JToken GetBlock(JArray _params)
{
JToken key = _params[0];
bool verbose = _params.Count >= 2 && _params[1].AsBoolean();
using var snapshot = system.GetSnapshot();
Block block;
if (key is JNumber)
{
uint index = uint.Parse(key.AsString());
block = NativeContract.Ledger.GetBlock(snapshot, index);
}
else
{
UInt256 hash = UInt256.Parse(key.AsString());
block = NativeContract.Ledger.GetBlock(snapshot, hash);
}
if (block == null)
throw new RpcException(-100, "Unknown block");
if (verbose)
{
JObject json = Utility.BlockToJson(block, system.Settings);
json["confirmations"] = NativeContract.Ledger.CurrentIndex(snapshot) - block.Index + 1;
UInt256 hash = NativeContract.Ledger.GetBlockHash(snapshot, block.Index + 1);
if (hash != null)
json["nextblockhash"] = hash.ToString();
return json;
}
return Convert.ToBase64String(block.ToArray());
}
[RpcMethod]
protected virtual JToken GetBlockHeaderCount(JArray _params)
{
return (system.HeaderCache.Last?.Index ?? NativeContract.Ledger.CurrentIndex(system.StoreView)) + 1;
}
[RpcMethod]
protected virtual JToken GetBlockCount(JArray _params)
{
return NativeContract.Ledger.CurrentIndex(system.StoreView) + 1;
}
[RpcMethod]
protected virtual JToken GetBlockHash(JArray _params)
{
uint height = uint.Parse(_params[0].AsString());
var snapshot = system.StoreView;
if (height <= NativeContract.Ledger.CurrentIndex(snapshot))
{
return NativeContract.Ledger.GetBlockHash(snapshot, height).ToString();
}
throw new RpcException(-100, "Invalid Height");
}
[RpcMethod]
protected virtual JToken GetBlockHeader(JArray _params)
{
JToken key = _params[0];
bool verbose = _params.Count >= 2 && _params[1].AsBoolean();
var snapshot = system.StoreView;
Header header;
if (key is JNumber)
{
uint height = uint.Parse(key.AsString());
header = NativeContract.Ledger.GetHeader(snapshot, height);
}
else
{
UInt256 hash = UInt256.Parse(key.AsString());
header = NativeContract.Ledger.GetHeader(snapshot, hash);
}
if (header == null)
throw new RpcException(-100, "Unknown block");
if (verbose)
{
JObject json = header.ToJson(system.Settings);
json["confirmations"] = NativeContract.Ledger.CurrentIndex(snapshot) - header.Index + 1;
UInt256 hash = NativeContract.Ledger.GetBlockHash(snapshot, header.Index + 1);
if (hash != null)
json["nextblockhash"] = hash.ToString();
return json;
}
return Convert.ToBase64String(header.ToArray());
}
[RpcMethod]
protected virtual JToken GetContractState(JArray _params)
{
if (int.TryParse(_params[0].AsString(), out int contractId))
{
var contracts = NativeContract.ContractManagement.GetContractById(system.StoreView, contractId);
return contracts?.ToJson() ?? throw new RpcException(-100, "Unknown contract");
}
else
{
UInt160 script_hash = ToScriptHash(_params[0].AsString());
ContractState contract = NativeContract.ContractManagement.GetContract(system.StoreView, script_hash);
return contract?.ToJson() ?? throw new RpcException(-100, "Unknown contract");
}
}
private static UInt160 ToScriptHash(string keyword)
{
foreach (var native in NativeContract.Contracts)
{
if (keyword.Equals(native.Name, StringComparison.InvariantCultureIgnoreCase) || keyword == native.Id.ToString())
return native.Hash;
}
return UInt160.Parse(keyword);
}
[RpcMethod]
protected virtual JToken GetRawMemPool(JArray _params)
{
bool shouldGetUnverified = _params.Count >= 1 && _params[0].AsBoolean();
if (!shouldGetUnverified)
return new JArray(system.MemPool.GetVerifiedTransactions().Select(p => (JToken)p.Hash.ToString()));
JObject json = new();
json["height"] = NativeContract.Ledger.CurrentIndex(system.StoreView);
system.MemPool.GetVerifiedAndUnverifiedTransactions(
out IEnumerable<Transaction> verifiedTransactions,
out IEnumerable<Transaction> unverifiedTransactions);
json["verified"] = new JArray(verifiedTransactions.Select(p => (JToken)p.Hash.ToString()));
json["unverified"] = new JArray(unverifiedTransactions.Select(p => (JToken)p.Hash.ToString()));
return json;
}
[RpcMethod]
protected virtual JToken GetRawTransaction(JArray _params)
{
UInt256 hash = UInt256.Parse(_params[0].AsString());
bool verbose = _params.Count >= 2 && _params[1].AsBoolean();
if (system.MemPool.TryGetValue(hash, out Transaction tx) && !verbose)
return Convert.ToBase64String(tx.ToArray());
var snapshot = system.StoreView;
TransactionState state = NativeContract.Ledger.GetTransactionState(snapshot, hash);
tx ??= state?.Transaction;
if (tx is null) throw new RpcException(-100, "Unknown transaction");
if (!verbose) return Convert.ToBase64String(tx.ToArray());
JObject json = Utility.TransactionToJson(tx, system.Settings);
if (state is not null)
{
TrimmedBlock block = NativeContract.Ledger.GetTrimmedBlock(snapshot, NativeContract.Ledger.GetBlockHash(snapshot, state.BlockIndex));
json["blockhash"] = block.Hash.ToString();
json["confirmations"] = NativeContract.Ledger.CurrentIndex(snapshot) - block.Index + 1;
json["blocktime"] = block.Header.Timestamp;
}
return json;
}
[RpcMethod]
protected virtual JToken GetStorage(JArray _params)
{
using var snapshot = system.GetSnapshot();
if (!int.TryParse(_params[0].AsString(), out int id))
{
UInt160 hash = UInt160.Parse(_params[0].AsString());
ContractState contract = NativeContract.ContractManagement.GetContract(snapshot, hash);
if (contract is null) throw new RpcException(-100, "Unknown contract");
id = contract.Id;
}
byte[] key = Convert.FromBase64String(_params[1].AsString());
StorageItem item = snapshot.TryGet(new StorageKey
{
Id = id,
Key = key
});
if (item is null) throw new RpcException(-100, "Unknown storage");
return Convert.ToBase64String(item.Value.Span);
}
[RpcMethod]
protected virtual JToken FindStorage(JArray _params)
{
using var snapshot = system.GetSnapshot();
if (!int.TryParse(_params[0].AsString(), out int id))
{
UInt160 hash = UInt160.Parse(_params[0].AsString());
ContractState contract = NativeContract.ContractManagement.GetContract(snapshot, hash);
if (contract is null) throw new RpcException(-100, "Unknown contract");
id = contract.Id;
}
byte[] prefix = Convert.FromBase64String(_params[1].AsString());
byte[] prefix_key = StorageKey.CreateSearchPrefix(id, prefix);
if (!int.TryParse(_params[2].AsString(), out int start))
{
start = 0;
}
JObject json = new();
JArray jarr = new();
int pageSize = settings.FindStoragePageSize;
int i = 0;
using (var iter = snapshot.Find(prefix_key).Skip(count: start).GetEnumerator())
{
var hasMore = false;
while (iter.MoveNext())
{
if (i == pageSize)
{
hasMore = true;
break;
}
JObject j = new();
j["key"] = Convert.ToBase64String(iter.Current.Key.Key.Span);
j["value"] = Convert.ToBase64String(iter.Current.Value.Value.Span);
jarr.Add(j);
i++;
}
json["truncated"] = hasMore;
}
json["next"] = start + i;
json["results"] = jarr;
return json;
}
[RpcMethod]
protected virtual JToken GetTransactionHeight(JArray _params)
{
UInt256 hash = UInt256.Parse(_params[0].AsString());
uint? height = NativeContract.Ledger.GetTransactionState(system.StoreView, hash)?.BlockIndex;
if (height.HasValue) return height.Value;
throw new RpcException(-100, "Unknown transaction");
}
[RpcMethod]
protected virtual JToken GetNextBlockValidators(JArray _params)
{
using var snapshot = system.GetSnapshot();
var validators = NativeContract.NEO.GetNextBlockValidators(snapshot, system.Settings.ValidatorsCount);
return validators.Select(p =>
{
JObject validator = new();
validator["publickey"] = p.ToString();
validator["votes"] = (int)NativeContract.NEO.GetCandidateVote(snapshot, p);
return validator;
}).ToArray();
}
[RpcMethod]
protected virtual JToken GetCandidates(JArray _params)
{
using var snapshot = system.GetSnapshot();
byte[] script;
using (ScriptBuilder sb = new())
{
script = sb.EmitDynamicCall(NativeContract.NEO.Hash, "getCandidates", null).ToArray();
}
using ApplicationEngine engine = ApplicationEngine.Run(script, snapshot, settings: system.Settings, gas: settings.MaxGasInvoke);
JObject json = new();
try
{
var resultstack = engine.ResultStack.ToArray();
if (resultstack.Length > 0)
{
JArray jArray = new();
var validators = NativeContract.NEO.GetNextBlockValidators(snapshot, system.Settings.ValidatorsCount);
foreach (var item in resultstack)
{
var value = (VM.Types.Array)item;
foreach (Struct ele in value)
{
var publickey = ele[0].GetSpan().ToHexString();
json["publickey"] = publickey;
json["votes"] = ele[1].GetInteger().ToString();
json["active"] = validators.ToByteArray().ToHexString().Contains(publickey);
jArray.Add(json);
json = new();
}
return jArray;
}
}
}
catch (InvalidOperationException)
{
json["exception"] = "Invalid result.";
}
return json;
}
[RpcMethod]
protected virtual JToken GetCommittee(JArray _params)
{
return new JArray(NativeContract.NEO.GetCommittee(system.StoreView).Select(p => (JToken)p.ToString()));
}
[RpcMethod]
protected virtual JToken GetNativeContracts(JArray _params)
{
return new JArray(NativeContract.Contracts.Select(p => p.NativeContractToJson(system.Settings)));
}
}
}