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 16 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
55 changes: 55 additions & 0 deletions src/RpcServer/RelayActor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Akka.Actor;
using Neo.Network.P2P.Payloads;
using System.Collections.Generic;
using System.Threading.Tasks;
using static Neo.Ledger.Blockchain;

namespace Neo.Plugins
{
public class RelayActor : UntypedActor
{
private readonly NeoSystem neoSystem;
private readonly Dictionary<UInt256, IActorRef> senders = new Dictionary<UInt256, IActorRef>();
private readonly int expireMs;

public RelayActor(NeoSystem neoSystem, int expireMs)
{
this.neoSystem = neoSystem;
this.expireMs = expireMs;
Context.System.EventStream.Subscribe(Self, typeof(RelayResult));
}

protected override void OnReceive(object message)
{
switch (message)
{
case IInventory inventory:
{
UInt256 hash = inventory.Hash;
senders.Add(hash, Sender);
neoSystem.Blockchain.Tell(inventory);
// the sender will clean the record after expireMs
Task.Run(async () =>
{
shargon marked this conversation as resolved.
Show resolved Hide resolved
await Task.Delay(expireMs);
senders.Remove(hash);
});
break;
}
case RelayResult reason:
{
if (senders.Remove(reason.Inventory.Hash, out var actor))
bettybao1209 marked this conversation as resolved.
Show resolved Hide resolved
{
actor.Tell(reason);
}
break;
}
}
}

public static Props Props(NeoSystem neoSystem, int expire = 10000)
{
return Akka.Actor.Props.Create(() => new RelayActor(neoSystem, expire));
}
}
}
2 changes: 0 additions & 2 deletions src/RpcServer/RpcServer.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
20 changes: 12 additions & 8 deletions src/RpcServer/RpcServer.Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
using Neo.Ledger;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using System;
using System.Linq;
using static Neo.Ledger.Blockchain;

namespace Neo.Plugins
{
partial class RpcServer
{
private static readonly IActorRef relayActor = System.ActorSystem.ActorOf(RelayActor.Props(System));

[RpcMethod]
private JObject GetConnectionCount(JArray _params)
{
Expand Down Expand Up @@ -41,17 +45,19 @@ private JObject GetPeers(JArray _params)
return json;
}

private static JObject GetRelayResult(RelayResultReason reason, UInt256 hash)
private static JObject GetRelayResult(IInventory inventory)
{
if (reason == RelayResultReason.Succeed)
const int timeOut = 1000;
var result = relayActor.Ask<RelayResult>(inventory, TimeSpan.FromMilliseconds(timeOut)).Result;
if (result.Result == VerifyResult.Succeed)
{
var ret = new JObject();
ret["hash"] = hash.ToString();
ret["hash"] = inventory.Hash.ToString();
return ret;
}
else
{
throw new RpcException(-500, reason.ToString());
throw new RpcException(-500, result.Result.ToString());
}
}

Expand All @@ -70,16 +76,14 @@ 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 GetRelayResult(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 GetRelayResult(block);
}
}
}
2 changes: 1 addition & 1 deletion src/RpcServer/RpcServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.2.0" />
<PackageReference Include="Neo" Version="3.0.0-CI00863" />
<PackageReference Include="Neo" Version="3.0.0-CI00873" />
</ItemGroup>

</Project>
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