Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add relayresult #200

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/RpcServer/RpcServer.Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
#pragma warning disable IDE0060

using Akka.Actor;
using Akka.Event;
using Neo.IO;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using System;
using System.Linq;
using System.Threading.Tasks;
using static Neo.Ledger.Blockchain;

namespace Neo.Plugins
{
Expand Down Expand Up @@ -41,9 +45,9 @@ private JObject GetPeers(JArray _params)
return json;
}

private static JObject GetRelayResult(RelayResultReason reason, UInt256 hash)
private static JObject GetRelayResult(VerifyResult reason, UInt256 hash)
{
if (reason == RelayResultReason.Succeed)
if (reason == VerifyResult.Succeed)
{
var ret = new JObject();
ret["hash"] = hash.ToString();
Expand All @@ -70,16 +74,25 @@ private JObject GetVersion(JArray _params)
private JObject SendRawTransaction(JArray _params)
{
Transaction tx = _params[0].AsString().HexToBytes().AsSerializable<Transaction>();
RelayResultReason reason = System.Blockchain.Ask<RelayResultReason>(tx).Result;
return GetRelayResult(reason, tx.Hash);
return Send(tx);
}

[RpcMethod]
private JObject SubmitBlock(JArray _params)
{
Block block = _params[0].AsString().HexToBytes().AsSerializable<Block>();
RelayResultReason reason = System.Blockchain.Ask<RelayResultReason>(block).Result;
return GetRelayResult(reason, block.Hash);
return Send(block);
}

private JObject Send(IInventory inventory)
{
System.Blockchain.Tell(inventory);

int timeOut = 1000;
DateTime current = DateTime.Now;
while (rpcActor.Ask<RelayResult>(0).Result == null && DateTime.Now.Subtract(current).Milliseconds < timeOut) { Task.Delay(50); }
var result = rpcActor.Ask<RelayResult>(0).Result;
return GetRelayResult(result.Result, inventory.Hash);
}
}
}
29 changes: 29 additions & 0 deletions src/RpcServer/RpcServer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Akka.Actor;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand All @@ -16,17 +17,43 @@
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using static Neo.Ledger.Blockchain;

namespace Neo.Plugins
{
public sealed partial class RpcServer : Plugin
{
private static readonly Dictionary<string, Func<JArray, JObject>> methods = new Dictionary<string, Func<JArray, JObject>>();
private IWebHost host;
private static readonly IActorRef rpcActor = System.ActorSystem.ActorOf(RpcActor.Props());

public RpcServer()
{
RegisterMethods(this);
System.ActorSystem.EventStream.Subscribe(rpcActor, typeof(RelayResult));
}

private class RpcActor : UntypedActor
bettybao1209 marked this conversation as resolved.
Show resolved Hide resolved
{
public static Props Props()
{
return Akka.Actor.Props.Create(() => new RpcActor());
}

private RelayResult result;

protected override void OnReceive(object message)
{
switch (message)
{
case RelayResult reason:
result = reason;
break;
case 0:
Sender.Tell(result);
break;
}
}
}

private bool CheckAuth(HttpContext context)
Expand Down Expand Up @@ -246,5 +273,7 @@ public static void RegisterMethods(object handler)
methods[name] = (Func<JArray, JObject>)method.CreateDelegate(typeof(Func<JArray, JObject>), handler);
}
}


}
}
2 changes: 1 addition & 1 deletion tests/Neo.Network.RPC.Tests/RpcTestCases.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"error": {
"code": -500,
"message": "InsufficientFunds",
"data": " at Neo.Plugins.RpcServer.GetRelayResult(RelayResultReason reason, UInt256 hash)\r\n at Neo.Network.RPC.Models.RpcServer.SendRawTransaction(JArray _params)\r\n at Neo.Network.RPC.Models.RpcServer.ProcessRequest(HttpContext context, JObject request)"
"data": " at Neo.Plugins.RpcServer.GetRelayResult(VerifyResult reason, UInt256 hash)\r\n at Neo.Network.RPC.Models.RpcServer.SendRawTransaction(JArray _params)\r\n at Neo.Network.RPC.Models.RpcServer.ProcessRequest(HttpContext context, JObject request)"
}
}
},
Expand Down