Skip to content

Commit

Permalink
Merge pull request #2809 from rsksmart/vovchyk/trace-block-by-num
Browse files Browse the repository at this point in the history
feat(traceBlockByNumber): add debug_traceBlockByNumber RPC method
  • Loading branch information
Vovchyk authored Oct 22, 2024
2 parents e2e7637 + dd2de86 commit 526931c
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 82 deletions.
16 changes: 14 additions & 2 deletions rskj-core/src/jmh/java/co/rsk/jmh/web3/BenchmarkWeb3.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,25 @@ public void debugTraceTransaction_Params_ContractCall(DebugPlan plan) throws Ben
@Benchmark
@Timeout(time = 30)
public void debugTraceBlockByHash(DebugPlan plan) throws BenchmarkWeb3Exception {
plan.getWeb3Connector().debugTraceBlockByHash(plan.getBlock());
plan.getWeb3Connector().debugTraceBlockByHash(plan.getBlockHash());
}

@Benchmark
@Timeout(time = 30)
public void debugTraceBlockByHash_Params(DebugPlan plan) throws BenchmarkWeb3Exception {
plan.getWeb3Connector().debugTraceBlockByHash(plan.getBlock());
plan.getWeb3Connector().debugTraceBlockByHash(plan.getBlockHash(), plan.getDebugParams());
}

@Benchmark
@Timeout(time = 30)
public void debugTraceBlockByNumber(DebugPlan plan) throws BenchmarkWeb3Exception {
plan.getWeb3Connector().debugTraceBlockByNumber(plan.getBlockNumber());
}

@Benchmark
@Timeout(time = 30)
public void debugTraceBlockByNumber_Params(DebugPlan plan) throws BenchmarkWeb3Exception {
plan.getWeb3Connector().debugTraceBlockByNumber(plan.getBlockTag(), plan.getDebugParams());
}

@Benchmark
Expand Down
4 changes: 4 additions & 0 deletions rskj-core/src/jmh/java/co/rsk/jmh/web3/Web3Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public interface Web3Connector {

JsonNode debugTraceBlockByHash(String txHash, Map<String, String> params) throws HttpRpcException;

JsonNode debugTraceBlockByNumber(String bnOrId) throws HttpRpcException;

JsonNode debugTraceBlockByNumber(String bnOrId, Map<String, String> params) throws HttpRpcException;

EthAccounts ethAccounts() throws HttpRpcException;

EthHashrate ethHashrate() throws HttpRpcException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public Request<?, RskModuleWeb3j.GenericJsonResponse> debugTraceBlockByHash(Stri
public Request<?, RskModuleWeb3j.GenericJsonResponse> debugTraceBlockByHash(String txHash, Map<String, String> params) {
return new Request<>("debug_traceBlockByHash", Arrays.asList(txHash, params), web3jService, RskModuleWeb3j.GenericJsonResponse.class);
}

public Request<?, RskModuleWeb3j.GenericJsonResponse> debugTraceBlockByNumber(String bnOrId) {
return new Request<>("debug_traceBlockByNumber", Collections.singletonList(bnOrId), web3jService, RskModuleWeb3j.GenericJsonResponse.class);
}

public Request<?, RskModuleWeb3j.GenericJsonResponse> debugTraceBlockByNumber(String bnOrId, Map<String, String> params) {
return new Request<>("debug_traceBlockByNumber", Arrays.asList(bnOrId, params), web3jService, RskModuleWeb3j.GenericJsonResponse.class);
}
}
22 changes: 22 additions & 0 deletions rskj-core/src/jmh/java/co/rsk/jmh/web3/e2e/Web3ConnectorE2E.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,28 @@ public JsonNode debugTraceBlockByHash(String txHash, Map<String, String> params)
}
}

@Override
public JsonNode debugTraceBlockByNumber(String bnOrId) throws HttpRpcException {
try {
RskModuleWeb3j.GenericJsonResponse response = sendRequest(() -> debugModuleWeb3j.debugTraceBlockByNumber(bnOrId));
return response.getJson();
} catch (Exception e) {
e.printStackTrace();
throw new HttpRpcException(e);
}
}

@Override
public JsonNode debugTraceBlockByNumber(String bnOrId, Map<String, String> params) throws HttpRpcException {
try {
RskModuleWeb3j.GenericJsonResponse response = sendRequest(() -> debugModuleWeb3j.debugTraceBlockByNumber(bnOrId, params));
return response.getJson();
} catch (Exception e) {
e.printStackTrace();
throw new HttpRpcException(e);
}
}

