Skip to content

Commit

Permalink
fix resource holders index and query.
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelPawelec-RDX committed Jan 9, 2025
1 parent f8b83a4 commit 9ce1552
Show file tree
Hide file tree
Showing 7 changed files with 3,255 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ Release built: _not released yet_
- New filters are supported on the `/stream/transactions` endpoint:
- `transaction_status_filter` - Allows filtering by the transaction commit status (`Success`, `Failure`, `All`). Defaults to `All`.
- `balance_change_resources_filter` - Allows filtering to transactions which included non-fee related balance changes for all provided resources. Defaults to `[]`. We recommend integrators use this instead of the `manifest_resources_filter` in most instances.
- Improved the performance of the `/extensions/resource-holders/page` endpoint.

### Database changes
- New entries added to the `ledger_transaction_markers` table for each resource whose balance (excluding fee-related changes) was modified in a transaction. Each resource balance change will be represented by an entry with the `resource_balance_change` discriminator and the resource's `entity_id`.
- Removed `transaction_type.round_update` from the `ledger_transaction_markers` table. This should reduce database size and slightly improve the performance of the `/stream/transactions` endpoint.
- A new index `IX_ledger_transaction_markers_resource_balance_change` has been added to the `ledger_transaction_markers` table.
- A new index `IX_ledger_transactions_receipt_status_state_version` has been added to the `ledger_transactions` table.
- Replaced the `IX_resource_holders_entity_id_resource_entity_id_balance` index with the `IX_resource_holders_resource_entity_id_balance_id` index on the resource_holders table.

## 1.9.2
Release built: 9.12.2024
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ private static void HookupHistory(ModelBuilder modelBuilder)

modelBuilder
.Entity<ResourceHolder>()
.HasIndex(e => new { e.EntityId, e.ResourceEntityId, e.Balance });
.HasIndex(e => new { e.ResourceEntityId, e.Balance, e.Id });

modelBuilder
.Entity<EntityResourceTotalsHistory>()
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* Copyright 2021 Radix Publishing Ltd incorporated in Jersey (Channel Islands).
*
* Licensed under the Radix License, Version 1.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at:
*
* radixfoundation.org/licenses/LICENSE-v1
*
* The Licensor hereby grants permission for the Canonical version of the Work to be
* published, distributed and used under or by reference to the Licensor’s trademark
* Radix ® and use of any unregistered trade names, logos or get-up.
*
* The Licensor provides the Work (and each Contributor provides its Contributions) on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
* including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT,
* MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
*
* Whilst the Work is capable of being deployed, used and adopted (instantiated) to create
* a distributed ledger it is your responsibility to test and validate the code, together
* with all logic and performance of that code under all foreseeable scenarios.
*
* The Licensor does not make or purport to make and hereby excludes liability for all
* and any representation, warranty or undertaking in any form whatsoever, whether express
* or implied, to any entity or person, including any representation, warranty or
* undertaking, as to the functionality security use, value or other characteristics of
* any distributed ledger nor in respect the functioning or value of any tokens which may
* be created stored or transferred using the Work. The Licensor does not warrant that the
* Work or any use of the Work complies with any law or regulation in any territory where
* it may be implemented or used or that it will be appropriate for any specific purpose.
*
* Neither the licensor nor any current or former employees, officers, directors, partners,
* trustees, representatives, agents, advisors, contractors, or volunteers of the Licensor
* shall be liable for any direct or indirect, special, incidental, consequential or other
* losses of any kind, in tort, contract or otherwise (including but not limited to loss
* of revenue, income or profits, or loss of use or data, or loss of reputation, or loss
* of any economic or other opportunity of whatsoever nature or howsoever arising), arising
* out of or in connection with (without limitation of any use, misuse, of any ledger system
* or use made or its functionality or any performance or operation of any code or protocol
* caused by bugs or programming or logic errors or otherwise);
*
* A. any offer, purchase, holding, use, sale, exchange or transmission of any
* cryptographic keys, tokens or assets created, exchanged, stored or arising from any
* interaction with the Work;
*
* B. any failure in a transmission or loss of any token or assets keys or other digital
* artefacts due to errors in transmission;
*
* C. bugs, hacks, logic errors or faults in the Work or any communication;
*
* D. system software or apparatus including but not limited to losses caused by errors
* in holding or transmitting tokens by any third-party;
*
* E. breaches or failure of security including hacker attacks, loss or disclosure of
* password, loss of private key, unauthorised use or misuse of such passwords or keys;
*
* F. any losses including loss of anticipated savings or other benefits resulting from
* use of the Work or any changes to the Work (however implemented).
*
* You are solely responsible for; testing, validating and evaluation of all operation
* logic, functionality, security and appropriateness of using the Work for any commercial
* or non-commercial purpose and for any reproduction or redistribution by You of the
* Work. You assume all risks associated with Your use of the Work and the exercise of
* permissions under this License.
*/

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace RadixDlt.NetworkGateway.PostgresIntegration.Migrations
{
/// <inheritdoc />
public partial class FixResourceHoldersIndex : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_resource_holders_entity_id_resource_entity_id_balance",
table: "resource_holders");

