Beispiel #1
0
        /// <inheritdoc />
        public Task <ChainedHeader> LoadAsync(ChainedHeader genesisHeader)
        {
            Task <ChainedHeader> task = Task.Run(() =>
            {
                lock (this.lockObj)
                {
                    ChainedHeader tip = null;

                    ChainData data = this.chainStore.GetChainData(0);

                    if (data == null)
                    {
                        genesisHeader.SetChainStore(this.chainStore);
                        return(genesisHeader);
                    }

                    Guard.Assert(data.Hash == genesisHeader.HashBlock); // can't swap networks

                    int index = 0;
                    while (true)
                    {
                        data = this.chainStore.GetChainData((index));

                        if (data == null)
                        {
                            break;
                        }

                        tip = new ChainedHeader(data.Hash, data.Work, tip);
                        if (tip.Height == 0)
                        {
                            tip.SetChainStore(this.chainStore);
                        }
                        index++;
                    }

                    if (tip == null)
                    {
                        genesisHeader.SetChainStore(this.chainStore);
                        tip = genesisHeader;
                    }

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

            return(task);
        }
Beispiel #2
0
        /// <inheritdoc />
        public Task <ChainedHeader> LoadAsync(ChainedHeader genesisHeader)
        {
            Task <ChainedHeader> task = Task.Run(() =>
            {
                lock (this.lockObj)
                {
                    ChainedHeader tip = null;

                    ChainData data = this.chainStore.GetChainData(0);

                    if (data == null)
                    {
                        genesisHeader.SetChainStore(this.chainStore);
                        return(genesisHeader);
                    }

                    Guard.Assert(data.Hash == genesisHeader.HashBlock); // can't swap networks

                    var list = this.chainStore.GetChainData();

                    foreach (var chainedData in list)
                    {
                        // Create a new ChainedHeader with reference to previous. This will build a large object graph of all headers.
                        tip = new ChainedHeader(chainedData.Hash, chainedData.Work, tip);

                        if (tip.Height == 0)
                        {
                            tip.SetChainStore(this.chainStore);
                        }
                    }

                    if (tip == null)
                    {
                        genesisHeader.SetChainStore(this.chainStore);
                        tip = genesisHeader;
                    }

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

            return(task);
        }
Beispiel #3
0
        public ChainData GetChainData(int height)
        {
            byte[] bytes = null;

            lock (this.locker)
            {
                bytes = this.rocksdb.Get(DBH.Key(ChainTableName, BitConverter.GetBytes(height)));
            }

            if (bytes == null)
            {
                return(null);
            }

            var data = new ChainData();

            data.FromBytes(bytes, this.network.Consensus.ConsensusFactory);

            return(data);
        }