-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
begin implementing multiple fee recipient
- Loading branch information
Showing
9 changed files
with
300 additions
and
3 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
data/serializer/src/main/java/tech/pegasys/teku/provider/BLSPubKeyKeyDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2022 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.provider; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.KeyDeserializer; | ||
import tech.pegasys.teku.api.schema.BLSPubKey; | ||
|
||
public class BLSPubKeyKeyDeserializer extends KeyDeserializer { | ||
|
||
@Override | ||
public Object deserializeKey(String key, DeserializationContext ctxt) { | ||
return BLSPubKey.fromHexString(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
validator/client/src/main/java/tech/pegasys/teku/validator/client/ProposerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2022 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.validator.client; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import tech.pegasys.teku.api.schema.BLSPubKey; | ||
import tech.pegasys.teku.infrastructure.ssz.type.Bytes20; | ||
|
||
public class ProposerConfig { | ||
@JsonProperty(value = "proposer_config", required = true) | ||
private Map<BLSPubKey, Config> proposerConfig; | ||
|
||
@JsonProperty(value = "default_config", required = true) | ||
private Config defaultConfig; | ||
|
||
public Optional<Config> getConfigForPubKey(final String pubKey) { | ||
return getConfigForPubKey(BLSPubKey.fromHexString(pubKey)); | ||
} | ||
|
||
public Optional<Config> getConfigForPubKey(final BLSPubKey pubKey) { | ||
return Optional.ofNullable(proposerConfig.get(pubKey)); | ||
} | ||
|
||
public Optional<Config> getDefaultConfig() { | ||
return Optional.ofNullable(defaultConfig); | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class Config { | ||
@JsonProperty(value = "fee_recipient", required = true) | ||
private Bytes20 feeRecipient; | ||
|
||
public Bytes20 getFeeRecipient() { | ||
return feeRecipient; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
validator/client/src/main/java/tech/pegasys/teku/validator/client/ProposerConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2022 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.validator.client; | ||
|
||
import java.time.Duration; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import tech.pegasys.teku.infrastructure.async.AsyncRunner; | ||
import tech.pegasys.teku.infrastructure.async.Cancellable; | ||
import tech.pegasys.teku.infrastructure.async.SafeFuture; | ||
import tech.pegasys.teku.service.serviceutils.Service; | ||
|
||
public class ProposerConfigService extends Service { | ||
private static final Logger LOG = LogManager.getLogger(); | ||
static final Duration DEFAULT_REFRESH_RATE = Duration.ofMinutes(1); | ||
|
||
private final Duration refreshRate; | ||
private final AsyncRunner asyncRunner; | ||
private Optional<Cancellable> cancellable = Optional.empty(); | ||
private final AtomicBoolean running = new AtomicBoolean(false); | ||
|
||
public ProposerConfigService(final AsyncRunner asyncRunner) { | ||
this.asyncRunner = asyncRunner; | ||
this.refreshRate = DEFAULT_REFRESH_RATE; | ||
} | ||
|
||
@Override | ||
protected SafeFuture<?> doStart() { | ||
cancellable = | ||
Optional.of( | ||
asyncRunner.runWithFixedDelay( | ||
this::loadProposerConfig, | ||
refreshRate, | ||
error -> LOG.error("Failed to refresh proposer configuration", error))); | ||
// Run immediately on start | ||
loadProposerConfig(); | ||
return SafeFuture.COMPLETE; | ||
} | ||
|
||
@Override | ||
protected SafeFuture<?> doStop() { | ||
cancellable.ifPresent(Cancellable::cancel); | ||
cancellable = Optional.empty(); | ||
return SafeFuture.COMPLETE; | ||
} | ||
|
||
private void loadProposerConfig() { | ||
if (running.compareAndSet(false, true)) {} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../client/src/main/java/tech/pegasys/teku/validator/client/loader/ProposerConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2022 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.validator.client.loader; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
import tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException; | ||
import tech.pegasys.teku.provider.JsonProvider; | ||
import tech.pegasys.teku.validator.client.ProposerConfig; | ||
|
||
public class ProposerConfigLoader { | ||
private final JsonProvider jsonProvider = new JsonProvider(); | ||
|
||
public ProposerConfig getProposerConfig(final String source) { | ||
try { | ||
final ProposerConfig proposerConfig = | ||
jsonProvider.getObjectMapper().readValue(source, ProposerConfig.class); | ||
return proposerConfig; | ||
} catch (IOException ex) { | ||
throw new InvalidConfigurationException("Failed to proposer config from URL " + source, ex); | ||
} | ||
} | ||
|
||
public ProposerConfig getProposerConfig(final URL source) { | ||
try { | ||
final ProposerConfig proposerConfig = | ||
jsonProvider.getObjectMapper().readValue(source, ProposerConfig.class); | ||
return proposerConfig; | ||
} catch (IOException ex) { | ||
throw new InvalidConfigurationException("Failed to proposer config from URL " + source, ex); | ||
} | ||
} | ||
|
||
private Optional<URL> getUrl(final String source) { | ||
try { | ||
return Optional.of(new URL(source)); | ||
} catch (IOException e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ent/src/test/java/tech/pegasys/teku/validator/client/loader/ProposerConfigLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2022 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package tech.pegasys.teku.validator.client.loader; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.common.io.Resources; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Test; | ||
import tech.pegasys.teku.infrastructure.ssz.type.Bytes20; | ||
import tech.pegasys.teku.validator.client.ProposerConfig; | ||
import tech.pegasys.teku.validator.client.ProposerConfig.Config; | ||
|
||
public class ProposerConfigLoaderTest { | ||
private final ProposerConfigLoader loader = new ProposerConfigLoader(); | ||
|
||
@Test | ||
void shouldLoadValidConfig() { | ||
final URL resource = Resources.getResource("proposerConfig.json"); | ||
|
||
ProposerConfig config = loader.getProposerConfig(resource); | ||
Optional<Config> theConfig = | ||
config.getConfigForPubKey( | ||
"0xa057816155ad77931185101128655c0191bd0214c201ca48ed887f6c4c6adf334070efcd75140eada5ac83a92506dd7a"); | ||
assertThat(theConfig).isPresent(); | ||
assertThat(theConfig.get().getFeeRecipient()) | ||
.isEqualTo(Bytes20.fromHexString("0x50155530FCE8a85ec7055A5F8b2bE214B3DaeFd3")); | ||
|
||
Optional<Config> defaultConfig = config.getDefaultConfig(); | ||
assertThat(defaultConfig).isPresent(); | ||
assertThat(defaultConfig.get().getFeeRecipient()) | ||
.isEqualTo(Bytes20.fromHexString("0x6e35733c5af9B61374A128e6F85f553aF09ff89A")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"proposer_config": { | ||
"0xa057816155ad77931185101128655c0191bd0214c201ca48ed887f6c4c6adf334070efcd75140eada5ac83a92506dd7a": { | ||
"fee_recipient": "0x50155530FCE8a85ec7055A5F8b2bE214B3DaeFd3" | ||
} | ||
}, | ||
"default_config": { | ||
"fee_recipient": "0x6e35733c5af9B61374A128e6F85f553aF09ff89A" | ||
} | ||
} |