From 86e523faad4a19ab9d032eecde54f8f1b8660b1b Mon Sep 17 00:00:00 2001 From: TheDude Date: Thu, 27 Jun 2024 16:09:23 +0100 Subject: [PATCH] changed IStorage.cs to not depend on mondo types (#215) --- .../Controllers/QueryController.cs | 2 +- .../Storage/DatabaseRegistration.cs | 1 + .../Storage/IStorage.cs | 8 +-- .../Storage/Mongo/IMondoDbInfo.cs | 9 +++ .../Storage/Mongo/MondoDbInfo.cs | 63 +++++++++++++++++++ .../Storage/Mongo/MongoData.cs | 54 ++-------------- .../Mongo/SyncTasks/MongoDbBlockIndexer.cs | 8 ++- .../Mongo/Types/{MongoOp.cs => IndexView.cs} | 0 .../Storage/Mongo/Types/InputTable.cs | 10 +-- .../Mongo/Types/TransactionBlockTable.cs | 2 - .../Storage/Types/Input.cs | 14 +++++ 11 files changed, 103 insertions(+), 68 deletions(-) create mode 100644 src/Blockcore.Indexer.Core/Storage/Mongo/IMondoDbInfo.cs create mode 100644 src/Blockcore.Indexer.Core/Storage/Mongo/MondoDbInfo.cs rename src/Blockcore.Indexer.Core/Storage/Mongo/Types/{MongoOp.cs => IndexView.cs} (100%) create mode 100644 src/Blockcore.Indexer.Core/Storage/Types/Input.cs diff --git a/src/Blockcore.Indexer.Core/Controllers/QueryController.cs b/src/Blockcore.Indexer.Core/Controllers/QueryController.cs index 64f7a3fe..700b484e 100644 --- a/src/Blockcore.Indexer.Core/Controllers/QueryController.cs +++ b/src/Blockcore.Indexer.Core/Controllers/QueryController.cs @@ -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(hash)); } /// diff --git a/src/Blockcore.Indexer.Core/Storage/DatabaseRegistration.cs b/src/Blockcore.Indexer.Core/Storage/DatabaseRegistration.cs index d9337fc0..77f5e5e7 100644 --- a/src/Blockcore.Indexer.Core/Storage/DatabaseRegistration.cs +++ b/src/Blockcore.Indexer.Core/Storage/DatabaseRegistration.cs @@ -20,6 +20,7 @@ public static IServiceCollection AddMongoDatabase( this IServiceCollection servi services.AddTransient(); services.AddScoped(); services.AddSingleton(); + services.AddTransient(); //TODO add this for address driven blockchains //services.AddScoped(); diff --git a/src/Blockcore.Indexer.Core/Storage/IStorage.cs b/src/Blockcore.Indexer.Core/Storage/IStorage.cs index 319a3b9f..cd85cb36 100644 --- a/src/Blockcore.Indexer.Core/Storage/IStorage.cs +++ b/src/Blockcore.Indexer.Core/Storage/IStorage.cs @@ -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 @@ -42,7 +42,7 @@ Task> QuickBalancesLookupForAddressesWithHistoryCheckA QueryResult OrphanBlocks(int? offset, int limit); - ReorgBlockTable OrphanBlockByHash(string blockHash); + T OrphanBlockByHash(string blockHash) where T : class; QueryResult Richlist(int offset, int limit); @@ -50,12 +50,10 @@ Task> QuickBalancesLookupForAddressesWithHistoryCheckA long TotalBalance(); - Task> GetUnspentTransactionsByAddressAsync(string address,long confirmations, int offset, int limit); + Task> GetUnspentTransactionsByAddressAsync(string address,long confirmations, int offset, int limit); Task DeleteBlockAsync(string blockHash); - public List GetIndexesBuildProgress(); - public List GetBlockIndexIndexes(); public List GetMempoolTransactionIds(); diff --git a/src/Blockcore.Indexer.Core/Storage/Mongo/IMondoDbInfo.cs b/src/Blockcore.Indexer.Core/Storage/Mongo/IMondoDbInfo.cs new file mode 100644 index 00000000..5ec6eb1b --- /dev/null +++ b/src/Blockcore.Indexer.Core/Storage/Mongo/IMondoDbInfo.cs @@ -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 GetIndexesBuildProgress(); +} diff --git a/src/Blockcore.Indexer.Core/Storage/Mongo/MondoDbInfo.cs b/src/Blockcore.Indexer.Core/Storage/Mongo/MondoDbInfo.cs new file mode 100644 index 00000000..17e7d9c5 --- /dev/null +++ b/src/Blockcore.Indexer.Core/Storage/Mongo/MondoDbInfo.cs @@ -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 GetIndexesBuildProgress() + { + IMongoDatabase db = mongoDatabase.Client.GetDatabase("admin"); + var command = new BsonDocument { + { "currentOp", "1"}, + }; + BsonDocument currentOp = db.RunCommand(command); + + BsonElement inproc = currentOp.GetElement(0); + var arr = inproc.Value as BsonArray; + + var ret = new List(); + + 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; + } +} diff --git a/src/Blockcore.Indexer.Core/Storage/Mongo/MongoData.cs b/src/Blockcore.Indexer.Core/Storage/Mongo/MongoData.cs index 165189f3..19247086 100644 --- a/src/Blockcore.Indexer.Core/Storage/Mongo/MongoData.cs +++ b/src/Blockcore.Indexer.Core/Storage/Mongo/MongoData.cs @@ -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 { @@ -92,51 +92,7 @@ public bool DeleteTransactionsFromMempool(List transactionIds) return result.IsAcknowledged; //TODO should we change this to count == count of transaction ids? } - public List GetIndexesBuildProgress() - { - IMongoDatabase db = mongoDatabase.Client.GetDatabase("admin"); - var command = new BsonDocument { - { "currentOp", "1"}, - }; - BsonDocument currentOp = db.RunCommand(command); - - BsonElement inproc = currentOp.GetElement(0); - var arr = inproc.Value as BsonArray; - - var ret = new List(); - - 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) { @@ -317,7 +273,9 @@ private static QueryBlock MapQueryBlock(BlockTable blockTable) }; } - public ReorgBlockTable OrphanBlockByHash(string blockHash) + public T OrphanBlockByHash(string blockHash) where T : class => OrphanBlockByHash(blockHash) as T; + + private ReorgBlockTable OrphanBlockByHash(string blockHash) { FilterDefinition filter = Builders.Filter.Eq(info => info.BlockHash, blockHash); @@ -1186,7 +1144,7 @@ public int GetMemoryTransactionsCount() return globalState.LocalMempoolView.Count; } - public async Task> GetUnspentTransactionsByAddressAsync(string address, long confirmations, int offset, int limit) + public async Task> GetUnspentTransactionsByAddressAsync(string address, long confirmations, int offset, int limit) { SyncBlockInfo storeTip = globalState.StoreTip; @@ -1257,7 +1215,7 @@ public async Task> GetUnspentTransactionsByAddressAsync } }); - return new QueryResult + return new QueryResult { Items = results.OrderBy(o => o.BlockIndex), Total = totalTask.Result?.Count ?? 0, diff --git a/src/Blockcore.Indexer.Core/Storage/Mongo/SyncTasks/MongoDbBlockIndexer.cs b/src/Blockcore.Indexer.Core/Storage/Mongo/SyncTasks/MongoDbBlockIndexer.cs index c393ca3f..d2ca7ea5 100644 --- a/src/Blockcore.Indexer.Core/Storage/Mongo/SyncTasks/MongoDbBlockIndexer.cs +++ b/src/Blockcore.Indexer.Core/Storage/Mongo/SyncTasks/MongoDbBlockIndexer.cs @@ -17,6 +17,7 @@ public class MongoDbBlockIndexer : TaskRunner { private readonly IndexerSettings config; private readonly ILogger log; + readonly IMondoDbInfo mongoData; readonly IStorage data; private readonly System.Diagnostics.Stopwatch watch; @@ -30,12 +31,13 @@ public class MongoDbBlockIndexer : TaskRunner public MongoDbBlockIndexer( IOptions configuration, ILogger 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(); @@ -69,7 +71,7 @@ public override async Task OnExecute() { initialized = true; - List indexes = data.GetIndexesBuildProgress(); + List indexes = mongoData.GetIndexesBuildProgress(); if (indexes.Any()) { // if indexes are currently running go directly in to index mode @@ -88,7 +90,7 @@ public override async Task OnExecute() Runner.GlobalState.IndexMode = true; } - List ops = data.GetIndexesBuildProgress(); + List ops = mongoData.GetIndexesBuildProgress(); if (ops.Any()) { diff --git a/src/Blockcore.Indexer.Core/Storage/Mongo/Types/MongoOp.cs b/src/Blockcore.Indexer.Core/Storage/Mongo/Types/IndexView.cs similarity index 100% rename from src/Blockcore.Indexer.Core/Storage/Mongo/Types/MongoOp.cs rename to src/Blockcore.Indexer.Core/Storage/Mongo/Types/IndexView.cs diff --git a/src/Blockcore.Indexer.Core/Storage/Mongo/Types/InputTable.cs b/src/Blockcore.Indexer.Core/Storage/Mongo/Types/InputTable.cs index a8ccfb04..c7893347 100644 --- a/src/Blockcore.Indexer.Core/Storage/Mongo/Types/InputTable.cs +++ b/src/Blockcore.Indexer.Core/Storage/Mongo/Types/InputTable.cs @@ -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; } } } diff --git a/src/Blockcore.Indexer.Core/Storage/Mongo/Types/TransactionBlockTable.cs b/src/Blockcore.Indexer.Core/Storage/Mongo/Types/TransactionBlockTable.cs index d97ebd48..398825a7 100644 --- a/src/Blockcore.Indexer.Core/Storage/Mongo/Types/TransactionBlockTable.cs +++ b/src/Blockcore.Indexer.Core/Storage/Mongo/Types/TransactionBlockTable.cs @@ -1,5 +1,3 @@ -using System; - namespace Blockcore.Indexer.Core.Storage.Mongo.Types { public class TransactionBlockTable diff --git a/src/Blockcore.Indexer.Core/Storage/Types/Input.cs b/src/Blockcore.Indexer.Core/Storage/Types/Input.cs new file mode 100644 index 00000000..0d568a7a --- /dev/null +++ b/src/Blockcore.Indexer.Core/Storage/Types/Input.cs @@ -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; } +}