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");
}
}));
}
Aggregations