Search in sources :

Example 1 with SignedTransaction

use of com.hedera.hashgraph.sdk.proto.SignedTransaction in project hedera-sdk-java by hashgraph.

the class Transaction method fromBytes.

public static Transaction<?> fromBytes(byte[] bytes) throws InvalidProtocolBufferException {
    var txs = new LinkedHashMap<TransactionId, LinkedHashMap<AccountId, com.hedera.hashgraph.sdk.proto.Transaction>>();
    @Var TransactionBody.DataCase dataCase = TransactionBody.DataCase.DATA_NOT_SET;
    var list = TransactionList.parseFrom(bytes);
    if (list.getTransactionListList().isEmpty()) {
        var transaction = com.hedera.hashgraph.sdk.proto.Transaction.parseFrom(bytes).toBuilder();
        TransactionBody txBody;
        if (transaction.getSignedTransactionBytes().isEmpty()) {
            txBody = TransactionBody.parseFrom(transaction.getBodyBytes());
            transaction.setSignedTransactionBytes(SignedTransaction.newBuilder().setBodyBytes(transaction.getBodyBytes()).setSigMap(transaction.getSigMap()).build().toByteString()).clearBodyBytes().clearSigMap();
        } else {
            var signedTransaction = SignedTransaction.parseFrom(transaction.getSignedTransactionBytes());
            txBody = TransactionBody.parseFrom(signedTransaction.getBodyBytes());
        }
        dataCase = txBody.getDataCase();
        var account = AccountId.fromProtobuf(txBody.getNodeAccountID());
        var transactionId = TransactionId.fromProtobuf(txBody.getTransactionID());
        var linked = new LinkedHashMap<AccountId, com.hedera.hashgraph.sdk.proto.Transaction>();
        linked.put(account, transaction.build());
        txs.put(transactionId, linked);
    } else {
        for (var transaction : list.getTransactionListList()) {
            var signedTransaction = SignedTransaction.parseFrom(transaction.getSignedTransactionBytes());
            var txBody = TransactionBody.parseFrom(signedTransaction.getBodyBytes());
            if (dataCase.getNumber() == TransactionBody.DataCase.DATA_NOT_SET.getNumber()) {
                dataCase = txBody.getDataCase();
            }
            var account = AccountId.fromProtobuf(txBody.getNodeAccountID());
            var transactionId = TransactionId.fromProtobuf(txBody.getTransactionID());
            var linked = txs.containsKey(transactionId) ? Objects.requireNonNull(txs.get(transactionId)) : new LinkedHashMap<AccountId, com.hedera.hashgraph.sdk.proto.Transaction>();
            linked.put(account, transaction);
            txs.put(transactionId, linked);
        }
    }
    switch(dataCase) {
        case CONTRACTCALL:
            return new ContractExecuteTransaction(txs);
        case CONTRACTCREATEINSTANCE:
            return new ContractCreateTransaction(txs);
        case CONTRACTUPDATEINSTANCE:
            return new ContractUpdateTransaction(txs);
        case CONTRACTDELETEINSTANCE:
            return new ContractDeleteTransaction(txs);
        case ETHEREUMTRANSACTION:
            return new EthereumTransaction(txs);
        case CRYPTOADDLIVEHASH:
            return new LiveHashAddTransaction(txs);
        case CRYPTOCREATEACCOUNT:
            return new AccountCreateTransaction(txs);
        case CRYPTODELETE:
            return new AccountDeleteTransaction(txs);
        case CRYPTODELETELIVEHASH:
            return new LiveHashDeleteTransaction(txs);
        case CRYPTOTRANSFER:
            return new TransferTransaction(txs);
        case CRYPTOUPDATEACCOUNT:
            return new AccountUpdateTransaction(txs);
        case FILEAPPEND:
            return new FileAppendTransaction(txs);
        case FILECREATE:
            return new FileCreateTransaction(txs);
        case FILEDELETE:
            return new FileDeleteTransaction(txs);
        case FILEUPDATE:
            return new FileUpdateTransaction(txs);
        case SYSTEMDELETE:
            return new SystemDeleteTransaction(txs);
        case SYSTEMUNDELETE:
            return new SystemUndeleteTransaction(txs);
        case FREEZE:
            return new FreezeTransaction(txs);
        case CONSENSUSCREATETOPIC:
            return new TopicCreateTransaction(txs);
        case CONSENSUSUPDATETOPIC:
            return new TopicUpdateTransaction(txs);
        case CONSENSUSDELETETOPIC:
            return new TopicDeleteTransaction(txs);
        case CONSENSUSSUBMITMESSAGE:
            return new TopicMessageSubmitTransaction(txs);
        case TOKENASSOCIATE:
            return new TokenAssociateTransaction(txs);
        case TOKENBURN:
            return new TokenBurnTransaction(txs);
        case TOKENCREATION:
            return new TokenCreateTransaction(txs);
        case TOKENDELETION:
            return new TokenDeleteTransaction(txs);
        case TOKENDISSOCIATE:
            return new TokenDissociateTransaction(txs);
        case TOKENFREEZE:
            return new TokenFreezeTransaction(txs);
        case TOKENGRANTKYC:
            return new TokenGrantKycTransaction(txs);
        case TOKENMINT:
            return new TokenMintTransaction(txs);
        case TOKENREVOKEKYC:
            return new TokenRevokeKycTransaction(txs);
        case TOKENUNFREEZE:
            return new TokenUnfreezeTransaction(txs);
        case TOKENUPDATE:
            return new TokenUpdateTransaction(txs);
        case TOKENWIPE:
            return new TokenWipeTransaction(txs);
        case TOKEN_FEE_SCHEDULE_UPDATE:
            return new TokenFeeScheduleUpdateTransaction(txs);
        case SCHEDULECREATE:
            return new ScheduleCreateTransaction(txs);
        case SCHEDULEDELETE:
            return new ScheduleDeleteTransaction(txs);
        case SCHEDULESIGN:
            return new ScheduleSignTransaction(txs);
        case TOKEN_PAUSE:
            return new TokenPauseTransaction(txs);
        case TOKEN_UNPAUSE:
            return new TokenUnpauseTransaction(txs);
        case CRYPTOAPPROVEALLOWANCE:
            return new AccountAllowanceApproveTransaction(txs);
        case CRYPTODELETEALLOWANCE:
            return new AccountAllowanceDeleteTransaction(txs);
        default:
            throw new IllegalArgumentException("parsed transaction body has no data");
    }
}
Also used : Var(com.google.errorprone.annotations.Var) LinkedHashMap(java.util.LinkedHashMap) SignedTransaction(com.hedera.hashgraph.sdk.proto.SignedTransaction) SchedulableTransactionBody(com.hedera.hashgraph.sdk.proto.SchedulableTransactionBody) TransactionBody(com.hedera.hashgraph.sdk.proto.TransactionBody)

