Skip to content

Commit

Permalink
Performance optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGershony committed Mar 21, 2024
1 parent b14feba commit 5323067
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
31 changes: 16 additions & 15 deletions src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Blockcore.Indexer.Core.Settings;
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.Indexer.Core.Sync;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -55,13 +56,13 @@ public AngorMongoData(ILogger<AngorMongoDb> dbLogger, SyncConnection connection,

public async Task<ProjectStats?> GetProjectStatsAsync(string projectId)
{
var project = mongoDb.ProjectTable
var projectExists = mongoDb.ProjectTable
.AsQueryable()
.FirstOrDefault(_ => _.AngorKey == projectId);
.Any(_ => _.AngorKey == projectId);

if (project != null)
if (projectExists)
{
var total = await mongoDb.InvestmentTable.CountDocumentsAsync(Builders<Investment>.Filter.Eq(_ => _.AngorKey, project.AngorKey));
var total = await mongoDb.InvestmentTable.CountDocumentsAsync(Builders<Investment>.Filter.Eq(_ => _.AngorKey, projectId));

var sum = mongoDb.InvestmentTable.AsQueryable()
.Where(_ => _.AngorKey == projectId)
Expand Down Expand Up @@ -90,32 +91,32 @@ public AngorMongoData(ILogger<AngorMongoDb> dbLogger, SyncConnection connection,
var outputsSpentSummery = await mongoDb.InvestmentTable.Aggregate(PipelineDefinition<Investment, BsonDocument>.Create(
//Filter by project id
new BsonDocument("$match",
new BsonDocument("AngorKey", projectId)),
// Left join to input table on transaction id - not indexed and will need to be changed !!!
new BsonDocument(nameof(Investment.AngorKey), projectId)),
//Break down to object per stage outpoint for the inner join
new BsonDocument("$unwind",
new BsonDocument("path", "$" + nameof(Investment.StageOutpoint))),
// Inner join to input table on outpoint for each stage
new BsonDocument("$lookup",
new BsonDocument
{
{ "from", "Input" },
{ "localField", "TransactionId" },
{ "foreignField", "Outpoint.TransactionId" },
{ "localField", nameof(Investment.StageOutpoint) },
{ "foreignField", nameof(InputTable.Outpoint) },
{ "as", "inputs" }
}),
new BsonDocument("$unwind", "$inputs"),
//Filter by none address for the stages
new BsonDocument("$match",
new BsonDocument("inputs.Address", "none")), //Remove change address
//Aggregate the value by investment transaction and spending transaction and counting the number of trx in both
//Aggregate the value by investment transaction and spending transaction and counting the number of trx in both
new BsonDocument("$group",
new BsonDocument
{
{ "_id",
new BsonDocument
{
{ "TransactionId", "$TransactionId" },
{ "TrxHash", "$inputs.TrxHash" }
{ "TransactionId", "$" + nameof(Investment.TransactionId) },
{ "TrxHash", "$inputs." + nameof(InputTable.TrxHash)}
} },
{ "spent",
new BsonDocument("$sum", "$inputs.Value") },
new BsonDocument("$sum", "$inputs." + nameof(InputTable.Value)) },
{ "numTrx",
new BsonDocument("$sum", 1) }
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Blockcore.Indexer.Core.Storage.Mongo.Types;

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

public class Investment
Expand All @@ -14,5 +16,5 @@ public class Investment

public long AmountSats { get; set; }


public List<Outpoint> StageOutpoint { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public ProjectInvestmentsSyncRunner(IOptions<IndexerSettings> configuration, IL
{
this.angorMongoDb = angorMongoDb;
this.logger = logger;
Delay = TimeSpan.FromMinutes(1);
}


Expand Down Expand Up @@ -152,14 +153,17 @@ public override async Task<bool> OnExecute()
? Encoders.Hex.EncodeData(projectInfoScript.ToOps()[2].PushData)
: string.Empty;

var stages = allOutputsOnInvestmentTransaction.Where(_ => _.Address == "none");

return new Investment
{
InvestorPubKey = investorPubKey,
AngorKey = project.AngorKey,
AmountSats = allOutputsOnInvestmentTransaction.Where(_ => _.Address == "none").Sum(_ => _.Value),
AmountSats = stages.Sum(_ => _.Value),
BlockIndex = feeOutput.BlockIndex,
SecretHash = hashOfSecret,
TransactionId = feeOutput.Outpoint.TransactionId,
StageOutpoint = stages.Select(x => x.Outpoint).ToList()
};
}
}

0 comments on commit 5323067

Please sign in to comment.