Search in sources :

Example 21 with Key

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

the class ContractCreateTransitionLogicTest method rejectSerializationFailed.

@Test
void rejectSerializationFailed() {
    Key key = Key.getDefaultInstance();
    var op = ContractCreateTransactionBody.newBuilder().setFileID(bytecodeSrc).setInitialBalance(balance).setGas(gas).setAdminKey(key).setProxyAccountID(proxy);
    var txn = TransactionBody.newBuilder().setTransactionID(ourTxnId()).setContractCreateInstance(op);
    contractCreateTxn = txn.build();
    given(accessor.getTxn()).willReturn(contractCreateTxn);
    given(txnCtx.accessor()).willReturn(accessor);
    given(validator.attemptToDecodeOrThrow(key, SERIALIZATION_FAILED)).willThrow(new InvalidTransactionException(SERIALIZATION_FAILED));
    // when:
    Exception exception = assertThrows(InvalidTransactionException.class, () -> subject.doStateTransition());
    // then:
    assertEquals("SERIALIZATION_FAILED", exception.getMessage());
}
Also used : InvalidTransactionException(com.hedera.services.exceptions.InvalidTransactionException) JContractIDKey(com.hedera.services.legacy.core.jproto.JContractIDKey) JEd25519Key(com.hedera.services.legacy.core.jproto.JEd25519Key) Key(com.hederahashgraph.api.proto.java.Key) InvalidTransactionException(com.hedera.services.exceptions.InvalidTransactionException) Test(org.junit.jupiter.api.Test)

Example 22 with Key

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

the class HapiFileUpdate method opBodyDef.

