Skip to content

Commit

Permalink
feat: #438 store donation in transaction table. Add donation to the t…
Browse files Browse the repository at this point in the history
…reasury amount during ledger state calculation. (#441)

* feat: #438 store donation in transaction table. Add donation to the treasury amount during ledger state calculation.

* fix: #438 fix sonatype warning
  • Loading branch information
satran004 authored Feb 13, 2025
1 parent 7113c08 commit 2bfc9c0
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.bloxbean.cardano.yaci.store.staking.domain.PoolDetails;
import com.bloxbean.cardano.yaci.store.staking.storage.PoolStorage;
import com.bloxbean.cardano.yaci.store.staking.storage.PoolStorageReader;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionStorageReader;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.rewards.calculation.EpochCalculation;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class EpochRewardCalculationService {
private final EpochInfoService epochInfoService;
private final PoolStorage poolStorage;
private final PoolStorageReader poolStorageReader;
private final TransactionStorageReader transactionStorageReader;
private final BlockInfoService blockInfoService;
private final PoolStateService poolStateService;
private final EraService eraService;
Expand Down Expand Up @@ -310,6 +312,15 @@ public EpochCalculationResult calculateEpochRewards(int epoch) {
long end = System.currentTimeMillis();
log.debug("Epoch calculation took " + Math.round((end - start) / 1000.0) + "s");

//Get donations in previous epoch
var donationInPrevEpoch = transactionStorageReader.getTotalDonation(epoch - 1);
if (donationInPrevEpoch != null
&& donationInPrevEpoch.compareTo(BigInteger.ZERO) > 0) {
//add donation to the treasury value
epochCalculationResult.setTreasury(epochCalculationResult.getTreasury().add(donationInPrevEpoch));
}
log.info("Total donation in the epoch {} : {}", epoch - 1, donationInPrevEpoch);

return epochCalculationResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ public class Txn extends BlockAwareDomain {
private TxOuput collateralReturnJson;
private BigInteger totalCollateral;
private List<UtxoKey> referenceInputs;
private BigInteger treasuryDonation;
private Boolean invalid;
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public void handleTransactionEvent(TransactionEvent event) {
.totalCollateral(transaction.getBody().getTotalCollateral())
.collateralReturn(new UtxoKey(transaction.getTxHash(), outputs.size()))
.referenceInputs(referenceInputs)
.treasuryDonation(transaction.getBody().getDonation())
.invalid(transaction.isInvalid())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public interface TransactionStorageReader {
List<Txn> getTransactionsByBlockNumber(long blockNumber);

BigInteger getTotalFee(int epoch);
BigInteger getTotalDonation(int epoch);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public BigInteger getTotalFee(int epoch) {
return txnEntityRepository.getTotalFee(epoch);
}

@Override
public BigInteger getTotalDonation(int epoch) {
return txnEntityRepository.getTotalDonation(epoch);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public class TxnEntity extends BlockAwareEntity {
@Column(name = "reference_inputs")
private List<UtxoKey> referenceInputs;

@Column(name = "treasury_donation")
private BigInteger treasuryDonation;

@Column(name = "invalid")
private Boolean invalid;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ public interface TxnEntityRepository extends JpaRepository<TxnEntity, String> {
@Query("select sum(t.fee) from TxnEntity t where t.epoch = :epoch")
BigInteger getTotalFee(long epoch);

@Query("select sum(t.treasuryDonation) from TxnEntity t where t.epoch = :epoch and t.treasuryDonation is not null")
BigInteger getTotalDonation(long epoch);

int deleteBySlotGreaterThan(Long slot);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ create table transaction
validity_interval_start bigint,
collateral_return_json json,
tx_index integer,
treasury_donation bigint,
epoch integer,
block bigint,
block_time bigint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ create table transaction
validity_interval_start bigint,
collateral_return_json json,
tx_index integer,
treasury_donation bigint,
epoch integer,
block bigint,
block_time bigint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ create table transaction
validity_interval_start bigint,
collateral_return_json jsonb,
tx_index integer,
treasury_donation bigint,
epoch integer,
block bigint,
block_time bigint,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.bloxbean.cardano.yaci.store.transaction.storage.impl;

import com.bloxbean.cardano.yaci.store.transaction.domain.Txn;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionStorage;
import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionStorageReader;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.mapper.TxnMapper;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.mapper.TxnMapperImpl;
import com.bloxbean.cardano.yaci.store.transaction.storage.impl.repository.TxnEntityRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.util.List;

import static com.bloxbean.cardano.client.common.ADAConversionUtil.adaToLovelace;
import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
class TransactionStorageReaderImplTest {

@Autowired
private TxnEntityRepository txnEntityRepository;

private TransactionStorage transactionStorage;
private TransactionStorageReader transactionStorageReader;

private TxnMapper mapper;

@BeforeEach
void setUp() {
mapper = new TxnMapperImpl();
transactionStorageReader = new TransactionStorageReaderImpl(txnEntityRepository, mapper, null);
transactionStorage = new TransactionStorageImpl(txnEntityRepository, mapper, null);
}

@Test
void getTotalDonation() {
Txn txn1 = Txn.builder()
.txHash("0000000100000")
.epoch(4)
.treasuryDonation(adaToLovelace(30))
.build();

Txn txn2 = Txn.builder()
.txHash("1000000100000")
.epoch(4)
.treasuryDonation(adaToLovelace(90))
.build();

Txn txn3 = Txn.builder()
.txHash("2000000100000")
.epoch(4)
.build();


Txn txn4 = Txn.builder()
.txHash("3000000100000")
.epoch(5)
.treasuryDonation(adaToLovelace(50))
.build();

Txn txn5 = Txn.builder()
.txHash("4000000100000")
.epoch(5)
.treasuryDonation(adaToLovelace(10))
.build();

Txn txn6 = Txn.builder()
.txHash("6000000100000")
.epoch(6)
.build();


transactionStorage.saveAll(List.of(txn1, txn2, txn3, txn4, txn5, txn6));

var epoch4Donation = transactionStorageReader.getTotalDonation(4);
var epoch5Donation = transactionStorageReader.getTotalDonation(5);
var epoch6Donation = transactionStorageReader.getTotalDonation(6);

assertThat(epoch4Donation).isEqualTo(adaToLovelace(120));
assertThat(epoch5Donation).isEqualTo(adaToLovelace(60));
assertThat(epoch6Donation).isNull();

}
}

0 comments on commit 2bfc9c0

Please sign in to comment.