Search in sources :

Example 21 with ContractID

use of com.hederahashgraph.api.proto.java.ContractID in project hedera-mirror-node by hashgraph.

the class DomainUtilsTest method toEvmAddressContractID.

@Test
void toEvmAddressContractID() throws Exception {
    String expected = "00000001000000000000000200000000000000FF";
    ContractID contractId = ContractID.newBuilder().setShardNum(1).setRealmNum(2).setContractNum(255).build();
    ContractID contractIdEvm = ContractID.newBuilder().setEvmAddress(DomainUtils.fromBytes(Hex.decodeHex(expected))).build();
    assertThat(DomainUtils.toEvmAddress(contractId)).asHexString().isEqualTo(expected);
    assertThat(DomainUtils.toEvmAddress(contractIdEvm)).asHexString().isEqualTo(expected);
    assertThrows(InvalidEntityException.class, () -> DomainUtils.toEvmAddress((ContractID) null));
    assertThrows(InvalidEntityException.class, () -> DomainUtils.toEvmAddress(ContractID.getDefaultInstance()));
}
Also used : ByteString(com.google.protobuf.ByteString) ContractID(com.hederahashgraph.api.proto.java.ContractID) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with ContractID

use of com.hederahashgraph.api.proto.java.ContractID in project hedera-mirror-node by hashgraph.

the class ContractCallTransactionHandler method getEntity.

/**
 * First attempts to extract the contract ID from the receipt, which was populated in HAPI 0.23 for contract calls.
 * Otherwise, falls back to checking the transaction body which may contain an EVM address. In case of partial
 * mirror nodes, it's possible the database does not have the mapping for that EVM address in the body, hence the
 * need for prioritizing the receipt.
 *
 * @param recordItem to check
 * @return The contract ID associated with this contract call
 */
@Override
public EntityId getEntity(RecordItem recordItem) {
    ContractID contractIdBody = recordItem.getTransactionBody().getContractCall().getContractID();
    ContractID contractIdReceipt = recordItem.getRecord().getReceipt().getContractID();
    return entityIdService.lookup(contractIdReceipt, contractIdBody);
}
Also used : ContractID(com.hederahashgraph.api.proto.java.ContractID)

Example 23 with ContractID

use of com.hederahashgraph.api.proto.java.ContractID in project hedera-services by hashgraph.

the class ExtCodeCopyOperationSuite method verifiesExistence.

HapiApiSpec verifiesExistence() {
    final String CONTRACT = "extCodeCopyOpChecker";
    final String INVALID_ADDRESS = "0x0000000000000000000000000000000000123456";
    ByteString EMPTY_BYTECODE = ByteString.EMPTY;
    return defaultHapiSpec("VerifiesExistence").given(fileCreate("bytecode").path(ContractResources.EXT_CODE_OPERATIONS_CHECKER_CONTRACT), contractCreate("extCodeCopyOpChecker").bytecode("bytecode").gas(300_000L)).when().then(contractCall(CONTRACT, ContractResources.EXT_CODE_OP_CHECKER_CODE_COPY_OF, INVALID_ADDRESS).hasKnownStatus(INVALID_SOLIDITY_ADDRESS), contractCallLocal(CONTRACT, ContractResources.EXT_CODE_OP_CHECKER_CODE_COPY_OF, INVALID_ADDRESS).hasAnswerOnlyPrecheck(INVALID_SOLIDITY_ADDRESS), withOpContext((spec, opLog) -> {
        AccountID accountID = spec.registry().getAccountID(DEFAULT_PAYER);
        ContractID contractID = spec.registry().getContractId(CONTRACT);
        String accountSolidityAddress = asHexedSolidityAddress(accountID);
        String contractAddress = asHexedSolidityAddress(contractID);
        final var call = contractCall(CONTRACT, ContractResources.EXT_CODE_OP_CHECKER_CODE_COPY_OF, accountSolidityAddress).via("callRecord");
        final var callRecord = getTxnRecord("callRecord");
        final var accountCodeCallLocal = contractCallLocal(CONTRACT, ContractResources.EXT_CODE_OP_CHECKER_CODE_COPY_OF, accountSolidityAddress).saveResultTo("accountCode");
        final var contractCodeCallLocal = contractCallLocal(CONTRACT, ContractResources.EXT_CODE_OP_CHECKER_CODE_COPY_OF, contractAddress).saveResultTo("contractCode");
        final var getBytecodeCall = getContractBytecode(CONTRACT).saveResultTo("contractGetBytecode");
        allRunFor(spec, call, callRecord, accountCodeCallLocal, contractCodeCallLocal, getBytecodeCall);
        final var recordResult = callRecord.getResponseRecord().getContractCallResult();
        final var accountCode = spec.registry().getBytes("accountCode");
        final var contractCode = spec.registry().getBytes("contractCode");
        final var getBytecode = spec.registry().getBytes("contractGetBytecode");
        Assertions.assertEquals(EMPTY_BYTECODE, recordResult.getContractCallResult());
        Assertions.assertArrayEquals(EMPTY_BYTECODE.toByteArray(), accountCode);
        Assertions.assertArrayEquals(getBytecode, contractCode);
    }));
}
Also used : UtilVerbs.withOpContext(com.hedera.services.bdd.spec.utilops.UtilVerbs.withOpContext) TxnVerbs.contractCall(com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCall) CustomSpecAssert.allRunFor(com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor) ContractResources(com.hedera.services.bdd.spec.infrastructure.meta.ContractResources) ByteString(com.google.protobuf.ByteString) ContractID(com.hederahashgraph.api.proto.java.ContractID) QueryVerbs.getTxnRecord(com.hedera.services.bdd.spec.queries.QueryVerbs.getTxnRecord) List(java.util.List) TxnVerbs.fileCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.fileCreate) Logger(org.apache.logging.log4j.Logger) HapiApiSuite(com.hedera.services.bdd.suites.HapiApiSuite) HapiApiSpec.defaultHapiSpec(com.hedera.services.bdd.spec.HapiApiSpec.defaultHapiSpec) Assertions(org.junit.jupiter.api.Assertions) QueryVerbs.contractCallLocal(com.hedera.services.bdd.spec.queries.QueryVerbs.contractCallLocal) INVALID_SOLIDITY_ADDRESS(com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_SOLIDITY_ADDRESS) HapiPropertySource.asHexedSolidityAddress(com.hedera.services.bdd.spec.HapiPropertySource.asHexedSolidityAddress) QueryVerbs.getContractBytecode(com.hedera.services.bdd.spec.queries.QueryVerbs.getContractBytecode) TxnVerbs.contractCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCreate) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) AccountID(com.hederahashgraph.api.proto.java.AccountID) LogManager(org.apache.logging.log4j.LogManager) AccountID(com.hederahashgraph.api.proto.java.AccountID) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ContractID(com.hederahashgraph.api.proto.java.ContractID)

