public void AddGenesisBlock(ChainedHeader genesisHeader) { if (genesisHeader.Height != 0) { throw new ArgumentException("genesisHeader"); } lock (cachedHeaders) if (this.blockStorage.Value.TryAddChainedHeader(genesisHeader)) { this.cachedHeaders.Value[genesisHeader.Hash] = genesisHeader; ChainedHeaderAdded?.Invoke(genesisHeader); } }
private bool TryChainHeader(BlockHeader blockHeader, out ChainedHeader chainedHeader, bool suppressEvent) { if (TryGetChainedHeader(blockHeader.Hash, out chainedHeader)) { return(false); } else { ChainedHeader previousChainedHeader; if (TryGetChainedHeader(blockHeader.PreviousBlock, out previousChainedHeader)) { chainedHeader = ChainedHeader.CreateFromPrev(previousChainedHeader, blockHeader, DateTimeOffset.Now); if (chainedHeader == null) { return(false); } lock (cachedHeaders) if (this.blockStorage.Value.TryAddChainedHeader(chainedHeader)) { this.cachedHeaders.Value[chainedHeader.Hash] = chainedHeader; if (!suppressEvent) { ChainedHeaderAdded?.Invoke(chainedHeader); } return(true); } else { logger.Warn("Unexpected condition: validly chained header could not be added"); } } } chainedHeader = default(ChainedHeader); return(false); }
public void ChainHeaders(IEnumerable <BlockHeader> blockHeaders) { var addedHeaders = new List <ChainedHeader>(); try { foreach (var blockHeader in blockHeaders) { ChainedHeader chainedHeader; if (TryChainHeader(blockHeader, out chainedHeader, suppressEvent: true)) { addedHeaders.Add(chainedHeader); } } } finally { foreach (var chainedHeader in addedHeaders) { ChainedHeaderAdded?.Invoke(chainedHeader); } } }