/// <inheritdoc /> public Task <ChainedHeader> LoadAsync(ChainedHeader genesisHeader) { Task <ChainedHeader> task = Task.Run(() => { 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); }
public void SaveChainToDisk() { string dir = CreateTestDir(this); var chain = new ChainIndexer(this.Network); this.AppendBlock(chain); using (var repo = new ChainRepository(new RocksDbChainStore(chain.Network, new DataFolder(dir), chain))) { repo.SaveAsync(chain).GetAwaiter().GetResult(); } using (var engine = RocksDb.Open(new DbOptions().SetCreateIfMissing(), new DataFolder(dir).ChainPath)) { ChainedHeader tip = null; var itr = engine.NewIterator(); itr.SeekToFirst(); while (itr.Valid()) { if (itr.Key()[0] == 1) { var data = new ChainRepository.ChainRepositoryData(); data.FromBytes(itr.Value().ToArray(), this.Network.Consensus.ConsensusFactory); tip = new ChainedHeader(data.Hash, data.Work, tip); if (tip.Height == 0) { tip.SetChainStore(new ChainStore()); } } itr.Next(); } Assert.Equal(tip, chain.Tip); } }
public void SaveChainToDisk() { string dir = CreateTestDir(this); var chain = new ChainIndexer(KnownNetworks.StraxRegTest); this.AppendBlock(chain); using (var repo = new ChainRepository(new LevelDbChainStore(chain.Network, new DataFolder(dir), chain))) { repo.SaveAsync(chain).GetAwaiter().GetResult(); } using (var engine = new DB(new Options { CreateIfMissing = true }, new DataFolder(dir).ChainPath)) { ChainedHeader tip = null; var itr = engine.GetEnumerator(); while (itr.MoveNext()) { if (itr.Current.Key[0] == 1) { var data = new ChainRepository.ChainRepositoryData(); data.FromBytes(itr.Current.Value.ToArray(), this.Network.Consensus.ConsensusFactory); tip = new ChainedHeader(data.Hash, data.Work, tip); if (tip.Height == 0) { tip.SetChainStore(new ChainStore()); } } } Assert.Equal(tip, chain.Tip); } }
/// <inheritdoc /> public Task <ChainedHeader> LoadAsync(ChainedHeader genesisHeader) { Task <ChainedHeader> task = Task.Run(() => { 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 height = 0; while (true) { data = this.chainStore.GetChainData(height); if (data == null) { break; } tip = new ChainedHeader(data.Hash, data.Work, tip); if (tip.Height == 0) { tip.SetChainStore(this.chainStore); } if (height % 50_000 == 0) { if (this.signals != null) { this.signals.Publish(new FullNodeEvent() { Message = $"Loading chain at height {height}.", State = FullNodeState.Initializing.ToString() }); } this.logger.LogInformation($"Loading chain at height {height}."); } height++; } if (tip == null) { genesisHeader.SetChainStore(this.chainStore); tip = genesisHeader; } else { // Confirm that the chain tip exists in the headers table. this.chainStore.GetHeader(tip, tip.HashBlock); } this.locator = tip.GetLocator(); return(tip); }); return(task); }