This repository was archived by the owner on Nov 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathRpcVersion.cs
More file actions
113 lines (102 loc) · 4.63 KB
/
RpcVersion.cs
File metadata and controls
113 lines (102 loc) · 4.63 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
// Copyright (C) 2015-2024 The Neo Project.
//
// RpcVersion.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 Neo.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Neo.Network.RPC.Models
{
public class RpcVersion
{
public class RpcProtocol
{
public uint Network { get; set; }
public int ValidatorsCount { get; set; }
public uint MillisecondsPerBlock { get; set; }
public uint MaxValidUntilBlockIncrement { get; set; }
public uint MaxTraceableBlocks { get; set; }
public byte AddressVersion { get; set; }
public uint MaxTransactionsPerBlock { get; set; }
public int MemoryPoolMaxTransactions { get; set; }
public ulong InitialGasDistribution { get; set; }
public IReadOnlyDictionary<Hardfork, uint> Hardforks { get; set; }
public JObject ToJson()
{
JObject json = new();
json["network"] = Network;
json["validatorscount"] = ValidatorsCount;
json["msperblock"] = MillisecondsPerBlock;
json["maxvaliduntilblockincrement"] = MaxValidUntilBlockIncrement;
json["maxtraceableblocks"] = MaxTraceableBlocks;
json["addressversion"] = AddressVersion;
json["maxtransactionsperblock"] = MaxTransactionsPerBlock;
json["memorypoolmaxtransactions"] = MemoryPoolMaxTransactions;
json["initialgasdistribution"] = InitialGasDistribution;
json["hardforks"] = new JArray(Hardforks.Select(s => new JObject()
{
// Strip HF_ prefix.
["name"] = StripPrefix(s.Key.ToString(), "HF_"),
["blockheight"] = s.Value,
}));
return json;
}
public static RpcProtocol FromJson(JObject json)
{
return new()
{
Network = (uint)json["network"].AsNumber(),
ValidatorsCount = (int)json["validatorscount"].AsNumber(),
MillisecondsPerBlock = (uint)json["msperblock"].AsNumber(),
MaxValidUntilBlockIncrement = (uint)json["maxvaliduntilblockincrement"].AsNumber(),
MaxTraceableBlocks = (uint)json["maxtraceableblocks"].AsNumber(),
AddressVersion = (byte)json["addressversion"].AsNumber(),
MaxTransactionsPerBlock = (uint)json["maxtransactionsperblock"].AsNumber(),
MemoryPoolMaxTransactions = (int)json["memorypoolmaxtransactions"].AsNumber(),
InitialGasDistribution = (ulong)json["initialgasdistribution"].AsNumber(),
Hardforks = new Dictionary<Hardfork, uint>(((JArray)json["hardforks"]).Select(s =>
{
var name = s["name"].AsString();
// Add HF_ prefix to the hardfork response for proper Hardfork enum parsing.
return new KeyValuePair<Hardfork, uint>(Enum.Parse<Hardfork>(name.StartsWith("HF_") ? name : $"HF_{name}"), (uint)s["blockheight"].AsNumber());
})),
};
}
private static string StripPrefix(string s, string prefix)
{
return s.StartsWith(prefix) ? s.Substring(prefix.Length) : s;
}
}
public int TcpPort { get; set; }
public uint Nonce { get; set; }
public string UserAgent { get; set; }
public RpcProtocol Protocol { get; set; } = new();
public JObject ToJson()
{
JObject json = new();
json["network"] = Protocol.Network; // Obsolete
json["tcpport"] = TcpPort;
json["nonce"] = Nonce;
json["useragent"] = UserAgent;
json["protocol"] = Protocol.ToJson();
return json;
}
public static RpcVersion FromJson(JObject json)
{
return new()
{
TcpPort = (int)json["tcpport"].AsNumber(),
Nonce = (uint)json["nonce"].AsNumber(),
UserAgent = json["useragent"].AsString(),
Protocol = RpcProtocol.FromJson((JObject)json["protocol"])
};
}
}
}