@Override
protected Consumer<TransactionBody.Builder> opBodyDef(HapiApiSpec spec) throws Throwable {
    Optional<Key> wacl = useBadlyEncodedWacl ? Optional.of(badlyEncodedWacl()) : (useEmptyWacl ? Optional.of(emptyWacl()) : newWaclKey.map(spec.registry()::getKey));
    if (newContentsPath.isPresent()) {
        newContents = Optional.of(ByteString.copyFrom(Files.toByteArray(new File(newContentsPath.get()))));
    } else if (contentFn.isPresent()) {
        newContents = Optional.of(contentFn.get().apply(spec));
    } else if (propOverrides.isPresent() || propDeletions.isPresent()) {
        if (propOverrides.isEmpty()) {
            propOverrides = Optional.of(Collections.emptyMap());
        }
        ServicesConfigurationList defaults = readBaseProps(spec);
        ServicesConfigurationList.Builder list = ServicesConfigurationList.newBuilder();
        Map<String, String> overrides = propOverrides.get();
        Map<String, String> defaultPairs = defaults.getNameValueList().stream().collect(Collectors.toMap(Setting::getName, Setting::getValue));
        Set<String> keys = new HashSet<>();
        defaults.getNameValueList().stream().map(Setting::getName).filter(key -> !propDeletions.orElse(EMPTY_SET).contains(key)).forEach(keys::add);
        overrides.keySet().stream().forEach(keys::add);
        keys.forEach(key -> {
            if (overrides.containsKey(key)) {
                list.addNameValue(asSetting(key, overrides.get(key)));
            } else {
                list.addNameValue(asSetting(key, defaultPairs.get(key)));
            }
        });
        newContents = Optional.of(list.build().toByteString());
    }
    long nl = -1;
    if (expiryExtension.isPresent()) {
        try {
            var oldExpiry = spec.registry().getTimestamp(file).getSeconds();
            nl = oldExpiry - Instant.now().getEpochSecond() + expiryExtension.getAsLong();
        } catch (Exception ignore) {
        }
    } else if (lifetimeSecs.isPresent()) {
        nl = lifetimeSecs.get();
    }
    final OptionalLong newLifetime = (nl == -1) ? OptionalLong.empty() : OptionalLong.of(nl);
    var fid = TxnUtils.asFileId(file, spec);
    FileUpdateTransactionBody opBody = spec.txns().<FileUpdateTransactionBody, FileUpdateTransactionBody.Builder>body(FileUpdateTransactionBody.class, builder -> {
        builder.setFileID(fid);
        newMemo.ifPresent(s -> builder.setMemo(StringValue.newBuilder().setValue(s).build()));
        wacl.ifPresent(k -> builder.setKeys(k.getKeyList()));
        newContents.ifPresent(b -> builder.setContents(b));
        newLifetime.ifPresent(s -> builder.setExpirationTime(TxnFactory.expiryGiven(s)));
    });
    preUpdateCb.ifPresent(cb -> cb.accept(fid));
    return builder -> builder.setFileUpdate(opBody);
}
Also used : StringValue(com.google.protobuf.StringValue) ServicesConfigurationList(com.hederahashgraph.api.proto.java.ServicesConfigurationList) EMPTY_MAP(java.util.Collections.EMPTY_MAP) EMPTY_SET(java.util.Collections.EMPTY_SET) Map(java.util.Map) KeyList(com.hederahashgraph.api.proto.java.KeyList) TransactionResponse(com.hederahashgraph.api.proto.java.TransactionResponse) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ONE_HBAR(com.hedera.services.bdd.suites.HapiApiSuite.ONE_HBAR) ByteString(com.google.protobuf.ByteString) FileGetInfoResponse(com.hederahashgraph.api.proto.java.FileGetInfoResponse) List(java.util.List) Logger(org.apache.logging.log4j.Logger) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) ExchangeRateSet(com.hederahashgraph.api.proto.java.ExchangeRateSet) Optional(java.util.Optional) TxnUtils.suFrom(com.hedera.services.bdd.spec.transactions.TxnUtils.suFrom) FeeCalculator(com.hedera.services.bdd.spec.fees.FeeCalculator) HapiGetFileInfo(com.hedera.services.bdd.spec.queries.file.HapiGetFileInfo) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) Transaction(com.hederahashgraph.api.proto.java.Transaction) HapiTxnOp(com.hedera.services.bdd.spec.transactions.HapiTxnOp) Function(java.util.function.Function) Setting(com.hederahashgraph.api.proto.java.Setting) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OptionalLong(java.util.OptionalLong) TxnFactory(com.hedera.services.bdd.spec.transactions.TxnFactory) TxnUtils(com.hedera.services.bdd.spec.transactions.TxnUtils) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) Files(com.google.common.io.Files) HapiApiSuite(com.hedera.services.bdd.suites.HapiApiSuite) ExtantFileContext(com.hedera.services.usage.file.ExtantFileContext) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) QueryVerbs.getFileContents(com.hedera.services.bdd.spec.queries.QueryVerbs.getFileContents) FileUpdateTransactionBody(com.hederahashgraph.api.proto.java.FileUpdateTransactionBody) FileID(com.hederahashgraph.api.proto.java.FileID) ResponseCodeEnum(com.hederahashgraph.api.proto.java.ResponseCodeEnum) MoreObjects(com.google.common.base.MoreObjects) QueryVerbs.getFileInfo(com.hedera.services.bdd.spec.queries.QueryVerbs.getFileInfo) CustomSpecAssert.allRunFor(com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor) File(java.io.File) HapiGetFileContents(com.hedera.services.bdd.spec.queries.file.HapiGetFileContents) Consumer(java.util.function.Consumer) Key(com.hederahashgraph.api.proto.java.Key) Assertions(org.junit.jupiter.api.Assertions) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) Setting(com.hederahashgraph.api.proto.java.Setting) ByteString(com.google.protobuf.ByteString) FileUpdateTransactionBody(com.hederahashgraph.api.proto.java.FileUpdateTransactionBody) ServicesConfigurationList(com.hederahashgraph.api.proto.java.ServicesConfigurationList) OptionalLong(java.util.OptionalLong) File(java.io.File) Key(com.hederahashgraph.api.proto.java.Key) HashSet(java.util.HashSet)

Example 23 with Key

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

the class HapiScheduleCreate method defaultSigners.

@Override
protected List<Function<HapiApiSpec, Key>> defaultSigners() {
    List<Function<HapiApiSpec, Key>> signers = new ArrayList<>(List.of(spec -> spec.registry().getKey(effectivePayer(spec))));
    adminKey.ifPresent(k -> signers.add(spec -> spec.registry().getKey(k)));
    for (String added : initialSigners) {
        signers.add(spec -> spec.registry().getKey(added));
    }
    return signers;
}
Also used : Transaction(com.hederahashgraph.api.proto.java.Transaction) HapiTxnOp(com.hedera.services.bdd.spec.transactions.HapiTxnOp) ScheduleUtils(com.hedera.services.bdd.suites.schedule.ScheduleUtils) ScheduleCreate(com.hederahashgraph.api.proto.java.HederaFunctionality.ScheduleCreate) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HapiSpecSetup(com.hedera.services.bdd.spec.HapiSpecSetup) ScheduleCreateTransactionBody(com.hederahashgraph.api.proto.java.ScheduleCreateTransactionBody) TxnUtils(com.hedera.services.bdd.spec.transactions.TxnUtils) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) KeyList(com.hederahashgraph.api.proto.java.KeyList) BiConsumer(java.util.function.BiConsumer) TransactionResponse(com.hederahashgraph.api.proto.java.TransactionResponse) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) HapiPropertySource.asScheduleString(com.hedera.services.bdd.spec.HapiPropertySource.asScheduleString) MoreObjects(com.google.common.base.MoreObjects) SUCCESS(com.hederahashgraph.api.proto.java.ResponseCodeEnum.SUCCESS) SchedulableTransactionBody(com.hederahashgraph.api.proto.java.SchedulableTransactionBody) ByteString(com.google.protobuf.ByteString) 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) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Optional(java.util.Optional) TxnUtils.suFrom(com.hedera.services.bdd.spec.transactions.TxnUtils.suFrom) FeeCalculator(com.hedera.services.bdd.spec.fees.FeeCalculator) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HapiPropertySource.asScheduleString(com.hedera.services.bdd.spec.HapiPropertySource.asScheduleString) ByteString(com.google.protobuf.ByteString)

