From 1af3ca5c6f6ac9553e2cd3a61e6599fcccb75e31 Mon Sep 17 00:00:00 2001 From: Hong Minhee Date: Fri, 5 Apr 2019 18:29:08 +0900 Subject: [PATCH 1/4] Extract Libplanet.Explorer into a separate repo --- .../Controllers/ExplorerController.cs | 158 ------------------ .../GenericControllerFeatureProvider.cs | 46 ----- Libplanet.Explorer/ExplorerStartup.cs | 42 ----- .../Interfaces/IBlockchainStore.cs | 11 -- Libplanet.Explorer/Libplanet.Explorer.csproj | 18 -- Libplanet.Explorer/README.md | 63 ------- .../ViewModels/AddressViewModel.cs | 13 -- .../ViewModels/BlockViewModel.cs | 17 -- .../ViewModels/TransactionViewModel.cs | 17 -- Libplanet.sln | 14 -- 10 files changed, 399 deletions(-) delete mode 100644 Libplanet.Explorer/Controllers/ExplorerController.cs delete mode 100644 Libplanet.Explorer/Controllers/GenericControllerFeatureProvider.cs delete mode 100644 Libplanet.Explorer/ExplorerStartup.cs delete mode 100644 Libplanet.Explorer/Interfaces/IBlockchainStore.cs delete mode 100644 Libplanet.Explorer/Libplanet.Explorer.csproj delete mode 100644 Libplanet.Explorer/README.md delete mode 100644 Libplanet.Explorer/ViewModels/AddressViewModel.cs delete mode 100644 Libplanet.Explorer/ViewModels/BlockViewModel.cs delete mode 100644 Libplanet.Explorer/ViewModels/TransactionViewModel.cs diff --git a/Libplanet.Explorer/Controllers/ExplorerController.cs b/Libplanet.Explorer/Controllers/ExplorerController.cs deleted file mode 100644 index f995e09fdc5..00000000000 --- a/Libplanet.Explorer/Controllers/ExplorerController.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography; -using Libplanet.Action; -using Libplanet.Blockchain; -using Libplanet.Blockchain.Policies; -using Libplanet.Blocks; -using Libplanet.Explorer.Interfaces; -using Libplanet.Explorer.ViewModels; -using Libplanet.Tx; -using Microsoft.AspNetCore.Mvc; - -namespace Libplanet.Explorer.Controllers -{ - [GenericControllerNameConvention] - public class ExplorerController : Controller where T : IAction, new() - { - private readonly IBlockchainStore Store; - private readonly Guid _chainId; - - public string TimestampFormat = "yyyy-MM-ddTHH:mm:ss.ffffffZ"; - - public ExplorerController(IBlockchainStore store) - { - Store = store; - _chainId = store.ChainId; - } - - public BlockChain GetBlockChain() - { - // FIXME: policy should be configurable - var chain = new BlockChain( - new BlockPolicy(), Store.Store, _chainId); - - return chain; - } - - [HttpGet("/blocks/")] - public List> Index() - { - BlockChain chain = GetBlockChain(); - - return chain.Select(block => new Dictionary - { - { "hash", block.Hash.ToString() }, - { "timestamp", block.Timestamp.ToString(TimestampFormat) } - }) - .ToList(); - } - - [HttpGet("/blocks/{hash}/")] - public IActionResult getBlock(string hash) - { - Block block; - HashDigest blockHash; - BlockChain chain = GetBlockChain(); - - try - { - blockHash = HashDigest.FromString(hash); - } - catch (ArgumentException) - { - return BadRequest(new Dictionary - { - { "message", $"\"{hash}\" is not a proper hash." } - }); - } - - try - { - block = chain.Blocks[blockHash]; - } - catch (KeyNotFoundException) - { - return NotFound(new Dictionary - { - { "message", $"block(\"{hash}\") is not found" } - }); - } - - var model = new BlockViewModel - { - Index = block.Index, - Difficulty = block.Difficulty, - Nonce = block.Nonce.ToString(), - PreviousHash = block.PreviousHash.ToString(), - Miner = block.Miner?.ToHex(), - Timestamp = block.Timestamp.ToString(TimestampFormat), - TxIds = (block.Transactions - .OrderByDescending(tx => tx.Timestamp) - .Select(tx => new Dictionary - { - { "id", tx.Id.ToString() }, - { "timestamp", tx.Timestamp.ToString(TimestampFormat) } - })).ToList() - }; - return Ok(model); - } - - [HttpGet("/tx/{txIdString}/")] - public IActionResult getTransaction(string txIdString) - { - Transaction tx; - TxId txId; - BlockChain chain = GetBlockChain(); - - try - { - txId = new TxId(ByteUtil.ParseHex(txIdString)); - } - catch (ArgumentException) - { - return BadRequest(new Dictionary - { - { - "message", - $"\"{txIdString}\" is not a proper transaction id." - } - }); - } - - try - { - tx = chain.Transactions[txId]; - } - catch (KeyNotFoundException) - { - return NotFound(new Dictionary - { - { "message", $"Transaction(\"{txIdString}\") is not found" } - }); - } - - var model = new TransactionViewModel - { - Id = tx.Id.ToString(), - Signature = tx.Signature, - Timestamp = tx.Timestamp, - Signer = tx.Signer.ToHex(), - UpdatedAddresses = tx.UpdatedAddresses - .Select(a => a.ToHex()).ToArray(), - Actions = tx.Actions - .Select(act => new Dictionary - { - { - "type_id", - ActionTypeAttribute.ValueOf(act.GetType()) - }, - - }).ToList() - }; - - return Ok(model); - } - } -} diff --git a/Libplanet.Explorer/Controllers/GenericControllerFeatureProvider.cs b/Libplanet.Explorer/Controllers/GenericControllerFeatureProvider.cs deleted file mode 100644 index 74048d15170..00000000000 --- a/Libplanet.Explorer/Controllers/GenericControllerFeatureProvider.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Microsoft.AspNetCore.Mvc.ApplicationModels; -using Microsoft.AspNetCore.Mvc.ApplicationParts; -using Microsoft.AspNetCore.Mvc.Controllers; - -namespace Libplanet.Explorer.Controllers -{ - public class GenericControllerNameConvention - : Attribute, IControllerModelConvention - { - public void Apply(ControllerModel controller) - { - if (controller.ControllerType.GetGenericTypeDefinition() != - typeof(ExplorerController<>)) - { - return; - } - - var entityType = controller.ControllerType.GenericTypeArguments[0]; - controller.ControllerName = entityType.Name; - } - } - - public class GenericControllerFeatureProvider - : IApplicationFeatureProvider - { - public void PopulateFeature( - IEnumerable parts, ControllerFeature feature) - { - var entityType = typeof(T).GetTypeInfo(); - string typeName = entityType.Name + "Controller"; - - // Check to see if there is a "real" controller for this class - if (feature.Controllers.All(t => t.Name != typeName)) - { - // Create a generic controller for this type - var controllerType = typeof(ExplorerController<>) - .MakeGenericType(entityType).GetTypeInfo(); - feature.Controllers.Add(controllerType); - } - } - } -} diff --git a/Libplanet.Explorer/ExplorerStartup.cs b/Libplanet.Explorer/ExplorerStartup.cs deleted file mode 100644 index 902dac0cf24..00000000000 --- a/Libplanet.Explorer/ExplorerStartup.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Libplanet.Action; -using Libplanet.Explorer.Controllers; -using Libplanet.Explorer.Interfaces; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; - -namespace Libplanet.Explorer -{ - public class ExplorerStartup - where T : IAction - where TU : class, IBlockchainStore - { - public ExplorerStartup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - public void ConfigureServices(IServiceCollection services) - { - - services.AddMvc() - .ConfigureApplicationPartManager(p => - p.FeatureProviders.Add( - new GenericControllerFeatureProvider())); - services.AddSingleton(); - } - - public void Configure(IApplicationBuilder app, IHostingEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - - app.UseMvc(); - } - } -} diff --git a/Libplanet.Explorer/Interfaces/IBlockchainStore.cs b/Libplanet.Explorer/Interfaces/IBlockchainStore.cs deleted file mode 100644 index 0f59750e05e..00000000000 --- a/Libplanet.Explorer/Interfaces/IBlockchainStore.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using Libplanet.Store; - -namespace Libplanet.Explorer.Interfaces -{ - public interface IBlockchainStore - { - IStore Store { get; } - Guid ChainId { get; } - } -} diff --git a/Libplanet.Explorer/Libplanet.Explorer.csproj b/Libplanet.Explorer/Libplanet.Explorer.csproj deleted file mode 100644 index db00868cc99..00000000000 --- a/Libplanet.Explorer/Libplanet.Explorer.csproj +++ /dev/null @@ -1,18 +0,0 @@ - - - - netcoreapp2.0 - true - true - false - - - - - - - - - - - diff --git a/Libplanet.Explorer/README.md b/Libplanet.Explorer/README.md deleted file mode 100644 index d18d2ccd858..00000000000 --- a/Libplanet.Explorer/README.md +++ /dev/null @@ -1,63 +0,0 @@ -Libplanet.Explorer -================== - -Explore your block, transaction, and address with Libplanet.Explorer. - -The goal of this project is to give you the UI of the web or -native application. But we **only provide JSON API** for the present. - - -Getting started ---------------- - -1. Create store getter class that implements `IBlockchainStore`. You may want -to change the path of data directory or use another store that implements -`IStore`. - - ~~~~~~~~ csharp - namespace MyExplorer - { - public class MyStore : IBlockchainStore - { - public IStore Store { get; private set; } - - public MyStore() - { - Store = new FileStore("./data"); - } - } - } - ~~~~~~~~ -2. Create `Program` class to run your explorer program. -Note that `ExplorerStartup` needs two generic types.\ -The first is an action class that implements `IAction` in your game and -the latter is the store getter class which is added on step 1. - - ~~~~~~~~ csharp - public class Program - { - public static void Main(string[] args) - { - BuildWebHost(args).Run(); - } - - public static IWebHost BuildWebHost(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup>() - .Build(); - } - ~~~~~~~~ - - -Endpoints ---------- - -- `/blocks/`: Show a list of blocks. It contains the hash of block and -the creation timestamp. -- `/blocks/{hashString}/`: Show the details of the block that includes index, -difficulty, nonce, the hash of the previous block, reward beneficiary, -creation timestamp, and transaction ids. `{hashString}` is -- `/tx/{txIdString}/`: Show the details of the transaction that includes id, -signature, creation timestamp, signer address, recipient address, and actions. -- `/address/{addressIdString}/`: Show the details of address. The list of -transactions sent/received and state of it. diff --git a/Libplanet.Explorer/ViewModels/AddressViewModel.cs b/Libplanet.Explorer/ViewModels/AddressViewModel.cs deleted file mode 100644 index 19aab427614..00000000000 --- a/Libplanet.Explorer/ViewModels/AddressViewModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using Libplanet.Action; -using Libplanet.Tx; - -namespace Libplanet.Explorer.ViewModels -{ - public class AddressViewModel - { - public List> Tx { get; set; } - public AddressStateMap State { get; set; } - } -} diff --git a/Libplanet.Explorer/ViewModels/BlockViewModel.cs b/Libplanet.Explorer/ViewModels/BlockViewModel.cs deleted file mode 100644 index 5ef00cada53..00000000000 --- a/Libplanet.Explorer/ViewModels/BlockViewModel.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections.Generic; -using System.Security.Cryptography; -using Libplanet.Tx; - -namespace Libplanet.Explorer.ViewModels -{ - public class BlockViewModel - { - public long Index { get; set; } - public int Difficulty { get; set; } - public string Nonce { get; set; } - public string Miner { get; set; } - public string PreviousHash { get; set; } - public string Timestamp { get; set; } - public List> TxIds { get; set; } - } -} diff --git a/Libplanet.Explorer/ViewModels/TransactionViewModel.cs b/Libplanet.Explorer/ViewModels/TransactionViewModel.cs deleted file mode 100644 index fdeb894bb16..00000000000 --- a/Libplanet.Explorer/ViewModels/TransactionViewModel.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Security.Cryptography.X509Certificates; -using Libplanet.Tx; - -namespace Libplanet.Explorer.ViewModels -{ - public class TransactionViewModel - { - public string Id { get; set; } - public byte[] Signature { get; set; } - public DateTimeOffset Timestamp { get; set; } - public string Signer { get; set; } - public string[] UpdatedAddresses { get; set; } - public IEnumerable> Actions { get; set; } - } -} diff --git a/Libplanet.sln b/Libplanet.sln index ea9e3db3c08..b295a24234f 100644 --- a/Libplanet.sln +++ b/Libplanet.sln @@ -11,8 +11,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet", "Libplanet\Libp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Tests", "Libplanet.Tests\Libplanet.Tests.csproj", "{3BA7D9BE-EBBF-432E-9880-0E2D2C17FCF8}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Explorer", "Libplanet.Explorer\Libplanet.Explorer.csproj", "{CC035C07-8D2B-4608-BA09-3FBB290A3EA6}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -47,18 +45,6 @@ Global {3BA7D9BE-EBBF-432E-9880-0E2D2C17FCF8}.Release|x64.Build.0 = Release|Any CPU {3BA7D9BE-EBBF-432E-9880-0E2D2C17FCF8}.Release|x86.ActiveCfg = Release|Any CPU {3BA7D9BE-EBBF-432E-9880-0E2D2C17FCF8}.Release|x86.Build.0 = Release|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Debug|x64.ActiveCfg = Debug|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Debug|x64.Build.0 = Debug|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Debug|x86.ActiveCfg = Debug|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Debug|x86.Build.0 = Debug|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Release|Any CPU.Build.0 = Release|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Release|x64.ActiveCfg = Release|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Release|x64.Build.0 = Release|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Release|x86.ActiveCfg = Release|Any CPU - {CC035C07-8D2B-4608-BA09-3FBB290A3EA6}.Release|x86.Build.0 = Release|Any CPU {4F5DB8F5-D0F4-454C-95A7-87F53E5D5E36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4F5DB8F5-D0F4-454C-95A7-87F53E5D5E36}.Debug|Any CPU.Build.0 = Debug|Any CPU {4F5DB8F5-D0F4-454C-95A7-87F53E5D5E36}.Debug|x64.ActiveCfg = Debug|Any CPU From 7510d1a57eddcf90712e0dbc57612055e9a03034 Mon Sep 17 00:00:00 2001 From: Hong Minhee Date: Fri, 5 Apr 2019 18:31:03 +0900 Subject: [PATCH 2/4] Make BlockChain.Blocks public --- CHANGES.md | 2 ++ Libplanet/Blockchain/BlockChain.cs | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 183c46aa7c1..64fd82950cc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -180,6 +180,7 @@ To be released. to ignore peers if their version is different. [[#167]], [[#170]] - Added `IActionContext.Miner` property. [[#173]], [[#174]] - Renamed `Block.RewardBeneficiary` to `Block.Miner`. [[#174]] + - Added `BlockChain.Blocks` property. [[#176]] [#28]: https://github.com/planetarium/libplanet/issues/28 [#98]: https://github.com/planetarium/libplanet/issues/98 @@ -205,6 +206,7 @@ To be released. [#170]: https://github.com/planetarium/libplanet/pull/170 [#173]: https://github.com/planetarium/libplanet/issues/173 [#174]: https://github.com/planetarium/libplanet/pull/174 +[#176]: https://github.com/planetarium/libplanet/pull/176 [RFC 5389]: https://tools.ietf.org/html/rfc5389 [RFC 5766]: https://tools.ietf.org/html/rfc5766 diff --git a/Libplanet/Blockchain/BlockChain.cs b/Libplanet/Blockchain/BlockChain.cs index 2e676af3301..cbf14a4ea7f 100644 --- a/Libplanet/Blockchain/BlockChain.cs +++ b/Libplanet/Blockchain/BlockChain.cs @@ -62,7 +62,13 @@ public Block Tip public Guid Id { get; private set; } - internal IDictionary, Block> Blocks + /// + /// All s in the + /// storage, including orphan s. + /// Keys are es and values are + /// their corresponding s. + /// + public IDictionary, Block> Blocks { get; private set; } From 3c653ee194880924df15b0c79d72bc4d241b7a1b Mon Sep 17 00:00:00 2001 From: Hong Minhee Date: Fri, 5 Apr 2019 18:31:37 +0900 Subject: [PATCH 3/4] Make BlockChain.Transactions public --- CHANGES.md | 1 + Libplanet/Blockchain/BlockChain.cs | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 64fd82950cc..6d48b1dc6de 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -181,6 +181,7 @@ To be released. - Added `IActionContext.Miner` property. [[#173]], [[#174]] - Renamed `Block.RewardBeneficiary` to `Block.Miner`. [[#174]] - Added `BlockChain.Blocks` property. [[#176]] + - Added `BlockChain.Transactions` property. [[#176]] [#28]: https://github.com/planetarium/libplanet/issues/28 [#98]: https://github.com/planetarium/libplanet/issues/98 diff --git a/Libplanet/Blockchain/BlockChain.cs b/Libplanet/Blockchain/BlockChain.cs index cbf14a4ea7f..f24e4ecb346 100644 --- a/Libplanet/Blockchain/BlockChain.cs +++ b/Libplanet/Blockchain/BlockChain.cs @@ -73,7 +73,13 @@ public IDictionary, Block> Blocks get; private set; } - internal IDictionary> Transactions + /// + /// All s in the + /// storage, including orphan s. + /// Keys are s and values are + /// their corresponding s. + /// + public IDictionary> Transactions { get; private set; } From b00807a72bd02263c7763374eda64c15e3ec32d9 Mon Sep 17 00:00:00 2001 From: Hong Minhee Date: Fri, 5 Apr 2019 18:31:49 +0900 Subject: [PATCH 4/4] Hide internal parts from Libplanet.Explorer again --- Libplanet/Blockchain/BlockChain.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Libplanet/Blockchain/BlockChain.cs b/Libplanet/Blockchain/BlockChain.cs index f24e4ecb346..e0e220b01af 100644 --- a/Libplanet/Blockchain/BlockChain.cs +++ b/Libplanet/Blockchain/BlockChain.cs @@ -12,7 +12,6 @@ using Libplanet.Store; using Libplanet.Tx; -[assembly: InternalsVisibleTo("Libplanet.Explorer")] [assembly: InternalsVisibleTo("Libplanet.Tests")] namespace Libplanet.Blockchain {