Skip to content

Commit 9512b09

Browse files
committed
Code coverage improvements
Proxy Models hex parse improvements ProxyStorageAt empty response check
1 parent bc50cae commit 9512b09

File tree

13 files changed

+183
-74
lines changed

13 files changed

+183
-74
lines changed

src/main/java/io/api/etherscan/core/impl/ProxyApiProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.math.BigInteger;
2020
import java.util.Optional;
21+
import java.util.regex.Pattern;
2122

2223
/**
2324
* Proxy API Implementation
@@ -56,6 +57,8 @@ public class ProxyApiProvider extends BasicProvider implements IProxyApi {
5657
private static final String HEX_PARAM = "&hex=";
5758
private static final String TO_PARAM = "&to=";
5859

60+
private static final Pattern EMPTY_HEX = Pattern.compile("0x0+");
61+
5962
ProxyApiProvider(final IQueueManager queue,
6063
final String baseUrl,
6164
final IHttpExecutor executor) {
@@ -191,7 +194,7 @@ public Optional<String> storageAt(final String address, final long position) thr
191194

192195
final String urlParams = ACT_STORAGEAT_PARAM + ADDRESS_PARAM + address + POSITION_PARAM + compPosition + TAG_LAST_PARAM;
193196
final StringProxyTO response = getRequest(urlParams, StringProxyTO.class);
194-
return (BasicUtils.isEmpty(response.getResult()))
197+
return (BasicUtils.isEmpty(response.getResult()) || EMPTY_HEX.matcher(response.getResult()).matches())
195198
? Optional.empty()
196199
: Optional.of(response.getResult());
197200
}

src/main/java/io/api/etherscan/executor/impl/HttpExecutor.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
/**
2323
* Http client implementation
24-
* @see IHttpExecutor
2524
*
2625
* @author GoodforGod
26+
* @see IHttpExecutor
2727
* @since 28.10.2018
2828
*/
2929
public class HttpExecutor implements IHttpExecutor {
@@ -59,10 +59,9 @@ public HttpExecutor(final int connectTimeout,
5959
}
6060

6161
/**
62-
*
6362
* @param connectTimeout custom connection establish timeout in millis
64-
* @param readTimeout custom read timeout in millis
65-
* @param headers custom HTTP headers
63+
* @param readTimeout custom read timeout in millis
64+
* @param headers custom HTTP headers
6665
*/
6766
public HttpExecutor(final int connectTimeout,
6867
final int readTimeout,
@@ -88,15 +87,14 @@ public String get(final String urlAsString) {
8887
final HttpURLConnection connection = buildConnection(urlAsString, "GET");
8988
final int status = connection.getResponseCode();
9089
if (status == HTTP_MOVED_TEMP || status == HTTP_MOVED_PERM) {
91-
final String location = connection.getHeaderField("Location");
92-
return get(location);
90+
return get(connection.getHeaderField("Location"));
9391
}
9492

9593
final String data = readData(connection);
9694
connection.disconnect();
9795
return data;
9896
} catch (SocketTimeoutException e) {
99-
throw new ApiTimeoutException("Timeout: Could not establish connection for " + connectTimeout+ " millis", e);
97+
throw new ApiTimeoutException("Timeout: Could not establish connection for " + connectTimeout + " millis", e);
10098
} catch (Exception e) {
10199
throw new ConnectionException(e.getLocalizedMessage(), e);
102100
}
@@ -117,14 +115,15 @@ public String post(final String urlAsString, final String dataToPost) {
117115

118116
final int status = connection.getResponseCode();
119117
if (status == HTTP_MOVED_TEMP || status == HTTP_MOVED_PERM) {
120-
final String location = connection.getHeaderField("Location");
121-
return post(location, dataToPost);
118+
return post(connection.getHeaderField("Location"), dataToPost);
122119
}
123120

124121
final String data = readData(connection);
125122
connection.disconnect();
126123
return data;
127-
} catch (IOException e) {
124+
} catch (SocketTimeoutException e) {
125+
throw new ApiTimeoutException("Timeout: Could not establish connection for " + connectTimeout + " millis", e);
126+
} catch (Exception e) {
128127
throw new ConnectionException(e.getLocalizedMessage(), e);
129128
}
130129
}

src/main/java/io/api/etherscan/model/Log.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
public class Log {
1717

1818
private String blockNumber;
19-
private long _blockNumber;
19+
private Long _blockNumber;
2020
private String address;
2121
private String transactionHash;
2222
private String transactionIndex;
23-
private long _transactionIndex;
23+
private Long _transactionIndex;
2424
private String timeStamp;
2525
private LocalDateTime _timeStamp;
2626
private String data;
@@ -30,11 +30,11 @@ public class Log {
3030
private BigInteger _gasUsed;
3131
private List<String> topics;
3232
private String logIndex;
33-
private long _logIndex;
33+
private Long _logIndex;
3434

3535
//<editor-fold desc="Getters">
36-
public long getBlockNumber() {
37-
if(!BasicUtils.isEmpty(blockNumber)){
36+
public Long getBlockNumber() {
37+
if(_blockNumber == null && !BasicUtils.isEmpty(blockNumber)){
3838
_blockNumber = BasicUtils.parseHex(blockNumber).longValue();
3939
}
4040
return _blockNumber;
@@ -48,8 +48,8 @@ public String getTransactionHash() {
4848
return transactionHash;
4949
}
5050

51-
public long getTransactionIndex() {
52-
if(!BasicUtils.isEmpty(transactionIndex)){
51+
public Long getTransactionIndex() {
52+
if(_transactionIndex == null && !BasicUtils.isEmpty(transactionIndex)){
5353
_transactionIndex = BasicUtils.parseHex(transactionIndex).longValue();
5454
}
5555

@@ -90,8 +90,8 @@ public List<String> getTopics() {
9090
return topics;
9191
}
9292

93-
public long getLogIndex() {
94-
if(!BasicUtils.isEmpty(logIndex)){
93+
public Long getLogIndex() {
94+
if(_logIndex == null && !BasicUtils.isEmpty(logIndex)){
9595
_logIndex = BasicUtils.parseHex(logIndex).longValue();
9696
}
9797
return _logIndex;

src/main/java/io/api/etherscan/model/proxy/BlockProxy.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.api.etherscan.util.BasicUtils;
44

5+
import java.math.BigInteger;
56
import java.time.LocalDateTime;
67
import java.time.ZoneOffset;
78
import java.util.List;
@@ -15,10 +16,12 @@
1516
public class BlockProxy {
1617

1718
private String number;
19+
private Long _number;
1820
private String hash;
1921
private String parentHash;
2022
private String stateRoot;
2123
private String size;
24+
private Long _size;
2225
private String difficulty;
2326
private String totalDifficulty;
2427
private String timestamp;
@@ -30,7 +33,9 @@ public class BlockProxy {
3033
private String logsBloom;
3134
private String mixHash;
3235
private String gasUsed;
36+
private BigInteger _gasUsed;
3337
private String gasLimit;
38+
private BigInteger _gasLimit;
3439

3540
private String sha3Uncles;
3641
private List<String> uncles;
@@ -40,8 +45,10 @@ public class BlockProxy {
4045
private List<TxProxy> transactions;
4146

4247
//<editor-fold desc="Getters">
43-
public String getNumber() {
44-
return number;
48+
public Long getNumber() {
49+
if(_number == null && !BasicUtils.isEmpty(number))
50+
_number = BasicUtils.parseHex(number).longValue();
51+
return _number;
4552
}
4653

4754
public String getHash() {
@@ -56,8 +63,10 @@ public String getStateRoot() {
5663
return stateRoot;
5764
}
5865

59-
public String getSize() {
60-
return size;
66+
public Long getSize() {
67+
if(_size == null && !BasicUtils.isEmpty(size))
68+
_size = BasicUtils.parseHex(size).longValue();
69+
return _size;
6170
}
6271

6372
public String getDifficulty() {
@@ -94,12 +103,16 @@ public String getMixHash() {
94103
return mixHash;
95104
}
96105

97-
public String getGasUsed() {
98-
return gasUsed;
106+
public BigInteger getGasUsed() {
107+
if(_gasUsed == null && !BasicUtils.isEmpty(gasUsed))
108+
_gasUsed = BasicUtils.parseHex(gasUsed);
109+
return _gasUsed;
99110
}
100111

101-
public String getGasLimit() {
102-
return gasLimit;
112+
public BigInteger getGasLimit() {
113+
if(_gasLimit == null && !BasicUtils.isEmpty(gasLimit))
114+
_gasLimit = BasicUtils.parseHex(gasLimit);
115+
return _gasLimit;
103116
}
104117

105118
public String getSha3Uncles() {

src/main/java/io/api/etherscan/model/proxy/ReceiptProxy.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.api.etherscan.model.proxy;
22

33
import io.api.etherscan.model.Log;
4+
import io.api.etherscan.util.BasicUtils;
45

6+
import java.math.BigInteger;
57
import java.util.List;
68

79
/**
@@ -16,11 +18,15 @@ public class ReceiptProxy {
1618
private String from;
1719
private String to;
1820
private String blockNumber;
21+
private Long _blockNumber;
1922
private String blockHash;
2023
private String transactionHash;
2124
private String transactionIndex;
25+
private Long _transactionIndex;
2226
private String gasUsed;
27+
private BigInteger _gasUsed;
2328
private String cumulativeGasUsed;
29+
private BigInteger _cumulativeGasUsed;
2430
private String contractAddress;
2531

2632
private List<Log> logs;
@@ -39,8 +45,10 @@ public String getTo() {
3945
return to;
4046
}
4147

42-
public String getBlockNumber() {
43-
return blockNumber;
48+
public Long getBlockNumber() {
49+
if(_blockNumber == null && !BasicUtils.isEmpty(blockNumber))
50+
_blockNumber = BasicUtils.parseHex(blockNumber).longValue();
51+
return _blockNumber;
4452
}
4553

4654
public String getBlockHash() {
@@ -51,16 +59,22 @@ public String getTransactionHash() {
5159
return transactionHash;
5260
}
5361

54-
public String getTransactionIndex() {
55-
return transactionIndex;
62+
public Long getTransactionIndex() {
63+
if(_transactionIndex == null && !BasicUtils.isEmpty(transactionIndex))
64+
_transactionIndex = BasicUtils.parseHex(transactionIndex).longValue();
65+
return _transactionIndex;
5666
}
5767

58-
public String getGasUsed() {
59-
return gasUsed;
68+
public BigInteger getGasUsed() {
69+
if(_gasUsed == null && !BasicUtils.isEmpty(gasUsed))
70+
_gasUsed = BasicUtils.parseHex(gasUsed);
71+
return _gasUsed;
6072
}
6173

62-
public String getCumulativeGasUsed() {
63-
return cumulativeGasUsed;
74+
public BigInteger getCumulativeGasUsed() {
75+
if(_cumulativeGasUsed == null && !BasicUtils.isEmpty(cumulativeGasUsed))
76+
_cumulativeGasUsed = BasicUtils.parseHex(cumulativeGasUsed);
77+
return _cumulativeGasUsed;
6478
}
6579

6680
public String getContractAddress() {

src/main/java/io/api/etherscan/model/proxy/TxProxy.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.api.etherscan.model.proxy;
22

3+
import io.api.etherscan.util.BasicUtils;
4+
5+
import java.math.BigInteger;
6+
37
/**
48
* ! NO DESCRIPTION !
59
*
@@ -11,17 +15,22 @@ public class TxProxy {
1115
private String to;
1216
private String hash;
1317
private String transactionIndex;
18+
private Long _transactionIndex;
1419
private String from;
15-
private String gas;
1620
private String v;
1721
private String input;
1822
private String s;
1923
private String r;
2024
private String nonce;
25+
private Long _nonce;
2126
private String value;
27+
private String gas;
28+
private BigInteger _gas;
2229
private String gasPrice;
30+
private BigInteger _gasPrice;
2331
private String blockHash;
2432
private String blockNumber;
33+
private Long _blockNumber;
2534

2635
//<editor-fold desc="Getters">
2736
public String getTo() {
@@ -32,16 +41,20 @@ public String getHash() {
3241
return hash;
3342
}
3443

35-
public String getTransactionIndex() {
36-
return transactionIndex;
44+
public Long getTransactionIndex() {
45+
if(_transactionIndex == null && !BasicUtils.isEmpty(transactionIndex))
46+
_transactionIndex = BasicUtils.parseHex(transactionIndex).longValue();
47+
return _transactionIndex;
3748
}
3849

3950
public String getFrom() {
4051
return from;
4152
}
4253

43-
public String getGas() {
44-
return gas;
54+
public BigInteger getGas() {
55+
if(_gas == null && !BasicUtils.isEmpty(gas))
56+
_gas = BasicUtils.parseHex(gas);
57+
return _gas;
4558
}
4659

4760
public String getV() {
@@ -60,24 +73,30 @@ public String getR() {
6073
return r;
6174
}
6275

63-
public String getNonce() {
64-
return nonce;
76+
public Long getNonce() {
77+
if(_nonce == null && !BasicUtils.isEmpty(nonce))
78+
_nonce = BasicUtils.parseHex(nonce).longValue();
79+
return _nonce;
6580
}
6681

6782
public String getValue() {
6883
return value;
6984
}
7085

71-
public String getGasPrice() {
72-
return gasPrice;
86+
public BigInteger getGasPrice() {
87+
if(_gasPrice == null && !BasicUtils.isEmpty(gasPrice))
88+
_gasPrice = BasicUtils.parseHex(gasPrice);
89+
return _gasPrice;
7390
}
7491

7592
public String getBlockHash() {
7693
return blockHash;
7794
}
7895

79-
public String getBlockNumber() {
80-
return blockNumber;
96+
public Long getBlockNumber() {
97+
if(_blockNumber == null && !BasicUtils.isEmpty(blockNumber))
98+
_blockNumber = BasicUtils.parseHex(blockNumber).longValue();
99+
return _blockNumber;
81100
}
82101
//</editor-fold>
83102
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy