Search in sources :

Example 1 with CryptoAllowance

use of com.hedera.mirror.common.domain.entity.CryptoAllowance in project hedera-mirror-node by hashgraph.

the class SqlEntityListenerTest method onCryptoAllowanceHistory.

@ValueSource(ints = { 1, 2, 3 })
@ParameterizedTest
void onCryptoAllowanceHistory(int commitIndex) {
    // given
    final String idColumns = "payer_account_id, spender";
    var builder = domainBuilder.cryptoAllowance();
    CryptoAllowance cryptoAllowanceCreate = builder.get();
    CryptoAllowance cryptoAllowanceUpdate1 = builder.customize(c -> c.amount(999L)).get();
    cryptoAllowanceUpdate1.setTimestampLower(cryptoAllowanceCreate.getTimestampLower() + 1);
    CryptoAllowance cryptoAllowanceUpdate2 = builder.customize(c -> c.amount(0L)).get();
    cryptoAllowanceUpdate2.setTimestampLower(cryptoAllowanceCreate.getTimestampLower() + 2);
    // Expected merged objects
    CryptoAllowance mergedCreate = TestUtils.clone(cryptoAllowanceCreate);
    CryptoAllowance mergedUpdate1 = TestUtils.merge(cryptoAllowanceCreate, cryptoAllowanceUpdate1);
    CryptoAllowance mergedUpdate2 = TestUtils.merge(mergedUpdate1, cryptoAllowanceUpdate2);
    mergedCreate.setTimestampUpper(cryptoAllowanceUpdate1.getTimestampLower());
    // when
    sqlEntityListener.onCryptoAllowance(cryptoAllowanceCreate);
    if (commitIndex > 1) {
        completeFileAndCommit();
        assertThat(cryptoAllowanceRepository.findAll()).containsExactly(cryptoAllowanceCreate);
        assertThat(findHistory(CryptoAllowance.class, idColumns)).isEmpty();
    }
    sqlEntityListener.onCryptoAllowance(cryptoAllowanceUpdate1);
    if (commitIndex > 2) {
        completeFileAndCommit();
        assertThat(cryptoAllowanceRepository.findAll()).containsExactly(mergedUpdate1);
        assertThat(findHistory(CryptoAllowance.class, idColumns)).containsExactly(mergedCreate);
    }
    sqlEntityListener.onCryptoAllowance(cryptoAllowanceUpdate2);
    completeFileAndCommit();
    // then
    mergedUpdate1.setTimestampUpper(cryptoAllowanceUpdate2.getTimestampLower());
    assertThat(cryptoAllowanceRepository.findAll()).containsExactly(mergedUpdate2);
    assertThat(findHistory(CryptoAllowance.class, idColumns)).containsExactly(mergedCreate, mergedUpdate1);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SCHEDULE(com.hedera.mirror.common.domain.entity.EntityType.SCHEDULE) CryptoAllowanceRepository(com.hedera.mirror.importer.repository.CryptoAllowanceRepository) SneakyThrows(lombok.SneakyThrows) EntityId(com.hedera.mirror.common.domain.entity.EntityId) NonFeeTransfer(com.hedera.mirror.common.domain.transaction.NonFeeTransfer) ContractRepository(com.hedera.mirror.importer.repository.ContractRepository) ContractResultRepository(com.hedera.mirror.importer.repository.ContractResultRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Autowired(org.springframework.beans.factory.annotation.Autowired) NodeStakeRepository(com.hedera.mirror.importer.repository.NodeStakeRepository) ScheduleRepository(com.hedera.mirror.importer.repository.ScheduleRepository) EntityType(com.hedera.mirror.common.domain.entity.EntityType) TOKEN(com.hedera.mirror.common.domain.entity.EntityType.TOKEN) NftTransfer(com.hedera.mirror.common.domain.token.NftTransfer) TokenSupplyTypeEnum(com.hedera.mirror.common.domain.token.TokenSupplyTypeEnum) RecordFile(com.hedera.mirror.common.domain.transaction.RecordFile) NftAllowanceRepository(com.hedera.mirror.importer.repository.NftAllowanceRepository) Token(com.hedera.mirror.common.domain.token.Token) TokenId(com.hedera.mirror.common.domain.token.TokenId) TestUtils(com.hedera.mirror.importer.TestUtils) TransactionSignatureRepository(com.hedera.mirror.importer.repository.TransactionSignatureRepository) FileDataRepository(com.hedera.mirror.importer.repository.FileDataRepository) MethodSource(org.junit.jupiter.params.provider.MethodSource) ContractResult(com.hedera.mirror.common.domain.contract.ContractResult) TokenRepository(com.hedera.mirror.importer.repository.TokenRepository) ContractStateChange(com.hedera.mirror.common.domain.contract.ContractStateChange) TokenFreezeStatusEnum(com.hedera.mirror.common.domain.token.TokenFreezeStatusEnum) TopicMessage(com.hedera.mirror.common.domain.topic.TopicMessage) CryptoAllowance(com.hedera.mirror.common.domain.entity.CryptoAllowance) TokenPauseStatusEnum(com.hedera.mirror.common.domain.token.TokenPauseStatusEnum) StakingRewardTransferRepository(com.hedera.mirror.importer.repository.StakingRewardTransferRepository) Arguments(org.junit.jupiter.params.provider.Arguments) StandardCharsets(java.nio.charset.StandardCharsets) NftAllowance(com.hedera.mirror.common.domain.entity.NftAllowance) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) List(java.util.List) Nft(com.hedera.mirror.common.domain.token.Nft) Stream(java.util.stream.Stream) EntityRepository(com.hedera.mirror.importer.repository.EntityRepository) ContractLogRepository(com.hedera.mirror.importer.repository.ContractLogRepository) CryptoTransferRepository(com.hedera.mirror.importer.repository.CryptoTransferRepository) TokenAccountRepository(com.hedera.mirror.importer.repository.TokenAccountRepository) EthereumTransactionRepository(com.hedera.mirror.importer.repository.EthereumTransactionRepository) TokenAccount(com.hedera.mirror.common.domain.token.TokenAccount) ACCOUNT(com.hedera.mirror.common.domain.entity.EntityType.ACCOUNT) Schedule(com.hedera.mirror.common.domain.schedule.Schedule) TokenTypeEnum(com.hedera.mirror.common.domain.token.TokenTypeEnum) NftId(com.hedera.mirror.common.domain.token.NftId) Contract(com.hedera.mirror.common.domain.contract.Contract) TransactionRepository(com.hedera.mirror.importer.repository.TransactionRepository) Hex(org.apache.commons.codec.binary.Hex) ArrayList(java.util.ArrayList) LiveHashRepository(com.hedera.mirror.importer.repository.LiveHashRepository) DomainBuilder(com.hedera.mirror.common.domain.DomainBuilder) TransactionSignature(com.hedera.mirror.common.domain.transaction.TransactionSignature) NftTransferRepository(com.hedera.mirror.importer.repository.NftTransferRepository) ContractStateChangeRepository(com.hedera.mirror.importer.repository.ContractStateChangeRepository) LiveHash(com.hedera.mirror.common.domain.transaction.LiveHash) ValueSource(org.junit.jupiter.params.provider.ValueSource) NftRepository(com.hedera.mirror.importer.repository.NftRepository) TokenAllowanceRepository(com.hedera.mirror.importer.repository.TokenAllowanceRepository) TopicMessageRepository(com.hedera.mirror.importer.repository.TopicMessageRepository) ContractLog(com.hedera.mirror.common.domain.contract.ContractLog) TokenTransferRepository(com.hedera.mirror.importer.repository.TokenTransferRepository) Entity(com.hedera.mirror.common.domain.entity.Entity) Consumer(java.util.function.Consumer) IntegrationTest(com.hedera.mirror.importer.IntegrationTest) Key(com.hederahashgraph.api.proto.java.Key) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) RecordFileRepository(com.hedera.mirror.importer.repository.RecordFileRepository) TokenKycStatusEnum(com.hedera.mirror.common.domain.token.TokenKycStatusEnum) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Assertions(org.junit.jupiter.api.Assertions) TokenAllowance(com.hedera.mirror.common.domain.entity.TokenAllowance) Transaction(com.hedera.mirror.common.domain.transaction.Transaction) TokenTransfer(com.hedera.mirror.common.domain.token.TokenTransfer) CryptoAllowance(com.hedera.mirror.common.domain.entity.CryptoAllowance) ByteString(com.google.protobuf.ByteString) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with CryptoAllowance

