forked from planetarium/libplanet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
387 additions
and
233 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
sdk/node/Libplanet.Node.Executable/Explorer/BlockChainContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Reflection; | ||
using Libplanet.Explorer.Indexing; | ||
using Libplanet.Explorer.Interfaces; | ||
using Libplanet.Net; | ||
using Libplanet.Node.Services; | ||
using Libplanet.Store; | ||
|
||
namespace Libplanet.Node.API.Explorer; | ||
|
||
internal sealed class BlockChainContext( | ||
IBlockChainService blockChainService, ISwarmService swarmService) : IBlockChainContext | ||
{ | ||
public bool Preloaded => false; | ||
|
||
public Blockchain.BlockChain BlockChain => blockChainService.BlockChain; | ||
|
||
#pragma warning disable S3011 // Reflection should not be used to increase accessibility ... | ||
public IStore Store | ||
{ | ||
get | ||
{ | ||
var bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance; | ||
var propertyInfo = typeof(BlockChain).GetProperty("Store", bindingFlags) ?? | ||
throw new InvalidOperationException("Store property not found."); | ||
if (propertyInfo.GetValue(BlockChain) is IStore store) | ||
{ | ||
return store; | ||
} | ||
|
||
throw new InvalidOperationException("Store property is not IStore."); | ||
} | ||
} | ||
#pragma warning restore S3011 | ||
|
||
public Swarm Swarm => swarmService.Swarm; | ||
|
||
public IBlockChainIndex Index => throw new NotSupportedException(); | ||
} |
35 changes: 35 additions & 0 deletions
35
sdk/node/Libplanet.Node.Executable/Explorer/ExplorerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Libplanet.Explorer; | ||
using Libplanet.Node.Extensions; | ||
|
||
namespace Libplanet.Node.API.Explorer; | ||
|
||
public static class ExplorerExtensions | ||
{ | ||
public static IServiceCollection AddNodeExplorer( | ||
this IServiceCollection services) | ||
{ | ||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
services.AddSingleton<BlockChainContext>(); | ||
services.AddSingleton<ExplorerStartup<BlockChainContext>>(); | ||
serviceProvider = services.BuildServiceProvider(); | ||
var startUp | ||
= serviceProvider.GetRequiredService<ExplorerStartup<BlockChainContext>>(); | ||
startUp.ConfigureServices(services); | ||
|
||
return services; | ||
} | ||
|
||
public static IApplicationBuilder UseNodeExplorer(this IApplicationBuilder builder) | ||
{ | ||
var serviceProvider = builder.ApplicationServices; | ||
var environment = serviceProvider.GetRequiredService<IWebHostEnvironment>(); | ||
var startUp = serviceProvider.GetService<ExplorerStartup<BlockChainContext>>(); | ||
startUp?.Configure(builder, environment); | ||
|
||
return builder; | ||
} | ||
|
||
public static bool IsExplorerEnabled(this IHostApplicationBuilder builder) | ||
=> builder.Configuration.IsOptionsEnabled(ExplorerOptions.Position); | ||
} |
13 changes: 13 additions & 0 deletions
13
sdk/node/Libplanet.Node.Executable/Explorer/ExplorerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.ComponentModel; | ||
using Libplanet.Node.Options; | ||
|
||
namespace Libplanet.Node.API.Explorer; | ||
|
||
[Options(Position)] | ||
public sealed class ExplorerOptions : OptionsBase<ExplorerOptions>, IEnabledOptions | ||
{ | ||
public const string Position = "Explorer"; | ||
|
||
[DefaultValue(true)] | ||
public bool IsEnabled { get; set; } = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,59 @@ | ||
using Libplanet.Node.API.Explorer; | ||
using Libplanet.Node.API.Services; | ||
using Libplanet.Node.Extensions; | ||
using Libplanet.Node.Options.Schema; | ||
using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
|
||
SynchronizationContext.SetSynchronizationContext(new()); | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Logging.AddConsole(); | ||
|
||
if (builder.Environment.IsDevelopment()) | ||
{ | ||
builder.WebHost.ConfigureKestrel(options => | ||
{ | ||
// Setup a HTTP/2 endpoint without TLS. | ||
options.ListenLocalhost(5259, o => o.Protocols = | ||
HttpProtocols.Http1AndHttp2); | ||
options.ListenLocalhost(5260, o => o.Protocols = | ||
HttpProtocols.Http2); | ||
}); | ||
} | ||
|
||
// Additional configuration is required to successfully run gRPC on macOS. | ||
// For instructions on how to configure Kestrel and gRPC clients on macOS, | ||
// visit https://go.microsoft.com/fwlink/?linkid=2099682 | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
builder.Services.AddAuthorization(); | ||
builder.Services.AddAuthentication("Bearer").AddJwtBearer(); | ||
} | ||
|
||
// Add services to the container. | ||
builder.Services.AddGrpc(); | ||
builder.Services.AddGrpcReflection(); | ||
var libplanetBuilder = builder.Services.AddLibplanetNode(builder.Configuration); | ||
builder.Services.AddLibplanetNode(builder.Configuration); | ||
|
||
if (builder.IsExplorerEnabled()) | ||
{ | ||
builder.Services.AddNodeExplorer(); | ||
} | ||
|
||
var app = builder.Build(); | ||
var handlerMessage = """ | ||
Communication with gRPC endpoints must be made through a gRPC client. To learn how to | ||
create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909 | ||
"""; | ||
var schema = await OptionsSchemaBuilder.GetSchemaAsync(default); | ||
using var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
app.MapGrpcService<BlockChainGrpcService>(); | ||
app.MapGrpcService<SchemaGrpcService>(); | ||
app.MapGrpcService<BlockChainGrpcServiceV1>(); | ||
app.MapGrpcService<SchemaGrpcServiceV1>(); | ||
app.MapGet("/", () => handlerMessage); | ||
app.MapGet("/schema", () => schema); | ||
|
||
if (builder.Environment.IsDevelopment()) | ||
{ | ||
app.MapGrpcReflectionService().AllowAnonymous(); | ||
|
||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.MapSchemaBuilder("/v1/schema"); | ||
app.MapGet("/schema", context => Task.Run(() => context.Response.Redirect("/v1/schema"))); | ||
|
||
if (builder.IsExplorerEnabled()) | ||
{ | ||
app.UseNodeExplorer(); | ||
} | ||
|
||
await app.RunAsync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Libplanet.Node.Options; | ||
|
||
public interface IEnabledOptions | ||
{ | ||
bool IsEnabled { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
using Libplanet.Net; | ||
|
||
namespace Libplanet.Node.Services; | ||
|
||
public interface ISwarmService | ||
{ | ||
public event EventHandler? Started; | ||
|
||
public event EventHandler? Stopped; | ||
|
||
Swarm Swarm { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters