Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Blockcore/Consensus/Chain/ChainRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ public Task<ChainedHeader> LoadAsync(ChainedHeader genesisHeader)

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

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

if (data == null)
break;
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);

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

if (tip == null)
Expand Down
9 changes: 9 additions & 0 deletions src/Blockcore/Consensus/Chain/ChainStore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Blockcore.Consensus.BlockInfo;
using NBitcoin;

Expand All @@ -12,6 +13,8 @@ public interface IChainStore

bool PutHeader(BlockHeader blockHeader);

IEnumerable<ChainData> GetChainData();

ChainData GetChainData(int height);

void PutChainData(IEnumerable<ChainDataItem> items);
Expand Down Expand Up @@ -78,6 +81,12 @@ public bool PutHeader(BlockHeader blockHeader)
return this.headers.TryAdd(blockHeader.GetHash(), blockHeader);
}

public IEnumerable<ChainData> GetChainData()
{
// Order by height and return new array with ChainData.
return this.chainData.OrderBy(c => c.Key).Select(c => c.Value);
}

public ChainData GetChainData(int height)
{
if (!this.chainData.TryGetValue(height, out ChainData data))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Blockcore.Configuration;
using Blockcore.Consensus;
using Blockcore.Consensus.BlockInfo;
Expand Down Expand Up @@ -99,6 +100,34 @@ public bool PutHeader(BlockHeader blockHeader)
return true;
}

public IEnumerable<ChainData> GetChainData()
{
Dictionary<int, ChainData> list = new Dictionary<int, ChainData>();

lock (this.locker)
{
using (var iterator = this.leveldb.CreateIterator())
{
iterator.SeekToFirst();

while (iterator.IsValid() && iterator.Key()[0] == ChainTableName)
{
var height = BitConverter.ToInt32(iterator.Key().AsSpan(1));
byte[] bytes = iterator.Value();

var data = new ChainData();
data.FromBytes(bytes, this.network.Consensus.ConsensusFactory);
list.Add(height, data);

iterator.Next();
}
}
}

// Order by height and return new array with ChainData.
return list.OrderBy(c => c.Key).Select(c => c.Value);
}

public ChainData GetChainData(int height)
{
byte[] bytes = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Blockcore.Configuration;
using Blockcore.Consensus;
using Blockcore.Consensus.BlockInfo;
Expand Down Expand Up @@ -105,6 +106,34 @@ public bool PutHeader(BlockHeader blockHeader)
return true;
}

public IEnumerable<ChainData> GetChainData()
{
Dictionary<int, ChainData> list = new Dictionary<int, ChainData>();

lock (this.locker)
{
using (var iterator = this.rocksdb.NewIterator())
{
iterator.SeekToFirst();

while (iterator.Valid() && iterator.Key()[0] == ChainTableName)
{
var height = BitConverter.ToInt32(iterator.Key().AsSpan(1));
byte[] bytes = iterator.Value();

var data = new ChainData();
data.FromBytes(bytes, this.network.Consensus.ConsensusFactory);
list.Add(height, data);

iterator.Next();
}
}
}

// Order by height and return new array with ChainData.
return list.OrderBy(c => c.Key).Select(c => c.Value);
}

public ChainData GetChainData(int height)
{
byte[] bytes = null;
Expand Down