use of org.ethereum.vm.trace.ProgramTrace in project rskj by rsksmart.
the class TransactionExecutor method finalization.
public void finalization() {
if (!readyToExecute) {
return;
}
// RSK if local call gas balances must not be changed
if (localCall) {
return;
}
logger.trace("Finalize transaction {} {}", toBI(tx.getNonce()), tx.getHash());
cacheTrack.commit();
// Should include only LogInfo's that was added during not rejected transactions
List<LogInfo> notRejectedLogInfos = result.getLogInfoList().stream().filter(logInfo -> !logInfo.isRejected()).collect(Collectors.toList());
TransactionExecutionSummary.Builder summaryBuilder = TransactionExecutionSummary.builderFor(tx).gasLeftover(mEndGas).logs(notRejectedLogInfos).result(result.getHReturn());
if (result != null) {
// Accumulate refunds for suicides
result.addFutureRefund((long) result.getDeleteAccounts().size() * GasCost.SUICIDE_REFUND);
long gasRefund = Math.min(result.getFutureRefund(), result.getGasUsed() / 2);
RskAddress addr = tx.isContractCreation() ? tx.getContractAddress() : tx.getReceiveAddress();
mEndGas = mEndGas.add(BigInteger.valueOf(gasRefund));
summaryBuilder.gasUsed(toBI(result.getGasUsed())).gasRefund(toBI(gasRefund)).deletedAccounts(result.getDeleteAccounts()).internalTransactions(result.getInternalTransactions());
ContractDetails cdetails = track.getContractDetails(addr);
if (cdetails != null) {
summaryBuilder.storageDiff(cdetails.getStorage());
}
if (result.getException() != null) {
summaryBuilder.markAsFailed();
}
}
logger.trace("Building transaction execution summary");
TransactionExecutionSummary summary = summaryBuilder.build();
// Refund for gas leftover
track.addBalance(tx.getSender(), summary.getLeftover().add(summary.getRefund()));
logger.trace("Pay total refund to sender: [{}], refund val: [{}]", tx.getSender(), summary.getRefund());
// Transfer fees to miner
Coin summaryFee = summary.getFee();
// TODO: REMOVE THIS WHEN THE LocalBLockTests starts working with REMASC
if (config.isRemascEnabled()) {
logger.trace("Adding fee to remasc contract account");
track.addBalance(PrecompiledContracts.REMASC_ADDR, summaryFee);
} else {
track.addBalance(coinbase, summaryFee);
}
this.paidFees = summaryFee;
if (result != null) {
logger.trace("Processing result");
logs = notRejectedLogInfos;
result.getCodeChanges().forEach((key, value) -> track.saveCode(new RskAddress(key), value));
// Traverse list of suicides
result.getDeleteAccounts().forEach(address -> track.delete(new RskAddress(address)));
}
if (listener != null) {
listener.onTransactionExecuted(summary);
}
logger.trace("tx listener done");
if (config.vmTrace() && program != null && result != null) {
ProgramTrace trace = program.getTrace().result(result.getHReturn()).error(result.getException());
String txHash = tx.getHash().toHexString();
try {
saveProgramTraceFile(config, txHash, trace);
if (listener != null) {
listener.onVMTraceCreated(txHash, trace);
}
} catch (IOException e) {
String errorMessage = String.format("Cannot write trace to file: %s", e.getMessage());
panicProcessor.panic("executor", errorMessage);
logger.error(errorMessage);
}
}
logger.trace("tx finalization done");
}
use of org.ethereum.vm.trace.ProgramTrace in project rskj by rsksmart.
the class VMUtilsTest method saveZippedProgramTraceFile.
@Test
public void saveZippedProgramTraceFile() throws Exception {
Path traceFilePath = tempRule.newFolder().toPath();
ProgramTrace mockTrace = new ProgramTrace(config.getVmConfig(), null);
String mockTxHash = "1234";
VMUtils.saveProgramTraceFile(traceFilePath, mockTxHash, true, mockTrace);
ZipInputStream zipIn = new ZipInputStream(Files.newInputStream(traceFilePath.resolve(mockTxHash + ".zip")));
ZipEntry zippedTrace = zipIn.getNextEntry();
Assert.assertThat(zippedTrace.getName(), is(mockTxHash + ".json"));
ByteArrayOutputStream unzippedTrace = new ByteArrayOutputStream();
byte[] traceBuffer = new byte[2048];
int len;
while ((len = zipIn.read(traceBuffer)) > 0) {
unzippedTrace.write(traceBuffer, 0, len);
}
Assert.assertThat(new String(unzippedTrace.toByteArray()), is(Serializers.serializeFieldsOnly(mockTrace, true)));
}
use of org.ethereum.vm.trace.ProgramTrace in project rskj by rsksmart.
the class VMUtilsTest method savePlainProgramTraceFile.
@Test
public void savePlainProgramTraceFile() throws Exception {
Path traceFilePath = tempRule.newFolder().toPath();
ProgramTrace mockTrace = new ProgramTrace(config.getVmConfig(), null);
String mockTxHash = "1234";
VMUtils.saveProgramTraceFile(traceFilePath, mockTxHash, false, mockTrace);
String trace = new String(Files.readAllBytes(traceFilePath.resolve(mockTxHash + ".json")));
Assert.assertThat(trace, is(Serializers.serializeFieldsOnly(mockTrace, true)));
}
Aggregations