Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ChainIndexer GetHeader overloads #543

Open
wants to merge 3 commits into
base: release/1.6.0.0
Choose a base branch
from
Open
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
28 changes: 14 additions & 14 deletions src/NBitcoin.Tests/ChainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public void CanLoadAndSaveConcurrentChain()
cchain.Load(bytes);

Assert.Equal(cchain.Tip, chain.Tip);
Assert.NotNull(cchain.GetHeader(0));
Assert.NotNull(cchain.GetHeaderByHeight(0));

cchain = new ChainIndexer(this.networkTest);
cchain.Load(cchain.ToBytes());
Assert.NotNull(cchain.GetHeader(0));
Assert.NotNull(cchain.GetHeaderByHeight(0));
}

[Fact]
Expand All @@ -97,15 +97,15 @@ public void CanBuildConcurrentChain()
Assert.Equal(cchain.SetTip(chain.Tip), b0);
Assert.Equal(cchain.Tip, chain.Tip);

Assert.Equal(cchain.GetHeader(5), chain.Tip);
Assert.Equal(cchain.GetHeader(b5.HashBlock), chain.Tip);
Assert.Equal(cchain.GetHeaderByHeight(5), chain.Tip);
Assert.Equal(cchain.GetHeaderByHash(b5.HashBlock), chain.Tip);

Assert.Equal(cchain.SetTip(b1), b1);
Assert.Null(cchain.GetHeader(b5.HashBlock));
Assert.Null(cchain.GetHeader(b2.HashBlock));
Assert.Null(cchain.GetHeaderByHash(b5.HashBlock));
Assert.Null(cchain.GetHeaderByHash(b2.HashBlock));

Assert.Equal(cchain.SetTip(b5), b1);
Assert.Equal(cchain.GetHeader(b5.HashBlock), chain.Tip);
Assert.Equal(cchain.GetHeaderByHash(b5.HashBlock), chain.Tip);

chain.SetTip(b2);
this.AddBlock(chain);
Expand All @@ -115,10 +115,10 @@ public void CanBuildConcurrentChain()

Assert.Equal(cchain.SetTip(b6b), b2);

Assert.Null(cchain.GetHeader(b5.HashBlock));
Assert.Equal(cchain.GetHeader(b2.HashBlock), b2);
Assert.Equal(cchain.GetHeader(6), b6b);
Assert.Equal(cchain.GetHeader(5), b5b);
Assert.Null(cchain.GetHeaderByHash(b5.HashBlock));
Assert.Equal(cchain.GetHeaderByHash(b2.HashBlock), b2);
Assert.Equal(cchain.GetHeaderByHeight(6), b6b);
Assert.Equal(cchain.GetHeaderByHeight(5), b5b);
}

private ChainedHeader AddBlock(ChainIndexer chainIndexer)
Expand All @@ -127,7 +127,7 @@ private ChainedHeader AddBlock(ChainIndexer chainIndexer)
header.Nonce = RandomUtils.GetUInt32();
header.HashPrevBlock = chainIndexer.Tip.HashBlock;
chainIndexer.SetTip(header);
return chainIndexer.GetHeader(header.GetHash());
return chainIndexer.GetHeaderByHash(header.GetHash());
}

[Fact]
Expand Down Expand Up @@ -158,10 +158,10 @@ public void CanCalculateDifficulty()
int height = int.Parse(history.Split(',')[0]);
var expectedTarget = new Target(new BouncyCastle.Math.BigInteger(history.Split(',')[1], 10));

BlockHeader block = main.GetHeader(height).Header;
BlockHeader block = main.GetHeaderByHeight(height).Header;

Assert.Equal(expectedTarget, block.Bits);
Target target = main.GetHeader(height).GetWorkRequired(this.network);
Target target = main.GetHeaderByHeight(height).GetWorkRequired(this.network);
Assert.Equal(expectedTarget, target);
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/NBitcoin/ChainIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ChainIndexer
/// The tip height of the best known validated chain.
/// </summary>
public int Height => this.Tip.Height;
public ChainedHeader Genesis => this.GetHeader(0);
public ChainedHeader Genesis => this.GetHeaderByHeight(0);

