Skip to content

Commit

Permalink
Merge pull request #3940 from planetarium/exp/sdk/fix-swarm-service
Browse files Browse the repository at this point in the history
SwarmService instance was created twice
  • Loading branch information
s2quake authored Sep 5, 2024
2 parents 8fe6844 + 801224b commit 2e79a3c
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using Libplanet.Blockchain;
using Libplanet.Explorer.Indexing;
using Libplanet.Explorer.Interfaces;
using Libplanet.Net;
Expand All @@ -12,7 +13,7 @@ internal sealed class BlockChainContext(
{
public bool Preloaded => false;

public Blockchain.BlockChain BlockChain => blockChainService.BlockChain;
public BlockChain BlockChain => blockChainService.BlockChain;

#pragma warning disable S3011 // Reflection should not be used to increase accessibility ...
public IStore Store
Expand Down
2 changes: 1 addition & 1 deletion sdk/node/Libplanet.Node.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Communication with gRPC endpoints must be made through a gRPC client. To learn h
""";
using var app = builder.Build();

app.MapGrpcService<BlockChainGrpcServiceV1>();
app.MapGrpcService<BlockchainGrpcServiceV1>();
app.MapGrpcService<SchemaGrpcServiceV1>();
app.MapGet("/", () => handlerMessage);
if (builder.Environment.IsDevelopment())
Expand Down
2 changes: 1 addition & 1 deletion sdk/node/Libplanet.Node.Executable/Protos/blockchain.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ option csharp_namespace = "Libplanet.Node.API";

package node.blockchain.v1;

service BlockChain {
service Blockchain {
rpc GetGenesisBlock (GetGenesisBlockRequest) returns (GetGenesisBlockReply);
rpc GetTip(Empty) returns (GetTipReply);
rpc GetBlock(GetBlockRequest) returns (GetBlockReply);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Libplanet.Node.API.Services;

public class BlockChainGrpcServiceV1(IReadChainService blockChain) : BlockChain.BlockChainBase
public class BlockchainGrpcServiceV1(IReadChainService blockChain) : Blockchain.BlockchainBase
{
private readonly IReadChainService _blockChain = blockChain;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Libplanet.Node.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Libplanet.Node.Extensions.NodeBuilder;

Expand All @@ -25,8 +26,9 @@ public ILibplanetNodeBuilder WithSolo()

public ILibplanetNodeBuilder WithSwarm()
{
Services.AddSingleton<ISwarmService, SwarmService>();
Services.AddHostedService<SwarmService>();
Services.AddSingleton<SwarmService>();
Services.AddSingleton(s => (ISwarmService)s.GetRequiredService<SwarmService>());
Services.AddSingleton(s => (IHostedService)s.GetRequiredService<SwarmService>());
_scopeList.Add("Swarm");
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/node/Libplanet.Node/Options/StoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class StoreOptions : OptionsBase<StoreOptions>
/// The type of the store.
/// </summary>
[Description("The type of the store.")]
public StoreType Type { get; set; } = StoreType.Memory;
public StoreType Type { get; set; } = StoreType.InMemory;

/// <summary>
/// The root directory path of the store.
Expand Down
8 changes: 4 additions & 4 deletions sdk/node/Libplanet.Node/Options/StoreOptionsConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ internal sealed class StoreOptionsConfigurator(ILogger<StoreOptionsConfigurator>
{
protected override void OnConfigure(StoreOptions options)
{
if (options.Type == StoreType.Memory)
if (options.Type == StoreType.InMemory)
{
if (options.RootPath != string.Empty)
{
options.RootPath = string.Empty;
logger.LogWarning(
"RootPath is ignored because StoreType is {Memory}.", StoreType.Memory);
"RootPath is ignored because StoreType is {Memory}.", StoreType.InMemory);
}

if (options.StoreName != string.Empty)
{
options.StoreName = string.Empty;
logger.LogWarning(
"StorePath is ignored because StoreType is {Memory}.", StoreType.Memory);
"StorePath is ignored because StoreType is {Memory}.", StoreType.InMemory);
}

if (options.StateStoreName != string.Empty)
{
options.StateStoreName = string.Empty;
logger.LogWarning(
"StateStorePath is ignored because StoreType is {Memory}.", StoreType.Memory);
"StateStorePath is ignored because StoreType is {Memory}.", StoreType.InMemory);
}
}
else
Expand Down
6 changes: 2 additions & 4 deletions sdk/node/Libplanet.Node/Options/StoreType.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System.ComponentModel;

namespace Libplanet.Node.Options;

public enum StoreType
{
/// <summary>
/// Store data in disk.
/// </summary>
Disk,
RocksDB,

/// <summary>
/// Store data in memory.
/// </summary>
Memory,
InMemory,
}
4 changes: 2 additions & 2 deletions sdk/node/Libplanet.Node/Services/BlockChainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ private static (IStore, IStateStore) CreateStore(StoreOptions storeOptions)
{
return storeOptions.Type switch
{
StoreType.Disk => CreateDiskStore(),
StoreType.Memory => CreateMemoryStore(),
StoreType.RocksDB => CreateDiskStore(),
StoreType.InMemory => CreateMemoryStore(),
_ => throw new NotSupportedException($"Unsupported store type: {storeOptions.Type}"),
};

Expand Down

0 comments on commit 2e79a3c

Please sign in to comment.