Search in sources :

Example 71 with TransactionRecord

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

the class UtilVerbs method validateTransferListForBalances.

public static HapiSpecOperation validateTransferListForBalances(List<String> txns, List<String> accounts, Set<String> wereDeleted) {
    return assertionsHold((spec, assertLog) -> {
        Map<String, Long> actualBalances = accounts.stream().collect(Collectors.toMap((String account) -> asAccountString(spec.registry().getAccountID(account)), (String account) -> {
            if (wereDeleted.contains(account)) {
                return 0L;
            }
            long balance = -1L;
            try {
                BalanceSnapshot preOp = balanceSnapshot("x", account);
                allRunFor(spec, preOp);
                balance = spec.registry().getBalanceSnapshot("x");
            } catch (Throwable ignore) {
            }
            return balance;
        }));
        List<AccountAmount> transfers = new ArrayList<>();
        for (String txn : txns) {
            HapiGetTxnRecord subOp = getTxnRecord(txn).logged().payingWith(EXCHANGE_RATE_CONTROL);
            allRunFor(spec, subOp);
            TransactionRecord record = subOp.getResponse().getTransactionGetRecord().getTransactionRecord();
            transfers.addAll(record.getTransferList().getAccountAmountsList());
        }
        Map<String, Long> changes = changesAccordingTo(transfers);
        assertLog.info("Balance changes according to transfer list: " + changes);
        changes.entrySet().forEach(change -> {
            String account = change.getKey();
            long oldBalance = -1L;
            /* The account/contract may have just been created, no snapshot was taken. */
            try {
                oldBalance = spec.registry().getBalanceSnapshot(account + "Snapshot");
            } catch (Throwable ignore) {
            }
            long expectedBalance = change.getValue() + Math.max(0L, oldBalance);
            long actualBalance = actualBalances.getOrDefault(account, -1L);
            assertLog.info("Balance of " + account + " was expected to be " + expectedBalance + ", is actually " + actualBalance + "...");
            Assertions.assertEquals(expectedBalance, actualBalance, "New balance for " + account + " should be " + expectedBalance + " tinyBars.");
        });
    });
}
Also used : HapiGetTxnRecord(com.hedera.services.bdd.spec.queries.meta.HapiGetTxnRecord) OptionalLong(java.util.OptionalLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) HapiPropertySource.asAccountString(com.hedera.services.bdd.spec.HapiPropertySource.asAccountString) ByteString(com.google.protobuf.ByteString) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount)

Example 72 with TransactionRecord

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

the class RecordFileParser method parseFrom.

public static RecordFile parseFrom(File file) {
    FileInputStream stream = null;
    List<TxnHistory> histories = new LinkedList<>();
    byte[] prevHash = null;
    if (!file.exists()) {
        throw new IllegalArgumentException("No such file - " + file);
    }
    try {
        stream = new FileInputStream(file);
        DataInputStream dis = new DataInputStream(stream);
        prevHash = new byte[48];
        int record_format_version = dis.readInt();
        int version = dis.readInt();
        log.debug("File '{}' is: ", file);
        log.debug("  -> Record format v{}", record_format_version);
        log.debug("  -> HAPI protocol v{}", version);
        while (dis.available() != 0) {
            try {
                byte typeDelimiter = dis.readByte();
                switch(typeDelimiter) {
                    case TYPE_PREV_HASH:
                        dis.read(prevHash);
                        break;
                    case TYPE_RECORD:
                        int n = dis.readInt();
                        byte[] buffer = new byte[n];
                        dis.readFully(buffer);
                        Transaction signedTxn = Transaction.parseFrom(buffer);
                        n = dis.readInt();
                        buffer = new byte[n];
                        dis.readFully(buffer);
                        TransactionRecord record = TransactionRecord.parseFrom(buffer);
                        break;
                    default:
                        log.warn("Record file '{}' contained unrecognized delimiter |{}|", file, typeDelimiter);
                }
            } catch (Exception e) {
                log.warn("Problem parsing record file '{}'", file);
                break;
            }
        }
        metaDigest.reset();
        contentDigest.reset();
        byte[] everything = Files.readAllBytes(file.toPath());
        byte[] preface = Arrays.copyOfRange(everything, 0, 57);
        byte[] bodyHash = contentDigest.digest(Arrays.copyOfRange(everything, 57, everything.length));
        metaDigest.update(ArrayUtils.addAll(preface, bodyHash));
    } catch (FileNotFoundException e) {
        throw new IllegalStateException();
    } catch (IOException e) {
        log.error("Problem reading record file '{}'!", file, e);
    } catch (Exception e) {
        log.error("Problem parsing record file '{}'!", file, e);
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException ex) {
            log.error("Exception in closing stream for '{}'!", file, ex);
        }
    }
    return new RecordFile(prevHash, metaDigest.digest(), histories);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Transaction(com.hederahashgraph.api.proto.java.Transaction) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord)

