Skip to content

Commit

Permalink
test: add unit test for newly added feature
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Dec 30, 2024
1 parent b740696 commit 557e951
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions sdk/node/Libplanet.Node.Tests/Services/BlockChainServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
using Bencodex;
using Bencodex.Types;
using Libplanet.Common;
using Libplanet.Crypto;
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;

namespace Libplanet.Node.Tests.Services;

Expand All @@ -16,4 +22,70 @@ public void Create_Test()

Assert.Equal(1, blockChain.Count);
}

[Fact]
public void Create_Using_Genesis_Configuration_Test()
{
var genesisKey = new PrivateKey();
string tempDirectory = Path.GetTempPath();
string tempFilePath = Path.Combine(tempDirectory, Guid.NewGuid().ToString() + ".json");
Address accountA = new("0000000000000000000000000000000000000000");
Address accountB = new("0000000000000000000000000000000000000001");
Address addressA = new("0000000000000000000000000000000000000000");
Address addressB = new("0000000000000000000000000000000000000001");
var codec = new Codec();

try
{
string jsonContent = $@"
{{
""{accountA}"": {{
""{addressA}"": ""{ByteUtil.Hex(codec.Encode((Text)"A"))}"",
""{addressB}"": ""{ByteUtil.Hex(codec.Encode((Integer)123))}""
}},
""{accountB}"": {{
""{addressA}"": ""{ByteUtil.Hex(codec.Encode((Text)"B"))}"",
""{addressB}"": ""{ByteUtil.Hex(codec.Encode((Integer)456))}""
}}
}}";
File.WriteAllText(tempFilePath, jsonContent);
var configDict = new Dictionary<string, string>
{
{ "Genesis:GenesisConfigurationPath", tempFilePath },
{ "Genesis:GenesisKey", ByteUtil.Hex(genesisKey.ToByteArray()) },
};

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(configDict!)
.Build();

var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory, NullLoggerFactory>();
services.AddLogging();
services.AddLibplanetNode(configuration);
var serviceProvider = services.BuildServiceProvider();
var blockChainService = serviceProvider.GetRequiredService<IBlockChainService>();
var blockChain = blockChainService.BlockChain;

Assert.Equal(
(Text)"A",
blockChain.GetNextWorldState()?.GetAccountState(accountA).GetState(addressA));
Assert.Equal(
(Integer)123,
blockChain.GetNextWorldState()?.GetAccountState(accountA).GetState(addressB));
Assert.Equal(
(Text)"B",
blockChain.GetNextWorldState()?.GetAccountState(accountB).GetState(addressA));
Assert.Equal(
(Integer)456,
blockChain.GetNextWorldState()?.GetAccountState(accountB).GetState(addressB));
}
finally
{
if (File.Exists(tempFilePath))
{
File.Delete(tempFilePath);
}
}
}
}

0 comments on commit 557e951

Please sign in to comment.