From f4588f4783cd5d0e1ee4a7b8068485e66aab4c7c Mon Sep 17 00:00:00 2001 From: Francois de la Rouviere Date: Tue, 18 May 2021 12:52:27 +0100 Subject: [PATCH] Move init logic to initialize method (#560) --- .../CoinViews/Coindb/LeveldbCoindb.cs | 19 ++++++++++--------- .../CoinViews/Coindb/RocksDbCoindb.cs | 12 ++++++++---- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/Stratis.Bitcoin.Features.Consensus/CoinViews/Coindb/LeveldbCoindb.cs b/src/Stratis.Bitcoin.Features.Consensus/CoinViews/Coindb/LeveldbCoindb.cs index 9d80ab1563..434351e29a 100644 --- a/src/Stratis.Bitcoin.Features.Consensus/CoinViews/Coindb/LeveldbCoindb.cs +++ b/src/Stratis.Bitcoin.Features.Consensus/CoinViews/Coindb/LeveldbCoindb.cs @@ -23,6 +23,8 @@ public class LevelDbCoindb : ICoindb, IStakedb, IDisposable private static readonly byte rewindTable = 3; private static readonly byte stakeTable = 4; + private readonly string dataFolder; + /// Instance logger. private readonly ILogger logger; @@ -38,7 +40,7 @@ public class LevelDbCoindb : ICoindb, IStakedb, IDisposable private BackendPerformanceSnapshot latestPerformanceSnapShot; /// Access to dBreeze database. - private readonly DB leveldb; + private DB leveldb; private readonly DBreezeSerializer dBreezeSerializer; @@ -48,20 +50,15 @@ public LevelDbCoindb(Network network, DataFolder dataFolder, IDateTimeProvider d { } - public LevelDbCoindb(Network network, string folder, IDateTimeProvider dateTimeProvider, + public LevelDbCoindb(Network network, string dataFolder, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, INodeStats nodeStats, DBreezeSerializer dBreezeSerializer) { Guard.NotNull(network, nameof(network)); - Guard.NotEmpty(folder, nameof(folder)); + Guard.NotEmpty(dataFolder, nameof(dataFolder)); + this.dataFolder = dataFolder; this.dBreezeSerializer = dBreezeSerializer; - this.logger = loggerFactory.CreateLogger(this.GetType().FullName); - - // Open a connection to a new DB and create if not found - var options = new Options { CreateIfMissing = true }; - this.leveldb = new DB(options, folder); - this.network = network; this.performanceCounter = new BackendPerformanceCounter(dateTimeProvider); @@ -71,6 +68,10 @@ public LevelDbCoindb(Network network, string folder, IDateTimeProvider dateTimeP public void Initialize() { + // Open a connection to a new DB and create if not found + var options = new Options { CreateIfMissing = true }; + this.leveldb = new DB(options, this.dataFolder); + Block genesis = this.network.GetGenesis(); if (this.GetTipHash() == null) diff --git a/src/Stratis.Bitcoin.Features.Consensus/CoinViews/Coindb/RocksDbCoindb.cs b/src/Stratis.Bitcoin.Features.Consensus/CoinViews/Coindb/RocksDbCoindb.cs index 45f47a1127..d70ce8018d 100644 --- a/src/Stratis.Bitcoin.Features.Consensus/CoinViews/Coindb/RocksDbCoindb.cs +++ b/src/Stratis.Bitcoin.Features.Consensus/CoinViews/Coindb/RocksDbCoindb.cs @@ -23,11 +23,13 @@ public class RocksDbCoindb : ICoindb, IStakedb, IDisposable private static readonly byte rewindTable = 3; private static readonly byte stakeTable = 4; + private readonly string dataFolder; + /// Hash of the block which is currently the tip of the coinview. private HashHeightPair persistedCoinviewTip; private readonly DBreezeSerializer dBreezeSerializer; - private readonly DbOptions dbOptions; - private readonly RocksDb rocksDb; + private DbOptions dbOptions; + private RocksDb rocksDb; private BackendPerformanceSnapshot latestPerformanceSnapShot; private readonly ILogger logger; private readonly Network network; @@ -40,8 +42,7 @@ public RocksDbCoindb( INodeStats nodeStats, DBreezeSerializer dBreezeSerializer) { - this.dbOptions = new DbOptions().SetCreateIfMissing(true); - this.rocksDb = RocksDb.Open(this.dbOptions, dataFolder.CoindbPath); + this.dataFolder = dataFolder.CoindbPath; this.dBreezeSerializer = dBreezeSerializer; this.logger = LogManager.GetCurrentClassLogger(); this.network = network; @@ -53,6 +54,9 @@ public RocksDbCoindb( public void Initialize() { + this.dbOptions = new DbOptions().SetCreateIfMissing(true); + this.rocksDb = RocksDb.Open(this.dbOptions, this.dataFolder); + Block genesis = this.network.GetGenesis(); if (this.GetTipHash() == null)