Skip to content

Commit

Permalink
Code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
krzlabrdx committed Aug 19, 2024
1 parent 8353bf4 commit a88d7fd
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public enum EntityRelationship
ValidatorToLockedOwnerStakeUnitVault,
ValidatorToPendingOwnerStakeUnitUnlockVault,

StakeVaultOfValidator,
StakeUnitOfValidator,
ClaimTokenOfValidator,

// Account Lockers (used on related Vaults and KeyValueStores)
Expand All @@ -97,7 +97,7 @@ public enum EntityRelationship
ResourcePoolToResource,
ResourcePoolToResourceVault,

UnitVaultOfResourcePool,
UnitResourceOfResourcePool,
ResourceVaultOfResourcePool,

// Access Controllers
Expand Down
32 changes: 3 additions & 29 deletions src/RadixDlt.NetworkGateway.Abstractions/Numerics/TokenAmount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit a88d7fd

Please sign in to comment.