Skip to content

Commit 012ee37

Browse files
Consolidate MoneroRpcConnection
Co-authored-by: napoly napolytan@protonmail.com
1 parent 4646e32 commit 012ee37

10 files changed

Lines changed: 473 additions & 68 deletions

Monero.Test/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ RUN dotnet restore
88

99
RUN dotnet build --no-restore
1010

11-
CMD ["dotnet", "test", "--filter", "TestMoneroUtils", "--no-build", "--logger:trx"]
11+
CMD ["dotnet", "test", "--filter", "TestMoneroUtils|TestMoneroRpcConnection", "--no-build", "--logger:trx"]
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using Monero.Common;
2+
using Monero.Test.Utils;
3+
4+
namespace Monero.Test
5+
{
6+
public class TestMoneroRpcConnection
7+
{
8+
// Can copy connection
9+
[Fact]
10+
public void TestClone()
11+
{
12+
var connection = new MoneroRpcConnection("test", "user", "pass123", "test_zmq", 2);
13+
14+
var copy = connection.Clone();
15+
16+
Assert.True(connection.Equals(copy));
17+
18+
connection = new MoneroRpcConnection(TestUtils.DAEMON_RPC_URI);
19+
20+
TestConnection(connection, TestUtils.DAEMON_RPC_URI, true);
21+
22+
copy = connection.Clone();
23+
24+
Assert.True(connection.Equals(copy));
25+
}
26+
27+
// Can connect to node
28+
[Fact]
29+
public void TestNodeRpcConnection()
30+
{
31+
var connection = new MoneroRpcConnection(TestUtils.DAEMON_RPC_URI);
32+
33+
TestConnection(connection, TestUtils.DAEMON_RPC_URI, true);
34+
}
35+
36+
// Can connect to wallet
37+
[Fact]
38+
public void TestWalletRpcConnection()
39+
{
40+
var connection = new MoneroRpcConnection(TestUtils.WALLET_RPC_URI);
41+
42+
TestConnection(connection, TestUtils.WALLET_RPC_URI, true);
43+
}
44+
45+
// Test invalid url
46+
[Fact]
47+
public void TestInvalidConnection()
48+
{
49+
var connection = new MoneroRpcConnection("");
50+
51+
TestConnection(connection, "", false);
52+
}
53+
54+
// Can send request to RPC
55+
[Fact]
56+
public void TestSendRequest()
57+
{
58+
// Setup connection
59+
60+
var connection = new MoneroRpcConnection(TestUtils.DAEMON_RPC_URI);
61+
62+
TestConnection(connection, TestUtils.DAEMON_RPC_URI, true);
63+
64+
// Test monerod JSON request
65+
66+
var jsonResponse = connection.SendJsonRequest("get_info");
67+
68+
Assert.NotNull(jsonResponse);
69+
Assert.Null(jsonResponse.Error);
70+
Assert.NotNull(jsonResponse.Result);
71+
72+
// Test monerod PATH request
73+
74+
var pathResponse = connection.SendPathRequest("get_info");
75+
76+
Assert.NotNull(pathResponse);
77+
Assert.Null(pathResponse.GetValueOrDefault("error"));
78+
79+
// Test monerod BINARY request
80+
81+
// TODO implement MoneroRpcConnection.SendBinaryRequest()
82+
83+
//var binaryResponse = connection.SendBinaryRequest("get_outs.bin");
84+
//Assert.NotNull(binaryResponse);
85+
}
86+
87+
private static void TestConnection(MoneroRpcConnection? connection, string? uri, bool online)
88+
{
89+
Assert.NotNull(connection);
90+
91+
if (online)
92+
{
93+
Assert.NotNull(uri);
94+
Assert.NotEmpty(uri);
95+
Assert.Equal(uri, connection.GetUri());
96+
Assert.True(connection.IsClearnet());
97+
Assert.False(connection.IsOnion());
98+
Assert.False(connection.IsI2P());
99+
Assert.True(connection.CheckConnection());
100+
Assert.True(connection.IsOnline());
101+
Assert.True(connection.IsAuthenticated());
102+
Assert.True(connection.IsConnected());
103+
}
104+
else
105+
{
106+
Assert.Equal(uri, connection.GetUri());
107+
Assert.False(connection.IsClearnet());
108+
Assert.False(connection.IsOnion());
109+
Assert.False(connection.IsI2P());
110+
Assert.True(connection.CheckConnection());
111+
Assert.False(connection.IsOnline());
112+
Assert.Null(connection.IsAuthenticated());
113+
Assert.False(connection.IsConnected());
114+
}
115+
}
116+
117+
}
118+
}

Monero.Test/Utils/TestUtils.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ internal abstract class TestUtils
1010
public static readonly string MONERO_BINS_DIR = "";
1111

1212
// monero daemon rpc endpoint configuration (change per your configuration)
13-
public static readonly string DAEMON_RPC_URI = "localhost:28081";
13+
public static readonly string DAEMON_RPC_URI = "http://monerod:18081";
1414
public static readonly string DAEMON_RPC_USERNAME = "";
1515
public static readonly string DAEMON_RPC_PASSWORD = "";
1616
public static readonly string DAEMON_LOCAL_PATH = MONERO_BINS_DIR + "/monerod";
1717

