Skip to content

Commit

Permalink
Guard against initial failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Smrecz committed Apr 11, 2023
1 parent c9b656b commit d2671b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
21 changes: 18 additions & 3 deletions Tatum.Examples/Rpc/Examples/RpcGetBlockCount.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Tatum.Core.Models;
using Tatum.Rpc;
using Tatum.Rpc.Models;
using Xunit;

Expand Down Expand Up @@ -46,6 +43,24 @@ public async Task GetBlockCount_Example_Typed()
Assert.True(result.Value.Result > 0);
}

[Fact]
public async Task GetBlockCount_Example_WithParams()
{
var tatumSdk = await TatumSdk.InitAsync(config => config.EnableDebugMode = true);

var rpcCall = new JsonRpcCall
{
Id = "1",
JsonRpc = "2.0",
Method = "eth_getBlockByNumber",
Params = new object[] {"latest", true}
};

var result = await tatumSdk.Rpc.Ethereum.Call(rpcCall);

Assert.True(result.Success);
}

private class StatusResponse
{
[JsonPropertyName("result")] public int Result { get; set; }
Expand Down
15 changes: 13 additions & 2 deletions Tatum.Rpc/TatumRpcChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,24 @@ public async Task<Result<T>> Call<T>(JsonRpcCall request)
}
catch
{
if (_nodes.Count == 0)
throw;
if (_nodes.Count == 0)
{
if (_initialised)
{
throw;
}

await InitialiseInternal();
_initialised = true;
return await Call<T>(request);
}

_nodes.Remove(_activeNode);

if (_nodes.Count == 0)
{
throw new TatumOpenRpcException($"All nodes for {_chain} are down");
}

_activeNode = _nodes.Min;

Expand Down

0 comments on commit d2671b4

Please sign in to comment.