Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Dec 27, 2023
1 parent b6be5d8 commit cf5bebc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void Snapshot(StoreType storeType)
};
Guid chainId = chain.Id;

for (var i = 0; i < 2; i++)
for (var i = 0; i < 5; i++)
{
chain.MakeTransaction(GenesisHelper.ValidatorKey, new ActionBase[] { action });
Block block = chain.ProposeBlock(
Expand Down Expand Up @@ -334,9 +334,10 @@ public void Snapshot(StoreType storeType)
IStore storeAfterSnapshot = storeType.CreateStore(_storePath);
chainId = storeAfterSnapshot.GetCanonicalChainId() ?? new Guid();
var tipHashAfterSnapshot = storeAfterSnapshot.IndexBlockHash(chainId, -1);
var snapshotTipIndex = storeAfterSnapshot.GetBlockIndex((BlockHash)tipHashAfterSnapshot!);
var expectedGenesisPartitionSnapshotPath = Path.Combine(outputDirectory, "partition", $"snapshot-{genesisBlockEpoch}-{genesisBlockEpoch}.zip");
var expectedGenesisMetadataPath = Path.Combine(outputDirectory, "metadata", $"snapshot-{genesisBlockEpoch}-{genesisBlockEpoch}.json");
var expectedFullSnapshotPath = Path.Combine(outputDirectory, "full", $"{genesisHash}-snapshot-{tipHashAfterSnapshot}.zip");
var expectedFullSnapshotPath = Path.Combine(outputDirectory, "full", $"{genesisHash}-snapshot-{tipHashAfterSnapshot}-{snapshotTipIndex}.zip");
storeAfterSnapshot.Dispose();

Assert.True(File.Exists(expectedGenesisPartitionSnapshotPath));
Expand All @@ -360,8 +361,8 @@ public void Snapshot(StoreType storeType)
Assert.True(File.Exists(expectedPartitionSnapshotPath));
Assert.True(File.Exists(expectedStateSnapshotPath));
Assert.True(File.Exists(expectedMetadataPath));
Assert.Equal(3, indexCountBeforeSnapshot);
Assert.Equal(2, indexCountAfterSnapshot);
Assert.Equal(6, indexCountBeforeSnapshot);
Assert.Equal(5, indexCountAfterSnapshot);

Directory.Delete(outputDirectory, true);
}
Expand Down
23 changes: 17 additions & 6 deletions NineChronicles.Headless.Executable/Commands/ChainCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public void Snapshot(
IKeyValueStore stateKeyValueStore = new RocksDBKeyValueStore(statesPath);
IKeyValueStore newStateKeyValueStore = new RocksDBKeyValueStore(newStatesPath);
TrieStateStore stateStore = new TrieStateStore(stateKeyValueStore);
var newStateStore = new TrieStateStore(newStateKeyValueStore);
TrieStateStore newStateStore = new TrieStateStore(newStateKeyValueStore);

var canonicalChainId = store.GetCanonicalChainId();
if (!(canonicalChainId is { } chainId))
Expand Down Expand Up @@ -530,8 +530,9 @@ public void Snapshot(

var blockCommitBlock = store.GetBlock(tipHash);

// Add last block commits to store from tip until --block-before + 5 for buffer
for (var i = 0; i < blockBefore + 5; i++)
// 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);
Expand Down Expand Up @@ -570,10 +571,10 @@ public void Snapshot(
ImmutableHashSet<HashDigest<SHA256>> stateHashes =
ImmutableHashSet<HashDigest<SHA256>>.Empty.Add((HashDigest<SHA256>)snapshotTipStateRootHash!);

// Get 2 block digest before snapshot tip using snapshot previous block hash.
// Get 1 block digest before snapshot tip using snapshot previous block hash.
BlockHash? previousBlockHash = snapshotTipDigest?.Hash;
int count = 0;
const int maxStateDepth = 2;
const int maxStateDepth = 1;

while (previousBlockHash is { } pbh &&
store.GetBlockDigest(pbh) is { } previousBlockDigest &&
Expand Down Expand Up @@ -888,16 +889,22 @@ private void CopyStateStore(string storePath, string stateDirectory)
var storeTxBIndexPath = Path.Combine(storePath, "txbindex");
var storeStatesPath = Path.Combine(storePath, "states");
var storeChainPath = Path.Combine(storePath, "chain");
var storeBlockCommitPath = Path.Combine(storePath, "blockcommit");
var storeTxExecPath = Path.Combine(storePath, "txexec");
var stateDirBlockIndexPath = Path.Combine(stateDirectory, "block", "blockindex");
var stateDirTxIndexPath = Path.Combine(stateDirectory, "tx", "txindex");
var stateDirTxBIndexPath = Path.Combine(stateDirectory, "txbindex");
var stateDirStatesPath = Path.Combine(stateDirectory, "states");
var stateDirChainPath = Path.Combine(stateDirectory, "chain");
var stateDirBlockCommitPath = Path.Combine(stateDirectory, "blockcommit");
var stateDirTxExecPath = Path.Combine(stateDirectory, "txexec");
CopyDirectory(storeBlockIndexPath, stateDirBlockIndexPath, true);
CopyDirectory(storeTxIndexPath, stateDirTxIndexPath, true);
CopyDirectory(storeTxBIndexPath, stateDirTxBIndexPath, true);
CopyDirectory(storeStatesPath, stateDirStatesPath, true);
CopyDirectory(storeChainPath, stateDirChainPath, true);
CopyDirectory(storeBlockCommitPath, stateDirBlockCommitPath, true);
CopyDirectory(storeTxExecPath, stateDirTxExecPath, true);
}

private int GetMetaDataEpoch(
Expand All @@ -915,7 +922,7 @@ private int GetMetaDataEpoch(
}
catch (InvalidOperationException e)
{
Console.Error.WriteLine(e.Message);
_console.Error.WriteLine(e.Message);
return 0;
}
}
Expand Down Expand Up @@ -1016,6 +1023,10 @@ private void Fork(
IStore store)
{
store.ForkBlockIndexes(src, dest, branchPointHash);
if (store.GetBlockCommit(branchPointHash) is { })
{
store.PutChainBlockCommit(dest, store.GetBlockCommit(branchPointHash));
}
store.ForkTxNonces(src, dest);

for (
Expand Down

0 comments on commit cf5bebc

Please sign in to comment.