Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        public ChainState(ILogger <ChainState> logger,
                          IHeadersTree headersTree,
                          ICoinsView coinsView,
                          IBlockHeaderRepository blockHeaderRepository,
                          IConsensusParameters consensusParameters)
        {
            this.logger            = logger;
            HeadersTree            = headersTree;
            this.coinsView         = coinsView;
            _blockHeaderRepository = blockHeaderRepository;
            _consensusParameters   = consensusParameters;
            ChainTip   = headersTree.Genesis;
            BestHeader = headersTree.Genesis;

            _blockHeaderRepository.TryAdd(consensusParameters.GenesisHeader);
        }