From a59a95ef0b2d4d1f149ee8eea953cf5e8b6478a3 Mon Sep 17 00:00:00 2001 From: ilgyu Date: Tue, 9 Jul 2024 15:45:08 +0900 Subject: [PATCH] fix: Fix Not to create BlockChain --- NineChronicles.Snapshot/Program.cs | 49 +++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/NineChronicles.Snapshot/Program.cs b/NineChronicles.Snapshot/Program.cs index 39d0d8b..45c70c1 100644 --- a/NineChronicles.Snapshot/Program.cs +++ b/NineChronicles.Snapshot/Program.cs @@ -154,7 +154,6 @@ public void Snapshot( _stateStore, new NCActionLoader() ); - var originalChain = new BlockChain(blockPolicy, stagePolicy, _store, _stateStore, _store.GetBlock(genesisHash), blockChainStates, actionEvaluator); var tip = _store.GetBlock(tipHash); var potentialSnapshotTipIndex = tipIndex - blockBefore; @@ -162,14 +161,14 @@ public void Snapshot( var snapshotTip = _store.GetBlock(potentialSnapshotTipHash); _logger.Debug("Original Store Tip: #{0}\n1. LastCommit: {1}\n2. BlockCommit in Chain: {2}\n3. BlockCommit in Store: {3}", - tip.Index, tip.LastCommit, originalChain.GetBlockCommit(tipHash), _store.GetBlockCommit(tipHash)); + tip.Index, tip.LastCommit, GetChainBlockCommit(tipHash, chainId), _store.GetBlockCommit(tipHash)); _logger.Debug("Potential Snapshot Tip: #{0}\n1. LastCommit: {1}\n2. BlockCommit in Chain: {2}\n3. BlockCommit in Store: {3}", - potentialSnapshotTipIndex, snapshotTip.LastCommit, originalChain.GetBlockCommit(potentialSnapshotTipHash), _store.GetBlockCommit(potentialSnapshotTipHash)); + potentialSnapshotTipIndex, snapshotTip.LastCommit, GetChainBlockCommit(potentialSnapshotTipHash, chainId), _store.GetBlockCommit(potentialSnapshotTipHash)); var tipBlockCommit = _store.GetBlockCommit(tipHash) ?? - originalChain.GetBlockCommit(tipHash); + GetChainBlockCommit(tipHash, chainId); var potentialSnapshotTipBlockCommit = _store.GetBlockCommit(potentialSnapshotTipHash) ?? - originalChain.GetBlockCommit(potentialSnapshotTipHash); + GetChainBlockCommit(potentialSnapshotTipHash, chainId); // Add tip and the snapshot tip's block commit to store to avoid block validation during preloading if (potentialSnapshotTipBlockCommit != null) @@ -247,11 +246,12 @@ public void Snapshot( count++; } - var newChain = new BlockChain(blockPolicy, stagePolicy, _store, _stateStore, _store.GetBlock(genesisHash), blockChainStates, actionEvaluator); - var newTip = newChain.Tip; + var newTipHash = _store.IndexBlockHash(forkedId, -1) + ?? throw new CommandExitedException("The given chain seems empty.", -1); + var newTip = _store.GetBlock(newTipHash); var latestEpoch = (int) (newTip.Timestamp.ToUnixTimeSeconds() / epochUnitSeconds); _logger.Debug("Official Snapshot Tip: #{0}\n1. Timestamp: {1}\n2. Latest Epoch: {2}\n3. BlockCommit in Chain: {3}\n4. BlockCommit in Store: {4}", - newTip.Index, newTip.Timestamp.UtcDateTime, latestEpoch, newChain.GetBlockCommit(newTip.Hash), _store.GetBlockCommit(newTip.Hash)); + newTip.Index, newTip.Timestamp.UtcDateTime, latestEpoch, GetChainBlockCommit(newTip.Hash, forkedId), _store.GetBlockCommit(newTip.Hash)); _logger.Debug($"Snapshot-{snapshotType.ToString()} CopyStates Start."); var start = DateTimeOffset.Now; @@ -693,6 +693,39 @@ private JObject AddPreviousEpochs( return jsonObject; } + private BlockCommit GetChainBlockCommit(BlockHash blockHash, Guid chainId) + { + var tipHash = _store.IndexBlockHash(chainId, -1) + ?? throw new CommandExitedException("The given chain seems empty.", -1); + if (!(_store.GetBlockIndex(tipHash) is { } tipIndex)) + { + throw new CommandExitedException( + $"The index of {tipHash} doesn't exist.", + -1); + } + + if (!(_store.GetBlockIndex(blockHash) is { } blockIndex)) + { + throw new CommandExitedException( + $"The index of {blockHash} doesn't exist.", + -1); + } + + if (blockIndex == tipIndex) + { + return _store.GetChainBlockCommit(chainId); + } + + if (!(_store.IndexBlockHash(chainId, blockIndex + 1) is { } nextHash)) + { + throw new CommandExitedException( + $"The hash of index {blockIndex + 1} doesn't exist.", + -1); + } + + return _store.GetBlock(nextHash).LastCommit; + } + public class NCActionLoader : IActionLoader { private readonly IActionLoader _actionLoader;