Example 24 with ContractID

use of com.hederahashgraph.api.proto.java.ContractID in project hedera-services by hashgraph.

the class ExtCodeHashOperationSuite method verifiesExistence.

HapiApiSpec verifiesExistence() {
    final String CONTRACT = "extCodeHashOpChecker";
    final String INVALID_ADDRESS = "0x0000000000000000000000000000000000123456";
    final ByteString EXPECTED_ACCOUNT_HASH = ByteString.copyFrom(Hash.keccak256(Bytes.EMPTY).toArray());
    return defaultHapiSpec("VerifiesExistence").given(fileCreate("bytecode").path(ContractResources.EXT_CODE_OPERATIONS_CHECKER_CONTRACT), contractCreate(CONTRACT).bytecode("bytecode").gas(300_000L)).when().then(contractCall(CONTRACT, ContractResources.EXT_CODE_OP_CHECKER_HASH_OF, INVALID_ADDRESS).hasKnownStatus(INVALID_SOLIDITY_ADDRESS), contractCallLocal(CONTRACT, ContractResources.EXT_CODE_OP_CHECKER_HASH_OF, INVALID_ADDRESS).hasAnswerOnlyPrecheck(INVALID_SOLIDITY_ADDRESS), withOpContext((spec, opLog) -> {
        AccountID accountID = spec.registry().getAccountID(DEFAULT_PAYER);
        ContractID contractID = spec.registry().getContractId(CONTRACT);
        String accountSolidityAddress = asHexedSolidityAddress(accountID);
        String contractAddress = asHexedSolidityAddress(contractID);
        final var call = contractCall(CONTRACT, ContractResources.EXT_CODE_OP_CHECKER_HASH_OF, accountSolidityAddress).via("callRecord");
        final var callRecord = getTxnRecord("callRecord");
        final var accountCodeHashCallLocal = contractCallLocal(CONTRACT, ContractResources.EXT_CODE_OP_CHECKER_HASH_OF, accountSolidityAddress).saveResultTo("accountCodeHash");
        final var contractCodeHash = contractCallLocal(CONTRACT, ContractResources.EXT_CODE_OP_CHECKER_HASH_OF, contractAddress).saveResultTo("contractCodeHash");
        final var getBytecode = getContractBytecode(CONTRACT).saveResultTo("contractBytecode");
        allRunFor(spec, call, callRecord, accountCodeHashCallLocal, contractCodeHash, getBytecode);
        final var recordResult = callRecord.getResponseRecord().getContractCallResult();
        final var accountCodeHash = spec.registry().getBytes("accountCodeHash");
        final var contractCodeResult = spec.registry().getBytes("contractCodeHash");
        final var contractBytecode = spec.registry().getBytes("contractBytecode");
        final var expectedContractCodeHash = ByteString.copyFrom(Hash.keccak256(Bytes.of(contractBytecode)).toArray()).toByteArray();
        Assertions.assertEquals(EXPECTED_ACCOUNT_HASH, recordResult.getContractCallResult());
        Assertions.assertArrayEquals(EXPECTED_ACCOUNT_HASH.toByteArray(), accountCodeHash);
        Assertions.assertArrayEquals(expectedContractCodeHash, contractCodeResult);
    }));
}
Also used : UtilVerbs.withOpContext(com.hedera.services.bdd.spec.utilops.UtilVerbs.withOpContext) TxnVerbs.contractCall(com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCall) Bytes(org.apache.tuweni.bytes.Bytes) CustomSpecAssert.allRunFor(com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor) ContractResources(com.hedera.services.bdd.spec.infrastructure.meta.ContractResources) Hash(org.hyperledger.besu.crypto.Hash) ByteString(com.google.protobuf.ByteString) ContractID(com.hederahashgraph.api.proto.java.ContractID) QueryVerbs.getTxnRecord(com.hedera.services.bdd.spec.queries.QueryVerbs.getTxnRecord) List(java.util.List) TxnVerbs.fileCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.fileCreate) Logger(org.apache.logging.log4j.Logger) HapiApiSuite(com.hedera.services.bdd.suites.HapiApiSuite) HapiApiSpec.defaultHapiSpec(com.hedera.services.bdd.spec.HapiApiSpec.defaultHapiSpec) Assertions(org.junit.jupiter.api.Assertions) QueryVerbs.contractCallLocal(com.hedera.services.bdd.spec.queries.QueryVerbs.contractCallLocal) INVALID_SOLIDITY_ADDRESS(com.hederahashgraph.api.proto.java.ResponseCodeEnum.INVALID_SOLIDITY_ADDRESS) HapiPropertySource.asHexedSolidityAddress(com.hedera.services.bdd.spec.HapiPropertySource.asHexedSolidityAddress) QueryVerbs.getContractBytecode(com.hedera.services.bdd.spec.queries.QueryVerbs.getContractBytecode) TxnVerbs.contractCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCreate) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) AccountID(com.hederahashgraph.api.proto.java.AccountID) LogManager(org.apache.logging.log4j.LogManager) AccountID(com.hederahashgraph.api.proto.java.AccountID) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ContractID(com.hederahashgraph.api.proto.java.ContractID)

