Skip to content

Commit

Permalink
spotlessApply
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoreliovlabs committed Nov 20, 2024
1 parent 403065a commit eafc16e
Showing 1 changed file with 117 additions and 119 deletions.
236 changes: 117 additions & 119 deletions rskj-core/src/main/java/co/rsk/rpc/modules/debug/DebugModuleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import co.rsk.util.HexUtils;
import co.rsk.util.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.ethereum.core.Block;
import org.ethereum.core.Transaction;
import org.ethereum.db.BlockStore;
Expand All @@ -37,127 +40,122 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DebugModuleImpl implements DebugModule {
private static final Logger logger = LoggerFactory.getLogger("web3");
private static final Logger logger = LoggerFactory.getLogger("web3");

private final BlockStore blockStore;
private final ReceiptStore receiptStore;

private final BlockStore blockStore;
private final ReceiptStore receiptStore;
private final MessageHandler messageHandler;
private final BlockExecutor blockExecutor;

private final MessageHandler messageHandler;
private final BlockExecutor blockExecutor;

private final TxQuotaChecker txQuotaChecker;
private final Web3InformationRetriever web3InformationRetriever;
private final TxQuotaChecker txQuotaChecker;
private final Web3InformationRetriever web3InformationRetriever;

public DebugModuleImpl(
BlockStore blockStore,
ReceiptStore receiptStore,
MessageHandler messageHandler,
BlockExecutor blockExecutor,
TxQuotaChecker txQuotaChecker,
Web3InformationRetriever web3InformationRetriever) {
this.blockStore = blockStore;
this.receiptStore = receiptStore;
this.messageHandler = messageHandler;
this.blockExecutor = blockExecutor;
this.txQuotaChecker = txQuotaChecker;
this.web3InformationRetriever = web3InformationRetriever;
}

@Override
public String wireProtocolQueueSize() {
long n = messageHandler.getMessageQueueSize();
return HexUtils.toQuantityJsonHex(n);
}

@Override
public JsonNode traceTransaction(String transactionHash, Map<String, String> traceOptions) {
logger.trace("debug_traceTransaction for txHash: {}", StringUtils.trim(transactionHash));

TraceOptions options = toTraceOptions(traceOptions);

byte[] hash = HexUtils.stringHexToByteArray(transactionHash);
TransactionInfo txInfo = receiptStore.getInMainChain(hash, blockStore).orElse(null);

if (txInfo == null) {
logger.trace("No transaction info for txHash: {}", StringUtils.trim(transactionHash));
return null;
}

Block block = blockStore.getBlockByHash(txInfo.getBlockHash());
Block parent = blockStore.getBlockByHash(block.getParentHash().getBytes());
Transaction tx = block.getTransactionsList().get(txInfo.getIndex());
txInfo.setTransaction(tx);

ProgramTraceProcessor programTraceProcessor = new ProgramTraceProcessor(options);
blockExecutor.traceBlock(programTraceProcessor, 0, block, parent.getHeader(), false, false);

return programTraceProcessor.getProgramTraceAsJsonNode(tx.getHash());
}

@Override
public JsonNode traceBlockByHash(String blockHash, Map<String, String> traceOptions) {
logger.trace("debug_traceBlockByHash for blockHash: {}", StringUtils.trim(blockHash));

TraceOptions options = toTraceOptions(traceOptions);

byte[] bHash = HexUtils.stringHexToByteArray(blockHash);
Block block = blockStore.getBlockByHash(bHash);
if (block == null) {
logger.trace("No block is found for blockHash: {}", StringUtils.trim(blockHash));
return null;
}

return traceBlock(block, options);
}

@Override
public JsonNode traceBlockByNumber(String bnOrId, Map<String, String> traceOptions) {
logger.trace("debug_traceBlockByNumber for bnOrId: {}", StringUtils.trim(bnOrId));

TraceOptions options = toTraceOptions(traceOptions);

Block block = web3InformationRetriever.getBlock(bnOrId).orElse(null);
if (block == null) {
logger.trace("No block is found for bnOrId: {}", StringUtils.trim(bnOrId));
return null;
}

return traceBlock(block, options);
}

private JsonNode traceBlock(Block block, TraceOptions options) {
Block parent = blockStore.getBlockByHash(block.getParentHash().getBytes());

ProgramTraceProcessor programTraceProcessor = new ProgramTraceProcessor(options);
blockExecutor.traceBlock(programTraceProcessor, 0, block, parent.getHeader(), false, false);

List<Keccak256> txHashes = block.getTransactionsList().stream()
.map(Transaction::getHash)
.collect(Collectors.toList());

return programTraceProcessor.getProgramTracesAsJsonNode(txHashes);
}

private TraceOptions toTraceOptions(Map<String, String> traceOptions) {
TraceOptions options = new TraceOptions(traceOptions);

if (!options.getUnsupportedOptions().isEmpty()) {
// TODO: implement the logic that takes into account the remaining trace options.
logger.warn("Received {} unsupported trace options", options.getUnsupportedOptions().size());
}

return options;
}

@Override
public TxQuota accountTransactionQuota(String address) {
logger.trace("debug_accountTransactionQuota({})", StringUtils.trim(address));
RskAddress rskAddress = new RskAddress(address);
return this.txQuotaChecker.getTxQuota(rskAddress);
}
public DebugModuleImpl(
BlockStore blockStore,
ReceiptStore receiptStore,
MessageHandler messageHandler,
BlockExecutor blockExecutor,
TxQuotaChecker txQuotaChecker,
Web3InformationRetriever web3InformationRetriever) {
this.blockStore = blockStore;
this.receiptStore = receiptStore;
this.messageHandler = messageHandler;
this.blockExecutor = blockExecutor;
this.txQuotaChecker = txQuotaChecker;
this.web3InformationRetriever = web3InformationRetriever;
}

@Override
public String wireProtocolQueueSize() {
long n = messageHandler.getMessageQueueSize();
return HexUtils.toQuantityJsonHex(n);
}

@Override
public JsonNode traceTransaction(String transactionHash, Map<String, String> traceOptions) {
logger.trace("debug_traceTransaction for txHash: {}", StringUtils.trim(transactionHash));

TraceOptions options = toTraceOptions(traceOptions);

byte[] hash = HexUtils.stringHexToByteArray(transactionHash);
TransactionInfo txInfo = receiptStore.getInMainChain(hash, blockStore).orElse(null);

if (txInfo == null) {
logger.trace("No transaction info for txHash: {}", StringUtils.trim(transactionHash));
return null;
}

Block block = blockStore.getBlockByHash(txInfo.getBlockHash());
Block parent = blockStore.getBlockByHash(block.getParentHash().getBytes());
Transaction tx = block.getTransactionsList().get(txInfo.getIndex());
txInfo.setTransaction(tx);

ProgramTraceProcessor programTraceProcessor = new ProgramTraceProcessor(options);
blockExecutor.traceBlock(programTraceProcessor, 0, block, parent.getHeader(), false, false);

return programTraceProcessor.getProgramTraceAsJsonNode(tx.getHash());
}

@Override
public JsonNode traceBlockByHash(String blockHash, Map<String, String> traceOptions) {
logger.trace("debug_traceBlockByHash for blockHash: {}", StringUtils.trim(blockHash));

TraceOptions options = toTraceOptions(traceOptions);

byte[] bHash = HexUtils.stringHexToByteArray(blockHash);
Block block = blockStore.getBlockByHash(bHash);
if (block == null) {
logger.trace("No block is found for blockHash: {}", StringUtils.trim(blockHash));
return null;
}

return traceBlock(block, options);
}

@Override
public JsonNode traceBlockByNumber(String bnOrId, Map<String, String> traceOptions) {
logger.trace("debug_traceBlockByNumber for bnOrId: {}", StringUtils.trim(bnOrId));

TraceOptions options = toTraceOptions(traceOptions);

Block block = web3InformationRetriever.getBlock(bnOrId).orElse(null);
if (block == null) {
logger.trace("No block is found for bnOrId: {}", StringUtils.trim(bnOrId));
return null;
}

return traceBlock(block, options);
}

private JsonNode traceBlock(Block block, TraceOptions options) {
Block parent = blockStore.getBlockByHash(block.getParentHash().getBytes());

ProgramTraceProcessor programTraceProcessor = new ProgramTraceProcessor(options);
blockExecutor.traceBlock(programTraceProcessor, 0, block, parent.getHeader(), false, false);

List<Keccak256> txHashes =
block.getTransactionsList().stream().map(Transaction::getHash).collect(Collectors.toList());

return programTraceProcessor.getProgramTracesAsJsonNode(txHashes);
}

private TraceOptions toTraceOptions(Map<String, String> traceOptions) {
TraceOptions options = new TraceOptions(traceOptions);

if (!options.getUnsupportedOptions().isEmpty()) {
// TODO: implement the logic that takes into account the remaining trace options.
logger.warn("Received {} unsupported trace options", options.getUnsupportedOptions().size());
}

return options;
}

@Override
public TxQuota accountTransactionQuota(String address) {
logger.trace("debug_accountTransactionQuota({})", StringUtils.trim(address));
RskAddress rskAddress = new RskAddress(address);
return this.txQuotaChecker.getTxQuota(rskAddress);
}
}

0 comments on commit eafc16e

Please sign in to comment.