Search in sources :

Example 1 with Contract

use of com.hedera.mirror.common.domain.contract.Contract in project hedera-mirror-node by hashgraph.

the class SqlEntityListenerTest method onContractHistory.

@ValueSource(ints = { 1, 2, 3 })
@ParameterizedTest
void onContractHistory(int commitIndex) {
    // given
    Contract contractCreate = domainBuilder.contract().customize(c -> c.obtainerId(null)).get();
    Contract contractUpdate = contractCreate.toEntityId().toEntity();
    contractUpdate.setAutoRenewPeriod(30L);
    contractUpdate.setEvmAddress(contractCreate.getEvmAddress());
    contractUpdate.setExpirationTimestamp(500L);
    contractUpdate.setKey(domainBuilder.key());
    contractUpdate.setMemo("updated");
    contractUpdate.setTimestampLower(contractCreate.getTimestampLower() + 1);
    contractUpdate.setProxyAccountId(EntityId.of(100L, ACCOUNT));
    Contract contractDelete = contractCreate.toEntityId().toEntity();
    contractDelete.setDeleted(true);
    contractDelete.setEvmAddress(contractCreate.getEvmAddress());
    contractDelete.setTimestampLower(contractCreate.getTimestampLower() + 2);
    contractDelete.setObtainerId(EntityId.of(999L, EntityType.CONTRACT));
    // Expected merged objects
    Contract mergedCreate = TestUtils.clone(contractCreate);
    Contract mergedUpdate = TestUtils.merge(contractCreate, contractUpdate);
    Contract mergedDelete = TestUtils.merge(mergedUpdate, contractDelete);
    mergedCreate.setTimestampUpper(contractUpdate.getTimestampLower());
    // when
    sqlEntityListener.onContract(contractCreate);
    if (commitIndex > 1) {
        completeFileAndCommit();
        assertThat(contractRepository.findAll()).containsExactly(contractCreate);
        assertThat(findHistory(Contract.class)).isEmpty();
    }
    sqlEntityListener.onContract(contractUpdate);
    if (commitIndex > 2) {
        completeFileAndCommit();
        assertThat(contractRepository.findAll()).containsExactly(mergedUpdate);
        assertThat(findHistory(Contract.class)).containsExactly(mergedCreate);
    }
    sqlEntityListener.onContract(contractDelete);
    completeFileAndCommit();
    // then
    mergedUpdate.setTimestampUpper(contractDelete.getTimestampLower());
    assertThat(contractRepository.findAll()).containsExactly(mergedDelete);
    assertThat(findHistory(Contract.class)).containsExactly(mergedCreate, mergedUpdate);
    assertThat(entityRepository.count()).isZero();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) JdbcOperations(org.springframework.jdbc.core.JdbcOperations) 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) ScheduleRepository(com.hedera.mirror.importer.repository.ScheduleRepository) EntityType(com.hedera.mirror.common.domain.entity.EntityType) TransactionType(com.hedera.mirror.common.domain.transaction.TransactionType) 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) 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) Collection(java.util.Collection) CryptoAllowance(com.hedera.mirror.common.domain.entity.CryptoAllowance) TokenPauseStatusEnum(com.hedera.mirror.common.domain.token.TokenPauseStatusEnum) StandardCharsets(java.nio.charset.StandardCharsets) FileData(com.hedera.mirror.common.domain.file.FileData) 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) 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) NftTransferId(com.hedera.mirror.common.domain.token.NftTransferId) 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) Strings(org.bouncycastle.util.Strings) ValueSource(org.junit.jupiter.params.provider.ValueSource) NftRepository(com.hedera.mirror.importer.repository.NftRepository) TokenAllowanceRepository(com.hedera.mirror.importer.repository.TokenAllowanceRepository) CaseFormat(com.google.common.base.CaseFormat) 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) 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) Contract(com.hedera.mirror.common.domain.contract.Contract) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with Contract

use of com.hedera.mirror.common.domain.contract.Contract in project hedera-mirror-node by hashgraph.

the class EntityRecordItemListenerContractTest method contractUpdateAllToExisting.

