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