Merge branch 'main' into zkbesu

This commit is contained in:
Fabio Di Fabio
2024-09-30 08:59:16 +02:00
18 changed files with 159 additions and 76 deletions

View File

@@ -53,12 +53,12 @@ interface GenesisReader {
private final ObjectNode rootWithoutAllocations;
public FromObjectNode(final ObjectNode root) {
final var removedAllocations = root.remove(ALLOCATION_FIELD);
this.allocations =
removedAllocations != null
? (ObjectNode) removedAllocations
root.get(ALLOCATION_FIELD) != null
? (ObjectNode) root.get(ALLOCATION_FIELD)
: JsonUtil.createEmptyObjectNode();
this.rootWithoutAllocations = normalizeKeys(root);
this.rootWithoutAllocations =
normalizeKeys(root, field -> !field.getKey().equals(ALLOCATION_FIELD));
}
@Override

View File

@@ -25,6 +25,7 @@ import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
@@ -37,6 +38,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Predicates;
import org.apache.tuweni.bytes.Bytes;
/** The Json util class. */
@@ -59,11 +61,29 @@ public class JsonUtil {
* @return a copy of the json object with all keys in lower case.
*/
public static ObjectNode normalizeKeys(final ObjectNode objectNode) {
return normalizeKeys(objectNode, Predicates.alwaysTrue());
}
/**
* Converts all the object keys (but none of the string values) to lowercase for easier lookup.
* This is useful in cases such as the 'genesis.json' file where all keys are assumed to be case
* insensitive.
*
* @param objectNode The ObjectNode to be normalized
* @param fieldPredicate The predicate to filter the fields to normalize
* @return a copy of the json object with all keys in lower case.
*/
public static ObjectNode normalizeKeys(
final ObjectNode objectNode, final Predicate<Map.Entry<String, JsonNode>> fieldPredicate) {
final ObjectNode normalized = JsonUtil.createEmptyObjectNode();
objectNode
.fields()
.forEachRemaining(
entry -> {
if (!fieldPredicate.test(entry)) {
return;
}
final String key = entry.getKey();
final JsonNode value = entry.getValue();
final String normalizedKey = normalizeKey(key);

View File

@@ -53,6 +53,22 @@ public class GenesisReaderTest {
.containsExactly(new GenesisAccount(Address.BLS12_G2MUL, 0, Wei.ONE, null, Map.of(), null));
}
@Test
public void readGenesisFromObjectDoesNotModifyObjectNodeArg() {
final var configNode = mapper.createObjectNode();
configNode.put("londonBlock", 1);
final var allocNode = mapper.createObjectNode();
allocNode.put(Address.BLS12_G2MUL.toUnprefixedHexString(), generateAllocation(Wei.ONE));
final var rootNode = mapper.createObjectNode();
rootNode.put("chainId", 12);
rootNode.put(CONFIG_FIELD, configNode);
rootNode.put(ALLOCATION_FIELD, allocNode);
var rootNodeCopy = rootNode.deepCopy();
new GenesisReader.FromObjectNode(rootNode);
assertThat(rootNode).isEqualTo(rootNodeCopy);
}
@Test
public void readGenesisFromURL(@TempDir final Path folder) throws IOException {
final String jsonStr =

View File

@@ -147,6 +147,36 @@ public class JsonUtilTest {
assertThat(normalizedObj).isEqualTo(expectedObj);
}
@Test
public void normalizeKeys_predicate() {
final ObjectNode originalObj =
mapper
.createObjectNode()
.put("Ant", "Tiny")
.put("Ape", "Smart")
.put("Armadillo", "Armored")
.put("Cat", "Meow")
.put("Bat", "Flying")
.put("Cow", "Moo")
.put("Crocodile", "Snap")
.put("Bear", "Strong")
.put("Cheetah", "Fast")
.put("Beaver", "Builder");
final ObjectNode expectedObj =
mapper
.createObjectNode()
.put("cat", "Meow")
.put("cow", "Moo")
.put("cheetah", "Fast")
.put("crocodile", "Snap");
final ObjectNode normalizedObj =
JsonUtil.normalizeKeys(originalObj, s -> s.getKey().startsWith("C"));
assertThat(normalizedObj).isEqualTo(expectedObj);
}
@Test
public void getLong_nonExistentKey() {
final ObjectNode node = mapper.createObjectNode();