Skip to content

Commit f4588f4

Browse files
committed
Move init logic to initialize method (#560)
1 parent 736bb27 commit f4588f4

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/Stratis.Bitcoin.Features.Consensus/CoinViews/Coindb/LeveldbCoindb.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class LevelDbCoindb : ICoindb, IStakedb, IDisposable
2323
private static readonly byte rewindTable = 3;
2424
private static readonly byte stakeTable = 4;
2525

26+
private readonly string dataFolder;
27+
2628
/// <summary>Instance logger.</summary>
2729
private readonly ILogger logger;
2830

@@ -38,7 +40,7 @@ public class LevelDbCoindb : ICoindb, IStakedb, IDisposable
3840
private BackendPerformanceSnapshot latestPerformanceSnapShot;
3941

4042
/// <summary>Access to dBreeze database.</summary>
41-
private readonly DB leveldb;
43+
private DB leveldb;
4244

4345
private readonly DBreezeSerializer dBreezeSerializer;
4446

@@ -48,20 +50,15 @@ public LevelDbCoindb(Network network, DataFolder dataFolder, IDateTimeProvider d
4850
{
4951
}
5052

51-
public LevelDbCoindb(Network network, string folder, IDateTimeProvider dateTimeProvider,
53+
public LevelDbCoindb(Network network, string dataFolder, IDateTimeProvider dateTimeProvider,
5254
ILoggerFactory loggerFactory, INodeStats nodeStats, DBreezeSerializer dBreezeSerializer)
5355
{
5456
Guard.NotNull(network, nameof(network));
55-
Guard.NotEmpty(folder, nameof(folder));
57+
Guard.NotEmpty(dataFolder, nameof(dataFolder));
5658

59+
this.dataFolder = dataFolder;
5760
this.dBreezeSerializer = dBreezeSerializer;
58-
5961
this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
60-
61-
// Open a connection to a new DB and create if not found
62-
var options = new Options { CreateIfMissing = true };
63-
this.leveldb = new DB(options, folder);
64-
6562
this.network = network;
6663
this.performanceCounter = new BackendPerformanceCounter(dateTimeProvider);
6764

@@ -71,6 +68,10 @@ public LevelDbCoindb(Network network, string folder, IDateTimeProvider dateTimeP
7168

7269
public void Initialize()
7370
{
71+
// Open a connection to a new DB and create if not found
72+
var options = new Options { CreateIfMissing = true };
73+
this.leveldb = new DB(options, this.dataFolder);
74+
7475
Block genesis = this.network.GetGenesis();
7576

7677
if (this.GetTipHash() == null)

src/Stratis.Bitcoin.Features.Consensus/CoinViews/Coindb/RocksDbCoindb.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ public class RocksDbCoindb : ICoindb, IStakedb, IDisposable
2323
private static readonly byte rewindTable = 3;
2424
private static readonly byte stakeTable = 4;
2525

26+
private readonly string dataFolder;
27+
2628
/// <summary>Hash of the block which is currently the tip of the coinview.</summary>
2729
private HashHeightPair persistedCoinviewTip;
2830
private readonly DBreezeSerializer dBreezeSerializer;
29-
private readonly DbOptions dbOptions;
30-
private readonly RocksDb rocksDb;
31+
private DbOptions dbOptions;
32+
private RocksDb rocksDb;
3133
private BackendPerformanceSnapshot latestPerformanceSnapShot;
3234
private readonly ILogger logger;
3335
private readonly Network network;
@@ -40,8 +42,7 @@ public RocksDbCoindb(
4042
INodeStats nodeStats,
4143
DBreezeSerializer dBreezeSerializer)
4244
{
43-
this.dbOptions = new DbOptions().SetCreateIfMissing(true);
44-
this.rocksDb = RocksDb.Open(this.dbOptions, dataFolder.CoindbPath);
45+
this.dataFolder = dataFolder.CoindbPath;
4546
this.dBreezeSerializer = dBreezeSerializer;
4647
this.logger = LogManager.GetCurrentClassLogger();
4748
this.network = network;
@@ -53,6 +54,9 @@ public RocksDbCoindb(
5354

5455
public void Initialize()
5556
{
57+
this.dbOptions = new DbOptions().SetCreateIfMissing(true);
58+
this.rocksDb = RocksDb.Open(this.dbOptions, this.dataFolder);
59+
5660
Block genesis = this.network.GetGenesis();
5761

5862
if (this.GetTipHash() == null)

0 commit comments

Comments
 (0)