Search in sources :

Example 1 with HapiTxnOp

use of com.hedera.services.bdd.spec.transactions.HapiTxnOp in project hedera-services by hashgraph.

the class AdHocTokenTransfers method tokenTransfersFactory.

private Function<HapiApiSpec, OpProvider> tokenTransfersFactory() {
    var firstDir = new AtomicBoolean(Boolean.TRUE);
    var balanceInit = new AtomicLong();
    var tokensPerTxn = new AtomicInteger();
    var sendingAccountsPerToken = new AtomicInteger();
    var receivingAccountsPerToken = new AtomicInteger();
    List<String> treasuries = new ArrayList<>();
    Map<String, List<String>> senders = new HashMap<>();
    Map<String, List<String>> receivers = new HashMap<>();
    String targetAccount = targetNodeAccount();
    return spec -> new OpProvider() {

        @Override
        public List<HapiSpecOperation> suggestedInitializers() {
            var ciProps = spec.setup().ciPropertiesMap();
            balanceInit.set(ciProps.getLong("balanceInit"));
            tokensPerTxn.set(ciProps.getInteger("tokensPerTxn"));
            sendingAccountsPerToken.set(ciProps.getInteger("sendingAccountsPerToken"));
            receivingAccountsPerToken.set(ciProps.getInteger("receivingAccountsPerToken"));
            var initialSupply = (sendingAccountsPerToken.get() + receivingAccountsPerToken.get()) * balanceInit.get();
            List<HapiSpecOperation> initializers = new ArrayList<>();
            for (int i = 0; i < tokensPerTxn.get(); i++) {
                var token = "token" + i;
                var treasury = "treasury" + i;
                initializers.add(cryptoCreate(treasury));
                initializers.add(tokenCreate(token).treasury(treasury).initialSupply(initialSupply));
                treasuries.add(treasury);
                for (int j = 0; j < sendingAccountsPerToken.get(); j++) {
                    var sender = token + "sender" + j;
                    senders.computeIfAbsent(token, ignore -> new ArrayList<>()).add(sender);
                    initializers.add(cryptoCreate(sender).balance(5_000_000_000_000L).withRecharging().rechargeWindow(3));
                    initializers.add(tokenAssociate(sender, token));
                    initializers.add(cryptoTransfer(moving(balanceInit.get(), token).between(treasury, sender)));
                }
                for (int j = 0; j < receivingAccountsPerToken.get(); j++) {
                    var receiver = token + "receiver" + j;
                    receivers.computeIfAbsent(token, ignore -> new ArrayList<>()).add(receiver);
                    initializers.add(cryptoCreate(receiver).balance(5_000_000_000_000L).withRecharging().rechargeWindow(3));
                    initializers.add(tokenAssociate(receiver, token));
                    initializers.add(cryptoTransfer(moving(balanceInit.get(), token).between(treasury, receiver)));
                }
            }
            for (HapiSpecOperation op : initializers) {
                ((HapiTxnOp) op).hasRetryPrecheckFrom(NOISY_RETRY_PRECHECKS).setNode(targetAccount);
            }
            return initializers;
        }

        @Override
        public Optional<HapiSpecOperation> get() {
            HapiSpecOperation op;
            var numTokens = tokensPerTxn.get();
            var numSenders = sendingAccountsPerToken.get();
            var numReceivers = receivingAccountsPerToken.get();
            String effPayer = null;
            var now = "" + Instant.now();
            if (firstDir.get()) {
                var xfers = new TokenMovement[numTokens * numSenders];
                for (int i = 0; i < numTokens; i++) {
                    var token = "token" + i;
                    for (int j = 0; j < numSenders; j++) {
                        var receivers = new String[numReceivers];
                        for (int k = 0; k < numReceivers; k++) {
                            receivers[k] = token + "receiver" + k;
                        }
                        var source = token + "sender" + j;
                        if (effPayer == null) {
                            effPayer = source;
                        }
                        xfers[i * numSenders + j] = moving(numReceivers, token).distributing(source, receivers);
                    }
                }
                op = cryptoTransfer(xfers).hasKnownStatusFrom(NOISY_ALLOWED_STATUSES).hasRetryPrecheckFrom(NOISY_RETRY_PRECHECKS).memo(now).payingWith(effPayer).setNode(targetAccount).noLogging().deferStatusResolution();
                firstDir.set(Boolean.FALSE);
            } else {
                var xfers = new TokenMovement[numTokens * numReceivers];
                for (int i = 0; i < numTokens; i++) {
                    var token = "token" + i;
                    for (int j = 0; j < numReceivers; j++) {
                        var senders = new String[numSenders];
                        for (int k = 0; k < numSenders; k++) {
                            senders[k] = token + "sender" + k;
                        }
                        var source = token + "receiver" + j;
                        if (effPayer == null) {
                            effPayer = source;
                        }
                        xfers[i * numReceivers + j] = moving(numSenders, token).distributing(source, senders);
                    }
                }
                op = cryptoTransfer(xfers).hasKnownStatusFrom(NOISY_ALLOWED_STATUSES).hasRetryPrecheckFrom(NOISY_RETRY_PRECHECKS).memo(now).payingWith(effPayer).setNode(targetAccount).noLogging().deferStatusResolution();
                firstDir.set(Boolean.TRUE);
            }
            return Optional.of(op);
        }
    };
}
Also used : TxnVerbs.tokenAssociate(com.hedera.services.bdd.spec.transactions.TxnVerbs.tokenAssociate) UtilVerbs.runWithProvider(com.hedera.services.bdd.spec.utilops.UtilVerbs.runWithProvider) TxnVerbs.cryptoCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.cryptoCreate) UtilVerbs.withOpContext(com.hedera.services.bdd.spec.utilops.UtilVerbs.withOpContext) HapiTxnOp(com.hedera.services.bdd.spec.transactions.HapiTxnOp) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) MINUTES(java.util.concurrent.TimeUnit.MINUTES) TxnVerbs.cryptoTransfer(com.hedera.services.bdd.spec.transactions.TxnVerbs.cryptoTransfer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) PerfUtilOps.stdMgmtOf(com.hedera.services.bdd.suites.perf.PerfUtilOps.stdMgmtOf) TokenMovement.moving(com.hedera.services.bdd.spec.transactions.token.TokenMovement.moving) HapiApiSuite(com.hedera.services.bdd.suites.HapiApiSuite) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) NOISY_ALLOWED_STATUSES(com.hedera.services.bdd.spec.transactions.TxnUtils.NOISY_ALLOWED_STATUSES) OpProvider(com.hedera.services.bdd.spec.infrastructure.OpProvider) NOISY_RETRY_PRECHECKS(com.hedera.services.bdd.spec.transactions.TxnUtils.NOISY_RETRY_PRECHECKS) TxnVerbs.tokenCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.tokenCreate) Instant(java.time.Instant) QueryVerbs(com.hedera.services.bdd.spec.queries.QueryVerbs) TokenMovement(com.hedera.services.bdd.spec.transactions.token.TokenMovement) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) 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) LogManager(org.apache.logging.log4j.LogManager) TokenMovement(com.hedera.services.bdd.spec.transactions.token.TokenMovement) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OpProvider(com.hedera.services.bdd.spec.infrastructure.OpProvider) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HapiSpecOperation(com.hedera.services.bdd.spec.HapiSpecOperation) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with HapiTxnOp

