Skip to content

Commit

Permalink
fix proposer config loader (#8687)
Browse files Browse the repository at this point in the history
* fix proposer config loader
  • Loading branch information
mehdi-aouadi authored Oct 8, 2024
1 parent 4477e7c commit 29c81b8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
### Additions and Improvements

### Bug Fixes
- Fixed the proposer configuration file loading error at startup

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static tech.pegasys.teku.infrastructure.exceptions.ExitConstants.FATAL_EXIT_CODE;
import static tech.pegasys.teku.infrastructure.logging.StatusLogger.STATUS_LOG;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.http.HttpClient;
import java.nio.file.Path;
import java.time.Duration;
Expand Down Expand Up @@ -184,7 +183,7 @@ public static ValidatorClientService create(
ProposerConfigProvider.create(
asyncRunner,
validatorConfig.getRefreshProposerConfigFromSource(),
new ProposerConfigLoader(new ObjectMapper()),
new ProposerConfigLoader(),
services.getTimeProvider(),
validatorConfig.getProposerConfigSource());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
import tech.pegasys.teku.validator.client.ProposerConfig;

public class ProposerConfigLoader {
final ObjectMapper objectMapper;
private final ObjectMapper objectMapper;

public ProposerConfigLoader() {
this(new ObjectMapper());
this.objectMapper = new ObjectMapper();
addTekuMappers();
}

Expand All @@ -50,8 +50,8 @@ private void addTekuMappers() {
objectMapper.registerModule(module);
}

public ProposerConfigLoader(final ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
public ObjectMapper getObjectMapper() {
return this.objectMapper;
}

public ProposerConfig getProposerConfig(final URL source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.io.Resources;
import java.net.URL;
import java.util.Map;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes48;
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.ethereum.execution.types.Eth1Address;
Expand Down Expand Up @@ -147,6 +153,40 @@ void shouldNotLoadBuilderOverridesWithPubKeyInDefaultConfig() {
"\"publicKey\" is not allowed in \"default_config.builder.registrationOverrides\"");
}

@Test
public void shouldRegisterRequiredSerializersAndDeserializers() throws JsonProcessingException {
final ProposerConfigLoader loader = new ProposerConfigLoader();
final ObjectMapper objectMapper = loader.getObjectMapper();

final SimpleModule module = new SimpleModule("ProposerConfigLoader");
assertThat(objectMapper.getRegisteredModuleIds()).contains(module.getTypeId());

// Can deserialize BLSPublicKey
final String pubKey =
"0xa057816155ad77931185101128655c0191bd0214c201ca48ed887f6c4c6adf334070efcd75140eada5ac83a92506dd7a";
final String pubKeyJson = String.format("\"%s\"", pubKey);
final BLSPublicKey pubKeyFromJson = objectMapper.readValue(pubKeyJson, BLSPublicKey.class);
assertThat(pubKeyFromJson).isNotNull();
assertThat(pubKeyFromJson.toString()).isEqualTo(pubKey);

// Can serialize BLSPublicKey
final BLSPublicKey blsPubKey = BLSPublicKey.fromHexString(pubKey);
final String jsonFromPubKey = objectMapper.writeValueAsString(blsPubKey);
assertThat(jsonFromPubKey).isEqualTo(pubKeyJson);

// Can deserialize Bytes48 map key
final Bytes48 key = Bytes48.fromHexString(pubKey);
final Map<Bytes48, String> originalMap = Map.of(key, "value");
// Serialize the map to JSON
final String json = objectMapper.writeValueAsString(originalMap);
// Deserialize the JSON back to a map
final Map<Bytes48, String> deserializedMap =
objectMapper.readValue(json, new TypeReference<>() {});

// Verify that the deserialized map matches the original
assertThat(deserializedMap).isEqualTo(originalMap);
}

private void validateContent1(final ProposerConfig config) {
final Optional<Config> theConfig =
config.getConfigForPubKey(
Expand Down

0 comments on commit 29c81b8

Please sign in to comment.