Skip to content

Commit

Permalink
add custom bigInteger decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod authored and GrapeBaBa committed Sep 26, 2024
1 parent a444ed2 commit bf714f3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
58 changes: 58 additions & 0 deletions src/main/java/io/optimism/config/ChainBigIntegerDecoder.java
Original file line number Diff line number Diff line change
@@ -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<BigInteger> {

/**
* ChainBigIntegerDecoder constructor.
*/
public ChainBigIntegerDecoder() {}

@Override
protected GResultOf<BigInteger> leafDecode(String path, ConfigNode node, DecoderContext decoderContext) {
GResultOf<BigInteger> 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());
}
}
22 changes: 10 additions & 12 deletions src/main/java/io/optimism/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,16 @@ public static Config create(Path configPath, CliConfig cliConfig, ChainConfig ch
Map<String, String> 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())
Expand All @@ -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())
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/io/optimism/config/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 4 additions & 1 deletion src/test/resources/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
l2RpcUrl = "https://example2.com"

[config.chainConfig]
seqWindowSize = 111_111_111
seqWindowSize = 111_111_111

[config.chainConfig.systemConfig]
l1FeeScalar = 452312848583266388373324160190187140051835877600158453279134670530344387928

0 comments on commit bf714f3

Please sign in to comment.