[PAN-3012] Print warning when there are comments in genesis file (#1838)

The technique is to parse with comments disabled, and if a parse exception is
detected re-parse with comments allowed, and if successful complain at warn.

To turn this option off we just unwrap the try and delete the catch and update
the test.
Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Danno Ferrin 5 years ago committed by GitHub
parent faf7521fa0
commit 77a9e43812
  1. 26
      config/src/main/java/tech/pegasys/pantheon/config/GenesisConfigFile.java
  2. 11
      config/src/test/java/tech/pegasys/pantheon/config/GenesisConfigFileTest.java

@ -19,12 +19,17 @@ import java.io.IOException;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Streams; import com.google.common.collect.Streams;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class GenesisConfigFile { public class GenesisConfigFile {
static final Logger LOG = LogManager.getLogger();
public static final GenesisConfigFile DEFAULT = public static final GenesisConfigFile DEFAULT =
new GenesisConfigFile(JsonUtil.createEmptyObjectNode()); new GenesisConfigFile(JsonUtil.createEmptyObjectNode());
@ -53,10 +58,23 @@ public class GenesisConfigFile {
} }
public static GenesisConfigFile fromConfig(final String jsonString) { public static GenesisConfigFile fromConfig(final String jsonString) {
// TODO: Should we disable comments? try {
final boolean allowComments = true; final ObjectNode rootNode = JsonUtil.objectNodeFromString(jsonString, false);
final ObjectNode rootNode = JsonUtil.objectNodeFromString(jsonString, allowComments); return fromConfig(rootNode);
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 Pantheon this will not be supported.");
return fromConfig(rootNode);
} else {
throw re;
}
}
} }
public static GenesisConfigFile fromConfig(final ObjectNode config) { public static GenesisConfigFile fromConfig(final ObjectNode config) {

@ -203,6 +203,17 @@ public class GenesisConfigFileTest {
"31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095")); "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095"));
} }
@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.
}
private GenesisConfigFile configWithProperty(final String key, final String value) { private GenesisConfigFile configWithProperty(final String key, final String value) {
return GenesisConfigFile.fromConfig("{\"" + key + "\":\"" + value + "\"}"); return GenesisConfigFile.fromConfig("{\"" + key + "\":\"" + value + "\"}");
} }

Loading…
Cancel
Save