Skip to content

Commit

Permalink
feat: add genesis config
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed May 10, 2024
1 parent d376aaa commit 10c90cb
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Lib9c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ public static void AppendEmptyBlock(BlockChain blockChain)
block.Hash,
new[]
{
new VoteMetadata(block.Index, 0, block.Hash, block.Timestamp, ValidatorKey.PublicKey, VoteFlag.PreCommit).Sign(ValidatorKey),
new VoteMetadata(
block.Index,
0,
block.Hash,
block.Timestamp,
ValidatorKey.PublicKey,
BigInteger.One,
VoteFlag.PreCommit).Sign(ValidatorKey),
}.ToImmutableArray());
blockChain.Append(block, blockCommit);
}
Expand Down
83 changes: 83 additions & 0 deletions NineChronicles.Headless.Executable/Commands/DPoSGenesisCommand.cs
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
}
11 changes: 9 additions & 2 deletions NineChronicles.Headless.Tests/GraphQLTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,15 @@ InitializeStates initializeStates
0,
block.Hash,
ValidatorPrivateKeys.Select(
k => new VoteMetadata(block.Index, 0, block.Hash, block.Timestamp, k.PublicKey, VoteFlag.PreCommit).Sign(k))
.ToImmutableArray());
k => new VoteMetadata(
block.Index,
0,
block.Hash,
block.Timestamp,
k.PublicKey,
BigInteger.One,
VoteFlag.PreCommit).Sign(k))
.ToImmutableArray());

blockchain.Append(block, blockCommit);
var ncg = new GoldCurrencyState((Dictionary)blockchain.GetNextWorldState().GetLegacyState(Addresses.GoldCurrency))
Expand Down

0 comments on commit 10c90cb

Please sign in to comment.