use of com.hedera.mirror.common.domain.entity.CryptoAllowance in project hedera-mirror-node by hashgraph.

the class SqlEntityListenerTest method onCryptoAllowance.

@Test
void onCryptoAllowance() {
    // given
    CryptoAllowance cryptoAllowance1 = domainBuilder.cryptoAllowance().get();
    CryptoAllowance cryptoAllowance2 = domainBuilder.cryptoAllowance().get();
    // when
    sqlEntityListener.onCryptoAllowance(cryptoAllowance1);
    sqlEntityListener.onCryptoAllowance(cryptoAllowance2);
    completeFileAndCommit();
    // then
    assertThat(entityRepository.count()).isZero();
    assertThat(cryptoAllowanceRepository.findAll()).containsExactlyInAnyOrder(cryptoAllowance1, cryptoAllowance2);
    assertThat(findHistory(CryptoAllowance.class, "payer_account_id, spender")).isEmpty();
}
Also used : CryptoAllowance(com.hedera.mirror.common.domain.entity.CryptoAllowance) Test(org.junit.jupiter.api.Test) IntegrationTest(com.hedera.mirror.importer.IntegrationTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with CryptoAllowance

use of com.hedera.mirror.common.domain.entity.CryptoAllowance in project hedera-mirror-node by hashgraph.

the class CryptoAllowanceRepositoryTest method save.

@Test
void save() {
    CryptoAllowance cryptoAllowance = domainBuilder.cryptoAllowance().persist();
    assertThat(cryptoAllowanceRepository.findById(cryptoAllowance.getId())).get().isEqualTo(cryptoAllowance);
}
Also used : CryptoAllowance(com.hedera.mirror.common.domain.entity.CryptoAllowance) Test(org.junit.jupiter.api.Test)

Example 4 with CryptoAllowance

use of com.hedera.mirror.common.domain.entity.CryptoAllowance in project hedera-mirror-node by hashgraph.

the class CryptoAllowanceRepositoryTest method history.

/**
 * This test verifies that the domain object and table definition are in sync with the history table.
 */
@Test
void history() {
    CryptoAllowance cryptoAllowance = domainBuilder.cryptoAllowance().persist();
    jdbcOperations.update("insert into crypto_allowance_history select * from crypto_allowance");
    List<CryptoAllowance> cryptoAllowanceHistory = jdbcOperations.query("select * from crypto_allowance_history", ROW_MAPPER);
    assertThat(cryptoAllowanceRepository.findAll()).containsExactly(cryptoAllowance);
    assertThat(cryptoAllowanceHistory).containsExactly(cryptoAllowance);
}
Also used : CryptoAllowance(com.hedera.mirror.common.domain.entity.CryptoAllowance) Test(org.junit.jupiter.api.Test)

Example 5 with CryptoAllowance

use of com.hedera.mirror.common.domain.entity.CryptoAllowance in project hedera-mirror-node by hashgraph.

the class CryptoApproveAllowanceTransactionHandler method parseCryptoAllowances.

private void parseCryptoAllowances(List<com.hederahashgraph.api.proto.java.CryptoAllowance> cryptoAllowances, RecordItem recordItem) {
    var consensusTimestamp = recordItem.getConsensusTimestamp();
    var cryptoAllowanceState = new HashMap<CryptoAllowance.Id, CryptoAllowance>();
    var payerAccountId = recordItem.getPayerAccountId();
    // iterate the crypto allowance list in reverse order and honor the last allowance for the same owner and spender
    var iterator = cryptoAllowances.listIterator(cryptoAllowances.size());
    while (iterator.hasPrevious()) {
        var cryptoApproval = iterator.previous();
        EntityId ownerAccountId = getOwnerAccountId(cryptoApproval.getOwner(), payerAccountId);
        if (ownerAccountId == EntityId.EMPTY) {
            // and the partialDataAction is SKIP
            continue;
        }
        CryptoAllowance cryptoAllowance = new CryptoAllowance();
        cryptoAllowance.setAmount(cryptoApproval.getAmount());
        cryptoAllowance.setOwner(ownerAccountId.getId());
        cryptoAllowance.setPayerAccountId(payerAccountId);
        cryptoAllowance.setSpender(EntityId.of(cryptoApproval.getSpender()).getId());
        cryptoAllowance.setTimestampLower(consensusTimestamp);
        if (cryptoAllowanceState.putIfAbsent(cryptoAllowance.getId(), cryptoAllowance) == null) {
            entityListener.onCryptoAllowance(cryptoAllowance);
        }
    }
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) CryptoAllowance(com.hedera.mirror.common.domain.entity.CryptoAllowance) HashMap(java.util.HashMap)

Aggregations

CryptoAllowance (com.hedera.mirror.common.domain.entity.CryptoAllowance)7 Test (org.junit.jupiter.api.Test)5 IntegrationTest (com.hedera.mirror.importer.IntegrationTest)3 EntityId (com.hedera.mirror.common.domain.entity.EntityId)2 NftAllowance (com.hedera.mirror.common.domain.entity.NftAllowance)2 TokenAllowance (com.hedera.mirror.common.domain.entity.TokenAllowance)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ByteString (com.google.protobuf.ByteString)1 DomainBuilder (com.hedera.mirror.common.domain.DomainBuilder)1 Contract (com.hedera.mirror.common.domain.contract.Contract)1 ContractLog (com.hedera.mirror.common.domain.contract.ContractLog)1 ContractResult (com.hedera.mirror.common.domain.contract.ContractResult)1 ContractStateChange (com.hedera.mirror.common.domain.contract.ContractStateChange)1 Entity (com.hedera.mirror.common.domain.entity.Entity)1 EntityType (com.hedera.mirror.common.domain.entity.EntityType)1 ACCOUNT (com.hedera.mirror.common.domain.entity.EntityType.ACCOUNT)1 SCHEDULE (com.hedera.mirror.common.domain.entity.EntityType.SCHEDULE)1 TOKEN (com.hedera.mirror.common.domain.entity.EntityType.TOKEN)1 Schedule (com.hedera.mirror.common.domain.schedule.Schedule)1 Nft (com.hedera.mirror.common.domain.token.Nft)1