Skip to content

Commit

Permalink
Merge pull request #3946 from planetarium/exp/sdk/refactor-service
Browse files Browse the repository at this point in the history
Add StoreService and Test codes for each service
  • Loading branch information
s2quake authored Sep 5, 2024
2 parents 2e79a3c + 6be4104 commit cdd701e
Show file tree
Hide file tree
Showing 17 changed files with 367 additions and 195 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
33 changes: 33 additions & 0 deletions sdk/node/Libplanet.Node.Tests/BlockChainUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Libplanet.Blockchain;
using Libplanet.Crypto;
using Libplanet.Types.Blocks;

namespace Libplanet.Node.Tests;

internal static class BlockChainUtility
{
public static Task<Block> AppendBlockAsync(BlockChain blockChain)
=> AppendBlockAsync(blockChain, new PrivateKey());

public static async Task<Block> AppendBlockAsync(BlockChain blockChain, PrivateKey privateKey)
{
var tip = blockChain.Tip;
var height = tip.Index + 1;
var block = blockChain.ProposeBlock(
privateKey,
blockChain.GetBlockCommit(tip.Hash));
blockChain.Append(
block,
blockChain.GetBlockCommit(tip.Hash),
validate: false);

while (blockChain.Tip.Index < height)
{
await Task.Delay(100);
}

await Task.Delay(1000);

return block;
}
}
1 change: 1 addition & 0 deletions sdk/node/Libplanet.Node.Tests/Libplanet.Node.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<ItemGroup>
<ProjectReference Include="..\Libplanet.Node\Libplanet.Node.csproj" />
<ProjectReference Include="..\Libplanet.Node.Extensions\Libplanet.Node.Extensions.csproj" />
</ItemGroup>

</Project>
60 changes: 7 additions & 53 deletions sdk/node/Libplanet.Node.Tests/Services/BlockChainServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using Libplanet.Blockchain;
using Libplanet.Crypto;
using Libplanet.Node.Options;
using Libplanet.Node.Extensions;
using Libplanet.Node.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;

namespace Libplanet.Node.Tests.Services;

Expand All @@ -14,26 +10,8 @@ public class BlockChainServiceTest
[Fact]
public void Create_Test()
{
var services = new ServiceCollection();
services.AddLogging(configure => configure.AddProvider(NullLoggerProvider.Instance));
services.AddOptions<GenesisOptions>();
services.AddSingleton<IConfigureOptions<GenesisOptions>, GenesisOptionsConfigurator>();
services.AddOptions<StoreOptions>();
services.AddSingleton<IConfigureOptions<StoreOptions>, StoreOptionsConfigurator>();
services.AddOptions<SwarmOptions>();
services.AddSingleton<IConfigureOptions<SwarmOptions>, SwarmOptionsConfigurator>();

var serviceProvider = services.BuildServiceProvider();
var policyService = new PolicyService();
var logger = new NullLoggerFactory().CreateLogger<BlockChainService>();
var genesisOptions = serviceProvider.GetRequiredService<IOptions<GenesisOptions>>();
var storeOptions = serviceProvider.GetRequiredService<IOptions<StoreOptions>>();
var blockChainService = new BlockChainService(
genesisOptions: genesisOptions,
storeOptions: storeOptions,
policyService: policyService,
actionLoaderProviders: [],
logger: logger);
var serviceProvider = TestUtility.CreateServiceProvider();
var blockChainService = serviceProvider.GetRequiredService<IBlockChainService>();
var blockChain = blockChainService.BlockChain;

Assert.Equal(1, blockChain.Count);
Expand All @@ -42,40 +20,16 @@ public void Create_Test()
[Fact]
public async Task BlockAppended_TestAsync()
{
var services = new ServiceCollection();
services.AddLogging(configure => configure.AddProvider(NullLoggerProvider.Instance));
services.AddOptions<GenesisOptions>();
services.AddSingleton<IConfigureOptions<GenesisOptions>, GenesisOptionsConfigurator>();
services.AddOptions<StoreOptions>();
services.AddSingleton<IConfigureOptions<StoreOptions>, StoreOptionsConfigurator>();
services.AddOptions<SwarmOptions>();
services.AddSingleton<IConfigureOptions<SwarmOptions>, SwarmOptionsConfigurator>();
services.AddSingleton<PolicyService>();
services.AddSingleton<BlockChainService>();
var serviceProvider = services.BuildServiceProvider();
var blockChainService = serviceProvider.GetRequiredService<BlockChainService>();
var serviceProvider = TestUtility.CreateServiceProvider();
var blockChainService = serviceProvider.GetRequiredService<IBlockChainService>();
var blockChain = blockChainService.BlockChain;

var args = await Assert.RaisesAsync<BlockEventArgs>(
handler => blockChainService.BlockAppended += handler,
handler => blockChainService.BlockAppended -= handler,
async () => await AppendBlockAsync(new PrivateKey(), blockChain));
async () => await BlockChainUtility.AppendBlockAsync(blockChain));

Assert.Equal(args.Arguments.Block, blockChain.Tip);
Assert.Equal(2, blockChain.Count);
}

