Search in sources :

Example 1 with UtilVerbs.takeBalanceSnapshots

use of com.hedera.services.bdd.spec.utilops.UtilVerbs.takeBalanceSnapshots in project hedera-services by hashgraph.

the class ContractRecordsSanityCheckSuite method circularTransfersRecordSanityChecks.

private HapiApiSpec circularTransfersRecordSanityChecks() {
    int NUM_ALTRUISTS = 3;
    Function<String, Long> INIT_BALANCE_FN = ignore -> 1_000_000L;
    int INIT_KEEP_AMOUNT_DIVISOR = 2;
    BigInteger STOP_BALANCE = BigInteger.valueOf(399_999L);
    String[] CANONICAL_ACCOUNTS = { FUNDING, NODE, DEFAULT_PAYER };
    String[] altruists = IntStream.range(0, NUM_ALTRUISTS).mapToObj(i -> String.format("Altruist%s", new String(new char[] { (char) ('A' + i) }))).toArray(n -> new String[n]);
    return defaultHapiSpec("CircularTransfersRecordSanityChecks").given(flattened(fileCreate("bytecode").path(ContractResources.CIRCULAR_TRANSFERS_BYTECODE_PATH), Stream.of(altruists).map(name -> contractCreate(name).bytecode("bytecode")).toArray(n -> new HapiSpecOperation[n]), Stream.of(altruists).map(name -> contractCall(name, ContractResources.SET_NODES_ABI, spec -> new Object[] { Stream.of(altruists).map(a -> spec.registry().getContractId(a).getContractNum()).toArray() }).gas(120_000).via("txnFor" + name).sending(INIT_BALANCE_FN.apply(name))).toArray(n -> new HapiSpecOperation[n]), UtilVerbs.takeBalanceSnapshots(Stream.of(Stream.of(altruists), Stream.of(CANONICAL_ACCOUNTS)).flatMap(identity()).toArray(n -> new String[n])))).when(contractCall(altruists[0], ContractResources.RECEIVE_AND_SEND_ABI, INIT_KEEP_AMOUNT_DIVISOR, STOP_BALANCE).via("altruisticTxn")).then(validateTransferListForBalances("altruisticTxn", Stream.concat(Stream.of(CANONICAL_ACCOUNTS), Stream.of(altruists)).collect(toList())), validateRecordTransactionFees("altruisticTxn"), addLogInfo((spec, infoLog) -> {
        long[] finalBalances = IntStream.range(0, NUM_ALTRUISTS).mapToLong(ignore -> INIT_BALANCE_FN.apply("")).toArray();
        int i = 0, divisor = INIT_KEEP_AMOUNT_DIVISOR;
        while (true) {
            long toKeep = finalBalances[i] / divisor;
            if (toKeep < STOP_BALANCE.longValue()) {
                break;
            }
            int j = (i + 1) % NUM_ALTRUISTS;
            finalBalances[j] += (finalBalances[i] - toKeep);
            finalBalances[i] = toKeep;
            i = j;
            divisor++;
        }
        infoLog.info("Expected Final Balances");
        infoLog.info("-----------------------");
        for (i = 0; i < NUM_ALTRUISTS; i++) {
            infoLog.info("  " + i + " = " + finalBalances[i] + " tinyBars");
        }
    }));
}
Also used : IntStream(java.util.stream.IntStream) TxnVerbs.contractUpdate(com.hedera.services.bdd.spec.transactions.TxnVerbs.contractUpdate) UtilVerbs.validateTransferListForBalances(com.hedera.services.bdd.spec.utilops.UtilVerbs.validateTransferListForBalances) TxnVerbs.contractDelete(com.hedera.services.bdd.spec.transactions.TxnVerbs.contractDelete) Function(java.util.function.Function) ContractResources(com.hedera.services.bdd.spec.infrastructure.meta.ContractResources) UtilVerbs.addLogInfo(com.hedera.services.bdd.spec.utilops.UtilVerbs.addLogInfo) HapiApiSuite(com.hedera.services.bdd.suites.HapiApiSuite) HapiApiSpec.defaultHapiSpec(com.hedera.services.bdd.spec.HapiApiSpec.defaultHapiSpec) BigInteger(java.math.BigInteger) TxnVerbs.contractCall(com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCall) Set(java.util.Set) UtilVerbs.newKeyNamed(com.hedera.services.bdd.spec.utilops.UtilVerbs.newKeyNamed) KeyFactory(com.hedera.services.bdd.spec.keys.KeyFactory) List(java.util.List) TxnVerbs.fileCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.fileCreate) Collectors.toList(java.util.stream.Collectors.toList) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) UtilVerbs.takeBalanceSnapshots(com.hedera.services.bdd.spec.utilops.UtilVerbs.takeBalanceSnapshots) UtilVerbs(com.hedera.services.bdd.spec.utilops.UtilVerbs) HapiSpecOperation(com.hedera.services.bdd.spec.HapiSpecOperation) Function.identity(java.util.function.Function.identity) TxnVerbs.contractCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCreate) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) LogManager(org.apache.logging.log4j.LogManager) UtilVerbs.validateRecordTransactionFees(com.hedera.services.bdd.spec.utilops.UtilVerbs.validateRecordTransactionFees) BigInteger(java.math.BigInteger)

Aggregations

HapiApiSpec (com.hedera.services.bdd.spec.HapiApiSpec)1 HapiApiSpec.defaultHapiSpec (com.hedera.services.bdd.spec.HapiApiSpec.defaultHapiSpec)1 HapiSpecOperation (com.hedera.services.bdd.spec.HapiSpecOperation)1 ContractResources (com.hedera.services.bdd.spec.infrastructure.meta.ContractResources)1 KeyFactory (com.hedera.services.bdd.spec.keys.KeyFactory)1 TxnVerbs.contractCall (com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCall)1 TxnVerbs.contractCreate (com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCreate)1 TxnVerbs.contractDelete (com.hedera.services.bdd.spec.transactions.TxnVerbs.contractDelete)1 TxnVerbs.contractUpdate (com.hedera.services.bdd.spec.transactions.TxnVerbs.contractUpdate)1 TxnVerbs.fileCreate (com.hedera.services.bdd.spec.transactions.TxnVerbs.fileCreate)1 UtilVerbs (com.hedera.services.bdd.spec.utilops.UtilVerbs)1 UtilVerbs.addLogInfo (com.hedera.services.bdd.spec.utilops.UtilVerbs.addLogInfo)1 UtilVerbs.newKeyNamed (com.hedera.services.bdd.spec.utilops.UtilVerbs.newKeyNamed)1 UtilVerbs.takeBalanceSnapshots (com.hedera.services.bdd.spec.utilops.UtilVerbs.takeBalanceSnapshots)1 UtilVerbs.validateRecordTransactionFees (com.hedera.services.bdd.spec.utilops.UtilVerbs.validateRecordTransactionFees)1 UtilVerbs.validateTransferListForBalances (com.hedera.services.bdd.spec.utilops.UtilVerbs.validateTransferListForBalances)1 HapiApiSuite (com.hedera.services.bdd.suites.HapiApiSuite)1 BigInteger (java.math.BigInteger)1 List (java.util.List)1 Set (java.util.Set)1