Example 73 with TransactionRecord

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

the class UtilVerbs method validateRecordTransactionFees.

public static HapiSpecOperation validateRecordTransactionFees(String txn, Set<AccountID> feeRecipients) {
    return assertionsHold((spec, assertLog) -> {
        HapiGetTxnRecord subOp = getTxnRecord(txn).logged().payingWith(EXCHANGE_RATE_CONTROL).expectStrictCostAnswer();
        allRunFor(spec, subOp);
        TransactionRecord record = subOp.getResponse().getTransactionGetRecord().getTransactionRecord();
        long realFee = record.getTransferList().getAccountAmountsList().stream().filter(aa -> feeRecipients.contains(aa.getAccountID())).mapToLong(AccountAmount::getAmount).sum();
        Assertions.assertEquals(realFee, record.getTransactionFee(), "Inconsistent transactionFee field!");
    });
}
Also used : HapiGetTxnRecord(com.hedera.services.bdd.spec.queries.meta.HapiGetTxnRecord) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord)

Example 74 with TransactionRecord

use of com.hederahashgraph.api.proto.java.TransactionRecord in project hedera-mirror-node by hashgraph.

the class EntityRecordItemListenerContractTest method contractDeleteToExisting.

@ParameterizedTest
@EnumSource(ContractIdType.class)
void contractDeleteToExisting(ContractIdType contractIdType) {
    SetupResult setupResult = setupContract(CONTRACT_ID, contractIdType, true, true);
    Transaction transaction = contractDeleteTransaction(setupResult.protoContractId);
    TransactionBody transactionBody = getTransactionBody(transaction);
    TransactionRecord record = getContractTransactionRecord(transactionBody, ContractTransactionType.DELETE);
    RecordItem recordItem = new RecordItem(transaction, record);
    parseRecordItemAndCommit(recordItem);
    Contract dbContractEntity = getTransactionEntity(record.getConsensusTimestamp());
    assertAll(() -> assertEquals(1, transactionRepository.count()), () -> assertEquals(0, contractResultRepository.count()), () -> assertEquals(3, cryptoTransferRepository.count()), () -> assertEntities(setupResult.contract.toEntityId()), () -> assertTransactionAndRecord(transactionBody, record), () -> assertThat(dbContractEntity).isNotNull().returns(true, Contract::getDeleted).returns(recordItem.getConsensusTimestamp(), Contract::getTimestampLower).returns(EntityId.of(PAYER), Contract::getObtainerId).usingRecursiveComparison().ignoringFields("deleted", "obtainerId", "timestampRange").isEqualTo(setupResult.contract));
}
Also used : ContractUpdateTransactionBody(com.hederahashgraph.api.proto.java.ContractUpdateTransactionBody) ContractCallTransactionBody(com.hederahashgraph.api.proto.java.ContractCallTransactionBody) ContractDeleteTransactionBody(com.hederahashgraph.api.proto.java.ContractDeleteTransactionBody) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) ContractCreateTransactionBody(com.hederahashgraph.api.proto.java.ContractCreateTransactionBody) Transaction(com.hederahashgraph.api.proto.java.Transaction) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) Contract(com.hedera.mirror.common.domain.contract.Contract) RecordItem(com.hedera.mirror.common.domain.transaction.RecordItem) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 75 with TransactionRecord

