From 557e951d598137f08497e64b315864f439cc67bc Mon Sep 17 00:00:00 2001 From: Chanhyuck Ko Date: Mon, 30 Dec 2024 20:38:47 +0900 Subject: [PATCH] test: add unit test for newly added feature --- .../Services/BlockChainServiceTest.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/sdk/node/Libplanet.Node.Tests/Services/BlockChainServiceTest.cs b/sdk/node/Libplanet.Node.Tests/Services/BlockChainServiceTest.cs index 7e3a67e8316..6a93d1224c4 100644 --- a/sdk/node/Libplanet.Node.Tests/Services/BlockChainServiceTest.cs +++ b/sdk/node/Libplanet.Node.Tests/Services/BlockChainServiceTest.cs @@ -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; @@ -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 + { + { "Genesis:GenesisConfigurationPath", tempFilePath }, + { "Genesis:GenesisKey", ByteUtil.Hex(genesisKey.ToByteArray()) }, + }; + + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(configDict!) + .Build(); + + var services = new ServiceCollection(); + services.AddSingleton(); + services.AddLogging(); + services.AddLibplanetNode(configuration); + var serviceProvider = services.BuildServiceProvider(); + var blockChainService = serviceProvider.GetRequiredService(); + 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); + } + } + } }