Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

[BlockStore] Create SetTip method to set the store's tip and ChainState's HighestPersistedBlock #278

Merged
merged 7 commits into from
Aug 4, 2017
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Stratis.Bitcoin.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Stratis.Bitcoin.Tests.BlockStore.LoopTests
Expand Down Expand Up @@ -69,7 +68,7 @@ internal Block CreateBlock(int blockNumber)
{
var block = new Block();

for (int j = 0; j < 100; j++)
for (int j = 0; j < 50; j++)
{
var trx = new Transaction();

Expand Down Expand Up @@ -125,10 +124,5 @@ internal void AddToPendingStorage(BlockStoreLoop blockStoreLoop, Block block)
ChainedBlock chainedBlock = blockStoreLoop.Chain.GetBlock(block.GetHash());
blockStoreLoop.PendingStorage.TryAdd(block.GetHash(), new BlockPair(block, chainedBlock));
}

internal string TestDirectory
{
get { return Path.Combine("TestData"); }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void CanExecute_CheckNextChainedBlockExistStep()
List<Block> blocks = CreateBlocks(5);

// The BlockRepository has 5 blocks stored
using (var blockRepository = new BlockRepository(Network.Main, TestBase.AssureEmptyDirAsDataFolder(@"BlockStore\LoopTest_CheckExists")))
using (var blockRepository = new BlockRepository(Network.Main, TestBase.AssureEmptyDirAsDataFolder(@"BlockStore\LoopTest_Exists")))
{
blockRepository.PutAsync(blocks.Last().GetHash(), blocks).GetAwaiter().GetResult();

Expand All @@ -29,18 +29,16 @@ public void CanExecute_CheckNextChainedBlockExistStep()
ChainedBlock block03 = chain.GetBlock(blocks[3].GetHash());
var block04 = new ChainedBlock(blocks[4].Header, blocks[4].Header.GetHash(), block03);

BlockStoreLoop blockStoreLoop = CreateBlockStoreLoop(chain, blockRepository, @"BlockStore\LoopTest_CheckExists");
BlockStoreLoop blockStoreLoop = CreateBlockStoreLoop(chain, blockRepository, @"BlockStore\LoopTest_Exists");

Assert.Null(blockStoreLoop.StoredBlock);
Assert.Null(blockStoreLoop.StoreTip);

ChainedBlock nextChainedBlock = block04;
var checkExistsStep = new CheckNextChainedBlockExistStep(blockStoreLoop);
checkExistsStep.Execute(nextChainedBlock, new CancellationToken(), false).GetAwaiter().GetResult();

Assert.Equal(blockStoreLoop.StoredBlock.Header.GetHash(), block04.Header.GetHash());
Assert.Equal(blockStoreLoop.StoreTip.Header.GetHash(), block04.Header.GetHash());
Assert.Equal(blockStoreLoop.BlockRepository.BlockHash, block04.Header.GetHash());

chain = null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void CanExecute_DownloadBlocks()
List<Block> blocks = CreateBlocks(10);

// The repository has 5 blocks stored
using (var blockRepository = new BlockRepository(Network.Main, TestBase.AssureEmptyDirAsDataFolder(@"BlockStore\LoopTest_DownloadBlocks")))
using (var blockRepository = new BlockRepository(Network.Main, TestBase.AssureEmptyDirAsDataFolder(@"BlockStore\LoopTest_Download")))
{
blockRepository.PutAsync(blocks.Take(5).Last().GetHash(), blocks.Take(5).ToList()).GetAwaiter().GetResult();

Expand All @@ -28,7 +28,7 @@ public void CanExecute_DownloadBlocks()
AppendBlocks(chain, blocks.Take(10));

// Create block store loop
BlockStoreLoop blockStoreLoop = CreateBlockStoreLoop(chain, blockRepository, @"BlockStore\LoopTest_DownloadBlocks");
BlockStoreLoop blockStoreLoop = CreateBlockStoreLoop(chain, blockRepository, @"BlockStore\LoopTest_Download");

// Push blocks 5 - 9 to the downloaded blocks collection
blockStoreLoop.BlockPuller.InjectBlock(blocks[5].GetHash(), new DownloadedBlock() { Length = blocks[5].GetSerializedSize(), Block = blocks[5] }, new CancellationToken());
Expand All @@ -44,9 +44,7 @@ public void CanExecute_DownloadBlocks()
step.Execute(nextChainedBlock, new CancellationToken(), false).GetAwaiter().GetResult();

Assert.Equal(blocks[9].GetHash(), blockStoreLoop.BlockRepository.BlockHash);
Assert.Equal(blocks[9].GetHash(), blockStoreLoop.StoredBlock.HashBlock);

chain = null;
Assert.Equal(blocks[9].GetHash(), blockStoreLoop.StoreTip.HashBlock);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ public void CanExecute_Reorganise()
var block14 = new ChainedBlock(blocks[14].Header, blocks[14].Header.GetHash(), block13);

BlockStoreLoop blockStoreLoop = CreateBlockStoreLoop(chain, blockRepository, @"BlockStore\LoopTest_Reorganise");
blockStoreLoop.StoredBlock = block14;
blockStoreLoop.SetStoreTip(block14);

Assert.Equal(blockStoreLoop.StoredBlock.Header.GetHash(), block14.Header.GetHash());
Assert.Equal(blockStoreLoop.StoreTip.Header.GetHash(), block14.Header.GetHash());
Assert.Equal(blockStoreLoop.BlockRepository.BlockHash, block14.Header.GetHash());

//Reorganise (delete) blocks from the block repository that is not found
ChainedBlock nextChainedBlock = block10;
var reorganiseStep = new ReorganiseBlockRepositoryStep(blockStoreLoop);
reorganiseStep.Execute(nextChainedBlock, new CancellationToken(), false).GetAwaiter().GetResult();

Assert.Equal(blockStoreLoop.StoredBlock.Header.GetHash(), block10.Previous.Header.GetHash());
Assert.Equal(blockStoreLoop.StoreTip.Header.GetHash(), block10.Previous.Header.GetHash());
Assert.Equal(blockStoreLoop.BlockRepository.BlockHash, block10.Previous.Header.GetHash());

chain = null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void CanExecute_TryPending()
List<Block> blocks = CreateBlocks(15);

// The repository has 5 blocks stored
using (var blockRepository = new BlockRepository(Network.Main, TestBase.AssureEmptyDirAsDataFolder(@"BlockStore\LoopTest_TryPending")))
using (var blockRepository = new BlockRepository(Network.Main, TestBase.AssureEmptyDirAsDataFolder(@"BlockStore\LoopTest_Pending")))
{
blockRepository.PutAsync(blocks.Take(5).Last().GetHash(), blocks.Take(5).ToList()).GetAwaiter().GetResult();

Expand All @@ -27,7 +27,7 @@ public void CanExecute_TryPending()
AppendBlocks(chain, blocks.Take(10));

// Create block store loop
BlockStoreLoop blockStoreLoop = CreateBlockStoreLoop(chain, blockRepository, @"BlockStore\LoopTest_TryPending");
BlockStoreLoop blockStoreLoop = CreateBlockStoreLoop(chain, blockRepository, @"BlockStore\LoopTest_Pending");

// Add chained blocks 5 - 9 to PendingStorage
AddToPendingStorage(blockStoreLoop, blocks[5]);
Expand All @@ -43,9 +43,7 @@ public void CanExecute_TryPending()
processPendingStorageStep.Execute(nextChainedBlock, new CancellationToken(), false).GetAwaiter().GetResult();

Assert.Equal(blocks[9].GetHash(), blockStoreLoop.BlockRepository.BlockHash);
Assert.Equal(blocks[9].GetHash(), blockStoreLoop.StoredBlock.HashBlock);

chain = null;
Assert.Equal(blocks[9].GetHash(), blockStoreLoop.StoreTip.HashBlock);
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions Stratis.Bitcoin.Tests/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
using Stratis.Bitcoin.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Stratis.Bitcoin.Tests
{
public class TestBase
{
public static DataFolder AssureEmptyDirAsDataFolder(string dir)
{
{
var dataFolder = new DataFolder(new NodeSettings { DataDir = AssureEmptyDir(dir) });
return dataFolder;
}

public static string AssureEmptyDir(string dir)
{
if (Directory.Exists(dir))
{
Directory.Delete(dir, true);
}

return dir;
}
}
}
}
3 changes: 1 addition & 2 deletions Stratis.Bitcoin/Base/ChainState.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using NBitcoin;
using System;
using System.Collections.Generic;
using System.Threading;
using NBitcoin;

namespace Stratis.Bitcoin.Base
{
Expand Down Expand Up @@ -80,6 +80,5 @@ public ChainedBlock HighestPersistedBlock
{
get; set;
}

}
}
Loading