Disallow comments in Genesis JSON file. (#49)

* Disallow comments in Genesis JSON file

Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net>
pull/51/head
Abdelhamid Bakhta 5 years ago committed by GitHub
parent 8acd5d64e1
commit 231cd71c7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java
  2. 16
      config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java

@ -23,7 +23,6 @@ import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Streams;
import com.google.common.io.Resources;
@ -72,23 +71,7 @@ public class GenesisConfigFile {
}
public static GenesisConfigFile fromConfig(final String jsonString) {
try {
final ObjectNode rootNode = JsonUtil.objectNodeFromString(jsonString, false);
return fromConfig(rootNode);
} catch (final RuntimeException re) {
if (re.getCause() instanceof JsonParseException) {
// we had a runtime exception cause by a jsom parse exception.
// try again with comments enabled
final ObjectNode rootNode = JsonUtil.objectNodeFromString(jsonString, true);
// if we get here comments is what broke things, warn and move on.
LOG.warn(
"The provided genesis file contains comments. "
+ "In a future release of Besu this will not be supported.");
return fromConfig(rootNode);
} else {
throw re;
}
}
return fromConfig(JsonUtil.objectNodeFromString(jsonString, false));
}
public static GenesisConfigFile fromConfig(final ObjectNode config) {

@ -24,6 +24,7 @@ import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.fasterxml.jackson.core.JsonParseException;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.Test;
@ -208,14 +209,13 @@ public class GenesisConfigFileTest {
}
@Test
public void acceptComments() {
// this test will change in the future to reject comments.
final GenesisConfigFile config =
GenesisConfigFile.fromConfig(
"{\"config\": { \"chainId\": 2017 }\n/* C comment }*/\n//C++ comment }\n}");
assertThat(config.getConfigOptions().getChainId()).contains(new BigInteger("2017"));
// Unfortunately there is no good (non-flakey) way to assert logs.
public void mustNotAcceptComments() {
assertThatThrownBy(
() ->
GenesisConfigFile.fromConfig(
"{\"config\": { \"chainId\": 2017 }\n/* C comment }*/\n//C++ comment }\n}"))
.hasCauseInstanceOf(JsonParseException.class)
.hasMessageContaining("Unexpected character ('/'");
}
@Test

Loading…
Cancel
Save