diff --git a/src/Plugins/RpcServer/RpcServer.Node.cs b/src/Plugins/RpcServer/RpcServer.Node.cs index c1b5397c5b..23c731d01e 100644 --- a/src/Plugins/RpcServer/RpcServer.Node.cs +++ b/src/Plugins/RpcServer/RpcServer.Node.cs @@ -23,14 +23,23 @@ namespace Neo.Plugins.RpcServer { partial class RpcServer { - [RpcMethod] - protected internal virtual JToken GetConnectionCount(JArray _params) + + /// + /// Gets the current number of connections to the node. + /// + /// The number of connections as a JToken. + [RpcMethodWithParams] + protected internal virtual JToken GetConnectionCount() { return localNode.ConnectedCount; } - [RpcMethod] - protected internal virtual JToken GetPeers(JArray _params) + /// + /// Gets information about the peers connected to the node. + /// + /// A JObject containing information about unconnected, bad, and connected peers. + [RpcMethodWithParams] + protected internal virtual JToken GetPeers() { JObject json = new(); json["unconnected"] = new JArray(localNode.GetUnconnectedPeers().Select(p => @@ -51,9 +60,14 @@ protected internal virtual JToken GetPeers(JArray _params) return json; } + /// + /// Processes the result of a transaction or block relay and returns appropriate response or throws an exception. + /// + /// The verification result of the relay. + /// The hash of the transaction or block. + /// A JObject containing the hash if successful, otherwise throws an RpcException. private static JObject GetRelayResult(VerifyResult reason, UInt256 hash) { - switch (reason) { case VerifyResult.Succeed: @@ -109,8 +123,12 @@ private static JObject GetRelayResult(VerifyResult reason, UInt256 hash) } } - [RpcMethod] - protected internal virtual JToken GetVersion(JArray _params) + /// + /// Gets version information about the node, including network, protocol, and RPC settings. + /// + /// A JObject containing detailed version and configuration information. + [RpcMethodWithParams] + protected internal virtual JToken GetVersion() { JObject json = new(); json["tcpport"] = localNode.ListenerTcpPort; @@ -146,23 +164,39 @@ protected internal virtual JToken GetVersion(JArray _params) return json; } + /// + /// Removes a specified prefix from a string if it exists. + /// + /// The input string. + /// The prefix to remove. + /// The string with the prefix removed if it existed, otherwise the original string. private static string StripPrefix(string s, string prefix) { return s.StartsWith(prefix) ? s.Substring(prefix.Length) : s; } - [RpcMethod] - protected internal virtual JToken SendRawTransaction(JArray _params) + /// + /// Sends a raw transaction to the network. + /// + /// The base64-encoded transaction. + /// A JToken containing the result of the transaction relay. + [RpcMethodWithParams] + protected internal virtual JToken SendRawTransaction(string base64Tx) { - Transaction tx = Result.Ok_Or(() => Convert.FromBase64String(_params[0].AsString()).AsSerializable(), RpcError.InvalidParams.WithData($"Invalid Transaction Format: {_params[0]}")); + Transaction tx = Result.Ok_Or(() => Convert.FromBase64String(base64Tx).AsSerializable(), RpcError.InvalidParams.WithData($"Invalid Transaction Format: {base64Tx}")); RelayResult reason = system.Blockchain.Ask(tx).Result; return GetRelayResult(reason.Result, tx.Hash); } - [RpcMethod] - protected internal virtual JToken SubmitBlock(JArray _params) + /// + /// Submits a new block to the network. + /// + /// The base64-encoded block. + /// A JToken containing the result of the block submission. + [RpcMethodWithParams] + protected internal virtual JToken SubmitBlock(string base64Block) { - Block block = Result.Ok_Or(() => Convert.FromBase64String(_params[0].AsString()).AsSerializable(), RpcError.InvalidParams.WithData($"Invalid Block Format: {_params[0]}")); + Block block = Result.Ok_Or(() => Convert.FromBase64String(base64Block).AsSerializable(), RpcError.InvalidParams.WithData($"Invalid Block Format: {base64Block}")); RelayResult reason = system.Blockchain.Ask(block).Result; return GetRelayResult(reason.Result, block.Hash); } diff --git a/tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Node.cs b/tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Node.cs index cf674a449c..042bde0d30 100644 --- a/tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Node.cs +++ b/tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Node.cs @@ -29,7 +29,7 @@ partial class UT_RpcServer [TestMethod] public void TestGetConnectionCount() { - var result = _rpcServer.GetConnectionCount(new JArray()); + var result = _rpcServer.GetConnectionCount(); result.GetType().Should().Be(typeof(JNumber)); } @@ -44,7 +44,7 @@ public void TestGetPeers() localNode.AddPeers(new List() { new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 13332) }); var rpcServer = new RpcServer(neoSystem, RpcServerSettings.Default); - var result = rpcServer.GetPeers(new JArray()); + var result = rpcServer.GetPeers(); Assert.IsInstanceOfType(result, typeof(JObject)); var json = (JObject)result; json.ContainsProperty("unconnected").Should().BeTrue(); @@ -56,7 +56,7 @@ public void TestGetPeers() [TestMethod] public void TestGetVersion() { - var result = _rpcServer.GetVersion(new JArray()); + var result = _rpcServer.GetVersion(); Assert.IsInstanceOfType(result, typeof(JObject)); var json = (JObject)result; @@ -87,7 +87,7 @@ public void TestSendRawTransaction_Normal() var tx = TestUtils.CreateValidTx(snapshot, _wallet, _walletAccount); var txString = Convert.ToBase64String(tx.ToArray()); - var result = _rpcServer.SendRawTransaction(new JArray(txString)); + var result = _rpcServer.SendRawTransaction(txString); Assert.IsInstanceOfType(result, typeof(JObject)); Assert.IsTrue(((JObject)result).ContainsProperty("hash")); } @@ -96,7 +96,7 @@ public void TestSendRawTransaction_Normal() public void TestSendRawTransaction_InvalidTransactionFormat() { Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray("invalid_transaction_string")), + _rpcServer.SendRawTransaction("invalid_transaction_string"), "Should throw RpcException for invalid transaction format"); } @@ -108,7 +108,7 @@ public void TestSendRawTransaction_InsufficientBalance() var txString = Convert.ToBase64String(tx.ToArray()); var exception = Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray(txString)), + _rpcServer.SendRawTransaction(txString), "Should throw RpcException for insufficient balance"); Assert.AreEqual(RpcError.InsufficientFunds.Code, exception.HResult); } @@ -121,7 +121,7 @@ public void TestSendRawTransaction_InvalidSignature() var txString = Convert.ToBase64String(tx.ToArray()); var exception = Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray(txString)), + _rpcServer.SendRawTransaction(txString), "Should throw RpcException for invalid signature"); Assert.AreEqual(RpcError.InvalidSignature.Code, exception.HResult); } @@ -134,7 +134,7 @@ public void TestSendRawTransaction_InvalidScript() var txString = Convert.ToBase64String(tx.ToArray()); var exception = Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray(txString)), + _rpcServer.SendRawTransaction(txString), "Should throw RpcException for invalid script"); Assert.AreEqual(RpcError.InvalidScript.Code, exception.HResult); } @@ -147,7 +147,7 @@ public void TestSendRawTransaction_InvalidAttribute() var txString = Convert.ToBase64String(tx.ToArray()); var exception = Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray(txString)), + _rpcServer.SendRawTransaction(txString), "Should throw RpcException for invalid attribute"); // Transaction with invalid attribute can not pass the Transaction deserialization // and will throw invalid params exception. @@ -162,7 +162,7 @@ public void TestSendRawTransaction_Oversized() var txString = Convert.ToBase64String(tx.ToArray()); var exception = Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray(txString)), + _rpcServer.SendRawTransaction(txString), "Should throw RpcException for invalid format transaction"); // Oversized transaction will not pass the deserialization. Assert.AreEqual(RpcError.InvalidParams.Code, exception.HResult); @@ -176,7 +176,7 @@ public void TestSendRawTransaction_Expired() var txString = Convert.ToBase64String(tx.ToArray()); var exception = Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray(txString)), + _rpcServer.SendRawTransaction(txString), "Should throw RpcException for expired transaction"); Assert.AreEqual(RpcError.ExpiredTransaction.Code, exception.HResult); } @@ -191,7 +191,7 @@ public void TestSendRawTransaction_PolicyFailed() snapshot.Commit(); var exception = Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray(txString)), + _rpcServer.SendRawTransaction(txString), "Should throw RpcException for conflicting transaction"); Assert.AreEqual(RpcError.PolicyFailed.Code, exception.HResult); } @@ -205,7 +205,7 @@ public void TestSendRawTransaction_AlreadyInPool() var txString = Convert.ToBase64String(tx.ToArray()); var exception = Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray(txString)), + _rpcServer.SendRawTransaction(txString), "Should throw RpcException for transaction already in memory pool"); Assert.AreEqual(RpcError.AlreadyInPool.Code, exception.HResult); } @@ -218,7 +218,7 @@ public void TestSendRawTransaction_AlreadyInBlockchain() TestUtils.AddTransactionToBlockchain(snapshot, tx); snapshot.Commit(); var txString = Convert.ToBase64String(tx.ToArray()); - var exception = Assert.ThrowsException(() => _rpcServer.SendRawTransaction(new JArray(txString))); + var exception = Assert.ThrowsException(() => _rpcServer.SendRawTransaction(txString)); Assert.AreEqual(RpcError.AlreadyExists.Code, exception.HResult); } @@ -233,7 +233,7 @@ public void TestSubmitBlock_Normal() var block = TestUtils.CreateBlockWithValidTransactions(snapshot, _wallet, _walletAccount, 1); var blockString = Convert.ToBase64String(block.ToArray()); - var result = _rpcServer.SubmitBlock(new JArray(blockString)); + var result = _rpcServer.SubmitBlock(blockString); Assert.IsInstanceOfType(result, typeof(JObject)); Assert.IsTrue(((JObject)result).ContainsProperty("hash")); } @@ -244,7 +244,7 @@ public void TestSubmitBlock_InvalidBlockFormat() string invalidBlockString = TestUtils.CreateInvalidBlockFormat(); var exception = Assert.ThrowsException(() => - _rpcServer.SubmitBlock(new JArray(invalidBlockString)), + _rpcServer.SubmitBlock(invalidBlockString), "Should throw RpcException for invalid block format"); Assert.AreEqual(RpcError.InvalidParams.Code, exception.HResult); @@ -261,7 +261,7 @@ public void TestSubmitBlock_AlreadyExists() var blockString = Convert.ToBase64String(block.ToArray()); var exception = Assert.ThrowsException(() => - _rpcServer.SubmitBlock(new JArray(blockString)), + _rpcServer.SubmitBlock(blockString), "Should throw RpcException when block already exists"); Assert.AreEqual(RpcError.AlreadyExists.Code, exception.HResult); } @@ -275,7 +275,7 @@ public void TestSubmitBlock_InvalidBlock() var blockString = Convert.ToBase64String(block.ToArray()); var exception = Assert.ThrowsException(() => - _rpcServer.SubmitBlock(new JArray(blockString)), + _rpcServer.SubmitBlock(blockString), "Should throw RpcException for invalid block"); Assert.AreEqual(RpcError.VerificationFailed.Code, exception.HResult); } @@ -288,7 +288,7 @@ public void TestSubmitBlock_InvalidBlock() public void TestSendRawTransaction_NullInput() { var exception = Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray((string)null)), + _rpcServer.SendRawTransaction((string)null), "Should throw RpcException for null input"); Assert.AreEqual(RpcError.InvalidParams.Code, exception.HResult); } @@ -297,7 +297,7 @@ public void TestSendRawTransaction_NullInput() public void TestSendRawTransaction_EmptyInput() { var exception = Assert.ThrowsException(() => - _rpcServer.SendRawTransaction(new JArray(string.Empty)), + _rpcServer.SendRawTransaction(string.Empty), "Should throw RpcException for empty input"); Assert.AreEqual(RpcError.InvalidParams.Code, exception.HResult); } @@ -306,7 +306,7 @@ public void TestSendRawTransaction_EmptyInput() public void TestSubmitBlock_NullInput() { var exception = Assert.ThrowsException(() => - _rpcServer.SubmitBlock(new JArray((string)null)), + _rpcServer.SubmitBlock((string)null), "Should throw RpcException for null input"); Assert.AreEqual(RpcError.InvalidParams.Code, exception.HResult); } @@ -315,7 +315,7 @@ public void TestSubmitBlock_NullInput() public void TestSubmitBlock_EmptyInput() { var exception = Assert.ThrowsException(() => - _rpcServer.SubmitBlock(new JArray(string.Empty)), + _rpcServer.SubmitBlock(string.Empty), "Should throw RpcException for empty input"); Assert.AreEqual(RpcError.InvalidParams.Code, exception.HResult); }