1818
// monero wallet rpc configuration (change per your configuration)
19-
public static readonly int WALLET_RPC_PORT_START = 28084; // test wallet executables will bind to consecutive ports after these
19+
public static readonly int WALLET_RPC_PORT_START = 18082; // test wallet executables will bind to consecutive ports after these
2020
public static readonly bool WALLET_RPC_ZMQ_ENABLED = false;
2121
public static readonly int WALLET_RPC_ZMQ_PORT_START = 58083;
2222
public static readonly int WALLET_RPC_ZMQ_BIND_PORT_START = 48083; // TODO: zmq bind port necessary?
23-
public static readonly string WALLET_RPC_USERNAME = "rpc_user";
24-
public static readonly string WALLET_RPC_PASSWORD = "abc123";
23+
public static readonly string WALLET_RPC_USERNAME = "";
24+
public static readonly string WALLET_RPC_PASSWORD = "";
2525
public static readonly string WALLET_RPC_ZMQ_DOMAIN = "127.0.0.1";
26-
public static readonly string WALLET_RPC_DOMAIN = "localhost";
26+
public static readonly string WALLET_RPC_DOMAIN = "http://xmr_wallet";
2727
public static readonly string WALLET_RPC_URI = WALLET_RPC_DOMAIN + ":" + WALLET_RPC_PORT_START;
2828
public static readonly string WALLET_RPC_ZMQ_URI = "tcp://" + WALLET_RPC_ZMQ_DOMAIN + ":" + WALLET_RPC_ZMQ_PORT_START;
2929
public static readonly string WALLET_RPC_LOCAL_PATH = MONERO_BINS_DIR + "/monero-wallet-rpc";
@@ -38,7 +38,7 @@ internal abstract class TestUtils
3838

3939
// test wallet constants
4040
public static readonly ulong MAX_FEE = 75000000000;
41-
public static readonly MoneroNetworkType NETWORK_TYPE = MoneroNetworkType.TESTNET;
41+
public static readonly MoneroNetworkType NETWORK_TYPE = MoneroNetworkType.MAINNET;
4242
public static readonly string LANGUAGE = "English";
4343
public static readonly string SEED = "silk mocked cucumber lettuce hope adrenalin aching lush roles fuel revamp baptism wrist ulong tender teardrop midst pastry pigment equip frying inbound pinched ravine frying";
4444
public static readonly string ADDRESS = "A1y9sbVt8nqhZAVm3me1U18rUVXcjeNKuBd1oE2cTs8biA9cozPMeyYLhe77nPv12JA3ejJN3qprmREriit2fi6tJDi99RR";

Monero.Test/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ services:
1919
xmr_wallet:
2020
image: btcpayserver/monero:0.18.4.1
2121
container_name: xmr_wallet
22-
command: monero-wallet-rpc --log-level 2 --allow-mismatched-daemon-version --rpc-bind-ip=0.0.0.0 --disable-rpc-login --confirm-external-bind --rpc-bind-port=28082 --non-interactive --trusted-daemon --daemon-address=monerod:18081 --wallet-dir=/wallet --rpc-access-control-origins=* --tx-notify="/bin/sh ./scripts/notifier.sh -k -X GET https://host.docker.internal:14142/monerolikedaemoncallback/tx?cryptoCode=xmr&hash=%s"
22+
command: monero-wallet-rpc --log-level 2 --allow-mismatched-daemon-version --rpc-bind-ip=0.0.0.0 --disable-rpc-login --confirm-external-bind --rpc-bind-port=18082 --non-interactive --trusted-daemon --daemon-address=monerod:18081 --wallet-dir=/wallet --rpc-access-control-origins=* --tx-notify="/bin/sh ./scripts/notifier.sh -k -X GET https://host.docker.internal:14142/monerolikedaemoncallback/tx?cryptoCode=xmr&hash=%s"
2323
ports:
2424
- "18082:18082"
2525
volumes:

Monero/Common/MoneroError.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ namespace Monero.Common
33
{
44
public class MoneroError : Exception
55
{
6-
private int? _code;
6+
private readonly int? _code;
77

88
public MoneroError(string message, int? code = null) : base(message)
99
{
1010
_code = code;
1111
}
1212

13+
public MoneroError(Exception e) : base(e.Message) { }
14+
15+
public MoneroError(MoneroError e) : base(e.Message)
16+
{
17+
_code = e._code;
18+
}
19+
1320
public int? GetCode()
1421
{
1522
return _code;

Monero/Common/MoneroJsonRpcRequest.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@
22

33
namespace Monero.Common
44
{
5-
65
public class MoneroJsonRpcRequest : MoneroHttpRequest
76
{
87
[JsonProperty("jsonrpc", Order = 0)]
9-
public readonly static string Version = "2.0";
8+
public readonly string Version = "2.0";
109

1110
[JsonProperty("method", Order = 1)]
1211
public readonly string Method;
1312

1413
[JsonProperty("params", Order = 2, NullValueHandling = NullValueHandling.Ignore)]
15-
public Dictionary<string, object>? Params;
14+
public object? Params;
1615

17-
public MoneroJsonRpcRequest(string method, Dictionary<string, object>? parameters = null)
16+
public MoneroJsonRpcRequest(string method, object? parameters = null)
1817
{
1918
Method = method;
2019
Params = parameters;
2120
}
22-
2321
}
2422

25-
public class MoneroJsonRpcResponse
23+
public class MoneroJsonRpcResponse<T>
2624
{
25+
[JsonProperty("error")]
26+
public Dictionary<string, object>? Error;
27+
2728
[JsonProperty("result")]
28-
public Dictionary<string, object>? Result;
29+
public T? Result;
2930
}
30-
}
31+
32+
}

0 commit comments

Comments
 (0)