Skip to content

Commit

Permalink
Fix json properties (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Jun 9, 2021
1 parent 6981fc7 commit 43298b0
Show file tree
Hide file tree
Showing 25 changed files with 93 additions and 105 deletions.
4 changes: 2 additions & 2 deletions src/RpcClient/Models/RpcApplicationLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class Execution

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["trigger"] = Trigger;
json["vmstate"] = VMState;
json["gasconsumed"] = GasConsumed.ToString();
Expand Down Expand Up @@ -83,7 +83,7 @@ public class RpcNotifyEventArgs

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["contract"] = Contract.ToString();
json["eventname"] = EventName;
json["state"] = State.ToJson();
Expand Down
6 changes: 3 additions & 3 deletions src/RpcClient/Models/RpcInvokeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class RpcInvokeResult

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["script"] = Script;
json["state"] = State;
json["gasconsumed"] = GasConsumed.ToString();
Expand All @@ -43,7 +43,7 @@ public JObject ToJson()

public static RpcInvokeResult FromJson(JObject json)
{
RpcInvokeResult invokeScriptResult = new RpcInvokeResult
RpcInvokeResult invokeScriptResult = new()
{
Script = json["script"].AsString(),
State = json["state"].TryGetEnum<VMState>(),
Expand All @@ -69,7 +69,7 @@ public class RpcStack

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["type"] = Type;
json["value"] = Value;
return json;
Expand Down
7 changes: 4 additions & 3 deletions src/RpcClient/Models/RpcNativeContract.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using System.Linq;

namespace Neo.Network.RPC.Models
{
Expand All @@ -10,7 +11,7 @@ public class RpcNativeContract
public UInt160 Hash { get; set; }
public NefFile Nef { get; set; }
public ContractManifest Manifest { get; set; }
public uint ActiveBlockIndex { get; set; }
public uint[] UpdateHistory { get; set; }

public static RpcNativeContract FromJson(JObject json)
{
Expand All @@ -20,7 +21,7 @@ public static RpcNativeContract FromJson(JObject json)
Hash = UInt160.Parse(json["hash"].AsString()),
Nef = RpcNefFile.FromJson(json["nef"]),
Manifest = ContractManifest.FromJson(json["manifest"]),
ActiveBlockIndex = (uint)(json["activeblockindex"]?.AsNumber() ?? 0)
UpdateHistory = json["updatehistory"].GetArray().Select(u => (uint)u.GetInt32()).ToArray()
};
}

Expand All @@ -32,7 +33,7 @@ public JObject ToJson()
["hash"] = Hash.ToString(),
["nef"] = Nef.ToJson(),
["manifest"] = Manifest.ToJson(),
["activeblockindex"] = ActiveBlockIndex
["updatehistory"] = new JArray(UpdateHistory.Select(u => new JNumber(u)).ToArray())
};
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/RpcClient/Models/RpcNep17Balances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public class RpcNep17Balances

public JObject ToJson(ProtocolSettings protocolSettings)
{
JObject json = new JObject();
JObject json = new();
json["balance"] = Balances.Select(p => p.ToJson()).ToArray();
json["address"] = UserScriptHash.ToAddress(protocolSettings.AddressVersion);
return json;
}

public static RpcNep17Balances FromJson(JObject json, ProtocolSettings protocolSettings)
{
RpcNep17Balances nep17Balance = new RpcNep17Balances
RpcNep17Balances nep17Balance = new()
{
Balances = ((JArray)json["balance"]).Select(p => RpcNep17Balance.FromJson(p, protocolSettings)).ToList(),
UserScriptHash = json["address"].ToScriptHash(protocolSettings)
Expand All @@ -41,7 +41,7 @@ public class RpcNep17Balance

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["assethash"] = AssetHash.ToString();
json["amount"] = Amount.ToString();
json["lastupdatedblock"] = LastUpdatedBlock;
Expand All @@ -50,7 +50,7 @@ public JObject ToJson()

public static RpcNep17Balance FromJson(JObject json, ProtocolSettings protocolSettings)
{
RpcNep17Balance balance = new RpcNep17Balance
RpcNep17Balance balance = new()
{
AssetHash = json["assethash"].ToScriptHash(protocolSettings),
Amount = BigInteger.Parse(json["amount"].AsString()),
Expand Down
7 changes: 3 additions & 4 deletions src/RpcClient/Models/RpcNep17Transfers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.Wallets;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -17,7 +16,7 @@ public class RpcNep17Transfers

public JObject ToJson(ProtocolSettings protocolSettings)
{
JObject json = new JObject();
JObject json = new();
json["sent"] = Sent.Select(p => p.ToJson(protocolSettings)).ToArray();
json["received"] = Received.Select(p => p.ToJson(protocolSettings)).ToArray();
json["address"] = UserScriptHash.ToAddress(protocolSettings.AddressVersion);
Expand All @@ -26,7 +25,7 @@ public JObject ToJson(ProtocolSettings protocolSettings)

public static RpcNep17Transfers FromJson(JObject json, ProtocolSettings protocolSettings)
{
RpcNep17Transfers transfers = new RpcNep17Transfers
RpcNep17Transfers transfers = new()
{
Sent = ((JArray)json["sent"]).Select(p => RpcNep17Transfer.FromJson(p, protocolSettings)).ToList(),
Received = ((JArray)json["received"]).Select(p => RpcNep17Transfer.FromJson(p, protocolSettings)).ToList(),
Expand Down Expand Up @@ -54,7 +53,7 @@ public class RpcNep17Transfer

public JObject ToJson(ProtocolSettings protocolSettings)
{
JObject json = new JObject();
JObject json = new();
json["timestamp"] = TimestampMS;
json["assethash"] = AssetHash.ToString();
json["transferaddress"] = UserScriptHash?.ToAddress(protocolSettings.AddressVersion);
Expand Down
4 changes: 2 additions & 2 deletions src/RpcClient/Models/RpcPeers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RpcPeers

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["unconnected"] = new JArray(Unconnected.Select(p => p.ToJson()));
json["bad"] = new JArray(Bad.Select(p => p.ToJson()));
json["connected"] = new JArray(Connected.Select(p => p.ToJson()));
Expand All @@ -39,7 +39,7 @@ public class RpcPeer

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["address"] = Address;
json["port"] = Port;
return json;
Expand Down
2 changes: 1 addition & 1 deletion src/RpcClient/Models/RpcPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RpcPlugin

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["name"] = Name;
json["version"] = Version;
json["interfaces"] = new JArray(Interfaces.Select(p => (JObject)p));
Expand Down
2 changes: 1 addition & 1 deletion src/RpcClient/Models/RpcRawMemPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class RpcRawMemPool

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["height"] = Height;
json["verified"] = new JArray(Verified.Select(p => (JObject)p.ToString()));
json["unverified"] = new JArray(UnVerified.Select(p => (JObject)p.ToString()));
Expand Down
6 changes: 3 additions & 3 deletions src/RpcClient/Models/RpcResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class RpcResponse

public static RpcResponse FromJson(JObject json)
{
var response = new RpcResponse
RpcResponse response = new()
{
Id = json["id"],
JsonRpc = json["jsonrpc"].AsString(),
Expand All @@ -33,7 +33,7 @@ public static RpcResponse FromJson(JObject json)

public JObject ToJson()
{
var json = new JObject();
JObject json = new();
json["id"] = Id;
json["jsonrpc"] = JsonRpc;
json["error"] = Error?.ToJson();
Expand Down Expand Up @@ -62,7 +62,7 @@ public static RpcResponseError FromJson(JObject json)

public JObject ToJson()
{
var json = new JObject();
JObject json = new();
json["code"] = Code;
json["message"] = Message;
json["data"] = Data;
Expand Down
2 changes: 1 addition & 1 deletion src/RpcClient/Models/RpcUnclaimedGas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RpcUnclaimedGas

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["unclaimed"] = Unclaimed.ToString();
json["address"] = Address;
return json;
Expand Down
2 changes: 1 addition & 1 deletion src/RpcClient/Models/RpcValidateAddressResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RpcValidateAddressResult

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["address"] = Address;
json["isvalid"] = IsValid;
return json;
Expand Down
2 changes: 1 addition & 1 deletion src/RpcClient/Models/RpcValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RpcValidator

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["publickey"] = PublicKey;
json["votes"] = Votes.ToString();
json["active"] = Active;
Expand Down
2 changes: 1 addition & 1 deletion src/RpcClient/Models/RpcVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class RpcVersion

public JObject ToJson()
{
JObject json = new JObject();
JObject json = new();
json["network"] = Network;
json["tcpport"] = TcpPort;
json["wsport"] = WsPort;
Expand Down
2 changes: 0 additions & 2 deletions src/RpcClient/PolicyAPI.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Neo.SmartContract.Native;
using Neo.VM;
using System.Linq;
using System.Threading.Tasks;
using Neo.IO.Json;

namespace Neo.Network.RPC
{
Expand Down
6 changes: 3 additions & 3 deletions src/RpcClient/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,10 @@ public async Task<UInt256> SubmitBlockAsync(byte[] block)
/// </summary>
public async Task<RpcInvokeResult> InvokeFunctionAsync(string scriptHash, string operation, RpcStack[] stacks, params Signer[] signer)
{
List<JObject> parameters = new List<JObject> { scriptHash.AsScriptHash(), operation, stacks.Select(p => p.ToJson()).ToArray() };
List<JObject> parameters = new() { scriptHash.AsScriptHash(), operation, stacks.Select(p => p.ToJson()).ToArray() };
if (signer.Length > 0)
{
parameters.Add(signer.Select(p => (JObject)p.ToJson()).ToArray());
parameters.Add(signer.Select(p => p.ToJson()).ToArray());
}
var result = await RpcSendAsync(GetRpcName(), parameters.ToArray()).ConfigureAwait(false);
return RpcInvokeResult.FromJson(result);
Expand All @@ -398,7 +398,7 @@ public async Task<RpcInvokeResult> InvokeFunctionAsync(string scriptHash, string
/// </summary>
public async Task<RpcInvokeResult> InvokeScriptAsync(byte[] script, params Signer[] signers)
{
List<JObject> parameters = new List<JObject> { Convert.ToBase64String(script) };
List<JObject> parameters = new() { Convert.ToBase64String(script) };
if (signers.Length > 0)
{
parameters.Add(signers.Select(p => p.ToJson()).ToArray());
Expand Down
28 changes: 17 additions & 11 deletions src/RpcClient/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static string AsScriptHash(this string addressOrScriptHash)
public static KeyPair GetKeyPair(string key)
{
if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException(nameof(key)); }
if (key.StartsWith("0x")) { key = key.Substring(2); }
if (key.StartsWith("0x")) { key = key[2..]; }

if (key.Length == 52)
{
Expand All @@ -80,7 +80,7 @@ public static KeyPair GetKeyPair(string key)
public static UInt160 GetScriptHash(string account, ProtocolSettings protocolSettings)
{
if (string.IsNullOrEmpty(account)) { throw new ArgumentNullException(nameof(account)); }
if (account.StartsWith("0x")) { account = account.Substring(2); }
if (account.StartsWith("0x")) { account = account[2..]; }

if (account.Length == 34)
{
Expand Down Expand Up @@ -179,19 +179,25 @@ public static Signer SignerFromJson(JObject json, ProtocolSettings protocolSetti
{
Account = json["account"].ToScriptHash(protocolSettings),
Scopes = (WitnessScope)Enum.Parse(typeof(WitnessScope), json["scopes"].AsString()),
AllowedContracts = ((JArray)json["allowedContracts"])?.Select(p => p.ToScriptHash(protocolSettings)).ToArray(),
AllowedGroups = ((JArray)json["allowedGroups"])?.Select(p => ECPoint.Parse(p.AsString(), ECCurve.Secp256r1)).ToArray()
AllowedContracts = ((JArray)json["allowedcontracts"])?.Select(p => p.ToScriptHash(protocolSettings)).ToArray(),
AllowedGroups = ((JArray)json["allowedgroups"])?.Select(p => ECPoint.Parse(p.AsString(), ECCurve.Secp256r1)).ToArray()
};
}

public static TransactionAttribute TransactionAttributeFromJson(JObject json)
{
TransactionAttributeType usage = Enum.Parse<TransactionAttributeType>(json["type"].AsString());

switch (usage)
return usage switch
{
default: throw new FormatException();
}
TransactionAttributeType.HighPriority => new HighPriorityAttribute(),
TransactionAttributeType.OracleResponse => new OracleResponse()
{
Id = (ulong)json["id"].AsNumber(),
Code = Enum.Parse<OracleResponseCode>(json["code"].AsString()),
Result = Convert.FromBase64String(json["result"].AsString()),
},
_ => throw new FormatException(),
};
}

public static Witness WitnessFromJson(JObject json)
Expand All @@ -217,17 +223,17 @@ public static StackItem StackItemFromJson(JObject json)
case StackItemType.Integer:
return new Integer(new BigInteger(json["value"].AsNumber()));
case StackItemType.Array:
Array array = new Array();
Array array = new();
foreach (var item in (JArray)json["value"])
array.Add(StackItemFromJson(item));
return array;
case StackItemType.Struct:
Struct @struct = new Struct();
Struct @struct = new();
foreach (var item in (JArray)json["value"])
@struct.Add(StackItemFromJson(item));
return @struct;
case StackItemType.Map:
Map map = new Map();
Map map = new();
foreach (var item in (JArray)json["value"])
{
PrimitiveType key = (PrimitiveType)StackItemFromJson(item["key"]);
Expand Down
9 changes: 3 additions & 6 deletions src/RpcServer/RpcServer.Blockchain.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#pragma warning disable IDE0051
#pragma warning disable IDE0060

using Neo.IO;
using Neo.IO.Json;
using Neo.Network.P2P.Payloads;
Expand Down Expand Up @@ -134,7 +131,7 @@ protected virtual JObject GetRawMemPool(JArray _params)
if (!shouldGetUnverified)
return new JArray(system.MemPool.GetVerifiedTransactions().Select(p => (JObject)p.Hash.ToString()));

JObject json = new JObject();
JObject json = new();
json["height"] = NativeContract.Ledger.CurrentIndex(system.StoreView);
system.MemPool.GetVerifiedAndUnverifiedTransactions(
out IEnumerable<Transaction> verifiedTransactions,
Expand Down Expand Up @@ -207,7 +204,7 @@ protected virtual JObject GetNextBlockValidators(JArray _params)
{
return candidates.Select(p =>
{
JObject validator = new JObject();
JObject validator = new();
validator["publickey"] = p.PublicKey.ToString();
validator["votes"] = p.Votes.ToString();
validator["active"] = validators.Contains(p.PublicKey);
Expand All @@ -218,7 +215,7 @@ protected virtual JObject GetNextBlockValidators(JArray _params)
{
return validators.Select(p =>
{
JObject validator = new JObject();
JObject validator = new();
validator["publickey"] = p.ToString();
validator["votes"] = 0;
validator["active"] = true;
Expand Down
Loading

0 comments on commit 43298b0

Please sign in to comment.