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
Changes from 1 commit
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
46 changes: 42 additions & 4 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 @@ -70,16 +74,50 @@ private JObject GetVersion(JArray _params)
private JObject SendRawTransaction(JArray _params)
{
Transaction tx = _params[0].AsString().HexToBytes().AsSerializable<Transaction>();
VerifyResult reason = System.Blockchain.Ask<VerifyResult>(tx).Result;
return GetRelayResult(reason, tx.Hash);
return Send(tx);
}

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

private JObject Send(IInventory inventory)
{
var a = System.ActorSystem.ActorOf(RpcActor.Props());
System.ActorSystem.EventStream.Subscribe(a, typeof(RelayResult));
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
System.Blockchain.Tell(inventory);

int timeOut = 1000;
DateTime current = DateTime.Now;
while (a.Ask<RelayResult>(0).Result == null && DateTime.Now.Subtract(current).Milliseconds < timeOut) { Task.Delay(50); }
var result = a.Ask<RelayResult>(0).Result;
return GetRelayResult(result.Result, inventory.Hash);
}

private class RpcActor : UntypedActor
{
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;
}
}
}
}
}