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 23 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
3 changes: 3 additions & 0 deletions src/RpcServer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Neo.Network.RPC.Tests")]
48 changes: 48 additions & 0 deletions src/RpcServer/RelayActor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Akka.Actor;
using Neo.Network.P2P.Payloads;
using static Neo.Ledger.Blockchain;

namespace Neo.Plugins
{
public class RelayActor : UntypedActor
{
private readonly NeoSystem neoSystem;
private readonly SendersCollection senders;

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

protected override void OnReceive(object message)
{
switch (message)
{
case IInventory inventory:
{
UInt256 hash = inventory.Hash;
senders.Add(new ActorItem { Hash = hash, Actor = Sender });
neoSystem.Blockchain.Tell(inventory);
break;
}
case RelayResult reason:
{
UInt256 hash = reason.Inventory.Hash;
if (senders.TryGetValue(hash, out var entry))
{
entry.Actor.Tell(reason);
senders.Remove(entry.Hash);
}
break;
}
}
}

public static Props Props(NeoSystem neoSystem, int capacity = 1000)
{
return Akka.Actor.Props.Create(() => new RelayActor(neoSystem, capacity));
}
}
}
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);
}
}
}
4 changes: 2 additions & 2 deletions src/RpcServer/RpcServer.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>3.0.0-CI00863</Version>
<Version>3.0.0-CI00878</Version>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>Neo.Plugins</RootNamespace>
</PropertyGroup>
Expand All @@ -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-CI00878" />
</ItemGroup>

</Project>
37 changes: 37 additions & 0 deletions src/RpcServer/SendersCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Akka.Actor;
using System.Collections.ObjectModel;

namespace Neo.Plugins
{
internal class SendersCollection : KeyedCollection<UInt256, ActorItem>
{
private readonly int capacity;

public SendersCollection(int capacity)
{
this.capacity = capacity;
}

protected override UInt256 GetKeyForItem(ActorItem item)
{
return item.Hash;
}

protected override void InsertItem(int index, ActorItem newItem)
{
base.InsertItem(index, newItem);

if (Count > capacity)
{
RemoveAt(0);
}
}
}

internal class ActorItem
{
public UInt256 Hash { get; set; }

public IActorRef Actor { get; set; }
}
}
1 change: 1 addition & 0 deletions tests/Neo.Network.RPC.Tests/Neo.Network.RPC.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\RpcClient\RpcClient.csproj" />
<ProjectReference Include="..\..\src\RpcServer\RpcServer.csproj" />
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

<ItemGroup>
Expand Down
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
22 changes: 22 additions & 0 deletions tests/Neo.Network.RPC.Tests/SendersCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Plugins;
using System;
using System.Collections.Generic;
using System.Text;

namespace Neo.Plugins.Tests
{
[TestClass()]
public class SendersCollectionTests
{
[TestMethod()]
public void FixedDictionaryTest()
{
SendersCollection dictionary = new SendersCollection(2);
dictionary.Add(new ActorItem() { Hash = UInt256.Zero, Actor = null });
dictionary.Add(new ActorItem() { Hash = UInt256.Parse("0x530de76326a8662d1b730ba4fbdf011051eabd142015587e846da42376adf35f"), Actor = null });
dictionary.Add(new ActorItem() { Hash = UInt256.Parse("0x530de76326a8662d1b730ba4fbdf011051eabd142015587e846da42376adf350"), Actor = null });
Assert.AreEqual(2, dictionary.Count);
}
}
}