Skip to content

Commit

Permalink
feat: Implemented balance caluclation based on utxos (#198)
Browse files Browse the repository at this point in the history
* feat: Implemented balance caluclation based on utxos instead of account balance table

* refactor: sonar issues

* refactor: spotless

* chore: testing gha

* chore: testing gha

* chore: testing gha

* chore: testing gha

* chore: spotless

* merged main

* spotless

* removed unused function, fixed artillery

* reverting artillery tests

* feat: added pruning configurability

* reverted db

* refactor: improved query, returning constant for subquery

* refactor: add textblocks to query for better readability

* refactor: switched to a map to group objects
  • Loading branch information
Kammerlo authored May 28, 2024
1 parent 33fc39f commit 4a324c2
Show file tree
Hide file tree
Showing 27 changed files with 104 additions and 336 deletions.
4 changes: 2 additions & 2 deletions .env.IntegrationTest
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ GENESIS_CONWAY_PATH=/config/conway-genesis.json

## Yaci Indexer env
INDEXER_DOCKER_IMAGE_TAG=main
PRUNING_ENABLED=false

YACI_SPRING_PROFILES=postgres
# database profiles: h2, h2-testData, postgres
MEMPOOL_ENABLED=false
# Hasn't implemented yet
INITIAL_BALANCE_CALCULATION_BLOCK=0

## Ports
HOST_N2N_PORT=${CARDANO_NODE_PORT}
Expand Down
4 changes: 2 additions & 2 deletions .env.docker-compose
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ GENESIS_CONWAY_PATH=/config/conway-genesis.json

## Yaci Indexer env
INDEXER_DOCKER_IMAGE_TAG=main
PRUNING_ENABLED=false

YACI_SPRING_PROFILES=postgres
# database profiles: h2, h2-testData, postgres
MEMPOOL_ENABLED=false
# Hasn't implemented yet
INITIAL_BALANCE_CALCULATION_BLOCK=0

## Devkit env
DEVKIT_ENABLED=false
Expand Down
4 changes: 2 additions & 2 deletions .env.h2
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ GENESIS_CONWAY_PATH=./config/${NETWORK}/conway-genesis.json

## Yaci Indexer env
INDEXER_DOCKER_IMAGE_TAG=main
PRUNING_ENABLED=false

YACI_SPRING_PROFILES=h2
# database profiles: h2, h2-testData, postgres
MEMPOOL_ENABLED=false
# Hasn't implemented yet
INITIAL_BALANCE_CALCULATION_BLOCK=0

## Logger Config
LOG_FILE_PATH=logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,10 @@

import lombok.Builder;

import org.cardanofoundation.rosetta.api.account.model.entity.AddressBalanceEntity;

@Builder
public record AddressBalance(String address,
String unit,
Long slot,
BigInteger quantity,
Long number) {

public static AddressBalance fromEntity(AddressBalanceEntity addressBalanceEntity) {
return AddressBalance.builder()
.address(addressBalanceEntity.getAddress())
.unit(addressBalanceEntity.getUnit())
.slot(addressBalanceEntity.getSlot())
.quantity(addressBalanceEntity.getQuantity())
.number(addressBalanceEntity.getBlockNumber())
.build();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class AddressUtxoEntity {
@Column(name = "owner_addr")
private String ownerAddr;

@Column(name = "owner_stake_addr")
private String ownerStakeAddr;

@Type(JsonType.class)
private List<Amt> amounts;

@Column(name = "block")
private Long blockNumber;
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ List<AddressUtxoEntity> findAddressUtxoEntitiesByOutputIndexAndTxHash(Integer ou
"SELECT a FROM AddressUtxoEntity a LEFT OUTER JOIN TxInputEntity i ON a.txHash = i.txHash AND a.outputIndex = i.outputIndex WHERE a.ownerAddr = :address AND i.txHash IS NULL AND i.outputIndex IS NULL"
)
List<AddressUtxoEntity> findUtxosByAddress(@Param("address") String address);

@Query(value =
"""
SELECT a FROM AddressUtxoEntity a WHERE
a.ownerAddr = :address
AND NOT EXISTS(SELECT 1 FROM TxInputEntity o WHERE o.txHash = a.txHash AND o.outputIndex = a.outputIndex AND o.spentAtBlock <= :block)
AND a.blockNumber <= :block
""")
List<AddressUtxoEntity> findUnspentUtxosByAddressAndBlock(@Param("address") String address, @Param("block") long block);

@Query(value =
"""
SELECT a FROM AddressUtxoEntity a WHERE
a.ownerStakeAddr = :address
AND NOT EXISTS(SELECT o FROM TxInputEntity o WHERE o.txHash = a.txHash AND o.outputIndex = a.outputIndex AND o.spentAtBlock <= :block)
AND a.blockNumber <= :block
""")
List<AddressUtxoEntity> findUnspentUtxosByStakeAddressAndBlock(@Param("address") String stakeAddress, @Param("block") long block);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cardanofoundation.rosetta.api.account.service;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -98,19 +97,16 @@ private AccountBalanceResponse findBalanceDataByAddressAndBlock(String address,

return findBlockOrLast(number, hash)
.map(blockDto -> {
log.info("Looking for utxos for address {} and block {}", address, blockDto.getHash());
if (CardanoAddressUtils.isStakeAddress(address)) {
log.debug("Address is StakeAddress, get balance for {}", address);
BigInteger quantity = ledgerAccountService
.findStakeAddressBalanceQuantityByAddressAndBlock(address, blockDto.getNumber())
.orElse(BigInteger.ZERO);
return DataMapper.mapToStakeAddressBalanceResponse(blockDto, quantity);
log.info("Looking for utxos for address {} and block {}",
address,
blockDto.getHash());
List<AddressBalance> balances;
if(CardanoAddressUtils.isStakeAddress(address)) {
balances = ledgerAccountService.findBalanceByStakeAddressAndBlock(address, blockDto.getNumber());
} else {
log.debug("Address isn't StakeAddress");
List<AddressBalance> balances = ledgerAccountService
.findBalanceByAddressAndBlock(address, blockDto.getNumber());
return DataMapper.mapToAccountBalanceResponse(blockDto, balances);
balances = ledgerAccountService.findBalanceByAddressAndBlock(address, blockDto.getNumber());
}
return DataMapper.mapToAccountBalanceResponse(blockDto, balances);
})
.orElseThrow(ExceptionFactory::blockNotFoundException);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.cardanofoundation.rosetta.api.account.service;


import java.math.BigInteger;
import java.util.List;
import java.util.Optional;

import org.openapitools.client.model.Currency;

Expand All @@ -17,9 +15,8 @@ public interface LedgerAccountService {

List<AddressBalance> findBalanceByAddressAndBlock(String address, Long number);

List<Utxo> findUtxoByAddressAndCurrency(String address, List<Currency> currencies);
List<AddressBalance> findBalanceByStakeAddressAndBlock(String address, Long number);

Optional<BigInteger> findStakeAddressBalanceQuantityByAddressAndBlock(
String address, Long number);
List<Utxo> findUtxoByAddressAndCurrency(String address, List<Currency> currencies);

}
Loading

0 comments on commit 4a324c2

Please sign in to comment.