From f51d45fa63f2f120169fe46d4042541c32f38537 Mon Sep 17 00:00:00 2001 From: thinkAfCod Date: Wed, 25 Sep 2024 23:09:40 +0800 Subject: [PATCH] add custom bigInteger decoder --- .../config/ChainBigIntegerDecoder.java | 58 +++++++++++++++++++ .../main/java/io/optimism/config/Config.java | 22 ++++--- hildr-node/src/test/resources/test.toml | 5 +- 3 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 hildr-node/src/main/java/io/optimism/config/ChainBigIntegerDecoder.java diff --git a/hildr-node/src/main/java/io/optimism/config/ChainBigIntegerDecoder.java b/hildr-node/src/main/java/io/optimism/config/ChainBigIntegerDecoder.java new file mode 100644 index 00000000..32c622f8 --- /dev/null +++ b/hildr-node/src/main/java/io/optimism/config/ChainBigIntegerDecoder.java @@ -0,0 +1,58 @@ +package io.optimism.config; + +import java.math.BigInteger; +import org.apache.commons.lang3.StringUtils; +import org.github.gestalt.config.decoder.DecoderContext; +import org.github.gestalt.config.decoder.LeafDecoder; +import org.github.gestalt.config.decoder.Priority; +import org.github.gestalt.config.entity.ValidationError; +import org.github.gestalt.config.node.ConfigNode; +import org.github.gestalt.config.reflect.TypeCapture; +import org.github.gestalt.config.tag.Tags; +import org.github.gestalt.config.utils.GResultOf; + +/** + * Decode BigInteger field in Chain Config. + */ +public class ChainBigIntegerDecoder extends LeafDecoder { + + /** + * ChainBigIntegerDecoder constructor. + */ + public ChainBigIntegerDecoder() {} + + @Override + protected GResultOf leafDecode(String path, ConfigNode node, DecoderContext decoderContext) { + GResultOf results; + String value = node.getValue().orElse(""); + if (!org.github.gestalt.config.utils.StringUtils.isReal(value)) { + results = GResultOf.errors(new ValidationError.DecodingNumberParsing(path, node, name())); + } else { + try { + BigInteger bigInteger = new BigInteger(value); + results = GResultOf.result(bigInteger); + } catch (NumberFormatException e) { + results = GResultOf.errors(new ValidationError.DecodingNumberParsing(path, node, name())); + } + } + return results; + } + + @Override + public Priority priority() { + return Priority.HIGH; + } + + @Override + public String name() { + return "BigInteger-Cust"; + } + + @Override + public boolean canDecode(String path, Tags tags, ConfigNode configNode, TypeCapture type) { + if (StringUtils.isEmpty(path)) { + return false; + } + return path.startsWith("config.chainConfig") && BigInteger.class.isAssignableFrom(type.getRawType()); + } +} diff --git a/hildr-node/src/main/java/io/optimism/config/Config.java b/hildr-node/src/main/java/io/optimism/config/Config.java index fdf3ee7b..78a09aae 100644 --- a/hildr-node/src/main/java/io/optimism/config/Config.java +++ b/hildr-node/src/main/java/io/optimism/config/Config.java @@ -102,29 +102,27 @@ public static Config create(Path configPath, CliConfig cliConfig, ChainConfig ch MapConfigSource cliConfigSource = new MapConfigSource(cliProvider); Gestalt gestalt; + final GestaltBuilder gestaltBuilder = new GestaltBuilder() + .addDefaultDecoders() + .addDecoder(new ChainBigIntegerDecoder()) + .addConfigLoader(environmentVarsLoader) + .addConfigLoader(mapConfigLoader) + .addConfigLoader(tomlLoader) + .addConfigLoader(propertyLoader) + .setTreatMissingValuesAsErrors(false); if (configPath != null) { FileConfigSource tomlConfigSource = new FileConfigSource(configPath); - gestalt = new GestaltBuilder() - .addConfigLoader(environmentVarsLoader) - .addConfigLoader(mapConfigLoader) - .addConfigLoader(tomlLoader) - .addConfigLoader(propertyLoader) + gestalt = gestaltBuilder .addSource(defaultProviderConfigSource) .addSource(chainConfigSource) .addSource(tomlConfigSource) .addSource(cliConfigSource) - .setTreatMissingValuesAsErrors(false) .build(); } else { - gestalt = new GestaltBuilder() - .addConfigLoader(environmentVarsLoader) - .addConfigLoader(mapConfigLoader) - .addConfigLoader(tomlLoader) - .addConfigLoader(propertyLoader) + gestalt = gestaltBuilder .addSource(defaultProviderConfigSource) .addSource(chainConfigSource) .addSource(cliConfigSource) - .setTreatMissingValuesAsErrors(false) .build(); } gestalt.loadConfigs(); diff --git a/hildr-node/src/test/resources/test.toml b/hildr-node/src/test/resources/test.toml index 2be452f7..e46b06ba 100644 --- a/hildr-node/src/test/resources/test.toml +++ b/hildr-node/src/test/resources/test.toml @@ -2,4 +2,7 @@ l2RpcUrl = "https://example2.com" [config.chainConfig] -seqWindowSize = 111_111_111 \ No newline at end of file +seqWindowSize = 111_111_111 + +[config.chainConfig.systemConfig] +l1FeeScalar = 452312848583266388373324160190187140051835877600158453279134670530344387928 \ No newline at end of file