Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit c887e11

Browse files
authored
Merge pull request #967 from weisheme/issue_946
#946 - Fixes for small configuration bugs found by tests
2 parents 6488ed5 + 29626ef commit c887e11

File tree

6 files changed

+49
-16
lines changed

6 files changed

+49
-16
lines changed

ethereumj-core/src/main/java/org/ethereum/config/NodeFilter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,19 @@ public Entry(byte[] nodeId, String hostIpPattern) {
6565
}
6666

6767
public boolean accept(InetAddress nodeAddr) {
68+
if (hostIpPattern == null) return true;
6869
String ip = nodeAddr.getHostAddress();
6970
return hostIpPattern != null && ip.startsWith(hostIpPattern);
7071
}
7172

7273
public boolean accept(Node node) {
7374
try {
74-
return (nodeId == null || Arrays.equals(node.getId(), nodeId))
75-
&& (hostIpPattern == null || accept(InetAddress.getByName(node.getHost())));
75+
boolean shouldAcceptNodeId = nodeId == null || Arrays.equals(node.getId(), nodeId);
76+
if (!shouldAcceptNodeId) {
77+
return false;
78+
}
79+
InetAddress nodeAddress = InetAddress.getByName(node.getHost());
80+
return (hostIpPattern == null || accept(nodeAddress));
7681
} catch (UnknownHostException e) {
7782
return false;
7883
}

ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.ethereum.net.rlpx.Node;
3535
import org.ethereum.util.BuildInfo;
3636
import org.ethereum.util.ByteUtil;
37+
import org.ethereum.util.Utils;
3738
import org.ethereum.validator.BlockCustomHashRule;
3839
import org.ethereum.validator.BlockHeaderValidator;
3940
import org.slf4j.Logger;
@@ -665,7 +666,7 @@ public String customSolcPath() {
665666
public String privateKey() {
666667
if (config.hasPath("peer.privateKey")) {
667668
String key = config.getString("peer.privateKey");
668-
if (key.length() != 64) {
669+
if (key.length() != 64 || !Utils.isHexEncoded(key)) {
669670
throw new RuntimeException("The peer.privateKey needs to be Hex encoded and 32 byte length");
670671
}
671672
return key;

ethereumj-core/src/main/java/org/ethereum/util/Utils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,17 @@ public static void sleep(long ms) {
289289
Thread.currentThread().interrupt();
290290
}
291291
}
292+
293+
public static boolean isHexEncoded(String value) {
294+
if (value == null) return false;
295+
if ("".equals(value)) return true;
296+
297+
try {
298+
//noinspection ResultOfMethodCallIgnored
299+
new BigInteger(value, 16);
300+
return true;
301+
} catch (NumberFormatException e) {
302+
return false;
303+
}
304+
}
292305
}

ethereumj-core/src/test/java/org/ethereum/config/NodeFilterTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import org.spongycastle.util.encoders.Hex;
2626

2727
import java.net.InetAddress;
28-
import java.net.UnknownHostException;
2928

3029
import static junit.framework.TestCase.assertFalse;
3130
import static org.junit.Assert.assertTrue;
32-
import static org.junit.Assert.fail;
3331

3432
public class NodeFilterTest {
3533

@@ -134,11 +132,11 @@ public void acceptNullIpPatternAsCatchAllForNodes() throws Exception {
134132
}
135133

136134
@Test
137-
public void doNotAcceptNullIpPatternAsCatchAllForInetAddresses() throws Exception {
135+
public void acceptNullIpPatternAsCatchAllForInetAddresses() throws Exception {
138136
NodeFilter filter = new NodeFilter();
139137
filter.add(NODE_1, null);
140-
assertFalse(filter.accept(InetAddress.getByName("1.2.3.4")));
141-
assertFalse(filter.accept(InetAddress.getByName("255.255.255.255")));
138+
assertTrue(filter.accept(InetAddress.getByName("1.2.3.4")));
139+
assertTrue(filter.accept(InetAddress.getByName("255.255.255.255")));
142140
}
143141

144142
@Test
@@ -153,19 +151,17 @@ public void doNotAcceptInvalidNodeHostnameWhenUsingPattern() throws Exception {
153151
NodeFilter filter = new NodeFilter();
154152
filter.add(null, "1.2.3.4");
155153

156-
Node nodeWithInvalidHostname = new Node(
157-
"enode://" + Hex.toHexString(NODE_1) + "@unknown:30303");
154+
Node nodeWithInvalidHostname = new Node("enode://" + Hex.toHexString(NODE_1) + "@unknown:30303");
158155
assertFalse(filter.accept(nodeWithInvalidHostname));
159156
}
160157

161158
@Test
162-
public void acceptInvalidNodeHostnameWhenUsingWildcard() throws Exception {
159+
public void doNotAcceptInvalidNodeHostnameWhenUsingWildcard() throws Exception {
163160
NodeFilter filter = new NodeFilter();
164161
filter.add(null, null);
165162

166-
Node nodeWithInvalidHostname = new Node(
167-
"enode://" + Hex.toHexString(NODE_1) + "@unknown:30303");
168-
assertTrue(filter.accept(nodeWithInvalidHostname));
163+
Node nodeWithInvalidHostname = new Node("enode://" + Hex.toHexString(NODE_1) + "@unknown:30303");
164+
assertFalse(filter.accept(nodeWithInvalidHostname));
169165
}
170166

171167
private static Node createTestNode(String nodeName, String hostIpPattern) {

ethereumj-core/src/test/java/org/ethereum/config/SystemPropertiesTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,6 @@ private void assertInvalidPrivateKey(byte[] privateKey) {
395395
} catch (RuntimeException ignore) { }
396396
}
397397

398-
@Ignore
399398
@Test
400399
public void testExposeBugWhereNonHexEncodedIsAcceptedWithoutValidation() {
401400
SystemProperties props = new SystemProperties();

ethereumj-core/src/test/java/org/ethereum/util/UtilsTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.math.BigInteger;
2525

2626
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertTrue;
2729

2830
/**
2931
* @author Roman Mandeleil
@@ -105,9 +107,26 @@ public void testAddressStringToBytes() {
105107
assertEquals(expected, result);
106108
}
107109

110+
@Test
111+
public void testIsHexEncoded() {
112+
assertTrue(Utils.isHexEncoded("AAA"));
113+
assertTrue(Utils.isHexEncoded("6c386a4b26f73c802f34673f7248bb118f97424a"));
114+
assertFalse(Utils.isHexEncoded(null));
115+
assertFalse(Utils.isHexEncoded("I am not hex"));
116+
assertTrue(Utils.isHexEncoded(""));
117+
assertTrue(Utils.isHexEncoded(
118+
"6c386a4b26f73c802f34673f7248bb118f97424a" +
119+
"6c386a4b26f73c802f34673f7248bb118f97424a" +
120+
"6c386a4b26f73c802f34673f7248bb118f97424a" +
121+
"6c386a4b26f73c802f34673f7248bb118f97424a" +
122+
"6c386a4b26f73c802f34673f7248bb118f97424a" +
123+
"6c386a4b26f73c802f34673f7248bb118f97424a" +
124+
"6c386a4b26f73c802f34673f7248bb118f97424a"));
125+
}
126+
108127
@Test
109128
public void testLongToTimePeriod() {
110129
assertEquals("2.99s", Utils.longToTimePeriod(3000 - 12));
111130
assertEquals("1d21h", Utils.longToTimePeriod(45L * 3600 * 1000));
112131
}
113-
}
132+
}

0 commit comments

Comments
 (0)