mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 01:43:50 +01:00
Introduce wallet object contagion
This commit is contained in:
parent
d3e2d5a095
commit
0c144824c8
@ -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)
|
||||
{
|
||||
|
1565
BTCPayServer.Data/Migrations/20221123072311_WalletObjectInfection.Designer.cs
generated
Normal file
1565
BTCPayServer.Data/Migrations/20221123072311_WalletObjectInfection.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
@ -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");
|
||||
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user