-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
4 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
83 changes: 83 additions & 0 deletions
83
NineChronicles.Headless.Executable/Commands/DPoSGenesisCommand.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,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Numerics; | ||
using System.Text.Json; | ||
using Cocona; | ||
using Libplanet.Crypto; | ||
using Libplanet.RocksDBStore; | ||
using Libplanet.Store; | ||
using Nekoyume; | ||
using NineChronicles.Headless.Executable.IO; | ||
using Lib9cUtils = Lib9c.DevExtensions.Utils; | ||
|
||
namespace NineChronicles.Headless.Executable.Commands; | ||
|
||
public class DPoSGenesisCommand | ||
{ | ||
private readonly IConsole _console; | ||
|
||
public DPoSGenesisCommand(IConsole console) | ||
{ | ||
_console = console; | ||
} | ||
|
||
[Command(Description = "Mine a new genesis block")] | ||
public void Mine( | ||
[Argument("CONFIG", Description = "JSON config path to mine genesis block")] | ||
string storePath, | ||
string configPath = "./config.json") | ||
{ | ||
var options = new JsonSerializerOptions | ||
{ | ||
AllowTrailingCommas = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
}; | ||
string json = File.ReadAllText(configPath); | ||
DPoSGenesisConfig genesisConfig = JsonSerializer.Deserialize<DPoSGenesisConfig>(json, options); | ||
var privateKey = new PrivateKey(genesisConfig.Proposer); | ||
var initialNCGs = genesisConfig.InitialNCGs ?? new List<NCGConfig>(); | ||
var validators = genesisConfig.InitialValidators ?? new List<ValidatorConfig>(); | ||
string stateStorePath = Path.Combine(storePath, "states"); | ||
IStateStore stateStore = new TrieStateStore(new RocksDBKeyValueStore(storePath)); | ||
var block = DPoSBlockHelper.ProposeGenesisBlock( | ||
privateKey, | ||
stateStore, | ||
initialNCGs.ToDictionary( | ||
v => new Address(v.Address), | ||
v => (BigInteger)v.Amount), | ||
validators.ToDictionary( | ||
v => PublicKey.FromHex(v.PublicKey), | ||
v => (BigInteger)v.Power)); | ||
Lib9cUtils.ExportBlock(block, "genesis-block"); | ||
} | ||
|
||
#pragma warning disable S3459 | ||
[Serializable] | ||
private struct DPoSGenesisConfig | ||
{ | ||
public string Proposer { get; set; } // Required | ||
|
||
public List<NCGConfig>? InitialNCGs { get; set; } | ||
|
||
public List<ValidatorConfig>? InitialValidators { get; set; } | ||
} | ||
|
||
[Serializable] | ||
private struct NCGConfig | ||
{ | ||
public string Address { get; set; } | ||
|
||
public long Amount { get; set; } | ||
} | ||
|
||
[Serializable] | ||
private struct ValidatorConfig | ||
{ | ||
public string PublicKey { get; set; } | ||
|
||
public long Power { get; set; } | ||
} | ||
#pragma warning restore S3459 | ||
} |
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