Coverage Summary for Class: RskModuleImpl (co.rsk.rpc.modules.rsk)
Class |
Class, %
|
Method, %
|
Line, %
|
RskModuleImpl |
100%
(1/1)
|
100%
(6/6)
|
82%
(50/61)
|
1 package co.rsk.rpc.modules.rsk;
2
3 import co.rsk.core.bc.BlockHashesHelper;
4 import co.rsk.crypto.Keccak256;
5 import co.rsk.rpc.Web3InformationRetriever;
6 import co.rsk.trie.Trie;
7 import org.ethereum.core.Block;
8 import org.ethereum.core.Blockchain;
9 import org.ethereum.core.Transaction;
10 import org.ethereum.core.TransactionReceipt;
11 import org.ethereum.db.BlockStore;
12 import org.ethereum.db.ReceiptStore;
13 import org.ethereum.db.TransactionInfo;
14 import org.ethereum.rpc.TypeConverter;
15 import org.ethereum.util.RLP;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Optional;
23
24 import static org.ethereum.rpc.TypeConverter.stringHexToByteArray;
25
26 public class RskModuleImpl implements RskModule {
27 private static final Logger logger = LoggerFactory.getLogger("web3");
28
29 private final Blockchain blockchain;
30 private final BlockStore blockStore;
31 private final ReceiptStore receiptStore;
32 private final Web3InformationRetriever web3InformationRetriever;
33
34 public RskModuleImpl(Blockchain blockchain,
35 BlockStore blockStore,
36 ReceiptStore receiptStore,
37 Web3InformationRetriever web3InformationRetriever) {
38 this.blockchain = blockchain;
39 this.blockStore = blockStore;
40 this.receiptStore = receiptStore;
41 this.web3InformationRetriever = web3InformationRetriever;
42 }
43
44 @Override
45 public String getRawTransactionReceiptByHash(String transactionHash) {
46 String s = null;
47 try {
48 byte[] hash = stringHexToByteArray(transactionHash);
49 TransactionInfo txInfo = receiptStore.getInMainChain(hash, blockStore);
50
51 if (txInfo == null) {
52 logger.trace("No transaction info for {}", transactionHash);
53 return null;
54 }
55 return TypeConverter.toUnformattedJsonHex(txInfo.getReceipt().getEncoded());
56 } finally {
57 if (logger.isDebugEnabled()) {
58 logger.debug("rsk_getRawTransactionReceiptByHash({}): {}", transactionHash, s);
59 }
60 }
61 }
62
63 @Override
64 public String[] getTransactionReceiptNodesByHash(String blockHash, String transactionHash) {
65 String[] encodedNodes = null;
66
67 try {
68 Keccak256 txHash = new Keccak256(stringHexToByteArray(transactionHash));
69 Keccak256 bhash = new Keccak256(stringHexToByteArray(blockHash));
70 Block block = this.blockchain.getBlockByHash(bhash.getBytes());
71 List<Transaction> transactions = block.getTransactionsList();
72 List<TransactionReceipt> receipts = new ArrayList<>();
73
74 int ntxs = transactions.size();
75 int ntx = -1;
76
77 for (int k = 0; k < ntxs; k++) {
78 Transaction transaction = transactions.get(k);
79 Keccak256 txh = transaction.getHash();
80
81 Optional<TransactionInfo> txinfoOpt = this.receiptStore.get(txh, bhash);
82 if (!txinfoOpt.isPresent()) {
83 logger.error("Missing receipt for transaction {} in block {}", txh, bhash);
84 continue;
85 }
86
87 TransactionInfo txinfo = txinfoOpt.get();
88 receipts.add(txinfo.getReceipt());
89
90 if (txh.equals(txHash)) {
91 ntx = k;
92 }
93 }
94
95 if (ntx == -1) {
96 return null;
97 }
98 Trie trie = BlockHashesHelper.calculateReceiptsTrieRootFor(receipts);
99
100 List<Trie> nodes = trie.getNodes(RLP.encodeInt(ntx));
101 encodedNodes = new String[nodes.size()];
102
103 for (int k = 0; k < encodedNodes.length; k++) {
104 encodedNodes[k] = TypeConverter.toUnformattedJsonHex(nodes.get(k).toMessage());
105 }
106
107 return encodedNodes;
108 } finally {
109 if (logger.isDebugEnabled()) {
110 logger.debug("rsk_getTransactionReceiptNodesByHash({}): {}", blockHash, Arrays.toString(encodedNodes));
111 }
112 }
113 }
114
115
116 @Override
117 public String getRawBlockHeaderByHash(String blockHash) {
118 String s = null;
119 try {
120 byte[] bhash = stringHexToByteArray(blockHash);
121 Block b = this.blockchain.getBlockByHash(bhash);
122 return s = (b == null ? null : TypeConverter.toUnformattedJsonHex(b.getHeader().getEncoded()));
123 } finally {
124 if (logger.isDebugEnabled()) {
125 logger.debug("rsk_getRawBlockHeaderByHash({}): {}", blockHash, s);
126 }
127 }
128 }
129
130 @Override
131 public String getRawBlockHeaderByNumber(String bnOrId) {
132 String s = null;
133 try {
134 return s = web3InformationRetriever.getBlock(bnOrId)
135 .map(b -> TypeConverter.toUnformattedJsonHex(b.getHeader().getEncoded()))
136 .orElse(null);
137 } finally {
138 if (logger.isDebugEnabled()) {
139 logger.debug("rsk_getRawBlockHeaderByNumber({}): {}", bnOrId, s);
140 }
141 }
142 }
143 }