Coverage Summary for Class: DebugModuleImpl (co.rsk.rpc.modules.debug)
Class |
Class, %
|
Method, %
|
Line, %
|
DebugModuleImpl |
100%
(1/1)
|
50%
(2/4)
|
29.2%
(7/24)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2018 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.rpc.modules.debug;
20
21 import co.rsk.core.bc.BlockExecutor;
22 import co.rsk.net.MessageHandler;
23 import com.fasterxml.jackson.databind.JsonNode;
24 import org.ethereum.core.Block;
25 import org.ethereum.core.Transaction;
26 import org.ethereum.db.BlockStore;
27 import org.ethereum.db.ReceiptStore;
28 import org.ethereum.db.TransactionInfo;
29 import org.ethereum.rpc.TypeConverter;
30 import org.ethereum.vm.trace.ProgramTraceProcessor;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.util.Map;
35
36 import static org.ethereum.rpc.TypeConverter.stringHexToByteArray;
37
38 public class DebugModuleImpl implements DebugModule {
39 private static final Logger logger = LoggerFactory.getLogger("web3");
40
41 private final BlockStore blockStore;
42 private final ReceiptStore receiptStore;
43
44 private final MessageHandler messageHandler;
45 private final BlockExecutor blockExecutor;
46
47 public DebugModuleImpl(
48 BlockStore blockStore,
49 ReceiptStore receiptStore,
50 MessageHandler messageHandler,
51 BlockExecutor blockExecutor) {
52 this.blockStore = blockStore;
53 this.receiptStore = receiptStore;
54 this.messageHandler = messageHandler;
55 this.blockExecutor = blockExecutor;
56 }
57
58 @Override
59 public String wireProtocolQueueSize() {
60 long n = messageHandler.getMessageQueueSize();
61 return TypeConverter.toQuantityJsonHex(n);
62 }
63
64 @Override
65 public JsonNode traceTransaction(String transactionHash, Map<String, String> traceOptions) throws Exception {
66 logger.trace("debug_traceTransaction({}, {})", transactionHash, traceOptions);
67
68 if (traceOptions != null && !traceOptions.isEmpty()) {
69 // TODO: implement the logic that takes into account trace options.
70 logger.warn("Received {} trace options. For now trace options are being ignored", traceOptions);
71 }
72
73 byte[] hash = stringHexToByteArray(transactionHash);
74 TransactionInfo txInfo = receiptStore.getInMainChain(hash, blockStore);
75
76 if (txInfo == null) {
77 logger.trace("No transaction info for {}", transactionHash);
78 return null;
79 }
80
81 Block block = blockStore.getBlockByHash(txInfo.getBlockHash());
82 Block parent = blockStore.getBlockByHash(block.getParentHash().getBytes());
83 Transaction tx = block.getTransactionsList().get(txInfo.getIndex());
84 txInfo.setTransaction(tx);
85
86 ProgramTraceProcessor programTraceProcessor = new ProgramTraceProcessor();
87 blockExecutor.traceBlock(programTraceProcessor, 0, block, parent.getHeader(), false, false);
88
89 return programTraceProcessor.getProgramTraceAsJsonNode(tx.getHash());
90 }
91 }