Example 24 with Key

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

the class HapiContractUpdate method opBodyDef.

@Override
protected Consumer<TransactionBody.Builder> opBodyDef(HapiApiSpec spec) throws Throwable {
    Optional<Key> key = newKey.map(spec.registry()::getKey);
    ContractUpdateTransactionBody opBody = spec.txns().<ContractUpdateTransactionBody, ContractUpdateTransactionBody.Builder>body(ContractUpdateTransactionBody.class, b -> {
        if (contract.length() == HEXED_EVM_ADDRESS_LEN) {
            b.setContractID(ContractID.newBuilder().setEvmAddress(ByteString.copyFrom(CommonUtils.unhex(contract))));
        } else {
            b.setContractID(TxnUtils.asContractId(contract, spec));
        }
        if (useDeprecatedAdminKey) {
            b.setAdminKey(DEPRECATED_CID_ADMIN_KEY);
        } else if (wipeToThresholdKey) {
            b.setAdminKey(TxnUtils.EMPTY_THRESHOLD_KEY);
        } else if (useEmptyAdminKeyList) {
            b.setAdminKey(TxnUtils.EMPTY_KEY_LIST);
        } else {
            key.ifPresent(b::setAdminKey);
        }
        newExpirySecs.ifPresent(t -> b.setExpirationTime(Timestamp.newBuilder().setSeconds(t).build()));
        newMemo.ifPresent(s -> {
            if (useDeprecatedMemoField) {
                b.setMemo(s);
            } else {
                b.setMemoWrapper(StringValue.newBuilder().setValue(s).build());
            }
        });
        newAutoRenew.ifPresent(autoRenew -> b.setAutoRenewPeriod(Duration.newBuilder().setSeconds(autoRenew).build()));
        bytecode.ifPresent(f -> b.setFileID(TxnUtils.asFileId(bytecode.get(), spec)).build());
    });
    return builder -> builder.setContractUpdateInstance(opBody);
}
Also used : Transaction(com.hederahashgraph.api.proto.java.Transaction) HapiTxnOp(com.hedera.services.bdd.spec.transactions.HapiTxnOp) StringValue(com.google.protobuf.StringValue) HEXED_EVM_ADDRESS_LEN(com.hedera.services.bdd.spec.transactions.contract.HapiContractCall.HEXED_EVM_ADDRESS_LEN) ContractUpdateTransactionBody(com.hederahashgraph.api.proto.java.ContractUpdateTransactionBody) Duration(com.hederahashgraph.api.proto.java.Duration) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ContractID(com.hederahashgraph.api.proto.java.ContractID) OptionalLong(java.util.OptionalLong) TxnFactory(com.hedera.services.bdd.spec.transactions.TxnFactory) TxnUtils(com.hedera.services.bdd.spec.transactions.TxnUtils) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) TransactionResponse(com.hederahashgraph.api.proto.java.TransactionResponse) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) MoreObjects(com.google.common.base.MoreObjects) SUCCESS(com.hederahashgraph.api.proto.java.ResponseCodeEnum.SUCCESS) ByteString(com.google.protobuf.ByteString) Consumer(java.util.function.Consumer) Key(com.hederahashgraph.api.proto.java.Key) List(java.util.List) Logger(org.apache.logging.log4j.Logger) CommonUtils(com.swirlds.common.CommonUtils) DEPRECATED_CID_ADMIN_KEY(com.hedera.services.bdd.spec.transactions.contract.HapiContractCreate.DEPRECATED_CID_ADMIN_KEY) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Optional(java.util.Optional) FeeCalculator(com.hedera.services.bdd.spec.fees.FeeCalculator) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) LogManager(org.apache.logging.log4j.LogManager) ContractUpdateTransactionBody(com.hederahashgraph.api.proto.java.ContractUpdateTransactionBody) Key(com.hederahashgraph.api.proto.java.Key)

Example 25 with Key

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

