Skip to content

Commit

Permalink
feat: Add IStoreService
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Aug 30, 2024
1 parent 318173c commit e1eb5cd
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static ILibplanetNodeBuilder AddLibplanetNode(
services.AddSingleton<IConfigureOptions<SoloOptions>, SoloOptionsConfigurator>();

services.AddSingleton<PolicyService>();
services.AddSingleton<StoreService>();
services.AddSingleton(s => (IStoreService)s.GetRequiredService<StoreService>());
services.AddSingleton<IBlockChainService, BlockChainService>();
services.AddSingleton<IReadChainService, ReadChainService>();
services.AddSingleton<TransactionService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ public void Create_Test()

var serviceProvider = services.BuildServiceProvider();
var policyService = new PolicyService();
var logger = new NullLoggerFactory().CreateLogger<BlockChainService>();
var loggerFactory = new NullLoggerFactory();
var logger = loggerFactory.CreateLogger<BlockChainService>();
var genesisOptions = serviceProvider.GetRequiredService<IOptions<GenesisOptions>>();
var storeOptions = serviceProvider.GetRequiredService<IOptions<StoreOptions>>();
var storeService = new StoreService(
storeOptions, loggerFactory.CreateLogger<StoreService>());
var blockChainService = new BlockChainService(
genesisOptions: genesisOptions,
storeOptions: storeOptions,
storeService: storeService,
policyService: policyService,
actionLoaderProviders: [],
logger: logger);
Expand Down
9 changes: 5 additions & 4 deletions sdk/node/Libplanet.Node/Services/BlockChainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal sealed class BlockChainService : IBlockChainService, IActionRenderer

public BlockChainService(
IOptions<GenesisOptions> genesisOptions,
IOptions<StoreOptions> storeOptions,
IStoreService storeService,
PolicyService policyService,
IEnumerable<IActionLoaderProvider> actionLoaderProviders,
ILogger<BlockChainService> logger)
Expand All @@ -45,7 +45,8 @@ public BlockChainService(
_logger = logger;
_blockChain = CreateBlockChain(
genesisOptions: genesisOptions.Value,
storeOptions: storeOptions.Value,
store: storeService.Store,
stateStore: storeService.StateStore,
stagePolicy: policyService.StagePolicy,
renderers: [this],
actionLoaders: [.. actionLoaderProviders.Select(item => item.GetActionLoader())]);
Expand Down Expand Up @@ -91,12 +92,12 @@ void Action(object? state)

private static BlockChain CreateBlockChain(
GenesisOptions genesisOptions,
StoreOptions storeOptions,
IStore store,
IStateStore stateStore,
IStagePolicy stagePolicy,
IRenderer[] renderers,
IActionLoader[] actionLoaders)
{
var (store, stateStore) = CreateStore(storeOptions);
var actionLoader = new AggregateTypedActionLoader(actionLoaders);
var actionEvaluator = new ActionEvaluator(
policyActionsRegistry: new(),
Expand Down
10 changes: 10 additions & 0 deletions sdk/node/Libplanet.Node/Services/IStoreService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Libplanet.Store;

namespace Libplanet.Node.Services;

public interface IStoreService
{
IStore Store { get; }

IStateStore StateStore { get; }
}
50 changes: 50 additions & 0 deletions sdk/node/Libplanet.Node/Services/StoreService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Libplanet.Node.Options;
using Libplanet.RocksDBStore;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Libplanet.Node.Services;

internal sealed class StoreService : IStoreService
{
public StoreService(
IOptions<StoreOptions> storeOptions,
ILogger<StoreService> logger)
{
(Store, KeyValueStore) = CreateStore(storeOptions.Value);
StateStore = new TrieStateStore(KeyValueStore);
}

public IStore Store { get; }

public IKeyValueStore KeyValueStore { get; }

public IStateStore StateStore { get; }

private static (IStore, IKeyValueStore) CreateStore(StoreOptions storeOptions)
{
return storeOptions.Type switch
{
StoreType.RocksDB => CreateRocksDBStore(storeOptions),
StoreType.InMemory => CreateMemoryStore(),
_ => throw new NotSupportedException($"Unsupported store type: {storeOptions.Type}"),
};
}

private static (MemoryStore, MemoryKeyValueStore) CreateMemoryStore()
{
var store = new MemoryStore();
var keyValueStore = new MemoryKeyValueStore();
return (store, keyValueStore);
}

private static (RocksDBStore.RocksDBStore, RocksDBKeyValueStore) CreateRocksDBStore(
StoreOptions storeOptions)
{
var store = new RocksDBStore.RocksDBStore(storeOptions.StoreName);
var keyValueStore = new RocksDBKeyValueStore(storeOptions.StateStoreName);
return (store, keyValueStore);
}
}

0 comments on commit e1eb5cd

Please sign in to comment.