Skip to content

Commit

Permalink
Add new properties to IChain
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Aug 8, 2024
1 parent cce20c5 commit b327bbd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Src/Autarkysoft.Bitcoin/Blockchain/Chain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public Chain(IFileManager fileMan, IBlockVerifier blockVerifier, IConsensus c, I
public IClientTime Time { get; }
/// <inheritdoc/>
public int Height { get; private set; }
/// <inheritdoc/>
public int HeaderCount => headerList.Count;

private BlockchainState _state = BlockchainState.None;
/// <inheritdoc/>
Expand All @@ -141,6 +143,11 @@ public BlockchainState State
/// <inheritdoc/>
public Digest256 Tip { get; private set; }

/// <inheritdoc/>
public BlockHeader LastHeader => headerList[^1];

/// <inheritdoc/>
public event EventHandler NewHeaderEvent;
/// <inheritdoc/>
public event EventHandler HeaderSyncEndEvent;
/// <inheritdoc/>
Expand Down Expand Up @@ -597,11 +604,19 @@ public bool ProcessHeaders(BlockHeader[] headers, INodeStatus nodeStatus)
{
AppendHeadrs(headers.AsSpan().Slice(arrIndex, count).ToArray(), count);
nodeStatus.AddBigViolation();
if (count != 0)
{
NewHeaderEvent?.Invoke(this, EventArgs.Empty);
}
return false;
}
}

AppendHeadrs(headers.AsSpan()[arrIndex..].ToArray(), count);
if (count != 0)
{
NewHeaderEvent?.Invoke(this, EventArgs.Empty);
}

return true;
}
Expand Down
12 changes: 12 additions & 0 deletions Src/Autarkysoft.Bitcoin/Blockchain/IChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,26 @@ public interface IChain
/// </summary>
int Height { get; }
/// <summary>
/// Returns number of headers (should be higher than or equal to <see cref="Height"/>)
/// </summary>
int HeaderCount { get; }
/// <summary>
/// Hash of the last block in the chain
/// </summary>
Digest256 Tip { get; }
/// <summary>
/// Returns last header
/// </summary>
BlockHeader LastHeader { get; }
/// <summary>
/// Gets or sets the current blockchain state
/// </summary>
public BlockchainState State { get; set; }
/// <summary>
/// An event to be raised when a new block header is received (useful for UI updates, miner, etc).
/// </summary>
event EventHandler NewHeaderEvent;
/// <summary>
/// An event to be raised when the initial header sync is over (signals start of adding new nodes,
/// downloading missing blocks, etc).
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions Src/Tests/Bitcoin/Blockchain/MockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public BlockchainState State
}
}

public event EventHandler NewHeaderEvent;
public event EventHandler HeaderSyncEndEvent;
public event EventHandler BlockSyncEndEvent;

Expand All @@ -50,9 +51,22 @@ public int Height
}
}

internal int _hdrCount = -1;
public int HeaderCount
{
get
{
Assert.True(_hdrCount != -1, UnexpectedCall);
return _hdrCount;
}
}

internal Digest256 _tip;
public Digest256 Tip => _tip;

internal BlockHeader _lastHdr;
public BlockHeader LastHeader => _lastHdr;

internal Target? targetToReturn;
public Target GetNextTarget(in BlockHeader hdr)
{
Expand Down

0 comments on commit b327bbd

Please sign in to comment.