@ParameterizedTest
@EnumSource(ContractIdType.class)
void contractUpdateAllToExisting(ContractIdType contractIdType) {
    // first create the contract
    SetupResult setupResult = setupContract(CONTRACT_ID, contractIdType, true, true, c -> c.obtainerId(null));
    Contract contract = setupResult.contract;
    // now update
    Transaction transaction = contractUpdateAllTransaction(setupResult.protoContractId, true);
    TransactionBody transactionBody = getTransactionBody(transaction);
    TransactionRecord record = getContractTransactionRecord(transactionBody, ContractTransactionType.UPDATE);
    ContractUpdateTransactionBody contractUpdateTransactionBody = transactionBody.getContractUpdateInstance();
    parseRecordItemAndCommit(new RecordItem(transaction, record));
    assertAll(() -> assertEquals(1, transactionRepository.count()), () -> assertEntities(setupResult.contract.toEntityId()), () -> assertEquals(0, contractResultRepository.count()), () -> assertEquals(3, cryptoTransferRepository.count()), () -> assertTransactionAndRecord(transactionBody, record), () -> assertContractEntity(contractUpdateTransactionBody, record.getConsensusTimestamp()).returns(contract.getCreatedTimestamp(), Contract::getCreatedTimestamp).returns(contract.getFileId(), // FileId is ignored on updates by HAPI
    Contract::getFileId));
}
Also used : ContractUpdateTransactionBody(com.hederahashgraph.api.proto.java.ContractUpdateTransactionBody) ContractCallTransactionBody(com.hederahashgraph.api.proto.java.ContractCallTransactionBody) ContractDeleteTransactionBody(com.hederahashgraph.api.proto.java.ContractDeleteTransactionBody) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) ContractCreateTransactionBody(com.hederahashgraph.api.proto.java.ContractCreateTransactionBody) Transaction(com.hederahashgraph.api.proto.java.Transaction) ContractUpdateTransactionBody(com.hederahashgraph.api.proto.java.ContractUpdateTransactionBody) Contract(com.hedera.mirror.common.domain.contract.Contract) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) RecordItem(com.hedera.mirror.common.domain.transaction.RecordItem) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Contract

use of com.hedera.mirror.common.domain.contract.Contract in project hedera-mirror-node by hashgraph.

the class EntityRecordItemListenerContractTest method assertCreatedContract.

private void assertCreatedContract(RecordItem recordItem) {
    var contractCreateResult = recordItem.getRecord().getContractCreateResult();
    byte[] evmAddress = contractCreateResult.hasEvmAddress() ? DomainUtils.toBytes(contractCreateResult.getEvmAddress().getValue()) : null;
    EntityId createdId = EntityId.of(recordItem.getRecord().getReceipt().getContractID());
    var contractAssert = assertThat(contractRepository.findById(createdId.getId())).get().returns(recordItem.getConsensusTimestamp(), Contract::getCreatedTimestamp).returns(false, Contract::getDeleted).returns(evmAddress, Contract::getEvmAddress).returns(createdId.getId(), Contract::getId).returns(recordItem.getConsensusTimestamp(), Contract::getTimestampLower).returns(createdId.getEntityNum(), Contract::getNum).returns(createdId.getShardNum(), Contract::getShard).returns(createdId.getType(), Contract::getType);
    var contractCreateInstance = recordItem.getTransactionBody().getContractCreateInstance();
    contractAssert.returns(contractCreateInstance.getDeclineReward(), Contract::isDeclineReward);
    if (contractCreateInstance.getStakedIdCase() == ContractCreateTransactionBody.StakedIdCase.STAKEDID_NOT_SET) {
        return;
    }
    contractAssert.returns(Utility.getEpochDay(recordItem.getConsensusTimestamp()), Contract::getStakePeriodStart);
    if (contractCreateInstance.hasStakedAccountId()) {
        long accountId = AccountIdConverter.INSTANCE.convertToDatabaseColumn(EntityId.of(contractCreateInstance.getStakedAccountId()));
        contractAssert.returns(accountId, Contract::getStakedAccountId).returns(-1L, Contract::getStakedNodeId);
    } else {
        contractAssert.returns(contractCreateInstance.getStakedNodeId(), Contract::getStakedNodeId).returns(-1L, Contract::getStakedAccountId);
    }
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) Contract(com.hedera.mirror.common.domain.contract.Contract)

Example 4 with Contract

use of com.hedera.mirror.common.domain.contract.Contract in project hedera-mirror-node by hashgraph.

the class EntityRecordItemListenerContractTest method contractUpdateAllToNew.