use of com.hedera.services.bdd.spec.transactions.HapiTxnOp in project hedera-services by hashgraph.

the class TokenTransfersLoadProvider method tokenTransfersFactory.

private Function<HapiApiSpec, OpProvider> tokenTransfersFactory() {
    var firstDir = new AtomicBoolean(Boolean.TRUE);
    var balanceInit = new AtomicLong();
    var tokensPerTxn = new AtomicInteger();
    var sendingAccountsPerToken = new AtomicInteger();
    var receivingAccountsPerToken = new AtomicInteger();
    List<String> treasuries = new ArrayList<>();
    Map<String, List<String>> senders = new HashMap<>();
    Map<String, List<String>> receivers = new HashMap<>();
    return spec -> new OpProvider() {

        @Override
        public List<HapiSpecOperation> suggestedInitializers() {
            var ciProps = spec.setup().ciPropertiesMap();
            balanceInit.set(ciProps.getLong("balanceInit"));
            tokensPerTxn.set(ciProps.getInteger("tokensPerTxn"));
            sendingAccountsPerToken.set(ciProps.getInteger("sendingAccountsPerToken"));
            receivingAccountsPerToken.set(ciProps.getInteger("receivingAccountsPerToken"));
            var initialSupply = (sendingAccountsPerToken.get() + receivingAccountsPerToken.get()) * balanceInit.get();
            List<HapiSpecOperation> initializers = new ArrayList<>();
            initializers.add(tokenOpsEnablement());
            /* Temporary, can be removed after the public testnet state used in
				   restart tests includes a fee schedule with HTS resource prices. */
            if (spec.setup().defaultNode().equals(asAccount("0.0.3"))) {
                initializers.add(uploadDefaultFeeSchedules(GENESIS));
            } else {
                initializers.add(withOpContext((spec, opLog) -> {
                    log.info("\n\n" + bannerWith("Waiting for a fee schedule with token ops!"));
                    boolean hasKnownHtsFeeSchedules = false;
                    SysFileSerde<String> serde = new FeesJsonToGrpcBytes();
                    while (!hasKnownHtsFeeSchedules) {
                        var query = QueryVerbs.getFileContents(FEE_SCHEDULE).fee(10_000_000_000L);
                        try {
                            allRunFor(spec, query);
                            var contents = query.getResponse().getFileGetContents().getFileContents().getContents();
                            var schedules = serde.fromRawFile(contents.toByteArray());
                            hasKnownHtsFeeSchedules = schedules.contains("TokenCreate");
                        } catch (Exception e) {
                            var msg = e.toString();
                            msg = msg.substring(msg.indexOf(":") + 2);
                            log.info("Couldn't check for HTS fee schedules---'{}'", msg);
                        }
                        TimeUnit.SECONDS.sleep(3);
                    }
                    log.info("\n\n" + bannerWith("A fee schedule with token ops now available!"));
                    spec.tryReinitializingFees();
                }));
            }
            for (int i = 0; i < tokensPerTxn.get(); i++) {
                var token = "token" + i;
                var treasury = "treasury" + i;
                initializers.add(cryptoCreate(treasury));
                initializers.add(tokenCreate(token).treasury(treasury).initialSupply(initialSupply));
                treasuries.add(treasury);
                for (int j = 0; j < sendingAccountsPerToken.get(); j++) {
                    var sender = token + "sender" + j;
                    senders.computeIfAbsent(token, ignore -> new ArrayList<>()).add(sender);
                    initializers.add(cryptoCreate(sender));
                    initializers.add(tokenAssociate(sender, token));
                    initializers.add(cryptoTransfer(moving(balanceInit.get(), token).between(treasury, sender)));
                }
                for (int j = 0; j < receivingAccountsPerToken.get(); j++) {
                    var receiver = token + "receiver" + j;
                    receivers.computeIfAbsent(token, ignore -> new ArrayList<>()).add(receiver);
                    initializers.add(cryptoCreate(receiver));
                    initializers.add(tokenAssociate(receiver, token));
                    initializers.add(cryptoTransfer(moving(balanceInit.get(), token).between(treasury, receiver)));
                }
            }
            for (HapiSpecOperation op : initializers) {
                if (op instanceof HapiTxnOp) {
                    ((HapiTxnOp) op).hasRetryPrecheckFrom(NOISY_RETRY_PRECHECKS);
                }
            }
            return initializers;
        }

        @Override
        public Optional<HapiSpecOperation> get() {
            HapiSpecOperation op;
            var numTokens = tokensPerTxn.get();
            var numSenders = sendingAccountsPerToken.get();
            var numReceivers = receivingAccountsPerToken.get();
            if (firstDir.get()) {
                var xfers = new TokenMovement[numTokens * numSenders];
                for (int i = 0; i < numTokens; i++) {
                    var token = "token" + i;
                    for (int j = 0; j < numSenders; j++) {
                        var receivers = new String[numReceivers];
                        for (int k = 0; k < numReceivers; k++) {
                            receivers[k] = token + "receiver" + k;
                        }
                        xfers[i * numSenders + j] = moving(numReceivers, token).distributing(token + "sender" + j, receivers);
                    }
                }
                op = cryptoTransfer(xfers).hasKnownStatusFrom(OK, DUPLICATE_TRANSACTION, SUCCESS, UNKNOWN, INSUFFICIENT_PAYER_BALANCE).hasRetryPrecheckFrom(NOISY_RETRY_PRECHECKS).hasPrecheckFrom(OK, PLATFORM_NOT_ACTIVE).noLogging().deferStatusResolution();
                firstDir.set(Boolean.FALSE);
            } else {
                var xfers = new TokenMovement[numTokens * numReceivers];
                for (int i = 0; i < numTokens; i++) {
                    var token = "token" + i;
                    for (int j = 0; j < numReceivers; j++) {
                        var senders = new String[numSenders];
                        for (int k = 0; k < numSenders; k++) {
                            senders[k] = token + "sender" + k;
                        }
                        xfers[i * numReceivers + j] = moving(numSenders, token).distributing(token + "receiver" + j, senders);
                    }
                }
                op = cryptoTransfer(xfers).hasKnownStatusFrom(OK, DUPLICATE_TRANSACTION, SUCCESS, UNKNOWN, INSUFFICIENT_PAYER_BALANCE).hasRetryPrecheckFrom(NOISY_RETRY_PRECHECKS).hasPrecheckFrom(OK, PLATFORM_NOT_ACTIVE).noLogging().deferStatusResolution();
                firstDir.set(Boolean.TRUE);
            }
            return Optional.of(op);
        }
    };
}
Also used : TxnVerbs.tokenAssociate(com.hedera.services.bdd.spec.transactions.TxnVerbs.tokenAssociate) UtilVerbs.runWithProvider(com.hedera.services.bdd.spec.utilops.UtilVerbs.runWithProvider) INSUFFICIENT_PAYER_BALANCE(com.hederahashgraph.api.proto.java.ResponseCodeEnum.INSUFFICIENT_PAYER_BALANCE) OK(com.hederahashgraph.api.proto.java.ResponseCodeEnum.OK) TxnVerbs.cryptoCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.cryptoCreate) UtilVerbs.withOpContext(com.hedera.services.bdd.spec.utilops.UtilVerbs.withOpContext) HapiTxnOp(com.hedera.services.bdd.spec.transactions.HapiTxnOp) FeesJsonToGrpcBytes(com.hedera.services.bdd.suites.utils.sysfiles.serdes.FeesJsonToGrpcBytes) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) MINUTES(java.util.concurrent.TimeUnit.MINUTES) TxnVerbs.cryptoTransfer(com.hedera.services.bdd.spec.transactions.TxnVerbs.cryptoTransfer) TxnVerbs.fileUpdate(com.hedera.services.bdd.spec.transactions.TxnVerbs.fileUpdate) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) PerfUtilOps.tokenOpsEnablement(com.hedera.services.bdd.suites.perf.PerfUtilOps.tokenOpsEnablement) ArrayList(java.util.ArrayList) PerfUtilOps.stdMgmtOf(com.hedera.services.bdd.suites.perf.PerfUtilOps.stdMgmtOf) UtilVerbs.sleepFor(com.hedera.services.bdd.spec.utilops.UtilVerbs.sleepFor) FREEZE_ALREADY_SCHEDULED(com.hederahashgraph.api.proto.java.ResponseCodeEnum.FREEZE_ALREADY_SCHEDULED) TokenMovement.moving(com.hedera.services.bdd.spec.transactions.token.TokenMovement.moving) DUPLICATE_TRANSACTION(com.hederahashgraph.api.proto.java.ResponseCodeEnum.DUPLICATE_TRANSACTION) HapiApiSuite(com.hedera.services.bdd.suites.HapiApiSuite) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) OpProvider(com.hedera.services.bdd.spec.infrastructure.OpProvider) HapiPropertySource.asAccount(com.hedera.services.bdd.spec.HapiPropertySource.asAccount) NOISY_RETRY_PRECHECKS(com.hedera.services.bdd.spec.transactions.TxnUtils.NOISY_RETRY_PRECHECKS) UtilVerbs.uploadDefaultFeeSchedules(com.hedera.services.bdd.spec.utilops.UtilVerbs.uploadDefaultFeeSchedules) TxnVerbs.tokenCreate(com.hedera.services.bdd.spec.transactions.TxnVerbs.tokenCreate) SysFileSerde(com.hedera.services.bdd.suites.utils.sysfiles.serdes.SysFileSerde) PLATFORM_NOT_ACTIVE(com.hederahashgraph.api.proto.java.ResponseCodeEnum.PLATFORM_NOT_ACTIVE) UNKNOWN(com.hederahashgraph.api.proto.java.ResponseCodeEnum.UNKNOWN) QueryVerbs.getAccountBalance(com.hedera.services.bdd.spec.queries.QueryVerbs.getAccountBalance) SUCCESS(com.hederahashgraph.api.proto.java.ResponseCodeEnum.SUCCESS) CustomSpecAssert.allRunFor(com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor) QueryVerbs(com.hedera.services.bdd.spec.queries.QueryVerbs) TokenMovement(com.hedera.services.bdd.spec.transactions.token.TokenMovement) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) TxnFactory.bannerWith(com.hedera.services.bdd.spec.transactions.TxnFactory.bannerWith) List(java.util.List) Logger(org.apache.logging.log4j.Logger) UtilVerbs.freezeOnly(com.hedera.services.bdd.spec.utilops.UtilVerbs.freezeOnly) HapiSpecOperation(com.hedera.services.bdd.spec.HapiSpecOperation) Optional(java.util.Optional) HapiApiSpec(com.hedera.services.bdd.spec.HapiApiSpec) LogManager(org.apache.logging.log4j.LogManager) TokenMovement(com.hedera.services.bdd.spec.transactions.token.TokenMovement) SysFileSerde(com.hedera.services.bdd.suites.utils.sysfiles.serdes.SysFileSerde) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OpProvider(com.hedera.services.bdd.spec.infrastructure.OpProvider) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FeesJsonToGrpcBytes(com.hedera.services.bdd.suites.utils.sysfiles.serdes.FeesJsonToGrpcBytes) HapiTxnOp(com.hedera.services.bdd.spec.transactions.HapiTxnOp) HapiSpecOperation(com.hedera.services.bdd.spec.HapiSpecOperation) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

