diff --git a/src/main/java/io/optimism/config/ChainBigIntegerDecoder.java b/src/main/java/io/optimism/config/ChainBigIntegerDecoder.java new file mode 100644 index 00000000..32c622f8 --- /dev/null +++ b/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/src/main/java/io/optimism/config/Config.java b/src/main/java/io/optimism/config/Config.java index 5c924f53..d98a5c32 100644 --- a/src/main/java/io/optimism/config/Config.java +++ b/src/main/java/io/optimism/config/Config.java @@ -103,12 +103,16 @@ public static Config create(Path configPath, CliConfig cliConfig, ChainConfig ch Map cliProvider = cliConfig.toConfigMap(); Gestalt gestalt; + final GestaltBuilder gestaltBuilder = new GestaltBuilder() + .addDefaultDecoders() + .addDecoder(new ChainBigIntegerDecoder()) + .addConfigLoader(environmentVarsLoader) + .addConfigLoader(mapConfigLoader) + .addConfigLoader(tomlLoader) + .addConfigLoader(propertyLoader) + .setTreatMissingValuesAsErrors(false); if (configPath != null) { - gestalt = new GestaltBuilder() - .addConfigLoader(environmentVarsLoader) - .addConfigLoader(mapConfigLoader) - .addConfigLoader(tomlLoader) - .addConfigLoader(propertyLoader) + gestalt = gestaltBuilder .addSource(MapConfigSourceBuilder.builder() .setCustomConfig(defaultProvider) .build()) @@ -121,14 +125,9 @@ public static Config create(Path configPath, CliConfig cliConfig, ChainConfig ch .addSource(MapConfigSourceBuilder.builder() .setCustomConfig(cliProvider) .build()) - .setTreatMissingValuesAsErrors(false) .build(); } else { - gestalt = new GestaltBuilder() - .addConfigLoader(environmentVarsLoader) - .addConfigLoader(mapConfigLoader) - .addConfigLoader(tomlLoader) - .addConfigLoader(propertyLoader) + gestalt = gestaltBuilder .addSource(MapConfigSourceBuilder.builder() .setCustomConfig(defaultProvider) .build()) @@ -138,7 +137,6 @@ public static Config create(Path configPath, CliConfig cliConfig, ChainConfig ch .addSource(MapConfigSourceBuilder.builder() .setCustomConfig(cliProvider) .build()) - .setTreatMissingValuesAsErrors(false) .build(); } gestalt.loadConfigs(); diff --git a/src/test/java/io/optimism/config/ConfigTest.java b/src/test/java/io/optimism/config/ConfigTest.java index 4e2a9958..9e307fd4 100644 --- a/src/test/java/io/optimism/config/ConfigTest.java +++ b/src/test/java/io/optimism/config/ConfigTest.java @@ -105,7 +105,8 @@ void create() { assertEquals( BigInteger.valueOf(188), config.chainConfig().systemConfig().l1FeeOverhead()); assertEquals( - BigInteger.valueOf(684000), config.chainConfig().systemConfig().l1FeeScalar()); + new BigInteger("452312848583266388373324160190187140051835877600158453279134670530344387928"), + config.chainConfig().systemConfig().l1FeeScalar()); assertEquals( "0xff00000000000000000000000000000011155420", config.chainConfig().batchInbox()); diff --git a/src/test/resources/test.toml b/src/test/resources/test.toml index 2be452f7..e46b06ba 100644 --- a/src/test/resources/test.toml +++ b/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