Search in sources :

Example 1 with CommonUtils

use of com.swirlds.common.CommonUtils in project hedera-services by hashgraph.

the class HapiContractCall method opBodyDef.

@Override
protected Consumer<TransactionBody.Builder> opBodyDef(HapiApiSpec spec) throws Throwable {
    if (details.isPresent()) {
        ActionableContractCall actionable = spec.registry().getActionableCall(details.get());
        contract = actionable.getContract();
        abi = actionable.getDetails().getAbi();
        params = actionable.getDetails().getExampleArgs();
    } else if (paramsFn.isPresent()) {
        params = paramsFn.get().apply(spec);
    }
    byte[] callData;
    if (explicitHexedParams.isPresent()) {
        callData = explicitHexedParams.map(Supplier::get).map(CommonUtils::unhex).get();
    } else {
        final var paramsList = Arrays.asList(params);
        final var tupleExist = paramsList.stream().anyMatch(p -> p instanceof Tuple || p instanceof Tuple[]);
        if (tupleExist) {
            callData = encodeParametersWithTuple(params);
        } else {
            callData = (!abi.equals(FALLBACK_ABI)) ? CallTransaction.Function.fromJsonInterface(abi).encode(params) : new byte[] {};
        }
    }
    ContractCallTransactionBody opBody = spec.txns().<ContractCallTransactionBody, ContractCallTransactionBody.Builder>body(ContractCallTransactionBody.class, builder -> {
        if (!tryAsHexedAddressIfLenMatches) {
            builder.setContractID(spec.registry().getContractId(contract));
        } else {
            builder.setContractID(TxnUtils.asContractId(contract, spec));
        }
        builder.setFunctionParameters(ByteString.copyFrom(callData));
        sentTinyHbars.ifPresent(builder::setAmount);
        gas.ifPresent(builder::setGas);
    });
    return b -> b.setContractCall(opBody);
}
Also used : Transaction(com.hederahashgraph.api.proto.java.Transaction) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) Arrays(java.util.Arrays) TrieSigMapGenerator.uniqueWithFullPrefixesFor(com.hedera.services.bdd.spec.keys.TrieSigMapGenerator.uniqueWithFullPrefixesFor) HapiTxnOp(com.hedera.services.bdd.spec.transactions.HapiTxnOp) CallTransaction(org.ethereum.core.CallTransaction) Function(java.util.function.Function) Supplier(java.util.function.Supplier) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ArrayList(java.util.ArrayList) Tuple(com.esaulpaugh.headlong.abi.Tuple) QueryVerbs.getTxnRecord(com.hedera.services.bdd.spec.queries.QueryVerbs.getTxnRecord) TxnUtils(com.hedera.services.bdd.spec.transactions.TxnUtils) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) ContractCallTransactionBody(com.hederahashgraph.api.proto.java.ContractCallTransactionBody) TransactionResponse(com.hederahashgraph.api.proto.java.TransactionResponse) HashUtil.sha3(org.ethereum.crypto.HashUtil.sha3) GENESIS(com.hedera.services.bdd.suites.HapiApiSuite.GENESIS) TupleType(com.esaulpaugh.headlong.abi.TupleType) ResponseCodeEnum(com.hederahashgraph.api.proto.java.ResponseCodeEnum) ActionableContractCall(com.hedera.services.bdd.spec.infrastructure.meta.ActionableContractCall) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MoreObjects(com.google.common.base.MoreObjects) CustomSpecAssert.allRunFor(com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor) LongConsumer(java.util.function.LongConsumer) ByteString(com.google.protobuf.ByteString) Consumer(java.util.function.Consumer) Key(com.hederahashgraph.api.proto.java.Key) List(java.util.List) CommonUtils(com.swirlds.common.CommonUtils) ByteUtil(org.ethereum.util.ByteUtil) ObjLongConsumer(java.util.function.ObjLongConsumer) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Optional(java.util.Optional) TxnUtils.extractTxnId(com.hedera.services.bdd.spec.transactions.TxnUtils.extractTxnId) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) Collections(java.util.Collections) ActionableContractCall(com.hedera.services.bdd.spec.infrastructure.meta.ActionableContractCall) CommonUtils(com.swirlds.common.CommonUtils) ContractCallTransactionBody(com.hederahashgraph.api.proto.java.ContractCallTransactionBody) Tuple(com.esaulpaugh.headlong.abi.Tuple)

Example 2 with CommonUtils

use of com.swirlds.common.CommonUtils in project hedera-services by hashgraph.

the class HapiContractCreate method opBodyDef.

