Coverage Summary for Class: BlockResultDTO (org.ethereum.rpc.dto)
Class |
Class, %
|
Method, %
|
Line, %
|
BlockResultDTO |
100%
(1/1)
|
40.7%
(11/27)
|
81.5%
(75/92)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2019 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 org.ethereum.rpc.dto;
20
21 import co.rsk.core.BlockDifficulty;
22 import co.rsk.core.Coin;
23 import co.rsk.core.RskAddress;
24 import co.rsk.crypto.Keccak256;
25 import org.ethereum.core.Block;
26 import org.ethereum.core.BlockHeader;
27 import org.ethereum.core.Transaction;
28 import org.ethereum.db.BlockStore;
29 import org.ethereum.rpc.TypeConverter;
30
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34
35
36 public class BlockResultDTO {
37 private final String number; // QUANTITY - the block number. null when its pending block.
38 private final String hash; // DATA, 32 Bytes - hash of the block. null when its pending block.
39 private final String parentHash; // DATA, 32 Bytes - hash of the parent block.
40 private final String sha3Uncles; // DATA, 32 Bytes - SHA3 of the uncles data in the block.
41 private final String logsBloom; // DATA, 256 Bytes - the bloom filter for the logs of the block. null when its pending block.
42 private final String transactionsRoot; // DATA, 32 Bytes - the root of the transaction trie of the block.
43 private final String stateRoot; // DATA, 32 Bytes - the root of the final state trie of the block.
44 private final String receiptsRoot; // DATA, 32 Bytes - the root of the receipts trie of the block.
45 private final String miner; // DATA, 20 Bytes - the address of the beneficiary to whom the mining rewards were given.
46 private final String difficulty; // QUANTITY - integer of the difficulty for this block.
47 private final String totalDifficulty; // QUANTITY - integer of the total difficulty of the chain until this block.
48 private final String extraData; // DATA - the "extra data" field of this block
49 private final String size;//QUANTITY - integer the size of this block in bytes.
50 private final String gasLimit;//: QUANTITY - the maximum gas allowed in this block.
51 private final String gasUsed; // QUANTITY - the total used gas by all transactions in this block.
52 private final String timestamp; //: QUANTITY - the unix timestamp for when the block was collated.
53 private final List<Object> transactions; //: Collection - Collection of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.
54 private final List<String> uncles; //: Collection - Collection of uncle hashes.
55 private final String minimumGasPrice;
56 private final String bitcoinMergedMiningHeader;
57 private final String bitcoinMergedMiningCoinbaseTransaction;
58 private final String bitcoinMergedMiningMerkleProof;
59 private final String hashForMergedMining;
60 private final String paidFees;
61 private final String cumulativeDifficulty;
62
63 public BlockResultDTO(
64 Long number,
65 Keccak256 hash,
66 Keccak256 parentHash,
67 byte[] sha3Uncles,
68 byte[] logsBloom,
69 byte[] transactionsRoot,
70 byte[] stateRoot,
71 byte[] receiptsRoot,
72 RskAddress miner,
73 BlockDifficulty difficulty,
74 BlockDifficulty totalDifficulty,
75 BlockDifficulty cumulativeDifficulty,
76 byte[] extraData,
77 int size,
78 byte[] gasLimit,
79 long gasUsed,
80 long timestamp,
81 List<Object> transactions,
82 List<String> uncles,
83 Coin minimumGasPrice,
84 byte[] bitcoinMergedMiningHeader,
85 byte[] bitcoinMergedMiningCoinbaseTransaction,
86 byte[] bitcoinMergedMiningMerkleProof,
87 byte[] hashForMergedMining,
88 Coin paidFees) {
89 this.number = number != null ? TypeConverter.toQuantityJsonHex(number) : null;
90 this.hash = hash != null ? hash.toJsonString() : null;
91 this.parentHash = parentHash.toJsonString();
92 this.sha3Uncles = TypeConverter.toUnformattedJsonHex(sha3Uncles);
93 this.logsBloom = logsBloom != null ? TypeConverter.toUnformattedJsonHex(logsBloom) : null;
94 this.transactionsRoot = TypeConverter.toUnformattedJsonHex(transactionsRoot);
95 this.stateRoot = TypeConverter.toUnformattedJsonHex(stateRoot);
96 this.receiptsRoot = TypeConverter.toUnformattedJsonHex(receiptsRoot);
97 this.miner = miner != null ? TypeConverter.toUnformattedJsonHex(miner.getBytes()) : null;
98
99 this.difficulty = TypeConverter.toQuantityJsonHex(difficulty.getBytes());
100 this.totalDifficulty = TypeConverter.toQuantityJsonHex(totalDifficulty.getBytes());
101 this.cumulativeDifficulty = TypeConverter.toQuantityJsonHex(cumulativeDifficulty.getBytes());
102
103 this.extraData = TypeConverter.toUnformattedJsonHex(extraData);
104 this.size = TypeConverter.toQuantityJsonHex(size);
105 this.gasLimit = TypeConverter.toQuantityJsonHex(gasLimit);
106 this.gasUsed = TypeConverter.toQuantityJsonHex(gasUsed);
107 this.timestamp = TypeConverter.toQuantityJsonHex(timestamp);
108
109 this.transactions = Collections.unmodifiableList(transactions);
110 this.uncles = Collections.unmodifiableList(uncles);
111
112 this.minimumGasPrice = minimumGasPrice != null ? TypeConverter.toQuantityJsonHex(minimumGasPrice.getBytes()) : null;
113 this.bitcoinMergedMiningHeader = TypeConverter.toUnformattedJsonHex(bitcoinMergedMiningHeader);
114 this.bitcoinMergedMiningCoinbaseTransaction = TypeConverter.toUnformattedJsonHex(bitcoinMergedMiningCoinbaseTransaction);
115 this.bitcoinMergedMiningMerkleProof = TypeConverter.toUnformattedJsonHex(bitcoinMergedMiningMerkleProof);
116 this.hashForMergedMining = TypeConverter.toUnformattedJsonHex(hashForMergedMining);
117 this.paidFees = paidFees != null ? TypeConverter.toQuantityJsonHex(paidFees.getBytes()) : null;
118 }
119
120 public static BlockResultDTO fromBlock(Block b, boolean fullTx, BlockStore blockStore) {
121 if (b == null) {
122 return null;
123 }
124
125 byte[] mergeHeader = b.getBitcoinMergedMiningHeader();
126 boolean isPending = (mergeHeader == null || mergeHeader.length == 0) && !b.isGenesis();
127
128 Coin mgp = b.getMinimumGasPrice();
129
130 List<Object> transactions = new ArrayList<>();
131 List<Transaction> blockTransactions = b.getTransactionsList();
132 if (fullTx) {
133 for (int i = 0; i < blockTransactions.size(); i++) {
134 transactions.add(new TransactionResultDTO(b, i, blockTransactions.get(i)));
135 }
136 } else {
137 for (Transaction tx : blockTransactions) {
138 transactions.add(tx.getHash().toJsonString());
139 }
140 }
141
142 List<String> uncles = new ArrayList<>();
143
144 for (BlockHeader header : b.getUncleList()) {
145 uncles.add(header.getHash().toJsonString());
146 }
147
148 return new BlockResultDTO(
149 isPending ? null : b.getNumber(),
150 isPending ? null : b.getHash(),
151 b.getParentHash(),
152 b.getUnclesHash(),
153 isPending ? null : b.getLogBloom(),
154 b.getTxTrieRoot(),
155 b.getStateRoot(),
156 b.getReceiptsRoot(),
157 isPending ? null : b.getCoinbase(),
158 b.getDifficulty(),
159 blockStore.getTotalDifficultyForHash(b.getHash().getBytes()),
160 b.getCumulativeDifficulty(),
161 b.getExtraData(),
162 b.getEncoded().length,
163 b.getGasLimit(),
164 b.getGasUsed(),
165 b.getTimestamp(),
166 transactions,
167 uncles,
168 mgp,
169 b.getBitcoinMergedMiningHeader(),
170 b.getBitcoinMergedMiningCoinbaseTransaction(),
171 b.getBitcoinMergedMiningMerkleProof(),
172 b.getHashForMergedMining(),
173 b.getFeesPaidToMiner()
174 );
175 }
176
177 public String getNumber() {
178 return number;
179 }
180
181 public String getHash() {
182 return hash;
183 }
184
185 public String getParentHash() {
186 return parentHash;
187 }
188
189 public String getSha3Uncles() {
190 return sha3Uncles;
191 }
192
193 public String getLogsBloom() {
194 return logsBloom;
195 }
196
197 public String getTransactionsRoot() {
198 return transactionsRoot;
199 }
200
201 public String getStateRoot() {
202 return stateRoot;
203 }
204
205 public String getReceiptsRoot() {
206 return receiptsRoot;
207 }
208
209 public String getMiner() {
210 return miner;
211 }
212
213 public String getDifficulty() {
214 return difficulty;
215 }
216
217 public String getTotalDifficulty() {
218 return totalDifficulty;
219 }
220
221 public String getCumulativeDifficulty() { return cumulativeDifficulty; }
222
223 public String getExtraData() {
224 return extraData;
225 }
226
227 public String getSize() {
228 return size;
229 }
230
231 public String getGasLimit() {
232 return gasLimit;
233 }
234
235 public String getGasUsed() {
236 return gasUsed;
237 }
238
239 public String getTimestamp() {
240 return timestamp;
241 }
242
243 public List<Object> getTransactions() {
244 return Collections.unmodifiableList(transactions);
245 }
246
247 public List<String> getUncles() { return Collections.unmodifiableList(uncles); }
248
249 public String getMinimumGasPrice() {
250 return minimumGasPrice;
251 }
252
253 public String getBitcoinMergedMiningHeader() {
254 return bitcoinMergedMiningHeader;
255 }
256
257 public String getBitcoinMergedMiningCoinbaseTransaction() {
258 return bitcoinMergedMiningCoinbaseTransaction;
259 }
260
261 public String getBitcoinMergedMiningMerkleProof() {
262 return bitcoinMergedMiningMerkleProof;
263 }
264
265 public String getHashForMergedMining() {
266 return hashForMergedMining;
267 }
268
269 public String getPaidFees() {
270 return paidFees;
271 }
272 }