Skip to content

Commit 48ad51e

Browse files
committed
apply code review with nameof
1 parent 87dbe59 commit 48ad51e

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.SmartContract.cs

+31-30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Neo.IO;
1818
using Neo.Json;
1919
using Neo.Network.P2P.Payloads;
20+
using Neo.Network.P2P.Payloads.Conditions;
2021
using Neo.Persistence;
2122
using Neo.SmartContract;
2223
using Neo.SmartContract.Native;
@@ -47,68 +48,68 @@ public partial class UT_RpcServer
4748
static readonly JArray validatorSigner = [new JObject()
4849
{
4950
["account"] = ValidatorScriptHash.ToString(),
50-
["scopes"] = "CalledByEntry",
51-
["allowedcontracts"] = new JArray([NeoScriptHash.ToString(), GasScriptHash.ToString()]),
51+
["scopes"] = nameof(WitnessConditionType.CalledByEntry),
52+
["allowedcontracts"] = new JArray([NeoToken.NEO.Hash.ToString(), GasToken.GAS.Hash.ToString()]),
5253
["allowedgroups"] = new JArray([TestProtocolSettings.SoleNode.StandbyCommittee[0].ToString()]),
53-
["rules"] = new JArray([new JObject() { ["action"] = "Allow", ["condition"] = new JObject { ["type"] = "CalledByEntry" } }]),
54+
["rules"] = new JArray([new JObject() { ["action"] = nameof(WitnessRuleAction.Allow), ["condition"] = new JObject { ["type"] = nameof(WitnessConditionType.CalledByEntry) } }]),
5455
}];
5556
static readonly JArray multisigSigner = [new JObject()
5657
{
5758
["account"] = MultisigScriptHash.ToString(),
58-
["scopes"] = "CalledByEntry",
59+
["scopes"] = nameof(WitnessConditionType.CalledByEntry),
5960
}];
6061

6162
[TestMethod]
6263
public void TestInvokeFunction()
6364
{
6465
_rpcServer.wallet = _wallet;
6566

66-
JObject resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoScriptHash.ToString(), "totalSupply", new JArray([]), validatorSigner, true));
67+
JObject resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "totalSupply", new JArray([]), validatorSigner, true));
6768
Assert.AreEqual(resp.Count, 8);
6869
Assert.AreEqual(resp["script"], NeoTotalSupplyScript);
6970
Assert.IsTrue(resp.ContainsProperty("gasconsumed"));
7071
Assert.IsTrue(resp.ContainsProperty("diagnostics"));
71-
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], NeoScriptHash.ToString());
72+
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], NeoToken.NEO.Hash.ToString());
7273
Assert.IsTrue(((JArray)resp["diagnostics"]["storagechanges"]).Count == 0);
7374
Assert.AreEqual(resp["state"], "HALT");
7475
Assert.AreEqual(resp["exception"], null);
7576
Assert.AreEqual(((JArray)resp["notifications"]).Count, 0);
76-
Assert.AreEqual(resp["stack"][0]["type"], "Integer");
77+
Assert.AreEqual(resp["stack"][0]["type"], nameof(Neo.VM.Types.Integer));
7778
Assert.AreEqual(resp["stack"][0]["value"], "100000000");
7879
Assert.IsTrue(resp.ContainsProperty("tx"));
7980

80-
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoScriptHash.ToString(), "symbol"));
81+
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "symbol"));
8182
Assert.AreEqual(resp.Count, 6);
8283
Assert.IsTrue(resp.ContainsProperty("script"));
8384
Assert.IsTrue(resp.ContainsProperty("gasconsumed"));
8485
Assert.AreEqual(resp["state"], "HALT");
8586
Assert.AreEqual(resp["exception"], null);
8687
Assert.AreEqual(((JArray)resp["notifications"]).Count, 0);
87-
Assert.AreEqual(resp["stack"][0]["type"], "ByteString");
88+
Assert.AreEqual(resp["stack"][0]["type"], nameof(Neo.VM.Types.ByteString));
8889
Assert.AreEqual(resp["stack"][0]["value"], Convert.ToBase64String(Encoding.UTF8.GetBytes("NEO")));
8990

