-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3946 from planetarium/exp/sdk/refactor-service
Add StoreService and Test codes for each service
- Loading branch information
Showing
17 changed files
with
367 additions
and
195 deletions.
There are no files selected for viewing
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,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; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
42 changes: 42 additions & 0 deletions
42
sdk/node/Libplanet.Node.Tests/Services/ReadChainServiceTest.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,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
38
sdk/node/Libplanet.Node.Tests/Services/StoreServiceTest.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 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); | ||
} | ||
} |
Oops, something went wrong.