Skip to content

Commit

Permalink
test: Add BlockChainServiceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Sep 5, 2024
1 parent 1fa671b commit e073ae8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 57 deletions.
64 changes: 7 additions & 57 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,28 +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 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);
var blockChainService = new BlockChainService(
genesisOptions: genesisOptions,
storeService: storeService,
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 @@ -44,42 +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>();
services.AddSingleton<StoreService>();
services.AddSingleton(s => (IStoreService)s.GetRequiredService<StoreService>());
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);
}
}
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));
}
}

0 comments on commit e073ae8

Please sign in to comment.