diff --git a/src/RadixDlt.NetworkGateway.Abstractions/Model/EntityRelationship.cs b/src/RadixDlt.NetworkGateway.Abstractions/Model/EntityRelationship.cs index 18ab5bab1..e1b142d6d 100644 --- a/src/RadixDlt.NetworkGateway.Abstractions/Model/EntityRelationship.cs +++ b/src/RadixDlt.NetworkGateway.Abstractions/Model/EntityRelationship.cs @@ -83,7 +83,7 @@ public enum EntityRelationship ValidatorToLockedOwnerStakeUnitVault, ValidatorToPendingOwnerStakeUnitUnlockVault, - StakeVaultOfValidator, + StakeUnitOfValidator, ClaimTokenOfValidator, // Account Lockers (used on related Vaults and KeyValueStores) @@ -97,7 +97,7 @@ public enum EntityRelationship ResourcePoolToResource, ResourcePoolToResourceVault, - UnitVaultOfResourcePool, + UnitResourceOfResourcePool, ResourceVaultOfResourcePool, // Access Controllers diff --git a/src/RadixDlt.NetworkGateway.Abstractions/Numerics/TokenAmount.cs b/src/RadixDlt.NetworkGateway.Abstractions/Numerics/TokenAmount.cs index 4a4bb8ca4..914a73a1e 100644 --- a/src/RadixDlt.NetworkGateway.Abstractions/Numerics/TokenAmount.cs +++ b/src/RadixDlt.NetworkGateway.Abstractions/Numerics/TokenAmount.cs @@ -74,7 +74,6 @@ namespace RadixDlt.NetworkGateway.Abstractions.Numerics; public static readonly TokenAmount Zero; public static readonly TokenAmount NaN; - public static readonly TokenAmount OneFullUnit; public static readonly TokenAmount MaxValue; private const int DecimalPrecision = 18; @@ -84,10 +83,10 @@ namespace RadixDlt.NetworkGateway.Abstractions.Numerics; static TokenAmount() { _divisor = BigInteger.Pow(10, DecimalPrecision); + MaxValue = new TokenAmount(BigInteger.Pow(2, 192), 0); Zero = new TokenAmount(0); NaN = new TokenAmount(true); - OneFullUnit = new TokenAmount(_divisor); } private readonly BigInteger _subUnits; @@ -271,41 +270,16 @@ public int CompareTo(TokenAmount other) return isNaNComparison != 0 ? isNaNComparison : _subUnits.CompareTo(other._subUnits); } - // Heavily inspired by https://www.codeproject.com/Articles/5366079/BigDecimal-in-Csharp - // Licensed under CPOL: https://en.wikipedia.org/wiki/Code_Project_Open_License - // Author: Paulo Francisco Zemek. August, 01, 2023. private static TokenAmount Divide(TokenAmount dividend, TokenAmount divisor) { if (divisor == Zero) { - // This rule might look odd, but when simplifying expressions, x/x (x divided by x) is 1. - // So, to keep the rule true, 0 divided by 0 is also 1. - if (dividend == Zero) - { - return OneFullUnit; - } - - throw new DivideByZeroException($"{nameof(divisor)} can only be zero if {nameof(dividend)} is zero."); + return NaN; } var doublePrecisionDividendSubUnits = dividend._subUnits * _divisor; var divisorSubUnits = divisor._subUnits; - return FromSubUnits(doublePrecisionDividendSubUnits / divisorSubUnits); - } - - // Is there a faster approach that works with BigIntegers? - // It seems Log10 isn't faster at all. - private static int CountDigits(BigInteger value) - { - int count = 0; - - while (value > 0) - { - count++; - value /= 10; - } - - return count; + return new TokenAmount(doublePrecisionDividendSubUnits / divisorSubUnits); } } diff --git a/src/RadixDlt.NetworkGateway.PostgresIntegration/LedgerExtension/RelationshipProcessor.cs b/src/RadixDlt.NetworkGateway.PostgresIntegration/LedgerExtension/RelationshipProcessor.cs index 0d817f355..0af2f2f62 100644 --- a/src/RadixDlt.NetworkGateway.PostgresIntegration/LedgerExtension/RelationshipProcessor.cs +++ b/src/RadixDlt.NetworkGateway.PostgresIntegration/LedgerExtension/RelationshipProcessor.cs @@ -123,7 +123,7 @@ public void ScanUpsert(CoreModel.Substate substateData, ReferencedEntity referen _referencedEntities.Get((EntityAddress)validator.Value.StakeUnitResourceAddress).PostResolveConfigureLow((ResourceEntity ue) => { - ue.AddCorrelation(EntityRelationship.StakeVaultOfValidator, e.Id); + ue.AddCorrelation(EntityRelationship.StakeUnitOfValidator, e.Id); }); }); } @@ -153,7 +153,7 @@ public void ScanUpsert(CoreModel.Substate substateData, ReferencedEntity referen _referencedEntities.GetByDatabaseId(poolUnitResourceEntity.DatabaseId).PostResolveConfigureLow((ResourceEntity ue) => { - ue.AddCorrelation(EntityRelationship.UnitVaultOfResourcePool, e.Id); + ue.AddCorrelation(EntityRelationship.UnitResourceOfResourcePool, e.Id); }); var vault = oneResourcePool.Value.Vault; @@ -180,7 +180,7 @@ public void ScanUpsert(CoreModel.Substate substateData, ReferencedEntity referen _referencedEntities.GetByDatabaseId(poolUnitResourceEntity.DatabaseId).PostResolveConfigureLow((ResourceEntity ue) => { - ue.AddCorrelation(EntityRelationship.UnitVaultOfResourcePool, e.Id); + ue.AddCorrelation(EntityRelationship.UnitResourceOfResourcePool, e.Id); }); foreach (var poolVault in twoResourcePool.Value.Vaults) @@ -206,7 +206,7 @@ public void ScanUpsert(CoreModel.Substate substateData, ReferencedEntity referen _referencedEntities.GetByDatabaseId(poolUnitResourceEntity.DatabaseId).PostResolveConfigureLow((ResourceEntity ue) => { - ue.AddCorrelation(EntityRelationship.UnitVaultOfResourcePool, e.Id); + ue.AddCorrelation(EntityRelationship.UnitResourceOfResourcePool, e.Id); }); foreach (var vault in multiResourcePool.Value.Vaults) diff --git a/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240816104539_InitialCreate.Designer.cs b/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240816104539_InitialCreate.Designer.cs index 479937d46..ed9a12647 100644 --- a/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240816104539_InitialCreate.Designer.cs +++ b/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240816104539_InitialCreate.Designer.cs @@ -81,7 +81,11 @@ namespace RadixDlt.NetworkGateway.PostgresIntegration.Migrations { [DbContext(typeof(MigrationsDbContext))] +<<<<<<<< HEAD:src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240816104539_InitialCreate.Designer.cs [Migration("20240816104539_InitialCreate")] +======== + [Migration("20240814053744_InitialCreate")] +>>>>>>>> ab22a073 (Code clean-up):src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240814053744_InitialCreate.Designer.cs partial class InitialCreate { /// @@ -95,7 +99,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "account_default_deposit_rule", new[] { "accept", "reject", "allow_existing" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "account_resource_preference_rule", new[] { "allowed", "disallowed" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "authorized_depositor_badge_type", new[] { "resource", "non_fungible" }); - NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "entity_relationship", new[] { "component_to_instantiating_package", "vault_to_resource", "royalty_vault_of_component", "validator_to_stake_vault", "validator_to_pending_xrd_withdraw_vault", "validator_to_locked_owner_stake_unit_vault", "validator_to_pending_owner_stake_unit_unlock_vault", "stake_vault_of_validator", "claim_token_of_validator", "account_locker_of_locker", "account_locker_of_account", "resource_pool_to_unit_resource", "resource_pool_to_resource", "resource_pool_to_resource_vault", "unit_vault_of_resource_pool", "resource_vault_of_resource_pool", "access_controller_to_recovery_badge", "recovery_badge_of_access_controller" }); + NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "entity_relationship", new[] { "component_to_instantiating_package", "vault_to_resource", "royalty_vault_of_component", "validator_to_stake_vault", "validator_to_pending_xrd_withdraw_vault", "validator_to_locked_owner_stake_unit_vault", "validator_to_pending_owner_stake_unit_unlock_vault", "stake_unit_of_validator", "claim_token_of_validator", "account_locker_of_locker", "account_locker_of_account", "resource_pool_to_unit_resource", "resource_pool_to_resource", "resource_pool_to_resource_vault", "unit_resource_of_resource_pool", "resource_vault_of_resource_pool", "access_controller_to_recovery_badge", "recovery_badge_of_access_controller" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "entity_type", new[] { "global_consensus_manager", "global_fungible_resource", "global_non_fungible_resource", "global_generic_component", "internal_generic_component", "global_account_component", "global_package", "internal_key_value_store", "internal_fungible_vault", "internal_non_fungible_vault", "global_validator", "global_access_controller", "global_identity", "global_one_resource_pool", "global_two_resource_pool", "global_multi_resource_pool", "global_transaction_tracker", "global_account_locker" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "ledger_transaction_manifest_class", new[] { "general", "transfer", "validator_stake", "validator_unstake", "validator_claim", "account_deposit_settings_update", "pool_contribution", "pool_redemption" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "ledger_transaction_marker_event_type", new[] { "withdrawal", "deposit" }); diff --git a/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240816104539_InitialCreate.cs b/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240816104539_InitialCreate.cs index b0598805d..6fb983bbc 100644 --- a/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240816104539_InitialCreate.cs +++ b/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240816104539_InitialCreate.cs @@ -62,7 +62,11 @@ * permissions under this License. */ +<<<<<<<< HEAD:src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240816104539_InitialCreate.cs using System; +======== +using System; +>>>>>>>> ab22a073 (Code clean-up):src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/20240814053744_InitialCreate.cs using System.Collections.Generic; using System.Numerics; using Microsoft.EntityFrameworkCore.Migrations; @@ -85,7 +89,7 @@ protected override void Up(MigrationBuilder migrationBuilder) .Annotation("Npgsql:Enum:account_default_deposit_rule", "accept,reject,allow_existing") .Annotation("Npgsql:Enum:account_resource_preference_rule", "allowed,disallowed") .Annotation("Npgsql:Enum:authorized_depositor_badge_type", "resource,non_fungible") - .Annotation("Npgsql:Enum:entity_relationship", "component_to_instantiating_package,vault_to_resource,royalty_vault_of_component,validator_to_stake_vault,validator_to_pending_xrd_withdraw_vault,validator_to_locked_owner_stake_unit_vault,validator_to_pending_owner_stake_unit_unlock_vault,stake_vault_of_validator,claim_token_of_validator,account_locker_of_locker,account_locker_of_account,resource_pool_to_unit_resource,resource_pool_to_resource,resource_pool_to_resource_vault,unit_vault_of_resource_pool,resource_vault_of_resource_pool,access_controller_to_recovery_badge,recovery_badge_of_access_controller") + .Annotation("Npgsql:Enum:entity_relationship", "component_to_instantiating_package,vault_to_resource,royalty_vault_of_component,validator_to_stake_vault,validator_to_pending_xrd_withdraw_vault,validator_to_locked_owner_stake_unit_vault,validator_to_pending_owner_stake_unit_unlock_vault,stake_unit_of_validator,claim_token_of_validator,account_locker_of_locker,account_locker_of_account,resource_pool_to_unit_resource,resource_pool_to_resource,resource_pool_to_resource_vault,unit_resource_of_resource_pool,resource_vault_of_resource_pool,access_controller_to_recovery_badge,recovery_badge_of_access_controller") .Annotation("Npgsql:Enum:entity_type", "global_consensus_manager,global_fungible_resource,global_non_fungible_resource,global_generic_component,internal_generic_component,global_account_component,global_package,internal_key_value_store,internal_fungible_vault,internal_non_fungible_vault,global_validator,global_access_controller,global_identity,global_one_resource_pool,global_two_resource_pool,global_multi_resource_pool,global_transaction_tracker,global_account_locker") .Annotation("Npgsql:Enum:ledger_transaction_manifest_class", "general,transfer,validator_stake,validator_unstake,validator_claim,account_deposit_settings_update,pool_contribution,pool_redemption") .Annotation("Npgsql:Enum:ledger_transaction_marker_event_type", "withdrawal,deposit") diff --git a/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/MigrationsDbContextModelSnapshot.cs b/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/MigrationsDbContextModelSnapshot.cs index 41c32f112..4648c01d4 100644 --- a/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/MigrationsDbContextModelSnapshot.cs +++ b/src/RadixDlt.NetworkGateway.PostgresIntegration/Migrations/MigrationsDbContextModelSnapshot.cs @@ -92,7 +92,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "account_default_deposit_rule", new[] { "accept", "reject", "allow_existing" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "account_resource_preference_rule", new[] { "allowed", "disallowed" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "authorized_depositor_badge_type", new[] { "resource", "non_fungible" }); - NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "entity_relationship", new[] { "component_to_instantiating_package", "vault_to_resource", "royalty_vault_of_component", "validator_to_stake_vault", "validator_to_pending_xrd_withdraw_vault", "validator_to_locked_owner_stake_unit_vault", "validator_to_pending_owner_stake_unit_unlock_vault", "stake_vault_of_validator", "claim_token_of_validator", "account_locker_of_locker", "account_locker_of_account", "resource_pool_to_unit_resource", "resource_pool_to_resource", "resource_pool_to_resource_vault", "unit_vault_of_resource_pool", "resource_vault_of_resource_pool", "access_controller_to_recovery_badge", "recovery_badge_of_access_controller" }); + NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "entity_relationship", new[] { "component_to_instantiating_package", "vault_to_resource", "royalty_vault_of_component", "validator_to_stake_vault", "validator_to_pending_xrd_withdraw_vault", "validator_to_locked_owner_stake_unit_vault", "validator_to_pending_owner_stake_unit_unlock_vault", "stake_unit_of_validator", "claim_token_of_validator", "account_locker_of_locker", "account_locker_of_account", "resource_pool_to_unit_resource", "resource_pool_to_resource", "resource_pool_to_resource_vault", "unit_resource_of_resource_pool", "resource_vault_of_resource_pool", "access_controller_to_recovery_badge", "recovery_badge_of_access_controller" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "entity_type", new[] { "global_consensus_manager", "global_fungible_resource", "global_non_fungible_resource", "global_generic_component", "internal_generic_component", "global_account_component", "global_package", "internal_key_value_store", "internal_fungible_vault", "internal_non_fungible_vault", "global_validator", "global_access_controller", "global_identity", "global_one_resource_pool", "global_two_resource_pool", "global_multi_resource_pool", "global_transaction_tracker", "global_account_locker" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "ledger_transaction_manifest_class", new[] { "general", "transfer", "validator_stake", "validator_unstake", "validator_claim", "account_deposit_settings_update", "pool_contribution", "pool_redemption" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "ledger_transaction_marker_event_type", new[] { "withdrawal", "deposit" }); diff --git a/src/RadixDlt.NetworkGateway.PostgresIntegration/Services/NativeResourceDetailsResolver.cs b/src/RadixDlt.NetworkGateway.PostgresIntegration/Services/NativeResourceDetailsResolver.cs index 799d14ef1..f65123548 100644 --- a/src/RadixDlt.NetworkGateway.PostgresIntegration/Services/NativeResourceDetailsResolver.cs +++ b/src/RadixDlt.NetworkGateway.PostgresIntegration/Services/NativeResourceDetailsResolver.cs @@ -147,7 +147,7 @@ LIMIT 1 WHERE e.from_state_version <= var.state_version AND e.address = ANY(var.addresses) - AND e.correlated_entity_relationships && '{unit_vault_of_resource_pool, stake_vault_of_validator, claim_token_of_validator, recovery_badge_of_access_controller}'::entity_relationship[] + AND e.correlated_entity_relationships && '{unit_resource_of_resource_pool, stake_unit_of_validator, claim_token_of_validator, recovery_badge_of_access_controller}'::entity_relationship[] ), base_with_root AS ( SELECT @@ -158,7 +158,7 @@ base_with_root AS ( unnest(root.correlated_entity_ids) AS root_correlated_entity_id FROM base_entities base INNER JOIN entities root ON root.id = base.base_correlated_entity_id - WHERE base.base_correlated_entity_relationship = ANY('{unit_vault_of_resource_pool, stake_vault_of_validator, claim_token_of_validator, recovery_badge_of_access_controller}'::entity_relationship[]) + WHERE base.base_correlated_entity_relationship = ANY('{unit_resource_of_resource_pool, stake_unit_of_validator, claim_token_of_validator, recovery_badge_of_access_controller}'::entity_relationship[]) ) SELECT bwr.base_correlated_entity_relationship AS BaseRelationship,