Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib9c
Submodule Lib9c updated 132 files
6 changes: 5 additions & 1 deletion Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public void Constructor()
stateStore);
var actionLoader = new SingleActionLoader(typeof(DummyAction));
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
new PolicyActionsRegistry(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
stateStore,
actionLoader);
var genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator);
Expand Down
6 changes: 5 additions & 1 deletion Libplanet.Headless/Hosting/LibplanetNodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ IActionEvaluator BuildActionEvaluator(IActionEvaluatorConfiguration actionEvalua
actionLoader),
DefaultActionEvaluatorConfiguration _ =>
new ActionEvaluator(
_ => blockPolicy.BlockAction,
new PolicyActionsRegistry(
_ => blockPolicy.BeginBlockActions,
_ => blockPolicy.EndBlockActions,
_ => blockPolicy.BeginTxActions,
_ => blockPolicy.EndTxActions),
stateStore: StateStore,
actionTypeLoader: actionLoader),
ForkableActionEvaluatorConfiguration forkableActionEvaluatorConfiguration =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public void Balance(StoreType storeType)
IStagePolicy stagePolicy = new VolatileStagePolicy();
IBlockPolicy blockPolicy = new BlockPolicySource().GetPolicy();
ActionEvaluator actionEvaluator = new ActionEvaluator(
_ => blockPolicy.BlockAction,
new PolicyActionsRegistry(
_ => blockPolicy.BeginBlockActions,
_ => blockPolicy.EndBlockActions,
_ => blockPolicy.BeginTxActions,
_ => blockPolicy.EndTxActions),
stateStore,
new NCActionLoader());
BlockChain chain = BlockChain.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public ChainCommandTest()
public void Tip(StoreType storeType)
{
var actionEvaluator = new ActionEvaluator(
_ => new BlockPolicy().BlockAction,
new PolicyActionsRegistry(
_ => new BlockPolicy().BeginBlockActions,
_ => new BlockPolicy().EndBlockActions,
_ => new BlockPolicy().BeginTxActions,
_ => new BlockPolicy().EndTxActions),
new TrieStateStore(new MemoryKeyValueStore()),
new NCActionLoader());
Block genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator);
Expand Down Expand Up @@ -92,7 +96,11 @@ public void Inspect(StoreType storeType)
IStagePolicy stagePolicy = new VolatileStagePolicy();
IBlockPolicy blockPolicy = new BlockPolicySource().GetPolicy();
ActionEvaluator actionEvaluator = new ActionEvaluator(
_ => blockPolicy.BlockAction,
new PolicyActionsRegistry(
_ => blockPolicy.BeginBlockActions,
_ => blockPolicy.EndBlockActions,
_ => blockPolicy.BeginTxActions,
_ => blockPolicy.EndTxActions),
stateStore,
new NCActionLoader());
Block genesisBlock = BlockChain.ProposeGenesisBlock(
Expand Down Expand Up @@ -153,7 +161,11 @@ public void Truncate(StoreType storeType)
IStagePolicy stagePolicy = new VolatileStagePolicy();
IBlockPolicy blockPolicy = new BlockPolicySource().GetPolicy();
ActionEvaluator actionEvaluator = new ActionEvaluator(
_ => blockPolicy.BlockAction,
new PolicyActionsRegistry(
_ => blockPolicy.BeginBlockActions,
_ => blockPolicy.EndBlockActions,
_ => blockPolicy.BeginTxActions,
_ => blockPolicy.EndTxActions),
stateStore,
new NCActionLoader());
Block genesisBlock = BlockChain.ProposeGenesisBlock(
Expand Down Expand Up @@ -232,7 +244,11 @@ public void PruneState(StoreType storeType)
IStagePolicy stagePolicy = new VolatileStagePolicy();
IBlockPolicy blockPolicy = new BlockPolicySource().GetPolicy();
ActionEvaluator actionEvaluator = new ActionEvaluator(
_ => blockPolicy.BlockAction,
new PolicyActionsRegistry(
_ => blockPolicy.BeginBlockActions,
_ => blockPolicy.EndBlockActions,
_ => blockPolicy.BeginTxActions,
_ => blockPolicy.EndTxActions),
stateStore,
new NCActionLoader());
BlockChain chain = BlockChain.Create(
Expand Down Expand Up @@ -284,7 +300,11 @@ public void Snapshot(StoreType storeType)
IStagePolicy stagePolicy = new VolatileStagePolicy();
IBlockPolicy blockPolicy = new BlockPolicySource().GetPolicy();
ActionEvaluator actionEvaluator = new ActionEvaluator(
_ => blockPolicy.BlockAction,
new PolicyActionsRegistry(
_ => blockPolicy.BeginBlockActions,
_ => blockPolicy.EndBlockActions,
_ => blockPolicy.BeginTxActions,
_ => blockPolicy.EndTxActions),
stateStore,
new NCActionLoader());
BlockChain chain = BlockChain.Create(
Expand Down Expand Up @@ -445,6 +465,7 @@ private Block MineGenesisBlock()
block.Hash,
DateTimeOffset.UtcNow,
validator.PublicKey,
BigInteger.One,
VoteFlag.PreCommit).Sign(validator)))
: null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public void GetGenesisBlock(StoreType storeType)
{
IStore store = storeType.CreateStore(_storePath);
IActionEvaluator actionEvaluator = new ActionEvaluator(
_ => new BlockPolicy().BlockAction,
new PolicyActionsRegistry(
_ => new BlockPolicy().BeginBlockActions,
_ => new BlockPolicy().EndBlockActions,
_ => new BlockPolicy().BeginTxActions,
_ => new BlockPolicy().EndTxActions),
new TrieStateStore(new MemoryKeyValueStore()),
new NCActionLoader());
Block genesisBlock = BlockChain.ProposeGenesisBlock(actionEvaluator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void Balance(
_console.Error.Flush();
IEnumerable<Address> addrs = digest.TxIds
.Select(txId => store.GetTransaction(new TxId(txId.ToArray())))
.SelectMany(tx => tx.Actions is { } ca
.SelectMany(tx => tx!.Actions is { } ca
? ca.Select(a => ToAction(a))
.SelectMany(a =>
{
Expand Down
57 changes: 33 additions & 24 deletions NineChronicles.Headless.Executable/Commands/ChainCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@
using Libplanet.RocksDBStore;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Microsoft.Extensions.Configuration;
using Nekoyume.Action.Loader;
using Nekoyume.Blockchain.Policy;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NineChronicles.Headless.Executable.IO;
using NineChronicles.Headless.Executable.Store;
using Serilog;
using Serilog.Core;
using static NineChronicles.Headless.NCActionUtils;
using CoconaUtils = Libplanet.Extensions.Cocona.Utils;

Expand Down Expand Up @@ -82,7 +79,7 @@ public void Tip(

BlockHash tipHash = store.IndexBlockHash(chainId, -1)
?? throw new CommandExitedException("The given chain seems empty.", -1);
Block tip = store.GetBlock(tipHash);
Block tip = store.GetBlock(tipHash)!;
_console.Out.WriteLine(CoconaUtils.SerializeHumanReadable(tip.Header));
store.Dispose();
}
Expand Down Expand Up @@ -129,10 +126,14 @@ public void Inspect(
throw new CommandExitedException($"There is no genesis block: {storePath}", -1);
}

Block genesisBlock = store.GetBlock(gHash);
Block genesisBlock = store.GetBlock(gHash)!;
var blockChainStates = new BlockChainStates(store, stateStore);
var actionEvaluator = new ActionEvaluator(
_ => blockPolicy.BlockAction,
new PolicyActionsRegistry(
beginBlockActionsGetter: _ => blockPolicy.BeginBlockActions,
endBlockActionsGetter: _ => blockPolicy.EndBlockActions,
beginTxActionsGetter: _ => blockPolicy.BeginTxActions,
endTxActionsGetter: _ => blockPolicy.EndTxActions),
stateStore,
new NCActionLoader());
BlockChain chain = new BlockChain(
Expand Down Expand Up @@ -165,10 +166,10 @@ public void Inspect(
{
var block = store.GetBlock(item.value);
var previousBlock = store.GetBlock(
block.PreviousHash ?? block.Hash
block!.PreviousHash ?? block.Hash
);

var miningTime = block.Timestamp - previousBlock.Timestamp;
var miningTime = block.Timestamp - previousBlock!.Timestamp;
var txCount = 0;
var hackAndSlashCount = 0;
var rankingBattleCount = 0;
Expand Down Expand Up @@ -286,11 +287,11 @@ public void Truncate(
}

snapshotTipHash = hash;
} while (!stateStore.GetStateRoot(store.GetBlock(snapshotTipHash).StateRootHash).Recorded);
} while (!stateStore.GetStateRoot(store.GetBlock(snapshotTipHash)!.StateRootHash).Recorded);

var forkedId = Guid.NewGuid();

Fork(chainId, forkedId, snapshotTipHash, tip, store);
Fork(chainId, forkedId, snapshotTipHash, tip!, store);

store.SetCanonicalChainId(forkedId);
foreach (var id in store.ListChainIds().Where(id => !id.Equals(forkedId)))
Expand Down Expand Up @@ -478,17 +479,21 @@ public void Snapshot(
new BlockPolicy();
var blockChainStates = new BlockChainStates(store, stateStore);
var actionEvaluator = new ActionEvaluator(
_ => blockPolicy.BlockAction,
new PolicyActionsRegistry(
beginBlockActionsGetter: _ => blockPolicy.BeginBlockActions,
endBlockActionsGetter: _ => blockPolicy.EndBlockActions,
beginTxActionsGetter: _ => blockPolicy.BeginTxActions,
endTxActionsGetter: _ => blockPolicy.EndTxActions),
stateStore,
new NCActionLoader()
);
var originalChain = new BlockChain(blockPolicy, stagePolicy, store, stateStore,
store.GetBlock(genesisHash), blockChainStates, actionEvaluator);
var tip = store.GetBlock(tipHash);
Block tip = store.GetBlock(tipHash)!;

var potentialSnapshotTipIndex = tipIndex - blockBefore;
var potentialSnapshotTipHash = (BlockHash)store.IndexBlockHash(chainId, potentialSnapshotTipIndex)!;
var snapshotTip = store.GetBlock(potentialSnapshotTipHash);
var snapshotTip = store.GetBlock(potentialSnapshotTipHash)!;

_console.Out.WriteLine(
"Original Store Tip: #{0}\n1. LastCommit: {1}\n2. BlockCommit in Chain: {2}\n3. BlockCommit in Store: {3}",
Expand Down Expand Up @@ -521,23 +526,27 @@ public void Snapshot(
potentialSnapshotTipIndex);
blockBefore += 1;
potentialSnapshotTipBlockCommit = store
.GetBlock((BlockHash)store.IndexBlockHash(chainId, tip.Index - blockBefore + 1)!).LastCommit;
.GetBlock((BlockHash)store.IndexBlockHash(chainId, tip.Index - blockBefore + 1)!)!.LastCommit;
store.PutBlockCommit(tipBlockCommit);
store.PutChainBlockCommit(chainId, tipBlockCommit);
store.PutBlockCommit(potentialSnapshotTipBlockCommit);
store.PutChainBlockCommit(chainId, potentialSnapshotTipBlockCommit);
store.PutBlockCommit(potentialSnapshotTipBlockCommit!);
store.PutChainBlockCommit(chainId, potentialSnapshotTipBlockCommit!);
}

var blockCommitBlock = store.GetBlock(tipHash);
Block? blockCommitBlock = store.GetBlock(tipHash);
if (blockCommitBlock is not { } blockCommitBlockNotNull)
{
throw new NullReferenceException(nameof(blockChainStates));
}

// Add last block commits to store from tip until --block-before + 5 or tip if too short for buffer
var blockCommitRange = blockBefore + 5 < tip.Index ? blockBefore + 5 : tip.Index - 1;
for (var i = 0; i < blockCommitRange; i++)
{
_console.Out.WriteLine("Adding block #{0}'s block commit to the store", blockCommitBlock.Index - 1);
store.PutBlockCommit(blockCommitBlock.LastCommit);
store.PutChainBlockCommit(chainId, blockCommitBlock.LastCommit);
blockCommitBlock = store.GetBlock((BlockHash)blockCommitBlock.PreviousHash!);
_console.Out.WriteLine("Adding block #{0}'s block commit to the store", blockCommitBlockNotNull.Index - 1);
store.PutBlockCommit(blockCommitBlockNotNull.LastCommit!);
store.PutChainBlockCommit(chainId, blockCommitBlockNotNull.LastCommit!);
blockCommitBlock = store.GetBlock((BlockHash)blockCommitBlockNotNull.PreviousHash!);
}

var snapshotTipIndex = Math.Max(tipIndex - (blockBefore + 1), 0);
Expand All @@ -555,7 +564,7 @@ public void Snapshot(
}

snapshotTipHash = hash;
} while (!stateStore.GetStateRoot(store.GetBlock(snapshotTipHash).StateRootHash).Recorded);
} while (!stateStore.GetStateRoot(store.GetBlock(snapshotTipHash)!.StateRootHash).Recorded);

var forkedId = Guid.NewGuid();
Fork(chainId, forkedId, snapshotTipHash, tip, store);
Expand Down Expand Up @@ -1025,15 +1034,15 @@ private void Fork(
store.ForkBlockIndexes(src, dest, branchPointHash);
if (store.GetBlockCommit(branchPointHash) is { })
{
store.PutChainBlockCommit(dest, store.GetBlockCommit(branchPointHash));
store.PutChainBlockCommit(dest, store.GetBlockCommit(branchPointHash)!);
}
store.ForkTxNonces(src, dest);

for (
Block block = tip;
block.PreviousHash is { } hash
&& !block.Hash.Equals(branchPointHash);
block = store.GetBlock(hash))
block = store.GetBlock(hash)!)
{
IEnumerable<(Address, int)> signers = block
.Transactions
Expand Down
38 changes: 37 additions & 1 deletion NineChronicles.Headless.Executable/Commands/GenesisCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Libplanet.Types.Blocks;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Model.State;
using NineChronicles.Headless.Executable.IO;
using CoconaUtils = Libplanet.Extensions.Cocona.Utils;
Expand Down Expand Up @@ -236,6 +237,28 @@ out List<CreatePledge> pledgeActions
}
}

private void ProcessInitialNCGConfigs(
List<NCGConfig>? configs,
out Dictionary<Address, FungibleAssetValue> initialNCGs
)
{
_console.Out.WriteLine("\nProcessing initial NCG...");

initialNCGs = new Dictionary<Address, FungibleAssetValue>();
if (configs is { })
{
foreach (NCGConfig config in configs)
{
_console.Out.WriteLine($"Preparing {config.Amount} NCG for {config.Address}...");
Address agentAddress = new(config.Address);
initialNCGs.Add(
agentAddress,
new FungibleAssetValue(Asset.GovernanceToken, config.Amount, 0)
);
}
}
}

[Command(Description = "Mine a new genesis block")]
public void Mine(
[Argument("CONFIG", Description = "JSON config path to mine genesis block")]
Expand Down Expand Up @@ -263,6 +286,8 @@ public void Mine(

ProcessInitialPledgeConfigs(genesisConfig.InitialPledgeConfigs, out var initialPledges);

ProcessInitialNCGConfigs(genesisConfig.InitialNCGConfigs, out var initialNCGs);

// Mine genesis block
_console.Out.WriteLine("\nMining genesis block...\n");
Block block = BlockHelper.ProposeGenesisBlock(
Expand All @@ -276,7 +301,8 @@ public void Mine(
item => new PublicKey(ByteUtil.ParseHex(item.PublicKey)),
item => new BigInteger(item.Power)),
actionBases: adminMeads.Concat(initialMeads).Concat(initialPledges).Concat(GetAdditionalActionBases()),
goldCurrency: currency
goldCurrency: currency,
initialFavs: initialNCGs
);

Lib9cUtils.ExportBlock(block, "genesis-block");
Expand Down Expand Up @@ -418,6 +444,14 @@ private struct PledgeConfig
public int Mead { get; set; }
}

[Serializable]
private struct NCGConfig
{
public string Address { get; set; }

public int Amount { get; set; }
}

/// <summary>
/// Config to mine new genesis block.
/// </summary>
Expand Down Expand Up @@ -454,6 +488,8 @@ private struct GenesisConfig
public List<MeadConfig>? InitialMeadConfigs { get; set; }

public List<PledgeConfig>? InitialPledgeConfigs { get; set; }

public List<NCGConfig>? InitialNCGConfigs { get; set; }
}
#pragma warning restore S3459
}
Expand Down
Loading