Search in sources :

Example 41 with Contract

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

the class SqlEntityListenerTest method onContract.

@Test
void onContract() {
    // given
    Contract contract1 = domainBuilder.contract().get();
    Contract contract2 = domainBuilder.contract().customize(c -> c.fileId(null).initcode(domainBuilder.bytes(1024)).autoRenewAccountId(null).evmAddress(null)).get();
    // when
    sqlEntityListener.onContract(contract1);
    sqlEntityListener.onContract(contract2);
    completeFileAndCommit();
    // then
    assertThat(entityRepository.count()).isZero();
    assertThat(contractRepository.findAll()).containsExactlyInAnyOrder(contract1, contract2);
    assertThat(findHistory(Contract.class)).isEmpty();
}
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) Contract(com.hedera.mirror.common.domain.contract.Contract) Test(org.junit.jupiter.api.Test) IntegrationTest(com.hedera.mirror.importer.IntegrationTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 42 with Contract

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

the class SqlEntityListenerTest method onContractWithHistoryAndNonHistoryUpdates.

@ValueSource(booleans = { true, false })
@ParameterizedTest
void onContractWithHistoryAndNonHistoryUpdates(boolean nonHistoryBefore) {
    // given
    Contract contract = domainBuilder.contract().persist();
    Contract contractStakePeriodStart1 = contract.toEntityId().toEntity();
    contractStakePeriodStart1.setStakePeriodStart(2L);
    contractStakePeriodStart1.setTimestampRange(null);
    Contract contractStakePeriodStart2 = contract.toEntityId().toEntity();
    contractStakePeriodStart2.setStakePeriodStart(7L);
    contractStakePeriodStart2.setTimestampRange(null);
    Contract contractMemoUpdated = contract.toEntityId().toEntity();
    contractMemoUpdated.setMemo(domainBuilder.text(16));
    contractMemoUpdated.setTimestampLower(domainBuilder.timestamp());
    // when
    if (nonHistoryBefore) {
        sqlEntityListener.onContract(contractStakePeriodStart1);
        sqlEntityListener.onContract(contractStakePeriodStart2);
        sqlEntityListener.onContract(contractMemoUpdated);
    } else {
        sqlEntityListener.onContract(contractMemoUpdated);
        sqlEntityListener.onContract(contractStakePeriodStart1);
        sqlEntityListener.onContract(contractStakePeriodStart2);
    }
    completeFileAndCommit();
    // then
    Contract contractMerged = TestUtils.clone(contract);
    contractMerged.setMemo(contractMemoUpdated.getMemo());
    contractMerged.setStakePeriodStart(contractStakePeriodStart2.getStakePeriodStart());
    contractMerged.setTimestampRange(contractMemoUpdated.getTimestampRange());
    contract.setTimestampUpper(contractMemoUpdated.getTimestampLower());
    assertThat(contractRepository.findAll()).containsExactly(contractMerged);
    assertThat(findHistory(Contract.class)).containsExactly(contract);
}
Also used : Contract(com.hedera.mirror.common.domain.contract.Contract) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 43 with Contract

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

the class SqlEntityListenerTest method onContractHistory.

@ParameterizedTest(name = "{2} record files with {0}")
@MethodSource("provideParamsContractHistory")
void onContractHistory(String bytecodeSource, Consumer<Contract.ContractBuilder> customizer, int commitIndex) {
    // given
    Contract contractCreate = domainBuilder.contract().customize(c -> c.obtainerId(null)).customize(customizer).get();
    Contract contractUpdate = contractCreate.toEntityId().toEntity();
    contractUpdate.setAutoRenewAccountId(110L);
    contractUpdate.setAutoRenewPeriod(30L);
    contractUpdate.setExpirationTimestamp(500L);
    contractUpdate.setKey(domainBuilder.key());
    contractUpdate.setMaxAutomaticTokenAssociations(100);
    contractUpdate.setMemo("updated");
    contractUpdate.setTimestampLower(contractCreate.getTimestampLower() + 1);
    contractUpdate.setProxyAccountId(EntityId.of(100L, ACCOUNT));
    Contract contractDelete = contractCreate.toEntityId().toEntity();
    contractDelete.setDeleted(true);
    contractDelete.setPermanentRemoval(true);
    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 : Contract(com.hedera.mirror.common.domain.contract.Contract) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 44 with Contract

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

the class SqlEntityListenerTest method onContractWithNonHistoryUpdates.

@Test
void onContractWithNonHistoryUpdates() {
    // given
    // Update to non-history field with existing contract should just update nonce
    var existingContract = domainBuilder.contract().persist();
    Contract existingContractStakePeriodStart1 = existingContract.toEntityId().toEntity();
    existingContractStakePeriodStart1.setStakePeriodStart(2L);
    existingContractStakePeriodStart1.setTimestampRange(null);
    Contract existingContractStakePeriodStart2 = existingContract.toEntityId().toEntity();
    existingContractStakePeriodStart2.setStakePeriodStart(5L);
    existingContractStakePeriodStart2.setTimestampRange(null);
    // Update to non-history field with partial data should be discarded
    Contract nonExistingContract = domainBuilder.contract().get();
    Contract nonExistingContractStakePeriodStart1 = nonExistingContract.toEntityId().toEntity();
    nonExistingContractStakePeriodStart1.setStakePeriodStart(6L);
    nonExistingContractStakePeriodStart1.setTimestampRange(null);
    Contract nonExistingContractStakePeriodStart2 = nonExistingContract.toEntityId().toEntity();
    nonExistingContractStakePeriodStart2.setStakePeriodStart(8L);
    nonExistingContractStakePeriodStart2.setTimestampRange(null);
    // when
    sqlEntityListener.onContract(existingContractStakePeriodStart1);
    sqlEntityListener.onContract(existingContractStakePeriodStart2);
    sqlEntityListener.onContract(nonExistingContractStakePeriodStart1);
    sqlEntityListener.onContract(nonExistingContractStakePeriodStart2);
    completeFileAndCommit();
    existingContract.setStakePeriodStart(existingContractStakePeriodStart2.getStakePeriodStart());
    assertThat(contractRepository.findAll()).containsExactly(existingContract);
    Contract existingContractStakePeriodStart3 = existingContract.toEntityId().toEntity();
    existingContractStakePeriodStart3.setStakePeriodStart(10L);
    existingContractStakePeriodStart3.setTimestampRange(null);
    Contract nonExistingContractStakePeriodStart3 = domainBuilder.entityId(EntityType.CONTRACT).toEntity();
    nonExistingContractStakePeriodStart3.setStakePeriodStart(12L);
    nonExistingContractStakePeriodStart3.setTimestampRange(null);
    sqlEntityListener.onContract(existingContractStakePeriodStart3);
    sqlEntityListener.onContract(nonExistingContractStakePeriodStart3);
    completeFileAndCommit();
    // then
    existingContract.setStakePeriodStart(existingContractStakePeriodStart3.getStakePeriodStart());
    assertThat(contractRepository.findAll()).containsExactly(existingContract);
    assertThat(findHistory(Contract.class)).isEmpty();
}
Also used : Contract(com.hedera.mirror.common.domain.contract.Contract) Test(org.junit.jupiter.api.Test) IntegrationTest(com.hedera.mirror.importer.IntegrationTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 45 with Contract

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

the class ContractCreateTransactionHandlerTest method updateTransactionWithAliasNotFoundAndPartialDataActionSkip.

@Test
void updateTransactionWithAliasNotFoundAndPartialDataActionSkip() {
    recordParserProperties.setPartialDataAction(PartialDataAction.SKIP);
    var alias = DomainUtils.fromBytes(domainBuilder.key());
    var recordItem = recordItemBuilder.contractCreate().transactionBody(b -> b.getAutoRenewAccountIdBuilder().setAlias(alias)).build();
    var contractId = EntityId.of(recordItem.getRecord().getReceipt().getContractID());
    var timestamp = recordItem.getConsensusTimestamp();
    var transaction = domainBuilder.transaction().customize(t -> t.consensusTimestamp(timestamp).entityId(contractId)).get();
    when(entityIdService.lookup(AccountID.newBuilder().setAlias(alias).build())).thenThrow(new AliasNotFoundException("alias", ACCOUNT));
    transactionHandler.updateTransaction(transaction, recordItem);
    assertContract(contractId, timestamp, t -> assertThat(t).returns(null, Contract::getAutoRenewAccountId).returns(null, Contract::getEvmAddress).satisfies(c -> assertThat(c.getFileId().getId()).isPositive()).returns(null, Contract::getInitcode));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) BeforeEach(org.junit.jupiter.api.BeforeEach) Contract(com.hedera.mirror.common.domain.contract.Contract) PartialDataAction(com.hedera.mirror.importer.parser.PartialDataAction) EntityId(com.hedera.mirror.common.domain.entity.EntityId) Descriptors(com.google.protobuf.Descriptors) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EnumSource(org.junit.jupiter.params.provider.EnumSource) EthereumTransactionParser(com.hedera.mirror.importer.parser.record.ethereum.EthereumTransactionParser) Captor(org.mockito.Captor) EntityType(com.hedera.mirror.common.domain.entity.EntityType) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) RecordParserProperties(com.hedera.mirror.importer.parser.record.RecordParserProperties) ArgumentCaptor(org.mockito.ArgumentCaptor) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) AliasNotFoundException(com.hedera.mirror.importer.exception.AliasNotFoundException) TestUtils(com.hedera.mirror.importer.TestUtils) Mockito.doReturn(org.mockito.Mockito.doReturn) AccountID(com.hederahashgraph.api.proto.java.AccountID) Utility(com.hedera.mirror.importer.util.Utility) ContractResult(com.hedera.mirror.common.domain.contract.ContractResult) DomainUtils(com.hedera.mirror.common.util.DomainUtils) FileID(com.hederahashgraph.api.proto.java.FileID) ResponseCodeEnum(com.hederahashgraph.api.proto.java.ResponseCodeEnum) EntityIdEndec(com.hedera.mirror.common.domain.entity.EntityIdEndec) Range(com.google.common.collect.Range) Mockito.when(org.mockito.Mockito.when) AbstractEntity(com.hedera.mirror.common.domain.entity.AbstractEntity) StandardCharsets(java.nio.charset.StandardCharsets) Mockito.verify(org.mockito.Mockito.verify) TransactionReceipt(com.hederahashgraph.api.proto.java.TransactionReceipt) CONTRACT(com.hedera.mirror.common.domain.entity.EntityType.CONTRACT) ByteString(com.google.protobuf.ByteString) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) BytesValue(com.google.protobuf.BytesValue) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ContractCreateTransactionBody(com.hederahashgraph.api.proto.java.ContractCreateTransactionBody) EntityProperties(com.hedera.mirror.importer.parser.record.entity.EntityProperties) Assertions(org.junit.jupiter.api.Assertions) Message(com.google.protobuf.Message) ContractFunctionResult(com.hederahashgraph.api.proto.java.ContractFunctionResult) ACCOUNT(com.hedera.mirror.common.domain.entity.EntityType.ACCOUNT) RecordItem(com.hedera.mirror.common.domain.transaction.RecordItem) Transaction(com.hedera.mirror.common.domain.transaction.Transaction) AliasNotFoundException(com.hedera.mirror.importer.exception.AliasNotFoundException) Contract(com.hedera.mirror.common.domain.contract.Contract) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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