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

additional client/rpc tests #7

Merged
merged 8 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 2 deletions .github/workflows/dotnet-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [main]
pull_request:
branches: [main]
types: [opened, synchronize]

jobs:
build-test-cov:
Expand Down Expand Up @@ -42,6 +42,5 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./
flags: unittests, integrationtests
verbose: true
slug: thirdweb-dev/thirdweb-dotnet
6 changes: 5 additions & 1 deletion Thirdweb.Tests/Thirdweb.Client.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ public void SecretKeyAndBundleIdInitialization()
[Fact]
public void TimeoutOptions()
{
var client = new ThirdwebClient(secretKey: _secretKey, fetchTimeoutOptions: new TimeoutOptions(storage: 30000, rpc: 30000));
var client = new ThirdwebClient(secretKey: _secretKey, fetchTimeoutOptions: new TimeoutOptions(storage: 30000, rpc: 30000, other: 30000));
Assert.NotNull(client.FetchTimeoutOptions);
Assert.Equal(30000, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Storage));
Assert.Equal(30000, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Rpc));
Assert.Equal(30000, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Other));
Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Other + 1));
}

[Fact]
Expand All @@ -96,5 +98,7 @@ public void NoTimeoutOptions()
Assert.NotNull(client.FetchTimeoutOptions);
Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Storage));
Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Rpc));
Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Other));
Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Other + 1));
}
}
59 changes: 58 additions & 1 deletion Thirdweb.Tests/Thirdweb.RPC.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Thirdweb.Tests;
using System.Numerics;

namespace Thirdweb.Tests;

public class RpcTests : BaseTests
{
Expand Down Expand Up @@ -32,4 +34,59 @@ public async Task TestTimeout()
var exception = await Assert.ThrowsAsync<TimeoutException>(async () => await rpc.SendRequestAsync<string>("eth_chainId"));
_output.WriteLine($"TestTimeout Exception Message: {exception.Message}");
}

[Fact]
public async Task TestBatch()
{
var client = new ThirdwebClient(secretKey: _secretKey);
var rpc = ThirdwebRPC.GetRpcInstance(client, 1);
var req = rpc.SendRequestAsync<string>("eth_blockNumber");
_ = await rpc.SendRequestAsync<string>("eth_chainId");
var blockNumberTasks = new List<Task<string>>();
for (var i = 0; i < 100; i++)
{
blockNumberTasks.Add(rpc.SendRequestAsync<string>("eth_blockNumber"));
}
var results = await Task.WhenAll(blockNumberTasks);
Assert.Equal(100, results.Length);
Assert.All(results, result => Assert.StartsWith("0x", result));
Assert.All(results, result => Assert.Equal(results[0], result));
}

[Fact]
public async Task TestDeserialization()
{
var client = new ThirdwebClient(secretKey: _secretKey);
var rpc = ThirdwebRPC.GetRpcInstance(client, 1);
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () => await rpc.SendRequestAsync<BigInteger>("eth_blockNumber"));
Assert.Equal("Failed to deserialize RPC response.", exception.Message);
}

[Fact]
public void TestBadInitialization()
{
var clientException = Assert.Throws<ArgumentNullException>(() => ThirdwebRPC.GetRpcInstance(null, 0));
Assert.Equal("client", clientException.ParamName);
var chainIdException = Assert.Throws<ArgumentException>(() => ThirdwebRPC.GetRpcInstance(new ThirdwebClient(secretKey: _secretKey), 0));
Assert.Equal("Invalid Chain ID", chainIdException.Message);
}

[Fact]
public async Task TestBundleIdRpc()
{
var client = new ThirdwebClient(clientId: _clientIdBundleIdOnly, bundleId: _bundleIdBundleIdOnly);
var rpc = ThirdwebRPC.GetRpcInstance(client, 1);
var blockNumber = await rpc.SendRequestAsync<string>("eth_blockNumber");
Assert.NotNull(blockNumber);
Assert.StartsWith("0x", blockNumber);
}

[Fact]
public async Task TestRpcError()
{
var client = new ThirdwebClient(secretKey: _secretKey);
var rpc = ThirdwebRPC.GetRpcInstance(client, 1);
var exception = await Assert.ThrowsAsync<Exception>(async () => await rpc.SendRequestAsync<string>("eth_invalidMethod"));
Assert.Contains("RPC Error for request", exception.Message);
}
}
3 changes: 0 additions & 3 deletions Thirdweb/Thirdweb.RPC/RpcError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ public class RpcError

[JsonProperty("message")]
public string Message { get; set; }

[JsonProperty("data")]
public object Data { get; set; }
}
}
28 changes: 0 additions & 28 deletions Thirdweb/Thirdweb.RPC/RpcTimer.cs

This file was deleted.

42 changes: 21 additions & 21 deletions Thirdweb/Thirdweb.RPC/ThirdwebRPC.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Numerics;
using System.Numerics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Thirdweb
Expand All @@ -31,6 +26,16 @@ public class ThirdwebRPC

public static ThirdwebRPC GetRpcInstance(ThirdwebClient client, BigInteger chainId)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}

if (chainId == 0)
{
throw new ArgumentException("Invalid Chain ID");
}

var key = $"{client.ClientId}_{chainId}_{client.FetchTimeoutOptions.GetTimeout(TimeoutType.Rpc)}";

if (!_rpcs.ContainsKey(key))
Expand Down Expand Up @@ -99,11 +104,6 @@ static ThirdwebRPC()

private ThirdwebRPC(ThirdwebClient client, BigInteger chainId)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (chainId == 0)
throw new ArgumentException("Chain ID must be provided");

_clientId = client.ClientId;
_secretKey = client.SecretKey;
_bundleId = client.BundleId;
Expand All @@ -116,7 +116,9 @@ private ThirdwebRPC(ThirdwebClient client, BigInteger chainId)
private void SendBatchNow()
{
if (_pendingBatch.Count == 0)
{
return;
}

List<RpcRequest> batchToSend;
lock (_pendingBatch)
Expand All @@ -134,11 +136,19 @@ private async Task SendBatchAsync(List<RpcRequest> batch)

var requestMessage = new HttpRequestMessage(HttpMethod.Post, _rpcUrl) { Content = new StringContent(batchJson, Encoding.UTF8, "application/json") };
if (!string.IsNullOrEmpty(_clientId))
{
requestMessage.Headers.Add("x-client-id", _clientId);
}

if (!string.IsNullOrEmpty(_secretKey))
{
requestMessage.Headers.Add("x-secret-key", _secretKey);
}

if (!string.IsNullOrEmpty(_bundleId))
{
requestMessage.Headers.Add("x-bundle-id", _bundleId);
}

try
{
Expand All @@ -154,11 +164,6 @@ private async Task SendBatchAsync(List<RpcRequest> batch)
var responseJson = await response.Content.ReadAsStringAsync();
var responses = JsonConvert.DeserializeObject<List<RpcResponse<object>>>(responseJson);

if (responses == null)
{
throw new InvalidOperationException("Failed to deserialize RPC response.");
}

foreach (var rpcResponse in responses)
{
if (_responseCompletionSources.TryGetValue(rpcResponse.Id, out var tcs))
Expand Down Expand Up @@ -197,10 +202,5 @@ private async Task SendBatchAsync(List<RpcRequest> batch)
}
}
}

public void Dispose()
{
_batchSendTimer.Dispose();
}
}
}
Loading