Skip to content

Commit

Permalink
changed IStorage.cs to not depend on mondo types (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGershony authored Jun 27, 2024
1 parent e27a2c9 commit 86e523f
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 68 deletions.
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 @@ -203,7 +203,7 @@ public IActionResult GetOrphanBlocks([Range(0, int.MaxValue)] int? offset = null
[Route("block/orphan/{hash}")]
public IActionResult GetOrphanBlockByHash(string hash)
{
return OkItem(storage.OrphanBlockByHash(hash));
return OkItem(storage.OrphanBlockByHash<object>(hash));
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Blockcore.Indexer.Core/Storage/DatabaseRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static IServiceCollection AddMongoDatabase( this IServiceCollection servi
services.AddTransient<IMapMongoBlockToStorageBlock, MapMongoBlockToStorageBlock>();
services.AddScoped<TaskRunner, MongoDbBlockIndexer>();
services.AddSingleton<IStorageBatchFactory, MongoStorageBatchFactory>();
services.AddTransient<IMondoDbInfo, MondoDbInfo>();
//TODO add this for address driven blockchains
//services.AddScoped<TaskRunner, RichListScanning>();

Expand Down
8 changes: 3 additions & 5 deletions src/Blockcore.Indexer.Core/Storage/IStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Blockcore.Indexer.Core.Models;
using Blockcore.Indexer.Core.Storage.Mongo.Types;

using Blockcore.Indexer.Core.Storage.Types;

namespace Blockcore.Indexer.Core.Storage
Expand Down Expand Up @@ -42,20 +42,18 @@ Task<List<QueryAddressBalance>> QuickBalancesLookupForAddressesWithHistoryCheckA

QueryResult<QueryOrphanBlock> OrphanBlocks(int? offset, int limit);

ReorgBlockTable OrphanBlockByHash(string blockHash);
T OrphanBlockByHash<T>(string blockHash) where T : class;

QueryResult<BalanceForAddress> Richlist(int offset, int limit);

List<BalanceForAddress> AddressBalances(IEnumerable<string> addresses);

long TotalBalance();

Task<QueryResult<OutputTable>> GetUnspentTransactionsByAddressAsync(string address,long confirmations, int offset, int limit);
Task<QueryResult<Output>> GetUnspentTransactionsByAddressAsync(string address,long confirmations, int offset, int limit);

Task DeleteBlockAsync(string blockHash);

public List<IndexView> GetIndexesBuildProgress();

public List<string> GetBlockIndexIndexes();

public List<string> GetMempoolTransactionIds();
Expand Down
9 changes: 9 additions & 0 deletions src/Blockcore.Indexer.Core/Storage/Mongo/IMondoDbInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;
using Blockcore.Indexer.Core.Storage.Mongo.Types;

namespace Blockcore.Indexer.Core.Storage.Mongo;

public interface IMondoDbInfo
{
public List<IndexView> GetIndexesBuildProgress();
}
63 changes: 63 additions & 0 deletions src/Blockcore.Indexer.Core/Storage/Mongo/MondoDbInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Linq;
using Blockcore.Indexer.Core.Storage.Mongo.Types;
using MongoDB.Bson;
using MongoDB.Driver;

namespace Blockcore.Indexer.Core.Storage.Mongo;

public class MondoDbInfo : IMondoDbInfo
{
private readonly IMongoDatabase mongoDatabase;

public MondoDbInfo(IMongoDatabase mongoDatabase)
{
this.mongoDatabase = mongoDatabase;
}

public List<IndexView> GetIndexesBuildProgress()
{
IMongoDatabase db = mongoDatabase.Client.GetDatabase("admin");
var command = new BsonDocument {
{ "currentOp", "1"},
};
BsonDocument currentOp = db.RunCommand<BsonDocument>(command);

BsonElement inproc = currentOp.GetElement(0);
var arr = inproc.Value as BsonArray;

var ret = new List<IndexView>();

foreach (BsonValue bsonValue in arr)
{
BsonElement? desc = bsonValue.AsBsonDocument?.GetElement("desc");
if (desc != null)
{
bool track = desc?.Value.AsString.Contains("IndexBuildsCoordinatorMongod") ?? false;

if (track)
{
var indexed = new IndexView {Msg = bsonValue.AsBsonDocument?.GetElement("msg").Value.ToString()};

BsonElement? commandElement = bsonValue.AsBsonDocument?.GetElement("command");

string dbName = string.Empty;
if (commandElement.HasValue)
{
BsonDocument bsn = commandElement.Value.Value.AsBsonDocument;
dbName = bsn.GetElement("$db").Value.ToString();
indexed.Command = $"{bsn.GetElement(0).Value}-{bsn.GetElement(1).Value}";
}

if (dbName == mongoDatabase.DatabaseNamespace.DatabaseName)
{
ret.Add(indexed);
}

}
}
}

return ret;
}
}
54 changes: 6 additions & 48 deletions src/Blockcore.Indexer.Core/Storage/Mongo/MongoData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
using Blockcore.Indexer.Core.Sync;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Driver;
using Blockcore.NBitcoin.DataEncoders;
using MongoDB.Bson;

namespace Blockcore.Indexer.Core.Storage.Mongo
{
Expand Down Expand Up @@ -92,51 +92,7 @@ public bool DeleteTransactionsFromMempool(List<string> transactionIds)
return result.IsAcknowledged; //TODO should we change this to count == count of transaction ids?
}

public List<IndexView> GetIndexesBuildProgress()
{
IMongoDatabase db = mongoDatabase.Client.GetDatabase("admin");
var command = new BsonDocument {
{ "currentOp", "1"},
};
BsonDocument currentOp = db.RunCommand<BsonDocument>(command);

BsonElement inproc = currentOp.GetElement(0);
var arr = inproc.Value as BsonArray;

var ret = new List<IndexView>();

foreach (BsonValue bsonValue in arr)
{
BsonElement? desc = bsonValue.AsBsonDocument?.GetElement("desc");
if (desc != null)
{
bool track = desc?.Value.AsString.Contains("IndexBuildsCoordinatorMongod") ?? false;

if (track)
{
var indexed = new IndexView {Msg = bsonValue.AsBsonDocument?.GetElement("msg").Value.ToString()};

BsonElement? commandElement = bsonValue.AsBsonDocument?.GetElement("command");

string dbName = string.Empty;
if (commandElement.HasValue)
{
BsonDocument bsn = commandElement.Value.Value.AsBsonDocument;
dbName = bsn.GetElement("$db").Value.ToString();
indexed.Command = $"{bsn.GetElement(0).Value}-{bsn.GetElement(1).Value}";
}

if (dbName == mongoDatabase.DatabaseNamespace.DatabaseName)
{
ret.Add(indexed);
}

}
}
}

return ret;
}

public QueryTransaction GetTransaction(string transactionId)
{
Expand Down Expand Up @@ -317,7 +273,9 @@ private static QueryBlock MapQueryBlock(BlockTable blockTable)
};
}

public ReorgBlockTable OrphanBlockByHash(string blockHash)
public T OrphanBlockByHash<T>(string blockHash) where T : class => OrphanBlockByHash(blockHash) as T;

private ReorgBlockTable OrphanBlockByHash(string blockHash)
{
FilterDefinition<ReorgBlockTable> filter = Builders<ReorgBlockTable>.Filter.Eq(info => info.BlockHash, blockHash);

Expand Down Expand Up @@ -1186,7 +1144,7 @@ public int GetMemoryTransactionsCount()
return globalState.LocalMempoolView.Count;
}

public async Task<QueryResult<OutputTable>> GetUnspentTransactionsByAddressAsync(string address, long confirmations, int offset, int limit)
public async Task<QueryResult<Output>> GetUnspentTransactionsByAddressAsync(string address, long confirmations, int offset, int limit)
{
SyncBlockInfo storeTip = globalState.StoreTip;

Expand Down Expand Up @@ -1257,7 +1215,7 @@ public async Task<QueryResult<OutputTable>> GetUnspentTransactionsByAddressAsync
}
});

return new QueryResult<OutputTable>
return new QueryResult<Output>
{
Items = results.OrderBy(o => o.BlockIndex),
Total = totalTask.Result?.Count ?? 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class MongoDbBlockIndexer : TaskRunner
{
private readonly IndexerSettings config;
private readonly ILogger<MongoDbBlockIndexer> log;
readonly IMondoDbInfo mongoData;
readonly IStorage data;

private readonly System.Diagnostics.Stopwatch watch;
Expand All @@ -30,12 +31,13 @@ public class MongoDbBlockIndexer : TaskRunner
public MongoDbBlockIndexer(
IOptions<IndexerSettings> configuration,
ILogger<MongoDbBlockIndexer> logger,
IStorage data, IMongoDb db)
IStorage data, IMongoDb db, IMondoDbInfo mongoData)
: base(configuration, logger)
{
log = logger;
this.data = data;
this.db = db;
this.mongoData = mongoData;
config = configuration.Value;
watch = Stopwatch.Start();

Expand Down Expand Up @@ -69,7 +71,7 @@ public override async Task<bool> OnExecute()
{
initialized = true;

List<IndexView> indexes = data.GetIndexesBuildProgress();
List<IndexView> indexes = mongoData.GetIndexesBuildProgress();
if (indexes.Any())
{
// if indexes are currently running go directly in to index mode
Expand All @@ -88,7 +90,7 @@ public override async Task<bool> OnExecute()
Runner.GlobalState.IndexMode = true;
}

List<IndexView> ops = data.GetIndexesBuildProgress();
List<IndexView> ops = mongoData.GetIndexesBuildProgress();

if (ops.Any())
{
Expand Down
10 changes: 1 addition & 9 deletions src/Blockcore.Indexer.Core/Storage/Mongo/Types/InputTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@

namespace Blockcore.Indexer.Core.Storage.Mongo.Types
{
public class InputTable
public class InputTable : Input
{
public Outpoint Outpoint { get; set; }

public string Address { get; set; }

public long Value { get; set; }

public string TrxHash { get; set; }

public uint BlockIndex { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Blockcore.Indexer.Core.Storage.Mongo.Types
{
public class TransactionBlockTable
Expand Down
14 changes: 14 additions & 0 deletions src/Blockcore.Indexer.Core/Storage/Types/Input.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Blockcore.Indexer.Core.Storage.Types;

public class Input
{
public Outpoint Outpoint { get; set; }

public string Address { get; set; }

public long Value { get; set; }

public string TrxHash { get; set; }

public uint BlockIndex { get; set; }
}

0 comments on commit 86e523f

Please sign in to comment.