Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow comments in Genesis JSON file. #49

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand Down Expand Up @@ -72,23 +71,7 @@ public static GenesisConfigFile development() {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;

Expand Down Expand Up @@ -208,14 +209,13 @@ public void shouldGetLargeChainId() {
}

@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
Expand Down