Skip to content

Commit 76e933e

Browse files
authored
Expose counter for getnativecontracts (#900)
* RpcServer: unify getnativecontracts and getcontractstate, fix #852 Reuse the same model and have updatecounter in the output. All the other fields were the same already, so old RpcClient can get results without any problems. Signed-off-by: Roman Khimov <[email protected]> * RpcClient: use ContractState for GetNativeContractsAsync Now it's exactly the same server-side, so can be reused. Notice that UpdateCounter trick allows to parse responses from old servers (that are lacking "updatecounter" for this call), this simplifies upgrade, but can be removed in future versions. Signed-off-by: Roman Khimov <[email protected]> --------- Signed-off-by: Roman Khimov <[email protected]>
1 parent 66573ed commit 76e933e

File tree

5 files changed

+12
-65
lines changed

5 files changed

+12
-65
lines changed

src/RpcClient/Models/RpcNativeContract.cs

-47
This file was deleted.

src/RpcClient/RpcClient.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public static ContractState ContractStateFromJson(JObject json)
260260
return new ContractState
261261
{
262262
Id = (int)json["id"].AsNumber(),
263-
UpdateCounter = (ushort)json["updatecounter"].AsNumber(),
263+
UpdateCounter = (ushort)(json["updatecounter"]?.AsNumber() ?? 0),
264264
Hash = UInt160.Parse(json["hash"].AsString()),
265265
Nef = RpcNefFile.FromJson((JObject)json["nef"]),
266266
Manifest = ContractManifest.FromJson((JObject)json["manifest"])
@@ -270,10 +270,10 @@ public static ContractState ContractStateFromJson(JObject json)
270270
/// <summary>
271271
/// Get all native contracts.
272272
/// </summary>
273-
public async Task<RpcNativeContract[]> GetNativeContractsAsync()
273+
public async Task<ContractState[]> GetNativeContractsAsync()
274274
{
275275
var result = await RpcSendAsync(GetRpcName()).ConfigureAwait(false);
276-
return ((JArray)result).Select(p => RpcNativeContract.FromJson((JObject)p)).ToArray();
276+
return ((JArray)result).Select(p => ContractStateFromJson((JObject)p)).ToArray();
277277
}
278278

279279
/// <summary>

src/RpcServer/RpcServer.Blockchain.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ protected virtual JToken GetCommittee(JArray _params)
333333
[RpcMethod]
334334
protected virtual JToken GetNativeContracts(JArray _params)
335335
{
336-
return new JArray(NativeContract.Contracts.Select(p => p.NativeContractToJson(system)));
336+
return new JArray(NativeContract.Contracts.Select(p => NativeContract.ContractManagement.GetContract(system.StoreView, p.Hash).ToJson()));
337337
}
338338
}
339339
}

src/RpcServer/Utility.cs

-14
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,5 @@ public static JObject TransactionToJson(Transaction tx, ProtocolSettings setting
3232
json["netfee"] = tx.NetworkFee.ToString();
3333
return json;
3434
}
35-
36-
public static JObject NativeContractToJson(this NativeContract contract, NeoSystem system)
37-
{
38-
var state = contract.GetContractState(system.Settings,
39-
NativeContract.Ledger.CurrentIndex(system.StoreView));
40-
41-
return new JObject
42-
{
43-
["id"] = contract.Id,
44-
["hash"] = contract.Hash.ToString(),
45-
["nef"] = state.Nef.ToJson(),
46-
["manifest"] = state.Manifest.ToJson()
47-
};
48-
}
4935
}
5036
}

tests/Neo.Network.RPC.Tests/RpcTestCases.json

+8
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,7 @@
10371037
"result": [
10381038
{
10391039
"id": -1,
1040+
"updatecounter": 0,
10401041
"hash": "0xa501d7d7d10983673b61b7a2d3a813b36f9f0e43",
10411042
"nef": {
10421043
"magic": 860243278,
@@ -1206,6 +1207,7 @@
12061207
},
12071208
{
12081209
"id": -2,
1210+
"updatecounter": 1,
12091211
"hash": "0x971d69c6dd10ce88e7dfffec1dc603c6125a8764",
12101212
"nef": {
12111213
"magic": 860243278,
@@ -1303,6 +1305,7 @@
13031305
},
13041306
{
13051307
"id": -3,
1308+
"updatecounter": 0,
13061309
"hash": "0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5",
13071310
"nef": {
13081311
"magic": 860243278,
@@ -1507,6 +1510,7 @@
15071510
},
15081511
{
15091512
"id": -4,
1513+
"updatecounter": 0,
15101514
"hash": "0xd2a4cff31913016155e38e474a2c06d08be276cf",
15111515
"nef": {
15121516
"magic": 860243278,
@@ -1615,6 +1619,7 @@
16151619
},
16161620
{
16171621
"id": -5,
1622+
"updatecounter": 0,
16181623
"hash": "0x79bcd398505eb779df6e67e4be6c14cded08e2f2",
16191624
"nef": {
16201625
"magic": 860243278,
@@ -1796,6 +1801,7 @@
17961801
},
17971802
{
17981803
"id": -6,
1804+
"updatecounter": 0,
17991805
"hash": "0x597b1471bbce497b7809e2c8f10db67050008b02",
18001806
"nef": {
18011807
"magic": 860243278,
@@ -1859,6 +1865,7 @@
18591865
},
18601866
{
18611867
"id": -7,
1868+
"updatecounter": 0,
18621869
"hash": "0x8dc0e742cbdfdeda51ff8a8b78d46829144c80ee",
18631870
"nef": {
18641871
"magic": 860243278,
@@ -1967,6 +1974,7 @@
19671974
},
19681975
{
19691976
"id": -8,
1977+
"updatecounter": 0,
19701978
"hash": "0xa2b524b68dfe43a9d56af84f443c6b9843b8028c",
19711979
"nef": {
19721980
"magic": 860243278,

0 commit comments

Comments
 (0)