Ejemplo n.º 1
0
        /// <inheritdoc />
        public Task <ChainedHeader> LoadAsync(ChainedHeader genesisHeader)
        {
            Task <ChainedHeader> task = Task.Run(() =>
            {
                ChainedHeader tip = null;

                byte[] firstRow = this.leveldb.Get(BitConverter.GetBytes(0));

                if (firstRow == null)
                {
                    genesisHeader.SetBlockHeaderStore(this.blockHeaderStore);
                    return(genesisHeader);
                }

                BlockHeader nextHeader = this.dataStoreSerializer.Deserialize <BlockHeader>(firstRow);
                Guard.Assert(nextHeader.GetHash() == genesisHeader.HashBlock); // can't swap networks

                int index = 1;
                while (true)
                {
                    byte[] row = this.leveldb.Get(BitConverter.GetBytes(index));

                    if (row == null)
                    {
                        break;
                    }

                    if ((tip != null) && (nextHeader.HashPrevBlock != tip.HashBlock))
                    {
                        break;
                    }

                    BlockHeader blockHeader = this.dataStoreSerializer.Deserialize <BlockHeader>(row);
                    tip = new ChainedHeader(nextHeader, blockHeader.HashPrevBlock, tip);
                    if (tip.Height == 0)
                    {
                        tip.SetBlockHeaderStore(this.blockHeaderStore);
                    }
                    nextHeader = blockHeader;
                    index++;
                }

                if (nextHeader != null)
                {
                    tip = new ChainedHeader(nextHeader, nextHeader.GetHash(), tip);
                }

                if (tip == null)
                {
                    genesisHeader.SetBlockHeaderStore(this.blockHeaderStore);
                    tip = genesisHeader;
                }

                this.locator = tip.GetLocator();
                return(tip);
            });

            return(task);
        }