Пример #1
0
        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);
                }
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
                }
            }
        }