Skip to content
This repository has been archived by the owner on Nov 25, 2023. It is now read-only.

Commit

Permalink
reuse the rpc client
Browse files Browse the repository at this point in the history
  • Loading branch information
dkackman committed Nov 19, 2023
1 parent 5d74228 commit c73745a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 57 deletions.
52 changes: 52 additions & 0 deletions Web2Gateway/ChiaConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using chia.dotnet;

namespace Web2Gateway;

/// <summary>
/// Provides methods for interacting with the Chia blockchain.
/// </summary>
public sealed class ChiaConfig
{
private readonly IConfiguration _configuration;
private readonly ILogger<ChiaService> _logger;

public ChiaConfig(ILogger<ChiaService> logger, IConfiguration configuration) =>
(_logger, _configuration) = (logger, configuration);

private Config GetConfig()
{
// first see if we have a config file path in the appsettings.json
var configPath = _configuration.GetValue("App:chia_config_path", "");
if (!string.IsNullOrEmpty(configPath))
{
return Config.Open(configPath);
}

// if not use the chia default '~/.chia/mainnet/config/config.yaml'
return Config.Open();
}

public EndpointInfo GetDataLayerEndpoint()
{
// first check user secrets for the data_layer connection
// https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-7.0&tabs=windows
var dataLayerUri = _configuration.GetValue("data_layer_uri", "")!;
if (!string.IsNullOrEmpty(dataLayerUri))
{
_logger.LogInformation($"Connecting to {dataLayerUri}");
return new EndpointInfo()
{
Uri = new Uri(dataLayerUri),
// when stored in an environment variable the newlines might be escaped
Cert = _configuration.GetValue("data_layer_cert", "")!.Replace("\\n", "\n"),
Key = _configuration.GetValue("data_layer_key", "")!.Replace("\\n", "\n")
};
}
else
{
// if not present see if we can get it from the config file
return GetConfig().GetEndpoint("data_layer");
}
}
}

60 changes: 4 additions & 56 deletions Web2Gateway/ChiaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,18 @@ namespace Web2Gateway;
/// </summary>
public sealed class ChiaService
{
private readonly object _syncLock = new();
private EndpointInfo? _endpointInfo = null!;
private readonly HttpRpcClient _rpcClient;
private readonly ILogger<ChiaService> _logger;
private readonly IConfiguration _configuration;

public ChiaService(ILogger<ChiaService> logger, IConfiguration configuration) =>
(_logger, _configuration) = (logger, configuration);

private Config GetConfig()
{
// first see if we have a config file path in the appsettings.json
var configPath = _configuration.GetValue("App:chia_config_path", "");
if (!string.IsNullOrEmpty(configPath))
{
_logger.LogInformation("Using config file at {Path}", configPath);
return Config.Open(configPath);
}

// if not use the chia default '~/.chia/mainnet/config/config.yaml'
return Config.Open();
}

private EndpointInfo GetDataLayerEndpoint()
{
lock (_syncLock)
{
if (_endpointInfo is not null)
{
return _endpointInfo;
}

// first check user secrets for the data_layer connection
// https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-7.0&tabs=windows
var dataLayerUri = _configuration.GetValue("data_layer_uri", "")!;
if (!string.IsNullOrEmpty(dataLayerUri))
{
_endpointInfo = new EndpointInfo()
{
Uri = new Uri(dataLayerUri),
// when stored in an environment variable the newlines might be escaped
Cert = _configuration.GetValue("data_layer_cert", "")!.Replace("\\n", "\n"),
Key = _configuration.GetValue("data_layer_key", "")!.Replace("\\n", "\n")
};
}
else
{
// if not present see if we can get it from the config file
_endpointInfo = GetConfig().GetEndpoint("data_layer");
}

return _endpointInfo;
}
}
public ChiaService(HttpRpcClient rpcClient, ILogger<ChiaService> logger, IConfiguration configuration) =>
(_rpcClient, _logger, _configuration) = (rpcClient, logger, configuration);

public DataLayerProxy? GetDataLayer(CancellationToken stoppingToken)
{
try
{
var endpoint = GetDataLayerEndpoint();

_logger.LogInformation("Connecting to data layer at {Uri}", endpoint.Uri);
var rpcClient = new HttpRpcClient(endpoint);

return new DataLayerProxy(rpcClient, "DlMirrorSync");
return new DataLayerProxy(_rpcClient, "DlMirrorSync");
}
catch (Exception ex)
{
Expand Down
5 changes: 4 additions & 1 deletion Web2Gateway/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Web2Gateway;
using chia.dotnet;

// doing all of this in the mini-api expressjs-like approach
// instead of the IActionResult approach
Expand Down Expand Up @@ -28,7 +29,9 @@

builder.Services.AddControllers();

builder.Services.AddSingleton<ChiaService>()
builder.Services.AddSingleton<ChiaConfig>()
.AddSingleton((provider) => new HttpRpcClient(provider.GetService<ChiaConfig>()!.GetDataLayerEndpoint()))
.AddSingleton<ChiaService>()
.AddSingleton<G2To3Service>()
.AddEndpointsApiExplorer()
.AddSwaggerGen()
Expand Down

0 comments on commit c73745a

Please sign in to comment.