public bool TryGetAtHeight(int height, [MaybeNullWhen(false)] out HeaderNode?headerNode) { using (GlobalLocks.ReadOnMainAsync().GetAwaiter().GetResult()) { return(HeadersTree.TryGetNodeOnBestChain(height, out headerNode)); } }
public HeaderNode AddToBlockIndex(BlockHeader header) { using (GlobalLocks.WriteOnMainAsync().GetAwaiter().GetResult()) { // Check for duplicate if (TryGetKnownHeaderNode(header.Hash, out HeaderNode? headerNode)) { return(headerNode); } if (!TryGetKnownHeaderNode(header.PreviousBlockHash, out HeaderNode? previousHeader)) { ThrowHelper.ThrowNotSupportedException("Previous hash not found (shouldn't happen)."); } headerNode = new HeaderNode(header, previousHeader); if (BestHeader == null || BestHeader.ChainWork < headerNode.ChainWork) { BestHeader = headerNode; } HeadersTree.Add(headerNode); _blockHeaderRepository.TryAdd(header); return(headerNode); } }
public bool TryGetNext(HeaderNode headerNode, [MaybeNullWhen(false)] out HeaderNode nextHeaderNode) { using (GlobalLocks.ReadOnMainAsync().GetAwaiter().GetResult()) { if (IsInBestChain(headerNode) && HeadersTree.TryGetNodeOnBestChain(headerNode.Height + 1, out nextHeaderNode)) { return(true); } nextHeaderNode = null; return(false); } }
public bool IsInBestChain(HeaderNode headerNode) { return(HeadersTree.IsInBestChain(headerNode)); }
public BlockLocator?GetLocator(HeaderNode headerNode) { using Microsoft.VisualStudio.Threading.AsyncReaderWriterLock.Releaser readMainLock = GlobalLocks.ReadOnMainAsync().GetAwaiter().GetResult(); return(HeadersTree.GetLocator(headerNode)); }
public bool TryGetKnownHeaderNode(UInt256?blockHash, [MaybeNullWhen(false)] out HeaderNode node) { //using var readLock = new ReadLock(this.theLock); using Microsoft.VisualStudio.Threading.AsyncReaderWriterLock.Releaser readMainLock = GlobalLocks.ReadOnMainAsync().GetAwaiter().GetResult(); return(HeadersTree.TryGetNode(blockHash, false, out node)); }
public bool TryGetBestChainHeaderNode(UInt256 blockHash, [MaybeNullWhen(false)] out HeaderNode node) { using Microsoft.VisualStudio.Threading.AsyncReaderWriterLock.Releaser readMainLock = GlobalLocks.ReadOnMainAsync().GetAwaiter().GetResult(); return(HeadersTree.TryGetNode(blockHash, true, out node)); }