use of com.hederahashgraph.api.proto.java.TransactionRecord in project hedera-mirror-node by hashgraph.

the class RecordFileParserTest method recordItem.

private RecordItem recordItem(long timestamp) {
    CryptoTransferTransactionBody cryptoTransfer = CryptoTransferTransactionBody.newBuilder().build();
    TransactionBody transactionBody = TransactionBody.newBuilder().setCryptoTransfer(cryptoTransfer).build();
    SignedTransaction signedTransaction = SignedTransaction.newBuilder().setBodyBytes(transactionBody.toByteString()).setSigMap(SignatureMap.newBuilder().build()).build();
    Transaction transaction = Transaction.newBuilder().setSignedTransactionBytes(signedTransaction.toByteString()).build();
    TransactionRecord transactionRecord = TransactionRecord.newBuilder().setConsensusTimestamp(Timestamp.newBuilder().setNanos((int) timestamp)).build();
    return new RecordItem(transaction, transactionRecord);
}
Also used : TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) CryptoTransferTransactionBody(com.hederahashgraph.api.proto.java.CryptoTransferTransactionBody) Transaction(com.hederahashgraph.api.proto.java.Transaction) SignedTransaction(com.hederahashgraph.api.proto.java.SignedTransaction) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) SignedTransaction(com.hederahashgraph.api.proto.java.SignedTransaction) RecordItem(com.hedera.mirror.common.domain.transaction.RecordItem) CryptoTransferTransactionBody(com.hederahashgraph.api.proto.java.CryptoTransferTransactionBody)

Aggregations

TransactionRecord (com.hederahashgraph.api.proto.java.TransactionRecord)107 TransactionBody (com.hederahashgraph.api.proto.java.TransactionBody)86 RecordItem (com.hedera.mirror.common.domain.transaction.RecordItem)84 Transaction (com.hederahashgraph.api.proto.java.Transaction)83 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)74 Test (org.junit.jupiter.api.Test)69 FileAppendTransactionBody (com.hederahashgraph.api.proto.java.FileAppendTransactionBody)30 FileUpdateTransactionBody (com.hederahashgraph.api.proto.java.FileUpdateTransactionBody)30 CryptoAddLiveHashTransactionBody (com.hederahashgraph.api.proto.java.CryptoAddLiveHashTransactionBody)28 FileCreateTransactionBody (com.hederahashgraph.api.proto.java.FileCreateTransactionBody)28 SignedTransaction (com.hederahashgraph.api.proto.java.SignedTransaction)28 CryptoCreateTransactionBody (com.hederahashgraph.api.proto.java.CryptoCreateTransactionBody)26 CryptoDeleteLiveHashTransactionBody (com.hederahashgraph.api.proto.java.CryptoDeleteLiveHashTransactionBody)26 CryptoUpdateTransactionBody (com.hederahashgraph.api.proto.java.CryptoUpdateTransactionBody)26 UtilityTest (com.hedera.mirror.importer.util.UtilityTest)23 ContractCallTransactionBody (com.hederahashgraph.api.proto.java.ContractCallTransactionBody)19 ContractCreateTransactionBody (com.hederahashgraph.api.proto.java.ContractCreateTransactionBody)19 ContractUpdateTransactionBody (com.hederahashgraph.api.proto.java.ContractUpdateTransactionBody)19 Entity (com.hedera.mirror.common.domain.entity.Entity)17 EnumSource (org.junit.jupiter.params.provider.EnumSource)13