the class HapiCryptoCreate method opBodyDef.

@Override
protected Consumer<TransactionBody.Builder> opBodyDef(HapiApiSpec spec) throws Throwable {
    key = key != null ? key : netOf(spec, keyName, keyShape, keyType, Optional.of(this::effectiveKeyGen));
    long amount = balanceFn.map(fn -> fn.apply(spec)).orElse(initialBalance.orElse(-1L));
    initialBalance = (amount >= 0) ? Optional.of(amount) : Optional.empty();
    CryptoCreateTransactionBody opBody = spec.txns().<CryptoCreateTransactionBody, CryptoCreateTransactionBody.Builder>body(CryptoCreateTransactionBody.class, b -> {
        b.setKey(key);
        proxy.ifPresent(b::setProxyAccountID);
        entityMemo.ifPresent(b::setMemo);
        sendThresh.ifPresent(b::setSendRecordThreshold);
        receiveThresh.ifPresent(b::setReceiveRecordThreshold);
        initialBalance.ifPresent(b::setInitialBalance);
        receiverSigRequired.ifPresent(b::setReceiverSigRequired);
        autoRenewDurationSecs.ifPresent(s -> b.setAutoRenewPeriod(Duration.newBuilder().setSeconds(s).build()));
        maxAutomaticTokenAssociations.ifPresent(b::setMaxAutomaticTokenAssociations);
    });
    return b -> b.setCryptoCreateAccount(opBody);
}
Also used : Transaction(com.hederahashgraph.api.proto.java.Transaction) Arrays(java.util.Arrays) SigControl(com.hedera.services.bdd.spec.keys.SigControl) HapiTxnOp(com.hedera.services.bdd.spec.transactions.HapiTxnOp) Duration(com.hederahashgraph.api.proto.java.Duration) Function(java.util.function.Function) CryptoCreateMeta(com.hedera.services.usage.crypto.CryptoCreateMeta) BaseTransactionMeta(com.hedera.services.usage.BaseTransactionMeta) UsageAccumulator(com.hedera.services.usage.state.UsageAccumulator) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) TxnUtils.netOf(com.hedera.services.bdd.spec.transactions.TxnUtils.netOf) TransactionResponse(com.hederahashgraph.api.proto.java.TransactionResponse) SigValueObj(com.hederahashgraph.fee.SigValueObj) AccountID(com.hederahashgraph.api.proto.java.AccountID) CryptoCreateTransactionBody(com.hederahashgraph.api.proto.java.CryptoCreateTransactionBody) FeeData(com.hederahashgraph.api.proto.java.FeeData) MoreObjects(com.google.common.base.MoreObjects) HapiPropertySource(com.hedera.services.bdd.spec.HapiPropertySource) SUCCESS(com.hederahashgraph.api.proto.java.ResponseCodeEnum.SUCCESS) ByteString(com.google.protobuf.ByteString) 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) AdapterUtils(com.hedera.services.bdd.spec.fees.AdapterUtils) HederaFunctionality(com.hederahashgraph.api.proto.java.HederaFunctionality) Optional(java.util.Optional) TxnUtils.suFrom(com.hedera.services.bdd.spec.transactions.TxnUtils.suFrom) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) LogManager(org.apache.logging.log4j.LogManager) KeyType(com.hedera.services.bdd.spec.keys.KeyFactory.KeyType) CryptoCreateTransactionBody(com.hederahashgraph.api.proto.java.CryptoCreateTransactionBody)

Aggregations

Key (com.hederahashgraph.api.proto.java.Key)43 ByteString (com.google.protobuf.ByteString)20 Test (org.junit.jupiter.api.Test)20 ThresholdKey (com.hederahashgraph.api.proto.java.ThresholdKey)14 KeyList (com.hederahashgraph.api.proto.java.KeyList)12 List (java.util.List)12 ArrayList (java.util.ArrayList)10 HapiApiSpec (com.hedera.services.bdd.spec.HapiApiSpec)8 PrivateKey (java.security.PrivateKey)8 Optional (java.util.Optional)7 LogManager (org.apache.logging.log4j.LogManager)7 Logger (org.apache.logging.log4j.Logger)7 HapiApiSuite (com.hedera.services.bdd.suites.HapiApiSuite)6 IOException (java.io.IOException)6 Map (java.util.Map)6 MoreObjects (com.google.common.base.MoreObjects)5 AccountID (com.hederahashgraph.api.proto.java.AccountID)5 HederaFunctionality (com.hederahashgraph.api.proto.java.HederaFunctionality)5 Transaction (com.hederahashgraph.api.proto.java.Transaction)5 TransactionBody (com.hederahashgraph.api.proto.java.TransactionBody)5