Introduce wallet object contagion

This commit is contained in:
Kukks 2022-11-23 08:24:24 +01:00
parent d3e2d5a095
commit 0c144824c8
No known key found for this signature in database
GPG Key ID: 8E5530D9D1C93097
5 changed files with 1640 additions and 3 deletions

View File

@ -19,6 +19,7 @@ namespace BTCPayServer.Data
public WalletObjectData A { get; set; }
public WalletObjectData B { get; set; }
public int InfectionRate { get; set; } = 0;
internal static void OnModelCreating(ModelBuilder builder, DatabaseFacade databaseFacade)
{

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BTCPayServer.Migrations
{
public partial class WalletObjectInfection : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "InfectionRate",
table: "WalletObjectLinks",
type: "INTEGER",
nullable: false,
defaultValue: 0);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "InfectionRate",
table: "WalletObjectLinks");
}
}
}

View File

@ -886,8 +886,12 @@ namespace BTCPayServer.Migrations
b.Property<string>("Data")
.HasColumnType("TEXT");
b.Property<int>("InfectionRate")
b.HasKey("WalletId", "AType", "AId", "BType", "BId");
.HasColumnType("INTEGER");
b.HasKey("WalletId", "AType", "AId", "BType", "BId");
b.HasIndex("WalletId", "BType", "BId");

View File

@ -325,7 +325,7 @@ namespace BTCPayServer.Services
}
}
public async Task EnsureWalletObjectLink(WalletObjectId a, WalletObjectId b, JObject? data = null)
public async Task EnsureWalletObjectLink(WalletObjectId a, WalletObjectId b, JObject? data = null, int infection = 0)
{
SortWalletObjectLinks(ref a, ref b);
await using var ctx = _ContextFactory.CreateContext();
@ -336,18 +336,59 @@ namespace BTCPayServer.Services
AId = a.Id,
BType = b.Type,
BId = b.Id,
Data = data?.ToString(Formatting.None)
Data = data?.ToString(Formatting.None),
InfectionRate = infection
};
ctx.WalletObjectLinks.Add(l);
try
{
await ctx.SaveChangesAsync();
await SpreadInfection(a, b, data, infection);
}
catch (DbUpdateException) // already exists
{
}
}
private async Task SpreadInfection(WalletObjectId a, WalletObjectId b, JObject? data = null, int infectionRate = 0 )
{
SortWalletObjectLinks(ref a, ref b);
await using var ctx = _ContextFactory.CreateContext();
var contagion = await ctx.WalletObjectLinks
.Include(data => data.Parent)
.ThenInclude(data => data.ChildLinks)
.Include(data => data.Parent)
.ThenInclude(data => data.ParentLinks)
.Include(data => data.Child)
.ThenInclude(data => data.ChildLinks)
.Include(data => data.Child)
.ThenInclude(data => data.ParentLinks)
.SingleOrDefaultAsync(data =>
data.WalletId == a.WalletId.ToString() &&
data.ParentType == a.Type &&
data.ParentId == a.Id &&
data.ChildType == b.Type &&
data.ChildId == b.Id
);
if (contagion is null)
{
await EnsureWalletObjectLink(a, b, data, infectionRate);
}
if ( contagion.InfectionRate == 0)
{
return;
}
foreach (WalletObjectData parentNeighbour in contagion.Parent.GetNeighbours())
{
await EnsureWalletObjectLink(b, new WalletObjectId(b.WalletId, parentNeighbour.Type, parentNeighbour.Id), data, infectionRate-1);
}
foreach (WalletObjectData childNeighbour in contagion.Child.GetNeighbours())
{
await EnsureWalletObjectLink(a, new WalletObjectId(a.WalletId, childNeighbour.Type, childNeighbour.Id), data, infectionRate-1);
}
}
class WalletObjectIdComparer : IComparer<WalletObjectId>
{
public static readonly WalletObjectIdComparer Instance = new WalletObjectIdComparer();