Search in sources :

Example 1 with Payment

use of com.hedera.services.bdd.spec.fees.Payment in project hedera-services by hashgraph.

the class HapiQueryOp method fittedPayment.

private Transaction fittedPayment(HapiApiSpec spec) throws Throwable {
    if (explicitPayment.isPresent()) {
        return explicitPayment.get().signedTxnFor(spec);
    } else if (nodePaymentFn.isPresent()) {
        return finalizedTxn(spec, opDef(spec, nodePaymentFn.get().apply(spec)));
    } else if (nodePayment.isPresent()) {
        return finalizedTxn(spec, opDef(spec, nodePayment.get()));
    } else {
        long initNodePayment = costOnlyNodePayment(spec);
        Transaction payment = finalizedTxn(spec, opDef(spec, initNodePayment), true);
        if (!loggingOff) {
            log.info(spec.logPrefix() + "Paying for COST_ANSWER of " + this + " with " + txnToString(payment));
        }
        long realNodePayment = timedCostLookupWith(spec, payment);
        if (recordsNodePayment) {
            spec.registry().saveAmount(nodePaymentName, realNodePayment);
        }
        if (!suppressStats) {
            spec.incrementNumLedgerOps();
        }
        if (expectedCostAnswerPrecheck() != OK) {
            return null;
        }
        if (spec.setup().costSnapshotMode() != OFF) {
            spec.recordPayment(new Payment(initNodePayment, self().getClass().getSimpleName(), COST_ANSWER_QUERY_COST));
            spec.recordPayment(new Payment(realNodePayment, self().getClass().getSimpleName(), ANSWER_ONLY_QUERY_COST));
        }
        txnSubmitted = payment;
        if (!loggingOff) {
            log.info(spec.logPrefix() + "--> Node payment for " + this + " is " + realNodePayment + " tinyBars.");
        }
        if (expectStrictCostAnswer) {
            Transaction insufficientPayment = finalizedTxn(spec, opDef(spec, realNodePayment - 1));
            submitWith(spec, insufficientPayment);
            if (INSUFFICIENT_TX_FEE != reflectForPrecheck(response)) {
                String errMsg = String.format("Strict cost of answer! suppose to be %s, but get %s", INSUFFICIENT_TX_FEE, reflectForPrecheck(response));
                log.error(errMsg);
                throw new HapiQueryPrecheckStateException(errMsg);
            } else {
                log.info("Query with node payment of {} tinyBars got INSUFFICIENT_TX_FEE as expected!", realNodePayment - 1);
            }
        }
        return finalizedTxn(spec, opDef(spec, realNodePayment));
    }
}
Also used : Payment(com.hedera.services.bdd.spec.fees.Payment) Transaction(com.hederahashgraph.api.proto.java.Transaction) HapiQueryPrecheckStateException(com.hedera.services.bdd.spec.exceptions.HapiQueryPrecheckStateException) TxnUtils.txnToString(com.hedera.services.bdd.spec.transactions.TxnUtils.txnToString)

Example 2 with Payment

use of com.hedera.services.bdd.spec.fees.Payment in project hedera-services by hashgraph.

the class HapiApiSpec method compareWithSnapshot.

private void compareWithSnapshot() {
    int nActual = costs.size();
    int nExpected = costSnapshot.size();
    boolean allMatch = (nActual == nExpected);
    if (!allMatch) {
        log.error("Expected " + nExpected + " payments to be recorded, not " + nActual + "!");
    }
    for (int i = 0; i < Math.min(nActual, nExpected); i++) {
        Payment actual = costs.get(i);
        Payment expected = costSnapshot.get(i);
        if (!actual.equals(expected)) {
            allMatch = false;
            log.error("Expected " + expected + " for payment " + i + ", not " + actual + "!");
        }
    }
    if (!allMatch) {
        status = FAILED;
    }
}
Also used : Payment(com.hedera.services.bdd.spec.fees.Payment)

Example 3 with Payment

use of com.hedera.services.bdd.spec.fees.Payment in project hedera-services by hashgraph.

the class HapiApiSpec method takeCostSnapshot.

private void takeCostSnapshot() {
    try {
        Properties deserializedCosts = new Properties();
        for (int i = 0; i < costs.size(); i++) {
            Payment cost = costs.get(i);
            deserializedCosts.put(String.format("%d.%s", i, cost.entryName()), "" + cost.tinyBars);
        }
        File file = new File(costSnapshotFilePath());
        CharSink sink = Files.asCharSink(file, Charset.forName("UTF-8"));
        deserializedCosts.store(sink.openBufferedStream(), "Cost snapshot");
    } catch (Exception e) {
        log.warn("Couldn't take cost snapshot to file '" + costSnapshotFile() + "!", e);
    }
}
Also used : Payment(com.hedera.services.bdd.spec.fees.Payment) CharSink(com.google.common.io.CharSink) Properties(java.util.Properties) File(java.io.File) IOException(java.io.IOException)

Example 4 with Payment

use of com.hedera.services.bdd.spec.fees.Payment in project hedera-services by hashgraph.

the class HapiTxnOp method publishFeeChargedTo.

private void publishFeeChargedTo(HapiApiSpec spec) throws Throwable {
    if (recordOfSubmission == null) {
        lookupSubmissionRecord(spec);
    }
    long fee = recordOfSubmission.getTransactionFee();
    spec.recordPayment(new Payment(fee, self().getClass().getSimpleName(), TXN_FEE));
}
Also used : Payment(com.hedera.services.bdd.spec.fees.Payment)

Example 5 with Payment

use of com.hedera.services.bdd.spec.fees.Payment in project hedera-services by hashgraph.

the class HapiApiSpec method costSnapshotFrom.

public static List<Payment> costSnapshotFrom(String loc) {
    Properties serializedCosts = new Properties();
    final ByteSource source = Files.asByteSource(new File(loc));
    try (InputStream inStream = source.openBufferedStream()) {
        serializedCosts.load(inStream);
    } catch (IOException ie) {
        log.error("Couldn't load cost snapshots as requested!", ie);
        throw new IllegalArgumentException(ie);
    }
    Map<Integer, Payment> costsByOrder = new HashMap<>();
    serializedCosts.forEach((a, b) -> {
        String meta = (String) a;
        long amount = Long.valueOf((String) b);
        int i = meta.indexOf(".");
        costsByOrder.put(Integer.valueOf(meta.substring(0, i)), Payment.fromEntry(meta.substring(i + 1), amount));
    });
    return IntStream.range(0, costsByOrder.size()).mapToObj(costsByOrder::get).collect(toList());
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Payment(com.hedera.services.bdd.spec.fees.Payment) ByteSource(com.google.common.io.ByteSource) File(java.io.File)

Aggregations

Payment (com.hedera.services.bdd.spec.fees.Payment)6 IOException (java.io.IOException)3 File (java.io.File)2 Properties (java.util.Properties)2 ByteSource (com.google.common.io.ByteSource)1 CharSink (com.google.common.io.CharSink)1 ByteString (com.google.protobuf.ByteString)1 HapiQueryPrecheckStateException (com.hedera.services.bdd.spec.exceptions.HapiQueryPrecheckStateException)1 TxnUtils.txnToString (com.hedera.services.bdd.spec.transactions.TxnUtils.txnToString)1 Transaction (com.hederahashgraph.api.proto.java.Transaction)1 BufferedWriter (java.io.BufferedWriter)1 InputStream (java.io.InputStream)1 Files.readString (java.nio.file.Files.readString)1 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1