Skip to content

Commit

Permalink
Decoupling mongo db dependencies to allow adding postgreSQL (#213)
Browse files Browse the repository at this point in the history
* Moved mongo service registration into mongo namespace

* Removed mongo dependency from storage operations class

* Moved the rich list scanning into mongo (this is actually only relevant for address based blockchains unlike BTC)

* The block indexing sync task is dedicated to mongo db and has been moved into the mongo namespace

* Moved Mongo driver dependency into mongo namespace

* Removed stats syncer and stats controller dependency on mongo namespace

* Removed mongo dependency from insight controller

* Removed mongo dependency from query controller

* Removed mongo dependency from QueryOrphanBlock.cs

* Removed mongo dependency from InsertStats class

* Commented out unused code that has a dependency on mongo namespace

* Renamed settings properties from prefix mongo to db

* Removed mongo dependency from core tasks

* Fixed text in controller

* Decoupled the storage batch from Mongo db dependency

* Deleted the utxo cache service that was not in use
  • Loading branch information
DavidGershony authored Jun 21, 2024
1 parent 0c7e84e commit e27a2c9
Show file tree
Hide file tree
Showing 57 changed files with 429 additions and 350 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ The default configurations are read from [appsetting.json](src/Blockcore.Indexer
// Syncing parameters
"SyncBlockchain": true,
"SyncMemoryPool": true,
"MongoBatchSize": 10000000,
"MongoBatchCount" : 10000,
"DbBatchSize": 10000000,
"DbBatchCount" : 10000,
"ParallelRequestsToTransactionRpc": 50,
"DetailedTrace": 0,
"MaxItemsInQueue": 10,
Expand Down Expand Up @@ -226,4 +226,4 @@ Blockcore.Indexer.Client.BitcoinCommunicationException: Daemon Failed Url = 'htt
---> System.Net.Sockets.SocketException (111): Connection refused
```

Issue: This happens when the DNS name is not accessible.
Issue: This happens when the DNS name is not accessible.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Blockcore.Indexer.Core.Storage.Mongo.Types;
using Blockcore.Indexer.Core.Storage.Types;

Check warning on line 1 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Investment.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Non-nullable property 'AngorKey' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [/home/runner/work/blockcore-indexer/blockcore-indexer/src/Blockcore.Indexer.Angor/Blockcore.Indexer.Angor.csproj]

Check warning on line 1 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Investment.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Non-nullable property 'InvestorPubKey' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [/home/runner/work/blockcore-indexer/blockcore-indexer/src/Blockcore.Indexer.Angor/Blockcore.Indexer.Angor.csproj]

Check warning on line 1 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Investment.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Non-nullable property 'SecretHash' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [/home/runner/work/blockcore-indexer/blockcore-indexer/src/Blockcore.Indexer.Angor/Blockcore.Indexer.Angor.csproj]

Check warning on line 1 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Investment.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Non-nullable property 'TransactionId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [/home/runner/work/blockcore-indexer/blockcore-indexer/src/Blockcore.Indexer.Angor/Blockcore.Indexer.Angor.csproj]

namespace Blockcore.Indexer.Angor.Storage.Mongo.Types;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Blockcore.Indexer.Angor.Storage.Mongo.Types;
using Blockcore.Indexer.Core.Settings;
using Blockcore.Indexer.Core.Storage.Mongo.Types;
using Blockcore.Indexer.Core.Storage.Types;
using Blockcore.Indexer.Core.Sync.SyncTasks;
using Blockcore.NBitcoin.DataEncoders;
using Microsoft.Extensions.Options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Blockcore.Indexer.Core.Operations.Types;
using Blockcore.Indexer.Core.Settings;
using Blockcore.Indexer.Core.Storage.Mongo.Types;
using Blockcore.Indexer.Core.Storage.Types;
using Blockcore.Indexer.Core.Sync.SyncTasks;
using Blockcore.NBitcoin;
using Blockcore.NBitcoin.BIP32;
Expand Down
5 changes: 3 additions & 2 deletions src/Blockcore.Indexer.Angor/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
}
},
"Indexer": {
"DbType": "MongoDb",
"ConnectionString": "mongodb://{Symbol}-mongo",
"DatabaseNameSubfix": true,

Expand All @@ -34,8 +35,8 @@
// Syncing parameters
"SyncBlockchain": true,
"SyncMemoryPool": true,
"MongoBatchSize": 10000000,
"MongoBatchCount" : 10000,
"DbBatchSize": 10000000,
"DbBatchCount" : 10000,
"ParallelRequestsToTransactionRpc": 50,
"DetailedTrace": 0,
"MaxItemsInQueue": 10,
Expand Down
1 change: 1 addition & 0 deletions src/Blockcore.Indexer.Cirrus/CirrusStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddSingleton<ICirrusStorage, CirrusMongoData>();
services.AddSingleton<ICirrusMongoDb, CirrusMongoDb>();
services.Replace(new ServiceDescriptor(typeof(IStorageBatchFactory), typeof(CirrusStorageBatchFactory),ServiceLifetime.Singleton));

services.AddControllers()
.AddApplicationPart(typeof(Startup).Assembly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

namespace Blockcore.Indexer.Cirrus.Operations.Types
{
public class CirrusStorageBatch
public class CirrusStorageBatch : MongoStorageBatch
{
public List<CirrusContractTable> CirrusContractTable { get; set; } = new List<CirrusContractTable>();

public List<CirrusContractCodeTable> CirrusContractCodeTable { get; set; } = new List<CirrusContractCodeTable>();
public List<CirrusContractTable> CirrusContractTable { get; set; } = new ();

public List<CirrusContractCodeTable> CirrusContractCodeTable { get; set; } = new ();
}
}
2 changes: 1 addition & 1 deletion src/Blockcore.Indexer.Cirrus/Storage/CirrusBlockMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Blockcore.Indexer.Cirrus.Storage.Mongo.Types;
using Blockcore.Indexer.Cirrus.Storage.Types;
using Blockcore.Indexer.Core.Client.Types;
using Blockcore.Indexer.Core.Storage;
using Blockcore.Indexer.Core.Storage.Mongo;
using Blockcore.Indexer.Core.Storage.Mongo.Types;
using Blockcore.Indexer.Core.Storage.Types;
using Blockcore.NBitcoin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class CirrusMongoStorageOperations : MongoStorageOperations
public CirrusMongoStorageOperations(
SyncConnection syncConnection,
IStorage storage,
IUtxoCache utxoCache,
IOptions<IndexerSettings> configuration,
GlobalState globalState,
IMapMongoBlockToStorageBlock mongoBlockToStorageBlock,
Expand All @@ -38,7 +37,6 @@ public CirrusMongoStorageOperations(
base(
syncConnection,
db,
utxoCache,
configuration,
globalState,
mongoBlockToStorageBlock,
Expand All @@ -52,12 +50,7 @@ public CirrusMongoStorageOperations(

protected override void OnAddToStorageBatch(StorageBatch storageBatch, SyncBlockTransactionsOperation item)
{
storageBatch.ExtraData ??= new CirrusStorageBatch();

if (!(storageBatch.ExtraData is CirrusStorageBatch cirrusStorageBatch))
{
throw new ArgumentNullException(nameof(cirrusStorageBatch));
}
CirrusStorageBatch cirrusStorageBatch = storageBatch as CirrusStorageBatch;

foreach (Transaction transaction in item.Transactions)
{
Expand Down Expand Up @@ -125,10 +118,7 @@ protected override void OnAddToStorageBatch(StorageBatch storageBatch, SyncBlock

protected override void OnPushStorageBatch(StorageBatch storageBatch)
{
if (!(storageBatch.ExtraData is CirrusStorageBatch cirrusStorageBatch))
{
throw new ArgumentNullException(nameof(cirrusStorageBatch));
}
CirrusStorageBatch cirrusStorageBatch = storageBatch as CirrusStorageBatch;

var t1 = Task.Run(() =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Blockcore.Indexer.Cirrus.Operations.Types;
using Blockcore.Indexer.Core.Operations.Types;
using Blockcore.Indexer.Core.Storage;

namespace Blockcore.Indexer.Cirrus.Storage.Mongo;

public class CirrusStorageBatchFactory : IStorageBatchFactory
{
public StorageBatch GetStorageBatch() => new CirrusStorageBatch();
}
6 changes: 3 additions & 3 deletions src/Blockcore.Indexer.Cirrus/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
},
"Indexer": {
// "ConnectionString": "mongodb://localhost",
"DbType": "MongoDb",
"ConnectionString": "mongodb://{Symbol}-mongo",
"DatabaseNameSubfix": true,

Expand All @@ -34,8 +34,8 @@
// Syncing parameters
"SyncBlockchain": true,
"SyncMemoryPool": true,
"MongoBatchSize": 10000000,
"MongoBatchCount" : 10000,
"DbBatchSize": 10000000,
"DbBatchCount" : 10000,
"ParallelRequestsToTransactionRpc": 50,
"DetailedTrace": 0,
"MaxItemsInQueue": 10,
Expand Down
2 changes: 0 additions & 2 deletions src/Blockcore.Indexer.Core/Client/Types/PeerInfo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using MongoDB.Bson.Serialization.Attributes;
using Newtonsoft.Json;

namespace Blockcore.Indexer.Core.Client.Types
{
[BsonIgnoreExtraElements]
public class PeerInfo
{
public string Addr { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions src/Blockcore.Indexer.Core/Controllers/InsightController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private Supply CalculateSupply()
/// <returns></returns>
private List<Wallet> RetrieveWallets()
{
// TODO: Funds should be stored in MongoDB, and be editable by individual chains and not hard-coded.
// TODO: Funds should be stored in the DB, and be editable by individual chains and not hard-coded.
var funds = new List<Wallet>();

List<Wallet> wallets = insightConfiguration.Wallets;
Expand All @@ -181,7 +181,7 @@ private List<Wallet> RetrieveWallets()
{
if (wallet.Address != null && wallet.Address.Length > 0)
{
List<Storage.Mongo.Types.RichlistTable> balances = storage.AddressBalances(wallet.Address);
var balances = storage.AddressBalances(wallet.Address);
long balance = balances.Sum(b => b.Balance);
wallet.Balance = balance;
}
Expand All @@ -207,7 +207,7 @@ private decimal CalculateCirculatingSupply()
}
}

List<Storage.Mongo.Types.RichlistTable> balances = storage.AddressBalances(listOfAddress);
var balances = storage.AddressBalances(listOfAddress);
long walletBalances = balances.Sum(b => b.Balance);

long circulatingSupply = totalBalance - walletBalances;
Expand Down
2 changes: 1 addition & 1 deletion src/Blockcore.Indexer.Core/Controllers/QueryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public IActionResult GetAddressTransactions([MinLength(4)][MaxLength(100)] strin
[Route("address/{address}/transactions/unspent")]
public async Task<IActionResult> GetAddressTransactionsUnspent([MinLength(30)][MaxLength(100)] string address, long confirmations = 0, [Range(0, int.MaxValue)] int offset = 0, [Range(1, 50)] int limit = 10)
{
QueryResult<Storage.Mongo.Types.OutputTable> result = await storage.GetUnspentTransactionsByAddressAsync(address, confirmations, offset, limit);
var result = await storage.GetUnspentTransactionsByAddressAsync(address, confirmations, offset, limit);

return OkPaging(result);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Blockcore.Indexer.Core/Controllers/StatsController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Blockcore.Indexer.Core.Client.Types;
using Blockcore.Indexer.Core.Handlers;
using Blockcore.Indexer.Core.Models;
using Blockcore.Indexer.Core.Storage;
using Blockcore.Indexer.Core.Storage.Mongo;
using Microsoft.AspNetCore.Mvc;

namespace Blockcore.Indexer.Core.Controllers
Expand All @@ -18,15 +19,15 @@ public class StatsController : ControllerBase
{
private readonly StatsHandler statsHandler;

private readonly MongoData storage;
private readonly IStorage storage;

/// <summary>
/// Initializes a new instance of the <see cref="StatsController"/> class.
/// </summary>
public StatsController(StatsHandler statsHandler, IStorage storage)
{
this.statsHandler = statsHandler;
this.storage = storage as MongoData;
this.storage = storage;
}

[HttpGet]
Expand Down Expand Up @@ -83,7 +84,9 @@ public async Task<IActionResult> Peers()
[Route("peers/{date}")]
public IActionResult Peers(DateTime date)
{
List<Client.Types.PeerInfo> list = storage.GetPeerFromDate(date);
List<PeerInfo> list = storage.GetPeerFromDate(date)
.Select(x => x as PeerInfo)
.ToList();
return Ok(list);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Blockcore.Indexer.Core/Models/BalanceForAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Blockcore.Indexer.Core.Models;

public class BalanceForAddress
{
public string Address { get; set; }
public long Balance { get; set; }
}
22 changes: 5 additions & 17 deletions src/Blockcore.Indexer.Core/Models/QueryBlock.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using System.Collections.Generic;

namespace Blockcore.Indexer.Core.Models
{
public class QueryBlockResults
{
public IEnumerable<QueryBlock> Blocks { get; set; }

public int Total { get; set; }
}

public class QueryBlock
{

/// <summary>
/// Gets or sets the Symbol.
/// </summary>
Expand Down Expand Up @@ -60,16 +53,16 @@ public class QueryBlock

public string Bits { get; set; }

public double Difficulty { get; set; }

public string ChainWork { get; set; }

public string Merkleroot { get; set; }

public long Nonce { get; set; }

public long Version { get; set; }

public double Difficulty { get; set; }

public string ChainWork { get; set; }

public string PosBlockSignature { get; set; }

public string PosModifierv2 { get; set; }
Expand All @@ -81,10 +74,5 @@ public class QueryBlock
public string PosBlockTrust { get; set; }

public string PosChainTrust { get; set; }

/// <summary>
/// Gets or sets the transactions.
/// </summary>
public IEnumerable<QueryTransaction> Transactions { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/Blockcore.Indexer.Core/Models/QueryBlockResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Blockcore.Indexer.Core.Models;

public class QueryBlockResults
{
public IEnumerable<QueryBlock> Blocks { get; set; }

public int Total { get; set; }
}
5 changes: 2 additions & 3 deletions src/Blockcore.Indexer.Core/Models/QueryOrphanBlock.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Blockcore.Indexer.Core.Storage.Mongo.Types;
using Blockcore.Indexer.Core.Storage.Types;

namespace Blockcore.Indexer.Core.Models
{
Expand All @@ -9,6 +8,6 @@ public class QueryOrphanBlock
public DateTime Created { get; set; }
public uint BlockIndex { get; set; }
public string BlockHash { get; set; }
public BlockTable Block { get; set; }
public QueryBlock Block { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface IStorageOperations

SyncBlockInfo PushStorageBatch(StorageBatch storageBatch);

InsertStats InsertMempoolTransactions(SyncBlockTransactionsOperation item);
void InsertMempoolTransactions(SyncBlockTransactionsOperation item);
}
}
19 changes: 0 additions & 19 deletions src/Blockcore.Indexer.Core/Operations/IUtxoCache.cs

This file was deleted.

8 changes: 0 additions & 8 deletions src/Blockcore.Indexer.Core/Operations/Types/InsertStats.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Collections.Generic;
using Blockcore.Indexer.Core.Storage.Mongo.Types;

namespace Blockcore.Indexer.Core.Operations.Types
{
#region Using Directives
Expand All @@ -23,10 +20,5 @@ public class InsertStats
/// Gets or sets the outputs.
/// </summary>
public int InputsOutputs { get; set; }

/// <summary>
/// Gets or sets the items.
/// </summary>
public List<MempoolTable> Items { get; set; }
}
}
Loading

0 comments on commit e27a2c9

Please sign in to comment.