public ChainIndexer()
{
Expand Down Expand Up @@ -90,7 +90,7 @@ public ChainedHeader FindFork(IEnumerable<uint256> hashes)
// Find the first block the caller has in the main chain.
foreach (uint256 hash in hashes)
{
ChainedHeader chainedHeader = this.GetHeader(hash);
ChainedHeader chainedHeader = this.GetHeaderByHash(hash);
if (chainedHeader != null)
return chainedHeader;
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public ChainedHeader FindFork(BlockLocator locator)
/// <returns>Enumeration of chained block headers after given block hash.</returns>
public IEnumerable<ChainedHeader> EnumerateAfter(uint256 blockHash)
{
ChainedHeader block = this.GetHeader(blockHash);
ChainedHeader block = this.GetHeaderByHash(blockHash);

if (block == null)
return new ChainedHeader[0];
Expand Down Expand Up @@ -146,7 +146,7 @@ public IEnumerable<ChainedHeader> EnumerateToTip(ChainedHeader block)
/// <returns>Enumeration of chained block headers from the given block hash to tip.</returns>
public IEnumerable<ChainedHeader> EnumerateToTip(uint256 blockHash)
{
ChainedHeader block = this.GetHeader(blockHash);
ChainedHeader block = this.GetHeaderByHash(blockHash);
if (block == null)
yield break;

Expand All @@ -168,7 +168,7 @@ public virtual IEnumerable<ChainedHeader> EnumerateAfter(ChainedHeader block)

while (true)
{
ChainedHeader b = this.GetHeader(i);
ChainedHeader b = this.GetHeaderByHeight(i);
if ((b == null) || (b.Previous != prev))
yield break;

Expand Down Expand Up @@ -215,7 +215,7 @@ public void Remove(ChainedHeader removeTip)
/// <summary>
/// Get a <see cref="ChainedHeader"/> based on it's hash.
/// </summary>
public virtual ChainedHeader GetHeader(uint256 id)
public virtual ChainedHeader GetHeaderByHash(uint256 id)
{
lock (this.lockObject)
{
Expand All @@ -228,7 +228,7 @@ public virtual ChainedHeader GetHeader(uint256 id)
/// <summary>
/// Get a <see cref="ChainedHeader"/> based on it's height.
/// </summary>
public virtual ChainedHeader GetHeader(int height)
public virtual ChainedHeader GetHeaderByHeight(int height)
{
lock (this.lockObject)
{
Expand All @@ -241,12 +241,12 @@ public virtual ChainedHeader GetHeader(int height)
/// <summary>
/// Get a <see cref="ChainedHeader"/> based on it's height.
/// </summary>
public ChainedHeader this[int key] => this.GetHeader(key);
public ChainedHeader this[int key] => this.GetHeaderByHeight(key);

/// <summary>
/// Get a <see cref="ChainedHeader"/> based on it's hash.
/// </summary>
public ChainedHeader this[uint256 id] => this.GetHeader(id);
public ChainedHeader this[uint256 id] => this.GetHeaderByHash(id);

public override string ToString()
{
Expand Down
6 changes: 3 additions & 3 deletions src/Stratis.Bitcoin.Features.Api/NodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public IActionResult GetBlockHeader([FromQuery] string hash, bool isJsonFormat =

this.logger.LogDebug("GetBlockHeader {0}", hash);

BlockHeader blockHeader = this.chainIndexer?.GetHeader(uint256.Parse(hash))?.Header;
BlockHeader blockHeader = this.chainIndexer?.GetHeaderByHash(uint256.Parse(hash))?.Header;

if (blockHeader == null)
return this.NotFound($"Block header for '{hash}' not found");
Expand Down Expand Up @@ -723,7 +723,7 @@ internal ChainedHeader GetTransactionBlock(uint256 trxid, ChainIndexer chain)
uint256 blockid = this.blockStore?.GetBlockIdByTransactionId(trxid);
if (blockid != null)
{
block = chain?.GetHeader(blockid);
block = chain?.GetHeaderByHash(blockid);
}

return block;
Expand All @@ -745,7 +745,7 @@ private ChainedHeader GetTransactionBlock(uint256 trxid)

uint256 blockid = this.blockStore?.GetBlockIdByTransactionId(trxid);
if (blockid != null)
block = this.chainIndexer?.GetHeader(blockid);
block = this.chainIndexer?.GetHeaderByHash(blockid);

return block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public AddressIndexerTests()
var mockingContext = new MockingContext(mockingServices);

this.addressIndexer = mockingContext.GetService<IAddressIndexer>();
this.genesisHeader = mockingContext.GetService<ChainIndexer>().GetHeader(0);
this.genesisHeader = mockingContext.GetService<ChainIndexer>().GetHeaderByHeight(0);
this.consensusManagerMock = mockingContext.GetService<Mock<IConsensusManager>>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void GetBlockCount_ReturnsHeightFromChainState()
logger.Setup(l => l.CreateLogger(It.IsAny<string>())).Returns(Mock.Of<ILogger>);

chainState.Setup(c => c.ConsensusTip)
.Returns(chainIndexer.GetHeader(2));
.Returns(chainIndexer.GetHeaderByHeight(2));

var controller = new BlockStoreController(new StraxMain(), logger.Object, store.Object, chainState.Object, chainIndexer, addressIndexer.Object, utxoIndexer.Object, scriptAddressReader);

Expand All @@ -188,7 +188,7 @@ public void Get_Blocks_Should_Return_Blocks()
logger.Setup(l => l.CreateLogger(It.IsAny<string>())).Returns(Mock.Of<ILogger>);

chainState.Setup(c => c.ConsensusTip)
.Returns(chainIndexer.GetHeader(2));
.Returns(chainIndexer.GetHeaderByHeight(2));

store.Setup(s => s.GetBlocks(It.IsAny<List<uint256>>())).Returns((List<uint256> hashes) => hashes.Select(h => chainIndexer[h].Block).ToList());

Expand All @@ -214,7 +214,7 @@ private static (Mock<IBlockStore> store, BlockStoreController controller) GetCon

var chain = new Mock<ChainIndexer>();
Block block = Block.Parse(BlockAsHex, new StraxMain().Consensus.ConsensusFactory);
chain.Setup(c => c.GetHeader(It.IsAny<uint256>())).Returns(new ChainedHeader(block.Header, block.Header.GetHash(), 1));
chain.Setup(c => c.GetHeaderByHash(It.IsAny<uint256>())).Returns(new ChainedHeader(block.Header, block.Header.GetHash(), 1));
chain.Setup(x => x.Tip).Returns(new ChainedHeader(block.Header, block.Header.GetHash(), 1));

var controller = new BlockStoreController(new StraxMain(), logger.Object, store.Object, chainState.Object, chain.Object, addressIndexer.Object, utxoIndexer.Object, scriptAddressReader);
Expand Down
20 changes: 10 additions & 10 deletions src/Stratis.Bitcoin.Features.BlockStore.Tests/BlockStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ public async Task BatchIsSavedAfterSizeThresholdReachedAsync()
// Send all the blocks to the block store except for the last one because that will trigger batch saving because of reaching the tip.
for (int i = 1; i < count; i++)
{
ChainedHeader header = longChainIndexer.GetHeader(i);
ChainedHeader header = longChainIndexer.GetHeaderByHeight(i);

this.blockStoreQueue.AddToPending(new ChainedHeaderBlock(block, header));
}

await WaitUntilQueueIsEmptyAsync().ConfigureAwait(false);
Assert.Equal(longChainIndexer.GetHeader(count - 1), this.chainState.BlockStoreTip);
Assert.Equal(longChainIndexer.GetHeaderByHeight(count - 1), this.chainState.BlockStoreTip);
Assert.True(this.repositorySavesCount > 0);
}

Expand All @@ -219,7 +219,7 @@ public async Task BatchIsSavedOnShutdownAsync()

for (int i = 1; i < this.chainIndexer.Height - 1; i++)
{
lastHeader = this.chainIndexer.GetHeader(i);
lastHeader = this.chainIndexer.GetHeaderByHeight(i);
Block block = this.network.Consensus.ConsensusFactory.CreateBlock();
block.GetSerializedSize();

Expand Down Expand Up @@ -253,7 +253,7 @@ public async Task BatchIsSavedWhenAtConsensusTipAsync()

for (int i = 1; i <= this.chainIndexer.Height; i++)
{
ChainedHeader lastHeader = this.chainIndexer.GetHeader(i);
ChainedHeader lastHeader = this.chainIndexer.GetHeaderByHeight(i);
Block block = this.network.Consensus.ConsensusFactory.CreateBlock();
block.GetSerializedSize();

Expand Down Expand Up @@ -303,7 +303,7 @@ public async Task ReorgedBlocksAreNotSavedAsync()
Block block = this.network.Consensus.ConsensusFactory.CreateBlock();
block.GetSerializedSize();

this.blockStoreQueue.AddToPending(new ChainedHeaderBlock(block, alternativeChainIndexer.GetHeader(i)));
this.blockStoreQueue.AddToPending(new ChainedHeaderBlock(block, alternativeChainIndexer.GetHeaderByHeight(i)));
}

// Present second chain which has more work and reorgs blocks from genesis.
Expand All @@ -312,7 +312,7 @@ public async Task ReorgedBlocksAreNotSavedAsync()
Block block = this.network.Consensus.ConsensusFactory.CreateBlock();
block.GetSerializedSize();

this.blockStoreQueue.AddToPending(new ChainedHeaderBlock(block, this.chainIndexer.GetHeader(i)));
this.blockStoreQueue.AddToPending(new ChainedHeaderBlock(block, this.chainIndexer.GetHeaderByHeight(i)));
}

await this.WaitUntilQueueIsEmptyAsync().ConfigureAwait(false);
Expand All @@ -325,7 +325,7 @@ public async Task ReorgedBlocksAreNotSavedAsync()
this.blockStoreQueue.Dispose();

// Make sure that blocks only from 2nd chain were saved.
Assert.Equal(this.chainIndexer.GetHeader(realChainLenght - 1), this.chainState.BlockStoreTip);
Assert.Equal(this.chainIndexer.GetHeaderByHeight(realChainLenght - 1), this.chainState.BlockStoreTip);
Assert.Equal(1, this.repositorySavesCount);
Assert.Equal(realChainLenght - 1, this.repositoryTotalBlocksSaved);
}
Expand Down Expand Up @@ -356,11 +356,11 @@ public async Task ReorgedBlocksAreDeletedFromRepositoryIfReorgDetectedAsync()
Block block = this.network.Consensus.ConsensusFactory.CreateBlock();
block.GetSerializedSize();

this.blockStoreQueue.AddToPending(new ChainedHeaderBlock(block, this.chainIndexer.GetHeader(i)));
this.blockStoreQueue.AddToPending(new ChainedHeaderBlock(block, this.chainIndexer.GetHeaderByHeight(i)));
}

// Create alternative chain with fork point at 450.
ChainedHeader prevBlock = this.chainIndexer.GetHeader(450);
ChainedHeader prevBlock = this.chainIndexer.GetHeaderByHeight(450);
var alternativeBlocks = new List<ChainedHeader>();
for (int i = 0; i < 100; i++)
{
Expand Down Expand Up @@ -410,7 +410,7 @@ public async Task ReorgedBlocksAreDeletedFromRepositoryIfReorgDetectedAsync()
if (i == this.chainIndexer.Height)
blockStoreFlushCondition.Setup(s => s.ShouldFlush).Returns(true);

this.blockStoreQueue.AddToPending(new ChainedHeaderBlock(block, this.chainIndexer.GetHeader(i)));
this.blockStoreQueue.AddToPending(new ChainedHeaderBlock(block, this.chainIndexer.GetHeaderByHeight(i)));
}

await this.WaitUntilQueueIsEmptyAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ public LastBalanceDecreaseTransactionModel GetLastBalanceDecreaseTransaction(str
if (lastBalanceHeight == 0)
return null;

ChainedHeader header = this.chainIndexer.GetHeader(lastBalanceHeight);
ChainedHeader header = this.chainIndexer.GetHeaderByHeight(lastBalanceHeight);

if (header == null)
return null;
Expand Down
6 changes: 3 additions & 3 deletions src/Stratis.Bitcoin.Features.BlockStore/BlockStoreQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,12 @@ public Block GetBlock(uint256 blockHash)

if (block == null)
{
ChainedHeader chainedHeader = this.chainIndexer.GetHeader(blockHash);
ChainedHeader chainedHeader = this.chainIndexer.GetHeaderByHash(blockHash);
if (chainedHeader != null && chainedHeader.Height <= this.blockRepository.TipHashAndHeight.Height)
{
// The block we were looking for occurs at a height that would be present in the block repository.
// If the block repository tip is on the consensus chain then the block should have been present.
if (this.chainIndexer.GetHeader(this.blockRepository.TipHashAndHeight.Hash) != null)
if (this.chainIndexer.GetHeaderByHash(this.blockRepository.TipHashAndHeight.Hash) != null)
{
this.logger.LogError("The block repository is missing some blocks that should be present given the advertised tip.");
this.logger.LogInformation("You will have to re-sync your node to correct this issue.");
Expand Down Expand Up @@ -416,7 +416,7 @@ private void SetStoreTip(ChainedHeader newTip)
/// <returns>The store tip set by this method.</returns>
private ChainedHeader RecoverStoreTip()
{
ChainedHeader blockStoreTip = this.chainIndexer.GetHeader(this.blockRepository.TipHashAndHeight.Hash);
ChainedHeader blockStoreTip = this.chainIndexer.GetHeaderByHash(this.blockRepository.TipHashAndHeight.Hash);
if (blockStoreTip != null)
return blockStoreTip;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public IActionResult GetBlock([FromQuery] SearchByHashRequest query)
{
uint256 blockId = uint256.Parse(query.Hash);

ChainedHeader chainedHeader = this.chainIndexer.GetHeader(blockId);
ChainedHeader chainedHeader = this.chainIndexer.GetHeaderByHash(blockId);

if (chainedHeader == null)
return this.NotFound("Block not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void ConstructProvenHeaderPayload_Consecutive_Headers()
var hashes = new List<uint256>();
for (int i = 1; i < 5; i++)
{
var chainedHeaderToAdd = chain.GetHeader(i);
var chainedHeaderToAdd = chain.GetHeaderByHeight(i);
hashes.Add(chainedHeaderToAdd.HashBlock);
}
hashes.Reverse();
Expand Down
Loading