@ParameterizedTest
@EnumSource(value = ContractIdType.class, names = { "PLAIN", "PARSABLE_EVM" })
void contractUpdateAllToNew(ContractIdType contractIdType) {
    SetupResult setupResult = setupContract(CONTRACT_ID, contractIdType, false, false, c -> c.obtainerId(null));
    Transaction transaction = contractUpdateAllTransaction(setupResult.protoContractId, true);
    TransactionBody transactionBody = getTransactionBody(transaction);
    TransactionRecord record = getContractTransactionRecord(transactionBody, ContractTransactionType.UPDATE);
    ContractUpdateTransactionBody contractUpdateTransactionBody = transactionBody.getContractUpdateInstance();
    parseRecordItemAndCommit(new RecordItem(transaction, record));
    assertAll(() -> assertEquals(1, transactionRepository.count()), () -> assertEntities(setupResult.contract.toEntityId()), () -> assertEquals(0, contractResultRepository.count()), () -> assertEquals(3, cryptoTransferRepository.count()), () -> assertTransactionAndRecord(transactionBody, record), () -> assertContractEntity(contractUpdateTransactionBody, record.getConsensusTimestamp()).returns(null, Contract::getCreatedTimestamp).returns(null, // FileId is ignored on updates by HAPI
    Contract::getFileId));
}
Also used : ContractUpdateTransactionBody(com.hederahashgraph.api.proto.java.ContractUpdateTransactionBody) ContractCallTransactionBody(com.hederahashgraph.api.proto.java.ContractCallTransactionBody) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) ContractCreateTransactionBody(com.hederahashgraph.api.proto.java.ContractCreateTransactionBody) Transaction(com.hederahashgraph.api.proto.java.Transaction) ContractUpdateTransactionBody(com.hederahashgraph.api.proto.java.ContractUpdateTransactionBody) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) Contract(com.hedera.mirror.common.domain.contract.Contract) RecordItem(com.hedera.mirror.common.domain.transaction.RecordItem) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with Contract

use of com.hedera.mirror.common.domain.contract.Contract in project hedera-mirror-node by hashgraph.

the class EntityRecordItemListenerContractTest method setupContract.

private SetupResult setupContract(ContractID contractId, ContractIdType contractIdType, boolean persist, boolean cache, Consumer<Contract.ContractBuilder> customizer) {
    EntityId entityId = EntityId.of(contractId);
    byte[] evmAddress = getEvmAddress(contractIdType, entityId);
    ContractID protoContractId = getContractId(CONTRACT_ID, evmAddress);
    var builder = domainBuilder.contract().customize(c -> c.evmAddress(evmAddress).id(entityId.getId()).num(entityId.getEntityNum()));
    if (customizer != null) {
        builder.customize(customizer);
    }
    Contract contract = persist ? builder.persist() : builder.get();
    if (cache) {
        contractIds.put(protoContractId, entityId);
    }
    return new SetupResult(contract, protoContractId);
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) ContractID(com.hederahashgraph.api.proto.java.ContractID) Contract(com.hedera.mirror.common.domain.contract.Contract)

Aggregations

Contract (com.hedera.mirror.common.domain.contract.Contract)49 Test (org.junit.jupiter.api.Test)32 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)30 EntityId (com.hedera.mirror.common.domain.entity.EntityId)21 TransactionBody (com.hederahashgraph.api.proto.java.TransactionBody)18 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)18 RecordItem (com.hedera.mirror.common.domain.transaction.RecordItem)17 IntegrationTest (com.hedera.mirror.importer.IntegrationTest)16 Assertions (org.junit.jupiter.api.Assertions)16 DomainUtils (com.hedera.mirror.common.util.DomainUtils)15 AccountID (com.hederahashgraph.api.proto.java.AccountID)15 ACCOUNT (com.hedera.mirror.common.domain.entity.EntityType.ACCOUNT)13 ContractID (com.hederahashgraph.api.proto.java.ContractID)13 ContractUpdateTransactionBody (com.hederahashgraph.api.proto.java.ContractUpdateTransactionBody)13 EnumSource (org.junit.jupiter.params.provider.EnumSource)13 Entity (com.hedera.mirror.common.domain.entity.Entity)12 AliasNotFoundException (com.hedera.mirror.importer.exception.AliasNotFoundException)12 ContractCreateTransactionBody (com.hederahashgraph.api.proto.java.ContractCreateTransactionBody)12 TransactionRecord (com.hederahashgraph.api.proto.java.TransactionRecord)12 BeforeEach (org.junit.jupiter.api.BeforeEach)12