9091
// This call triggers not only NEO but also unclaimed GAS
91-
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoScriptHash.ToString(), "transfer", new JArray([
92-
new JObject() { ["type"] = "Hash160", ["value"] = MultisigScriptHash.ToString() },
93-
new JObject() { ["type"] = "Hash160", ["value"] = ValidatorScriptHash.ToString() },
94-
new JObject() { ["type"] = "Integer", ["value"] = "1" },
95-
new JObject() { ["type"] = "Any" },
92+
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "transfer", new JArray([
93+
new JObject() { ["type"] = nameof(ContractParameterType.Hash160), ["value"] = MultisigScriptHash.ToString() },
94+
new JObject() { ["type"] = nameof(ContractParameterType.Hash160), ["value"] = ValidatorScriptHash.ToString() },
95+
new JObject() { ["type"] = nameof(ContractParameterType.Integer), ["value"] = "1" },
96+
new JObject() { ["type"] = nameof(ContractParameterType.Any) },
9697
]), multisigSigner, true));
9798
Assert.AreEqual(resp.Count, 7);
9899
Assert.AreEqual(resp["script"], NeoTransferScript);
99100
Assert.IsTrue(resp.ContainsProperty("gasconsumed"));
100101
Assert.IsTrue(resp.ContainsProperty("diagnostics"));
101-
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], NeoScriptHash.ToString());
102+
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], NeoToken.NEO.Hash.ToString());
102103
Assert.IsTrue(((JArray)resp["diagnostics"]["storagechanges"]).Count == 4);
103104
Assert.AreEqual(resp["state"], "HALT");
104105
Assert.AreEqual(resp["exception"], $"The smart contract or address {MultisigScriptHash.ToString()} is not found");
105106
JArray notifications = (JArray)resp["notifications"];
106107
Assert.AreEqual(notifications.Count, 2);
107108
Assert.AreEqual(notifications[0]["eventname"].AsString(), "Transfer");
108-
Assert.AreEqual(notifications[0]["contract"].AsString(), NeoScriptHash.ToString());
109+
Assert.AreEqual(notifications[0]["contract"].AsString(), NeoToken.NEO.Hash.ToString());
109110
Assert.AreEqual(notifications[0]["state"]["value"][2]["value"], "1");
110111
Assert.AreEqual(notifications[1]["eventname"].AsString(), "Transfer");
111-
Assert.AreEqual(notifications[1]["contract"].AsString(), GasScriptHash.ToString());
112+
Assert.AreEqual(notifications[1]["contract"].AsString(), GasToken.GAS.Hash.ToString());
112113
Assert.AreEqual(notifications[1]["state"]["value"][2]["value"], "50000000");
113114

114115
_rpcServer.wallet = null;
@@ -121,24 +122,24 @@ public void TestInvokeScript()
121122
Assert.AreEqual(resp.Count, 7);
122123
Assert.IsTrue(resp.ContainsProperty("gasconsumed"));
123124
Assert.IsTrue(resp.ContainsProperty("diagnostics"));
124-
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], NeoScriptHash.ToString());
125+
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], NeoToken.NEO.Hash.ToString());
125126
Assert.AreEqual(resp["state"], "HALT");
126127
Assert.AreEqual(resp["exception"], null);
127128
Assert.AreEqual(((JArray)resp["notifications"]).Count, 0);
128-
Assert.AreEqual(resp["stack"][0]["type"], "Integer");
129+
Assert.AreEqual(resp["stack"][0]["type"], nameof(Neo.VM.Types.Integer));
129130
Assert.AreEqual(resp["stack"][0]["value"], "100000000");
130131

131132
resp = (JObject)_rpcServer.InvokeScript(new JArray(NeoTransferScript));
132133
Assert.AreEqual(resp.Count, 6);
133-
Assert.AreEqual(resp["stack"][0]["type"], "Boolean");
134+
Assert.AreEqual(resp["stack"][0]["type"], nameof(Neo.VM.Types.Boolean));
134135
Assert.AreEqual(resp["stack"][0]["value"], false);
135136
}
136137

