Search in sources :

Example 1 with ServicesConfigurationList

use of com.hederahashgraph.api.proto.java.ServicesConfigurationList 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 2 with ServicesConfigurationList

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

the class HapiFileUpdate method readBaseProps.

private ServicesConfigurationList readBaseProps(HapiApiSpec spec) {
    if (dropUnmentionedProperties) {
        return ServicesConfigurationList.getDefaultInstance();
    }
    if (!basePropsFile.isPresent()) {
        if (!file.equals(HapiApiSuite.API_PERMISSIONS) && !file.equals(HapiApiSuite.APP_PROPERTIES)) {
            throw new IllegalStateException("Property overrides make no sense for file '" + file + "'!");
        }
        int getsRemaining = 10;
        var gotFileContents = false;
        HapiGetFileContents subOp = null;
        while (!gotFileContents) {
            try {
                var candSubOp = getFileContents(file);
                payer.ifPresent(name -> candSubOp.payingWith(payerToUse(name, spec)));
                allRunFor(spec, candSubOp);
                gotFileContents = true;
                subOp = candSubOp;
            } catch (Throwable ignore) {
                getsRemaining--;
            }
            if (getsRemaining == 0) {
                break;
            }
        }
        if (!gotFileContents) {
            Assertions.fail("Unable to use 'overridingProps', couldn't get existing file contents!");
        }
        try {
            byte[] bytes = subOp.getResponse().getFileGetContents().getFileContents().getContents().toByteArray();
            ServicesConfigurationList defaults = ServicesConfigurationList.parseFrom(bytes);
            return defaults;
        } catch (Exception e) {
            log.error("No available defaults for " + file + " --- aborting!", e);
            throw new IllegalStateException("Property overrides via fileUpdate must have available defaults!");
        }
    } else {
        String defaultsPath = basePropsFile.get();
        try {
            byte[] bytes = java.nio.file.Files.readAllBytes(new File(defaultsPath).toPath());
            ServicesConfigurationList defaults = ServicesConfigurationList.parseFrom(bytes);
            return defaults;
        } catch (Exception e) {
            log.error("No available defaults for " + file + " --- aborting!", e);
            throw new IllegalStateException("Property overrides via fileUpdate must have available defaults!");
        }
    }
}
Also used : ServicesConfigurationList(com.hederahashgraph.api.proto.java.ServicesConfigurationList) ByteString(com.google.protobuf.ByteString) HapiGetFileContents(com.hedera.services.bdd.spec.queries.file.HapiGetFileContents) File(java.io.File)

Aggregations

ByteString (com.google.protobuf.ByteString)2 HapiGetFileContents (com.hedera.services.bdd.spec.queries.file.HapiGetFileContents)2 ServicesConfigurationList (com.hederahashgraph.api.proto.java.ServicesConfigurationList)2 File (java.io.File)2 MoreObjects (com.google.common.base.MoreObjects)1 Files (com.google.common.io.Files)1 StringValue (com.google.protobuf.StringValue)1 HapiApiSpec (com.hedera.services.bdd.spec.HapiApiSpec)1 FeeCalculator (com.hedera.services.bdd.spec.fees.FeeCalculator)1 QueryVerbs.getFileContents (com.hedera.services.bdd.spec.queries.QueryVerbs.getFileContents)1 QueryVerbs.getFileInfo (com.hedera.services.bdd.spec.queries.QueryVerbs.getFileInfo)1 HapiGetFileInfo (com.hedera.services.bdd.spec.queries.file.HapiGetFileInfo)1 HapiTxnOp (com.hedera.services.bdd.spec.transactions.HapiTxnOp)1 TxnFactory (com.hedera.services.bdd.spec.transactions.TxnFactory)1 TxnUtils (com.hedera.services.bdd.spec.transactions.TxnUtils)1 TxnUtils.suFrom (com.hedera.services.bdd.spec.transactions.TxnUtils.suFrom)1 CustomSpecAssert.allRunFor (com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor)1 HapiApiSuite (com.hedera.services.bdd.suites.HapiApiSuite)1 ONE_HBAR (com.hedera.services.bdd.suites.HapiApiSuite.ONE_HBAR)1 ExtantFileContext (com.hedera.services.usage.file.ExtantFileContext)1