Search in sources :

Example 1 with CONTRACT_REVERT_EXECUTED

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

the class FibonacciPlusLoadProvider method contractOpsFactory.

private Function<HapiApiSpec, OpProvider> contractOpsFactory() {
    final String civilian = "civilian";
    final String bytecode = "bytecode";
    final SplittableRandom random = new SplittableRandom(1_234_567L);
    final IntFunction<String> contractNameFn = i -> "contract" + i;
    final int r = powerLawBaseReciprocal.get();
    final DoubleUnaryOperator logBaseReciprocal = x -> Math.log(x) / Math.log(r);
    final int numDiscreteSizes = (int) ceil(logBaseReciprocal.applyAsDouble(numContracts.get() * (r - 1)));
    double scale = powerLawScale.get();
    int numSlots = (int) Math.pow(scale, numDiscreteSizes - 1) * smallestNumSlots.get();
    int numContractsWithThisManySlots = 1;
    int nextContractNum = 0;
    for (int i = 0; i < numDiscreteSizes; i++) {
        log.info("Will use {} contracts with {} slots", numContractsWithThisManySlots, numSlots);
        for (int j = 0; j < numContractsWithThisManySlots; j++) {
            final var thisContractNum = nextContractNum++;
            final var thisContract = contractNameFn.apply(thisContractNum);
            contractSlots.put(thisContract, numSlots);
            if (validateStorage.get()) {
                final var slots = new BigInteger[numSlots];
                Arrays.fill(slots, BigInteger.ZERO);
                contractStorage.put(thisContract, slots);
            }
        }
        numSlots /= scale;
        numContractsWithThisManySlots *= r;
    }
    log.info("Will use {} contracts in total", nextContractNum);
    numContracts.set(nextContractNum);
    Supplier<String> randomCallChoice = () -> {
        final var iter = createdSoFar.iterator();
        final var n = createdSoFar.size();
        if (n == 1) {
            return iter.next();
        }
        for (int i = 0; i < random.nextInt(n - 1); i++, iter.next()) {
        /* No-op */
        }
        return iter.next();
    };
    final var that = this;
    return spec -> new OpProvider() {

        @Override
        public List<HapiSpecOperation> suggestedInitializers() {
            final List<HapiSpecOperation> inits = new ArrayList<>();
            inits.add(fileCreate(bytecode).path(FIBONACCI_PLUS_PATH).noLogging().payingWith(GENESIS));
            inits.add(cryptoCreate(civilian).balance(100 * ONE_MILLION_HBARS).payingWith(GENESIS));
            return inits;
        }

        @Override
        public Optional<HapiSpecOperation> get() {
            final var aCallNum = submittedOps.incrementAndGet();
            if (aCallNum == 1) {
                effStart.set(Instant.now());
            }
            final var choice = (createdSoFar.isEmpty() || random.nextDouble() > MIN_CALL_PROB) ? contractNameFn.apply(random.nextInt(numContracts.get())) : randomCallChoice.get();
            final HapiSpecOperation op;
            if (createdSoFar.contains(choice)) {
                final var n = slotsPerCall.get();
                final int[] targets = new int[n];
                final var m = contractSlots.get(choice);
                for (int i = 0; i < n; i++) {
                    targets[i] = random.nextInt(m);
                }
                final var targetsDesc = Arrays.toString(targets);
                if (verbose.get()) {
                    log.info("Calling {} with targets {} and fibN {}", choice, targetsDesc, fibN.get());
                }
                op = contractCall(choice, ADD_NTH_FIB_ABI, targets, fibN.get()).noLogging().payingWith(civilian).gas(GAS_TO_OFFER).exposingGasTo((code, gas) -> {
                    if (verbose.get()) {
                        log.info("(Tried to) call {} (targets = {}, fibN = {}) with {} gas --> {}", choice, targetsDesc, fibN.get(), gas, code);
                    }
                    that.observeExposedGas(gas);
                    if (code == SUCCESS && validateStorage.get()) {
                        final var curSlots = contractStorage.get(choice);
                        final var newSlots = Arrays.copyOf(curSlots, m);
                        for (int i = 0; i < n; i++) {
                            final var j = targets[i];
                            newSlots[j] = newSlots[j].add(fibNValue.get());
                        }
                        contractStorage.put(choice, newSlots);
                    }
                }).hasKnownStatusFrom(SUCCESS, CONTRACT_REVERT_EXECUTED, INSUFFICIENT_GAS).deferStatusResolution();
            } else {
                final var numSlots = contractSlots.get(choice);
                op = contractCreate(choice, FIBONACCI_PLUS_CONSTRUCTOR_ABI, numSlots).bytecode(bytecode).payingWith(civilian).balance(0L).gas(GAS_TO_OFFER).exposingGasTo((code, gas) -> {
                    if (code == SUCCESS) {
                        createdSoFar.add(choice);
                    }
                    log.info("(Tried to) create {} ({} slots) with {} gas --> {}", choice, numSlots, gas, code);
                    that.observeExposedGas(gas);
                }).noLogging().hasKnownStatusFrom(SUCCESS, INSUFFICIENT_GAS).deferStatusResolution();
            }
            return Optional.of(op);
        }
    };
}
Also used : UtilVerbs.runWithProvider(com.hedera.services.bdd.spec.utilops.UtilVerbs.runWithProvider) Arrays(java.util.Arrays) TxnVerbs.cryptoCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.cryptoCreate) UtilVerbs.sourcing(com.hedera.services.bdd.spec.utilops.UtilVerbs.sourcing) QueryVerbs.getExecTime(com.hedera.services.bdd.spec.queries.QueryVerbs.getExecTime) PerfUtilOps.stdMgmtOf(com.hedera.services.bdd.suites.perf.PerfUtilOps.stdMgmtOf) UtilVerbs.sleepFor(com.hedera.services.bdd.spec.utilops.UtilVerbs.sleepFor) FIBONACCI_PLUS_CONSTRUCTOR_ABI(com.hedera.services.bdd.spec.infrastructure.meta.ContractResources.FIBONACCI_PLUS_CONSTRUCTOR_ABI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) HapiApiSpec.defaultHapiSpec(com.hedera.services.bdd.spec.HapiApiSpec.defaultHapiSpec) SplittableRandom(java.util.SplittableRandom) QueryVerbs.contractCallLocal(com.hedera.services.bdd.spec.queries.QueryVerbs.contractCallLocal) BigInteger(java.math.BigInteger) ContractFnResultAsserts.isLiteralArrayResult(com.hedera.services.bdd.spec.assertions.ContractFnResultAsserts.isLiteralArrayResult) OpProvider(com.hedera.services.bdd.spec.infrastructure.OpProvider) TxnVerbs.contractCall(com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCall) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ContractFnResultAsserts.resultWith(com.hedera.services.bdd.spec.assertions.ContractFnResultAsserts.resultWith) Set(java.util.Set) Math.ceil(java.lang.Math.ceil) Instant(java.time.Instant) SUCCESS(com.hederahashgraph.api.proto.java.ResponseCodeEnum.SUCCESS) UtilVerbs.noOp(com.hedera.services.bdd.spec.utilops.UtilVerbs.noOp) List(java.util.List) Logger(org.apache.logging.log4j.Logger) HapiSpecOperation(com.hedera.services.bdd.spec.HapiSpecOperation) Optional(java.util.Optional) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) PerfUtilOps.mgmtOfIntProp(com.hedera.services.bdd.suites.perf.PerfUtilOps.mgmtOfIntProp) UtilVerbs.withOpContext(com.hedera.services.bdd.spec.utilops.UtilVerbs.withOpContext) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) PerfUtilOps.mgmtOfBooleanProp(com.hedera.services.bdd.suites.perf.PerfUtilOps.mgmtOfBooleanProp) HapiApiSuite(com.hedera.services.bdd.suites.HapiApiSuite) CURRENT_FIB_SLOTS_ABI(com.hedera.services.bdd.spec.infrastructure.meta.ContractResources.CURRENT_FIB_SLOTS_ABI) IntFunction(java.util.function.IntFunction) FIBONACCI_PLUS_PATH(com.hedera.services.bdd.spec.infrastructure.meta.ContractResources.FIBONACCI_PLUS_PATH) CustomSpecAssert.allRunFor(com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) TxnVerbs.fileCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.fileCreate) INSUFFICIENT_GAS(com.hederahashgraph.api.proto.java.ResponseCodeEnum.INSUFFICIENT_GAS) CONTRACT_REVERT_EXECUTED(com.hederahashgraph.api.proto.java.ResponseCodeEnum.CONTRACT_REVERT_EXECUTED) ADD_NTH_FIB_ABI(com.hedera.services.bdd.spec.infrastructure.meta.ContractResources.ADD_NTH_FIB_ABI) TxnVerbs.contractCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCreate) LogManager(org.apache.logging.log4j.LogManager) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ArrayList(java.util.ArrayList) OpProvider(com.hedera.services.bdd.spec.infrastructure.OpProvider) BigInteger(java.math.BigInteger) HapiSpecOperation(com.hedera.services.bdd.spec.HapiSpecOperation) SplittableRandom(java.util.SplittableRandom) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Aggregations

AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1 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 ContractFnResultAsserts.isLiteralArrayResult (com.hedera.services.bdd.spec.assertions.ContractFnResultAsserts.isLiteralArrayResult)1 ContractFnResultAsserts.resultWith (com.hedera.services.bdd.spec.assertions.ContractFnResultAsserts.resultWith)1 OpProvider (com.hedera.services.bdd.spec.infrastructure.OpProvider)1 ADD_NTH_FIB_ABI (com.hedera.services.bdd.spec.infrastructure.meta.ContractResources.ADD_NTH_FIB_ABI)1 CURRENT_FIB_SLOTS_ABI (com.hedera.services.bdd.spec.infrastructure.meta.ContractResources.CURRENT_FIB_SLOTS_ABI)1 FIBONACCI_PLUS_CONSTRUCTOR_ABI (com.hedera.services.bdd.spec.infrastructure.meta.ContractResources.FIBONACCI_PLUS_CONSTRUCTOR_ABI)1 FIBONACCI_PLUS_PATH (com.hedera.services.bdd.spec.infrastructure.meta.ContractResources.FIBONACCI_PLUS_PATH)1 QueryVerbs.contractCallLocal (com.hedera.services.bdd.spec.queries.QueryVerbs.contractCallLocal)1 QueryVerbs.getExecTime (com.hedera.services.bdd.spec.queries.QueryVerbs.getExecTime)1 TxnVerbs.contractCall (com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCall)1 TxnVerbs.contractCreate (com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCreate)1 TxnVerbs.cryptoCreate (com.hedera.services.bdd.spec.transactions.TxnVerbs.cryptoCreate)1 TxnVerbs.fileCreate (com.hedera.services.bdd.spec.transactions.TxnVerbs.fileCreate)1 CustomSpecAssert.allRunFor (com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor)1 UtilVerbs.noOp (com.hedera.services.bdd.spec.utilops.UtilVerbs.noOp)1 UtilVerbs.runWithProvider (com.hedera.services.bdd.spec.utilops.UtilVerbs.runWithProvider)1