Search in sources :

Example 1 with ProgramTrace

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");
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) EMPTY_BYTE_ARRAY(org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY) RskAddress(co.rsk.core.RskAddress) EthereumListenerAdapter(org.ethereum.listener.EthereumListenerAdapter) Coin(co.rsk.core.Coin) ArrayUtils.isEmpty(org.apache.commons.lang3.ArrayUtils.isEmpty) ContractDetails(org.ethereum.db.ContractDetails) BlockchainConfig(org.ethereum.config.BlockchainConfig) BigInteger(java.math.BigInteger) VMUtils.saveProgramTraceFile(org.ethereum.vm.VMUtils.saveProgramTraceFile) ProgramInvoke(org.ethereum.vm.program.invoke.ProgramInvoke) PanicProcessor(co.rsk.panic.PanicProcessor) Logger(org.slf4j.Logger) IOException(java.io.IOException) BlockStore(org.ethereum.db.BlockStore) EthereumListener(org.ethereum.listener.EthereumListener) Collectors(java.util.stream.Collectors) BIUtil(org.ethereum.util.BIUtil) ArrayUtils.getLength(org.apache.commons.lang3.ArrayUtils.getLength) ReceiptStore(org.ethereum.db.ReceiptStore) Program(org.ethereum.vm.program.Program) ProgramTrace(org.ethereum.vm.trace.ProgramTrace) List(java.util.List) VmConfig(co.rsk.config.VmConfig) org.ethereum.vm(org.ethereum.vm) ProgramResult(org.ethereum.vm.program.ProgramResult) ProgramInvokeFactory(org.ethereum.vm.program.invoke.ProgramInvokeFactory) RskSystemProperties(co.rsk.config.RskSystemProperties) Constants(org.ethereum.config.Constants) Coin(co.rsk.core.Coin) ProgramTrace(org.ethereum.vm.trace.ProgramTrace) RskAddress(co.rsk.core.RskAddress) IOException(java.io.IOException) ContractDetails(org.ethereum.db.ContractDetails)

Example 2 with ProgramTrace

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)));
}
Also used : Path(java.nio.file.Path) ProgramTrace(org.ethereum.vm.trace.ProgramTrace) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with ProgramTrace

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)));
}
Also used : Path(java.nio.file.Path) ProgramTrace(org.ethereum.vm.trace.ProgramTrace) Test(org.junit.Test)

Aggregations

ProgramTrace (org.ethereum.vm.trace.ProgramTrace)3 Path (java.nio.file.Path)2 Test (org.junit.Test)2 RskSystemProperties (co.rsk.config.RskSystemProperties)1 VmConfig (co.rsk.config.VmConfig)1 Coin (co.rsk.core.Coin)1 RskAddress (co.rsk.core.RskAddress)1 PanicProcessor (co.rsk.panic.PanicProcessor)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ArrayUtils.getLength (org.apache.commons.lang3.ArrayUtils.getLength)1 ArrayUtils.isEmpty (org.apache.commons.lang3.ArrayUtils.isEmpty)1 BlockchainConfig (org.ethereum.config.BlockchainConfig)1 Constants (org.ethereum.config.Constants)1