/
EthBlock.java
422 lines (346 loc) · 14.4 KB
/
EthBlock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package org.web3j.methods.response;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.web3j.protocol.ObjectMapperFactory;
import org.web3j.protocol.utils.Codec;
import org.web3j.protocol.jsonrpc20.Response;
/**
* <p>Block object returned by:
* <ul>
* <li>eth_getBlockByHash</li>
* <li>eth_getBlockByNumber</li>
* <li>eth_getUncleByBlockHashAndIndex</li>
* <li>eth_getUncleByBlockNumberAndIndex</li>
* </ul>
* </p>
* <p>
* <p>See
* <a href="https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gettransactionbyhash">docs</a>
* for further details.</p>
*/
public class EthBlock extends Response<EthBlock.Block> {
@Override
@JsonDeserialize(using = EthBlock.ResponseDeserialiser.class)
public void setResult(Block result) {
super.setResult(result);
}
public Optional<Block> getBlock() {
return Optional.ofNullable(getResult());
}
public static class Block {
private String number;
private String hash;
private String parentHash;
private String nonce;
private String sha3Uncles;
private String logsBloom;
private String transactionsRoot;
private String stateRoot;
private String receiptRoot;
private String miner;
private String difficulty;
private String totalDifficulty;
private String extraData;
private String size;
private String gasLimit;
private String gasUsed;
private String timestamp;
private List<TransactionResult> transactions;
private List<String> uncles;
public Block() {
}
public Block(String number, String hash, String parentHash, String nonce,
String sha3Uncles, String logsBloom, String transactionsRoot,
String stateRoot, String receiptRoot, String miner, String difficulty,
String totalDifficulty, String extraData, String size, String gasLimit,
String gasUsed, String timestamp, List<TransactionResult> transactions,
List<String> uncles) {
this.number = number;
this.hash = hash;
this.parentHash = parentHash;
this.nonce = nonce;
this.sha3Uncles = sha3Uncles;
this.logsBloom = logsBloom;
this.transactionsRoot = transactionsRoot;
this.stateRoot = stateRoot;
this.receiptRoot = receiptRoot;
this.miner = miner;
this.difficulty = difficulty;
this.totalDifficulty = totalDifficulty;
this.extraData = extraData;
this.size = size;
this.gasLimit = gasLimit;
this.gasUsed = gasUsed;
this.timestamp = timestamp;
this.transactions = transactions;
this.uncles = uncles;
}
public BigInteger getNumber() {
return Codec.decodeQuantity(number);
}
public void setNumber(String number) {
this.number = number;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
public String getParentHash() {
return parentHash;
}
public void setParentHash(String parentHash) {
this.parentHash = parentHash;
}
public String getNonce() {
return nonce;
}
public void setNonce(String nonce) {
this.nonce = nonce;
}
public String getSha3Uncles() {
return sha3Uncles;
}
public void setSha3Uncles(String sha3Uncles) {
this.sha3Uncles = sha3Uncles;
}
public String getLogsBloom() {
return logsBloom;
}
public void setLogsBloom(String logsBloom) {
this.logsBloom = logsBloom;
}
public String getTransactionsRoot() {
return transactionsRoot;
}
public void setTransactionsRoot(String transactionsRoot) {
this.transactionsRoot = transactionsRoot;
}
public String getStateRoot() {
return stateRoot;
}
public void setStateRoot(String stateRoot) {
this.stateRoot = stateRoot;
}
public String getReceiptRoot() {
return receiptRoot;
}
public void setReceiptRoot(String receiptRoot) {
this.receiptRoot = receiptRoot;
}
public String getMiner() {
return miner;
}
public void setMiner(String miner) {
this.miner = miner;
}
public BigInteger getDifficulty() {
return Codec.decodeQuantity(difficulty);
}
public void setDifficulty(String difficulty) {
this.difficulty = difficulty;
}
public BigInteger getTotalDifficulty() {
return Codec.decodeQuantity(totalDifficulty);
}
public void setTotalDifficulty(String totalDifficulty) {
this.totalDifficulty = totalDifficulty;
}
public String getExtraData() {
return extraData;
}
public void setExtraData(String extraData) {
this.extraData = extraData;
}
public BigInteger getSize() {
return Codec.decodeQuantity(size);
}
public void setSize(String size) {
this.size = size;
}
public BigInteger getGasLimit() {
return Codec.decodeQuantity(gasLimit);
}
public void setGasLimit(String gasLimit) {
this.gasLimit = gasLimit;
}
public BigInteger getGasUsed() {
return Codec.decodeQuantity(gasUsed);
}
public void setGasUsed(String gasUsed) {
this.gasUsed = gasUsed;
}
public BigInteger getTimestamp() {
return Codec.decodeQuantity(timestamp);
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public List<TransactionResult> getTransactions() {
return transactions;
}
@JsonDeserialize(using = ResultTransactionDeserialiser.class)
public void setTransactions(List<TransactionResult> transactions) {
this.transactions = transactions;
}
public List<String> getUncles() {
return uncles;
}
public void setUncles(List<String> uncles) {
this.uncles = uncles;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Block that = (Block) o;
if (number != null ? !number.equals(that.number) : that.number != null) return false;
if (hash != null ? !hash.equals(that.hash) : that.hash != null) return false;
if (parentHash != null ? !parentHash.equals(that.parentHash) : that.parentHash != null)
return false;
if (nonce != null ? !nonce.equals(that.nonce) : that.nonce != null) return false;
if (sha3Uncles != null ? !sha3Uncles.equals(that.sha3Uncles) : that.sha3Uncles != null)
return false;
if (logsBloom != null ? !logsBloom.equals(that.logsBloom) : that.logsBloom != null)
return false;
if (transactionsRoot != null ? !transactionsRoot.equals(that.transactionsRoot) : that.transactionsRoot != null)
return false;
if (stateRoot != null ? !stateRoot.equals(that.stateRoot) : that.stateRoot != null)
return false;
if (receiptRoot != null ? !receiptRoot.equals(that.receiptRoot) : that.receiptRoot != null)
return false;
if (miner != null ? !miner.equals(that.miner) : that.miner != null) return false;
if (difficulty != null ? !difficulty.equals(that.difficulty) : that.difficulty != null)
return false;
if (totalDifficulty != null ? !totalDifficulty.equals(that.totalDifficulty) : that.totalDifficulty != null)
return false;
if (extraData != null ? !extraData.equals(that.extraData) : that.extraData != null)
return false;
if (size != null ? !size.equals(that.size) : that.size != null) return false;
if (gasLimit != null ? !gasLimit.equals(that.gasLimit) : that.gasLimit != null)
return false;
if (gasUsed != null ? !gasUsed.equals(that.gasUsed) : that.gasUsed != null)
return false;
if (timestamp != null ? !timestamp.equals(that.timestamp) : that.timestamp != null)
return false;
if (transactions != null ? !transactions.equals(that.transactions) : that.transactions != null)
return false;
return uncles != null ? uncles.equals(that.uncles) : that.uncles == null;
}
@Override
public int hashCode() {
int result = number != null ? number.hashCode() : 0;
result = 31 * result + (hash != null ? hash.hashCode() : 0);
result = 31 * result + (parentHash != null ? parentHash.hashCode() : 0);
result = 31 * result + (nonce != null ? nonce.hashCode() : 0);
result = 31 * result + (sha3Uncles != null ? sha3Uncles.hashCode() : 0);
result = 31 * result + (logsBloom != null ? logsBloom.hashCode() : 0);
result = 31 * result + (transactionsRoot != null ? transactionsRoot.hashCode() : 0);
result = 31 * result + (stateRoot != null ? stateRoot.hashCode() : 0);
result = 31 * result + (receiptRoot != null ? receiptRoot.hashCode() : 0);
result = 31 * result + (miner != null ? miner.hashCode() : 0);
result = 31 * result + (difficulty != null ? difficulty.hashCode() : 0);
result = 31 * result + (totalDifficulty != null ? totalDifficulty.hashCode() : 0);
result = 31 * result + (extraData != null ? extraData.hashCode() : 0);
result = 31 * result + (size != null ? size.hashCode() : 0);
result = 31 * result + (gasLimit != null ? gasLimit.hashCode() : 0);
result = 31 * result + (gasUsed != null ? gasUsed.hashCode() : 0);
result = 31 * result + (timestamp != null ? timestamp.hashCode() : 0);
result = 31 * result + (transactions != null ? transactions.hashCode() : 0);
result = 31 * result + (uncles != null ? uncles.hashCode() : 0);
return result;
}
}
public interface TransactionResult<T> {
T get();
}
public static class TransactionHash implements TransactionResult<String> {
private String value;
public TransactionHash() {
}
public TransactionHash(String value) {
this.value = value;
}
@Override
public String get() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TransactionHash that = (TransactionHash) o;
return value != null ? value.equals(that.value) : that.value == null;
}
@Override
public int hashCode() {
return value != null ? value.hashCode() : 0;
}
}
public static class TransactionObject extends Transaction implements TransactionResult<Transaction> {
public TransactionObject() {
}
public TransactionObject(String hash, String nonce, String blockHash, String blockNumber,
String transactionIndex, String from, String to, String value,
String gasPrice, String gas, String input) {
super(hash, nonce, blockHash, blockNumber, transactionIndex, from, to, value,
gasPrice, gas, input);
}
@Override
public Transaction get() {
return this;
}
}
public static class ResultTransactionDeserialiser extends JsonDeserializer<List<TransactionResult>> {
private ObjectReader objectReader = ObjectMapperFactory.getObjectReader();
@Override
public List<TransactionResult> deserialize(
JsonParser jsonParser,
DeserializationContext deserializationContext) throws IOException {
List<TransactionResult> transactionResults = new ArrayList<>();
JsonToken nextToken = jsonParser.nextToken();
if (nextToken == JsonToken.START_OBJECT) {
Iterator<TransactionObject> transactionObjectIterator =
objectReader.readValues(jsonParser, TransactionObject.class);
while (transactionObjectIterator.hasNext()) {
transactionResults.add(transactionObjectIterator.next());
}
} else if (nextToken == JsonToken.VALUE_STRING) {
jsonParser.getValueAsString();
Iterator<TransactionHash> transactionHashIterator =
objectReader.readValues(jsonParser, TransactionHash.class);
while (transactionHashIterator.hasNext()) {
transactionResults.add(transactionHashIterator.next());
}
}
return transactionResults;
}
}
public static class ResponseDeserialiser extends JsonDeserializer<Block> {
private ObjectReader objectReader = ObjectMapperFactory.getObjectReader();
@Override
public Block deserialize(
JsonParser jsonParser,
DeserializationContext deserializationContext) throws IOException {
if (jsonParser.getCurrentToken() != JsonToken.VALUE_NULL) {
return objectReader.readValue(jsonParser, Block.class);
} else {
return null; // null is wrapped by Optional in above getter
}
}
}
}