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();
}
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));
}
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);
}
}
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));
}
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);
}
Aggregations