@Override
public EthAccounts ethAccounts() throws HttpRpcException {
try {
Expand Down
25 changes: 19 additions & 6 deletions rskj-core/src/jmh/java/co/rsk/jmh/web3/plan/DebugPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public class DebugPlan extends BasePlan {
private String transactionVT;
private String transactionContractCreation;
private String transactionContractCall;
private String block;
private String blockHash;
private String blockNumber;
private String blockTag;

private final Map<String, String> debugParams = new HashMap<>();

Expand Down Expand Up @@ -87,10 +89,13 @@ public void setUp(BenchmarkParams params) throws BenchmarkWeb3Exception {
}
}

block = configuration.getNullableProperty("debug.block");
if (block == null) {
block = web3Connector.ethGetBlockHashByNumber(BigInteger.ONE); // naive, valid only for regtest mode
blockHash = configuration.getNullableProperty("debug.block");
if (blockHash == null) {
blockHash = web3Connector.ethGetBlockHashByNumber(BigInteger.ONE); // naive, valid only for regtest mode
}

blockNumber = "0x1";
blockTag = "latest";
}

@TearDown(Level.Trial)
Expand All @@ -111,8 +116,16 @@ public String getTransactionContractCall() {
return transactionContractCall;
}

public String getBlock() {
return block;
public String getBlockHash() {
return blockHash;
}

public String getBlockNumber() {
return blockNumber;
}

public String getBlockTag() {
return blockTag;
}

public Map<String, String> getDebugParams() {
Expand Down
3 changes: 2 additions & 1 deletion rskj-core/src/main/java/co/rsk/RskContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,8 @@ public synchronized DebugModule getDebugModule() {
getReceiptStore(),
getNodeMessageHandler(),
getBlockExecutor(),
getTxQuotaChecker()
getTxQuotaChecker(),
getWeb3InformationRetriever()
);
}

Expand Down
10 changes: 9 additions & 1 deletion rskj-core/src/main/java/co/rsk/rpc/Web3DebugModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ default JsonNode debug_traceBlockByHash(String blockHash) throws Exception {
}

default JsonNode debug_traceBlockByHash(String blockHash, Map<String, String> traceOptions) throws Exception {
return getDebugModule().traceBlock(blockHash, traceOptions);
return getDebugModule().traceBlockByHash(blockHash, traceOptions);
}

default JsonNode debug_traceBlockByNumber(String bnOrId) throws Exception {
return debug_traceBlockByNumber(bnOrId, null);
}

default JsonNode debug_traceBlockByNumber(String bnOrId, Map<String, String> traceOptions) throws Exception {
return getDebugModule().traceBlockByNumber(bnOrId, traceOptions);
}

default TxQuota debug_accountTransactionQuota(String address) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public interface DebugModule {

JsonNode traceTransaction(String transactionHash, Map<String, String> traceOptions) throws Exception;

JsonNode traceBlock(String blockHash, Map<String, String> traceOptions) throws Exception;
JsonNode traceBlockByHash(String blockHash, Map<String, String> traceOptions) throws Exception;

JsonNode traceBlockByNumber(String bnOrId, Map<String, String> traceOptions) throws Exception;

TxQuota accountTransactionQuota(String address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import co.rsk.net.MessageHandler;
import co.rsk.net.handler.quota.TxQuota;
import co.rsk.net.handler.quota.TxQuotaChecker;
import co.rsk.rpc.Web3InformationRetriever;
import co.rsk.util.HexUtils;
import co.rsk.util.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;
import org.ethereum.core.Block;
import org.ethereum.core.Transaction;
Expand All @@ -49,18 +51,21 @@ public class DebugModuleImpl implements DebugModule {
private final BlockExecutor blockExecutor;

private final TxQuotaChecker txQuotaChecker;
private final Web3InformationRetriever web3InformationRetriever;

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

@Override
Expand All @@ -71,22 +76,15 @@ public String wireProtocolQueueSize() {

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

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());
}
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 {}", transactionHash);
logger.trace("No transaction info for txHash: {}", StringUtils.trim(transactionHash));
return null;
}

Expand All @@ -102,25 +100,37 @@ public JsonNode traceTransaction(String transactionHash, Map<String, String> tra
}

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

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());
}
TraceOptions options = toTraceOptions(traceOptions);

byte[] bHash = HexUtils.stringHexToByteArray(blockHash);
Block block = blockStore.getBlockByHash(bHash);
if (block == null) {
logger.trace("No block is found for {}", bHash);
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);
Expand All @@ -133,9 +143,20 @@ public JsonNode traceBlock(String blockHash, Map<String, String> traceOptions) {
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({})", address);
logger.trace("debug_accountTransactionQuota({})", StringUtils.trim(address));
RskAddress rskAddress = new RskAddress(address);
return this.txQuotaChecker.getTxQuota(rskAddress);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ repositoryLocator, new EthModuleWalletEnabled(wallet, transactionPool, signature
config.getCallGasCap()
);
TxPoolModule txPoolModule = new TxPoolModuleImpl(transactionPool, new ReceivedTxSignatureCache());
DebugModule debugModule = new DebugModuleImpl(null, null, Web3Mocks.getMockMessageHandler(), null, null);
DebugModule debugModule = new DebugModuleImpl(null, null, Web3Mocks.getMockMessageHandler(), null, null, null);

ChannelManager channelManager = new SimpleChannelManager();
return new Web3RskImpl(
Expand Down
Loading

0 comments on commit 526931c

Please sign in to comment.