private static async Task AppendBlockAsync(PrivateKey privateKey, BlockChain blockChain)
{
var tip = blockChain.Tip;
var block = blockChain.ProposeBlock(
privateKey,
blockChain.GetBlockCommit(tip.Hash));
blockChain.Append(
block,
blockChain.GetBlockCommit(tip.Hash),
validate: false);

await Task.Delay(1000);
}
}
87 changes: 0 additions & 87 deletions sdk/node/Libplanet.Node.Tests/Services/NodeServiceTest.cs

This file was deleted.

1 change: 0 additions & 1 deletion sdk/node/Libplanet.Node.Tests/Services/PeerTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Libplanet.Net;
using Libplanet.Net.Messages;
using Libplanet.Net.Transports;
using Libplanet.Node.Options;
using Libplanet.Node.Services;
using Moq;

Expand Down
42 changes: 42 additions & 0 deletions sdk/node/Libplanet.Node.Tests/Services/ReadChainServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Libplanet.Node.Services;
using Microsoft.Extensions.DependencyInjection;

namespace Libplanet.Node.Tests.Services;

public class ReadChainServiceTest
{
[Fact]
public void Tip_Test()
{
var serviceProvider = TestUtility.CreateServiceProvider();
var readChainService = serviceProvider.GetRequiredService<IReadChainService>();
var expectedBlock = readChainService.GetBlock(0);

Assert.Equal(expectedBlock, readChainService.Tip);
}

[Fact]
public async Task GetBlock_WithHash_TestAsync()
{
var serviceProvider = TestUtility.CreateServiceProvider();
var blockChainService = serviceProvider.GetRequiredService<IBlockChainService>();
var blockChain = blockChainService.BlockChain;
var readChainService = serviceProvider.GetRequiredService<IReadChainService>();
var expectedBlock = await BlockChainUtility.AppendBlockAsync(blockChain);

Assert.Equal(expectedBlock, readChainService.GetBlock(expectedBlock.Hash));
}

[Fact]
public async Task GetBlock_WithHeight_TestAsync()
{
var serviceProvider = TestUtility.CreateServiceProvider();
var blockChainService = serviceProvider.GetRequiredService<IBlockChainService>();
var blockChain = blockChainService.BlockChain;
var readChainService = serviceProvider.GetRequiredService<IReadChainService>();
var height = blockChain.Count;
var expectedBlock = await BlockChainUtility.AppendBlockAsync(blockChain);

Assert.Equal(expectedBlock, readChainService.GetBlock(height));
}
}
38 changes: 38 additions & 0 deletions sdk/node/Libplanet.Node.Tests/Services/StoreServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Libplanet.Node.Options;
using Libplanet.Node.Services;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Microsoft.Extensions.DependencyInjection;

namespace Libplanet.Node.Tests.Services;

public class StoreServiceTest
{
[Fact]
public void RocksDB_Test()
{
var settings = new Dictionary<string, string?>
{
[$"{StoreOptions.Position}:{nameof(StoreOptions.Type)}"] = $"{StoreType.RocksDB}",
};
var serviceProvider = TestUtility.CreateServiceProvider(settings);
var storeService = serviceProvider.GetRequiredService<IStoreService>();

Assert.IsType<RocksDBStore.RocksDBStore>(storeService.Store);
Assert.IsType<RocksDBStore.RocksDBKeyValueStore>(storeService.KeyValueStore);
}

[Fact]
public void InMemory_Test()
{
var settings = new Dictionary<string, string?>
{
[$"{StoreOptions.Position}:{nameof(StoreOptions.Type)}"] = $"{StoreType.InMemory}",
};
var serviceProvider = TestUtility.CreateServiceProvider(settings);
var storeService = serviceProvider.GetRequiredService<IStoreService>();

Assert.IsType<MemoryStore>(storeService.Store);
Assert.IsType<MemoryKeyValueStore>(storeService.KeyValueStore);
}
}
Loading

0 comments on commit cdd701e

Please sign in to comment.