migrationBuilder.CreateIndex(
name: "IX_resource_holders_resource_entity_id_balance_id",
table: "resource_holders",
columns: new[] { "resource_entity_id", "balance", "id" });
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_resource_holders_resource_entity_id_balance_id",
table: "resource_holders");

migrationBuilder.CreateIndex(
name: "IX_resource_holders_entity_id_resource_entity_id_balance",
table: "resource_holders",
columns: new[] { "entity_id", "resource_entity_id", "balance" });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1540,3 +1540,29 @@ BEGIN
END $EF$;
COMMIT;

START TRANSACTION;


DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20250109184346_FixResourceHoldersIndex') THEN
DROP INDEX "IX_resource_holders_entity_id_resource_entity_id_balance";
END IF;
END $EF$;

DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20250109184346_FixResourceHoldersIndex') THEN
CREATE INDEX "IX_resource_holders_resource_entity_id_balance_id" ON resource_holders (resource_entity_id, balance, id);
END IF;
END $EF$;

DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20250109184346_FixResourceHoldersIndex') THEN
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20250109184346_FixResourceHoldersIndex', '8.0.2');
END IF;
END $EF$;
COMMIT;

Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.HasIndex("EntityId", "ResourceEntityId")
.IsUnique();

b.HasIndex("EntityId", "ResourceEntityId", "Balance");
b.HasIndex("ResourceEntityId", "Balance", "Id");

b.ToTable("resource_holders");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ public ResourceHoldersQuerier(ReadOnlyDbContext dbContext, IDapperWrapper dapper
var cd = DapperExtensions.CreateCommandDefinition(
@"
SELECT
ro.id as Id,
rh.id as Id,
e.address AS EntityAddress,
CAST(ro.balance AS text) AS Balance,
ro.last_updated_at_state_version AS LastUpdatedAtStateVersion
FROM resource_holders ro
CAST(rh.balance AS text) AS Balance,
rh.last_updated_at_state_version AS LastUpdatedAtStateVersion
FROM resource_holders rh
INNER JOIN entities e
ON ro.entity_id = e.id
WHERE ro.resource_entity_id = @resourceEntityId
AND (ro.balance, ro.id) <= (Cast(@balanceBoundary AS numeric(1000,0)), @idBoundary)
ORDER BY (ro.balance, ro.entity_id) DESC
ON rh.entity_id = e.id
WHERE rh.resource_entity_id = @resourceEntityId
AND (rh.balance, rh.id) <= (Cast(@balanceBoundary AS numeric(1000,0)), @idBoundary)
ORDER BY rh.balance, rh.id DESC
LIMIT @limit",
parameters,
cancellationToken: token
Expand Down

0 comments on commit 9ce1552

Please sign in to comment.