@Override
protected Consumer<TransactionBody.Builder> opBodyDef(HapiApiSpec spec) throws Throwable {
    if (!omitAdminKey && !useDeprecatedAdminKey) {
        generateAdminKey(spec);
    }
    if (bytecodeFileFn.isPresent()) {
        bytecodeFile = Optional.of(bytecodeFileFn.get().get());
    }
    if (!bytecodeFile.isPresent()) {
        setBytecodeToDefaultContract(spec);
    }
    Optional<byte[]> params;
    if (explicitHexedParams.isPresent()) {
        params = explicitHexedParams.map(Supplier::get).map(CommonUtils::unhex);
    } else {
        params = abi.isPresent() ? Optional.of(CallTransaction.Function.fromJsonInterface(abi.get()).encodeArguments(args.get())) : Optional.empty();
    }
    FileID bytecodeFileId = TxnUtils.asFileId(bytecodeFile.get(), spec);
    ContractCreateTransactionBody opBody = spec.txns().<ContractCreateTransactionBody, ContractCreateTransactionBody.Builder>body(ContractCreateTransactionBody.class, b -> {
        if (useDeprecatedAdminKey) {
            b.setAdminKey(DEPRECATED_CID_ADMIN_KEY);
        } else if (!omitAdminKey) {
            b.setAdminKey(adminKey);
        }
        b.setFileID(bytecodeFileId);
        autoRenewPeriodSecs.ifPresent(p -> b.setAutoRenewPeriod(Duration.newBuilder().setSeconds(p).build()));
        balance.ifPresent(b::setInitialBalance);
        memo.ifPresent(b::setMemo);
        gas.ifPresent(b::setGas);
        proxy.ifPresent(p -> b.setProxyAccountID(asId(p, spec)));
        params.ifPresent(bytes -> b.setConstructorParameters(ByteString.copyFrom(bytes)));
    });
    return b -> b.setContractCreateInstance(opBody);
}
Also used : Transaction(com.hederahashgraph.api.proto.java.Transaction) SigControl(com.hedera.services.bdd.spec.keys.SigControl) HapiFileCreate(com.hedera.services.bdd.spec.transactions.file.HapiFileCreate) HapiTxnOp(com.hedera.services.bdd.spec.transactions.HapiTxnOp) ContractGetInfoResponse(com.hederahashgraph.api.proto.java.ContractGetInfoResponse) CallTransaction(org.ethereum.core.CallTransaction) HapiContractCall.doGasLookup(com.hedera.services.bdd.spec.transactions.contract.HapiContractCall.doGasLookup) Duration(com.hederahashgraph.api.proto.java.Duration) Function(java.util.function.Function) Supplier(java.util.function.Supplier) HapiSpecRegistry(com.hedera.services.bdd.spec.infrastructure.HapiSpecRegistry) TxnVerbs(com.hedera.services.bdd.spec.transactions.TxnVerbs) ContractID(com.hederahashgraph.api.proto.java.ContractID) OptionalLong(java.util.OptionalLong) TxnUtils.asId(com.hedera.services.bdd.spec.transactions.TxnUtils.asId) TxnUtils(com.hedera.services.bdd.spec.transactions.TxnUtils) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) TransactionResponse(com.hederahashgraph.api.proto.java.TransactionResponse) TxnUtils.solidityIdFrom(com.hedera.services.bdd.spec.transactions.TxnUtils.solidityIdFrom) FileID(com.hederahashgraph.api.proto.java.FileID) TxnUtils.equivAccount(com.hedera.services.bdd.spec.transactions.TxnUtils.equivAccount) ResponseCodeEnum(com.hederahashgraph.api.proto.java.ResponseCodeEnum) KeyGenerator(com.hedera.services.bdd.spec.keys.KeyGenerator) MoreObjects(com.google.common.base.MoreObjects) SUCCESS(com.hederahashgraph.api.proto.java.ResponseCodeEnum.SUCCESS) LongConsumer(java.util.function.LongConsumer) ByteString(com.google.protobuf.ByteString) KeyFactory(com.hedera.services.bdd.spec.keys.KeyFactory) Consumer(java.util.function.Consumer) TxnFactory.bannerWith(com.hedera.services.bdd.spec.transactions.TxnFactory.bannerWith) Key(com.hederahashgraph.api.proto.java.Key) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ContractCreateTransactionBody(com.hederahashgraph.api.proto.java.ContractCreateTransactionBody) CommonUtils(com.swirlds.common.CommonUtils) ObjLongConsumer(java.util.function.ObjLongConsumer) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Optional(java.util.Optional) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) LogManager(org.apache.logging.log4j.LogManager) ContractCreateTransactionBody(com.hederahashgraph.api.proto.java.ContractCreateTransactionBody) Supplier(java.util.function.Supplier) FileID(com.hederahashgraph.api.proto.java.FileID)

Aggregations

MoreObjects (com.google.common.base.MoreObjects)2 ByteString (com.google.protobuf.ByteString)2 HapiApiSpec (com.hedera.services.bdd.spec.HapiApiSpec)2 HapiTxnOp (com.hedera.services.bdd.spec.transactions.HapiTxnOp)2 TxnUtils (com.hedera.services.bdd.spec.transactions.TxnUtils)2 HederaFunctionality (com.hederahashgraph.api.proto.java.HederaFunctionality)2 Key (com.hederahashgraph.api.proto.java.Key)2 ResponseCodeEnum (com.hederahashgraph.api.proto.java.ResponseCodeEnum)2 Transaction (com.hederahashgraph.api.proto.java.Transaction)2 TransactionBody (com.hederahashgraph.api.proto.java.TransactionBody)2 TransactionResponse (com.hederahashgraph.api.proto.java.TransactionResponse)2 CommonUtils (com.swirlds.common.CommonUtils)2 List (java.util.List)2 Optional (java.util.Optional)2 Consumer (java.util.function.Consumer)2 Function (java.util.function.Function)2 LongConsumer (java.util.function.LongConsumer)2 ObjLongConsumer (java.util.function.ObjLongConsumer)2 Supplier (java.util.function.Supplier)2 CallTransaction (org.ethereum.core.CallTransaction)2