Example 2 with SignedTransaction

use of com.hedera.hashgraph.sdk.proto.SignedTransaction in project hedera-sdk-java by hashgraph.

the class RegenerateTransactionIdsTest method regeneratesTransactionIdsWhenTransactionExpiredIsReturned.

@Test
void regeneratesTransactionIdsWhenTransactionExpiredIsReturned() throws PrecheckStatusException, TimeoutException, InterruptedException {
    var transactionIds = new HashSet<TransactionId>();
    AtomicInteger count = new AtomicInteger(0);
    var responses = List.of(TransactionResponse.newBuilder().setNodeTransactionPrecheckCode(ResponseCodeEnum.TRANSACTION_EXPIRED).build(), TransactionResponse.newBuilder().setNodeTransactionPrecheckCode(ResponseCodeEnum.TRANSACTION_EXPIRED).build(), TransactionResponse.newBuilder().setNodeTransactionPrecheckCode(ResponseCodeEnum.TRANSACTION_EXPIRED).build(), TransactionResponse.newBuilder().setNodeTransactionPrecheckCode(ResponseCodeEnum.OK).build());
    var call = (Function<Object, Object>) o -> {
        try {
            var transaction = (Transaction) o;
            var signedTransaction = SignedTransaction.parseFrom(transaction.getSignedTransactionBytes());
            var transactionBody = TransactionBody.parseFrom(signedTransaction.getBodyBytes());
            var transactionId = TransactionId.fromProtobuf(transactionBody.getTransactionID());
            if (transactionIds.contains(transactionId)) {
                return Status.Code.ABORTED.toStatus().asRuntimeException();
            }
            transactionIds.add(transactionId);
            return responses.get(count.getAndIncrement());
        } catch (Throwable e) {
            return new RuntimeException(e);
        }
    };
    List<Object> responses1 = List.of(call, call, call, call);
    try (var mocker = Mocker.withResponses(List.of(responses1))) {
        new FileCreateTransaction().execute(mocker.client);
    }
}
Also used : Function(java8.util.function.Function) Transaction(com.hedera.hashgraph.sdk.proto.Transaction) SignedTransaction(com.hedera.hashgraph.sdk.proto.SignedTransaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

SignedTransaction (com.hedera.hashgraph.sdk.proto.SignedTransaction)2 Var (com.google.errorprone.annotations.Var)1 SchedulableTransactionBody (com.hedera.hashgraph.sdk.proto.SchedulableTransactionBody)1 Transaction (com.hedera.hashgraph.sdk.proto.Transaction)1 TransactionBody (com.hedera.hashgraph.sdk.proto.TransactionBody)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Function (java8.util.function.Function)1 Test (org.junit.jupiter.api.Test)1