HapiApiSpec (com.hedera.services.bdd.spec.HapiApiSpec)2 HapiSpecOperation (com.hedera.services.bdd.spec.HapiSpecOperation)2 OpProvider (com.hedera.services.bdd.spec.infrastructure.OpProvider)2 QueryVerbs (com.hedera.services.bdd.spec.queries.QueryVerbs)2 HapiTxnOp (com.hedera.services.bdd.spec.transactions.HapiTxnOp)2 NOISY_RETRY_PRECHECKS (com.hedera.services.bdd.spec.transactions.TxnUtils.NOISY_RETRY_PRECHECKS)2 TxnVerbs.cryptoCreate (com.hedera.services.bdd.spec.transactions.TxnVerbs.cryptoCreate)2 TxnVerbs.cryptoTransfer (com.hedera.services.bdd.spec.transactions.TxnVerbs.cryptoTransfer)2 TxnVerbs.tokenAssociate (com.hedera.services.bdd.spec.transactions.TxnVerbs.tokenAssociate)2 TxnVerbs.tokenCreate (com.hedera.services.bdd.spec.transactions.TxnVerbs.tokenCreate)2 TokenMovement (com.hedera.services.bdd.spec.transactions.token.TokenMovement)2 TokenMovement.moving (com.hedera.services.bdd.spec.transactions.token.TokenMovement.moving)2 UtilVerbs.runWithProvider (com.hedera.services.bdd.spec.utilops.UtilVerbs.runWithProvider)2 UtilVerbs.withOpContext (com.hedera.services.bdd.spec.utilops.UtilVerbs.withOpContext)2 HapiApiSuite (com.hedera.services.bdd.suites.HapiApiSuite)2 PerfUtilOps.stdMgmtOf (com.hedera.services.bdd.suites.perf.PerfUtilOps.stdMgmtOf)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2