Example 25 with ContractID

use of com.hederahashgraph.api.proto.java.ContractID in project hedera-services by hashgraph.

the class GetAccountBalanceAnswerTest method answersWithAccountBalanceWhenTheAccountIDIsContractID.

@Test
void answersWithAccountBalanceWhenTheAccountIDIsContractID() {
    // setup:
    ContractID id = asContract(accountIdLit);
    // given:
    CryptoGetAccountBalanceQuery op = CryptoGetAccountBalanceQuery.newBuilder().setContractID(id).build();
    Query query = Query.newBuilder().setCryptogetAccountBalance(op).build();
    // when:
    Response response = subject.responseGiven(query, view, OK);
    ResponseCodeEnum status = response.getCryptogetAccountBalance().getHeader().getNodeTransactionPrecheckCode();
    long answer = response.getCryptogetAccountBalance().getBalance();
    // expect:
    assertTrue(response.getCryptogetAccountBalance().hasHeader(), "Missing response header!");
    assertEquals(List.of(tokenBalanceWith(aToken, aBalance, 1), tokenBalanceWith(bToken, bBalance, 2), tokenBalanceWith(cToken, cBalance, 123), tokenBalanceWith(dToken, dBalance, 0)), response.getCryptogetAccountBalance().getTokenBalancesList());
    assertEquals(OK, status);
    assertEquals(balance, answer);
    assertEquals(asAccount(accountIdLit), response.getCryptogetAccountBalance().getAccountID());
}
Also used : Response(com.hederahashgraph.api.proto.java.Response) CryptoGetAccountBalanceResponse(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceResponse) ResponseCodeEnum(com.hederahashgraph.api.proto.java.ResponseCodeEnum) CryptoGetAccountBalanceQuery(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery) Query(com.hederahashgraph.api.proto.java.Query) CryptoGetAccountBalanceQuery(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery) ContractID(com.hederahashgraph.api.proto.java.ContractID) Test(org.junit.jupiter.api.Test)

Aggregations

ContractID (com.hederahashgraph.api.proto.java.ContractID)33 Test (org.junit.jupiter.api.Test)18 EntityId (com.hedera.mirror.common.domain.entity.EntityId)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 AccountID (com.hederahashgraph.api.proto.java.AccountID)9 Contract (com.hedera.mirror.common.domain.contract.Contract)7 Assertions (org.junit.jupiter.api.Assertions)7 ByteString (com.google.protobuf.ByteString)6 IntegrationTest (com.hedera.mirror.importer.IntegrationTest)6 ResponseCodeEnum (com.hederahashgraph.api.proto.java.ResponseCodeEnum)6 DomainUtils (com.hedera.mirror.common.util.DomainUtils)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Range (com.google.common.collect.Range)4 EntityType (com.hedera.mirror.common.domain.entity.EntityType)4 RecordItem (com.hedera.mirror.common.domain.transaction.RecordItem)4 RecordItemBuilder (com.hedera.mirror.importer.parser.domain.RecordItemBuilder)4 Utility (com.hedera.mirror.importer.util.Utility)4 ContractUpdateTransactionBody (com.hederahashgraph.api.proto.java.ContractUpdateTransactionBody)4 TransactionBody (com.hederahashgraph.api.proto.java.TransactionBody)4 List (java.util.List)4