137138
[TestMethod]
138139
public void TestTraverseIterator()
139140
{
140141
// GetAllCandidates that should return 0 candidates
141-
JObject resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoScriptHash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
142+
JObject resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
142143
string sessionId = resp["session"].AsString();
143144
string iteratorId = resp["stack"][0]["id"].AsString();
144145
JArray respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]);
@@ -147,10 +148,10 @@ public void TestTraverseIterator()
147148
Assert.ThrowsException<RpcException>(() => (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]), "Unknown session");
148149

149150
// register candidate in snapshot
150-
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoScriptHash.ToString(), "registerCandidate",
151+
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "registerCandidate",
151152
new JArray([new JObject()
152153
{
153-
["type"] = "PublicKey",
154+
["type"] = nameof(ContractParameterType.PublicKey),
154155
["value"] = TestProtocolSettings.SoleNode.StandbyCommittee[0].ToString(),
155156
}]), validatorSigner, true));
156157
Assert.AreEqual(resp["state"], "HALT");
@@ -168,25 +169,25 @@ public void TestTraverseIterator()
168169
engine.SnapshotCache.Commit();
169170

170171
// GetAllCandidates that should return 1 candidate
171-
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoScriptHash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
172+
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
172173
sessionId = resp["session"].AsString();
173174
iteratorId = resp["stack"][0]["id"].AsString();
174175
respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]);
175176
Assert.AreEqual(respArray.Count, 1);
176-
Assert.AreEqual(respArray[0]["type"], "Struct");
177+
Assert.AreEqual(respArray[0]["type"], nameof(Neo.VM.Types.Struct));
177178
JArray value = (JArray)respArray[0]["value"];
178179
Assert.AreEqual(value.Count, 2);
179-
Assert.AreEqual(value[0]["type"], "ByteString");
180+
Assert.AreEqual(value[0]["type"], nameof(Neo.VM.Types.ByteString));
180181
Assert.AreEqual(value[0]["value"], Convert.ToBase64String(TestProtocolSettings.SoleNode.StandbyCommittee[0].ToArray()));
181-
Assert.AreEqual(value[1]["type"], "Integer");
182+
Assert.AreEqual(value[1]["type"], nameof(Neo.VM.Types.Integer));
182183
Assert.AreEqual(value[1]["value"], "0");
183184

184185
// No result when traversed again
185186
respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]);
186187
Assert.AreEqual(respArray.Count, 0);
187188

188189
// GetAllCandidates again
189-
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoScriptHash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
190+
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
190191
sessionId = resp["session"].AsString();
191192
iteratorId = resp["stack"][0]["id"].AsString();
192193

@@ -201,7 +202,7 @@ public void TestTraverseIterator()
201202
// Mocking session timeout
202203
Thread.Sleep((int)_rpcServerSettings.SessionExpirationTime.TotalMilliseconds + 1);
203204
// build another session that did not expire
204-
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoScriptHash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
205+
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
205206
string notExpiredSessionId = resp["session"].AsString();
206207
string notExpiredIteratorId = resp["stack"][0]["id"].AsString();
207208
_rpcServer.OnTimer(new object());
@@ -212,7 +213,7 @@ public void TestTraverseIterator()
212213
Assert.AreEqual(respArray.Count, 1);
213214

214215
// Mocking disposal
215-
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoScriptHash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
216+
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
216217
sessionId = resp["session"].AsString();
217218
iteratorId = resp["stack"][0]["id"].AsString();
218219
_rpcServer.Dispose_SmartContract();

tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.cs

-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public partial class UT_RpcServer
3838
private readonly NEP6Wallet _wallet = TestUtils.GenerateTestWallet("123");
3939
private WalletAccount _walletAccount;
4040

41-
static readonly UInt160 NeoScriptHash = UInt160.Parse("0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5");
42-
static readonly UInt160 GasScriptHash = UInt160.Parse("0xd2a4cff31913016155e38e474a2c06d08be276cf");
4341
const byte NativePrefixAccount = 20;
4442
const byte NativePrefixTotalSupply = 11;
4543

0 commit comments

Comments
 (0)