forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added log for validator selection mode transition (hyperledger#2909)
* Added log for validator selection mode transition Signed-off-by: Lucas Saldanha <lucascrsaldanha@gmail.com>
- Loading branch information
1 parent
210ceaa
commit 9f83517
Showing
10 changed files
with
331 additions
and
5 deletions.
There are no files selected for viewing
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
41 changes: 41 additions & 0 deletions
41
config/src/test/java/org/hyperledger/besu/config/JsonQbftConfigOptionsTest.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,41 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.hyperledger.besu.config; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hyperledger.besu.config.JsonQbftConfigOptions.VALIDATOR_CONTRACT_ADDRESS; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.junit.Test; | ||
|
||
public class JsonQbftConfigOptionsTest { | ||
|
||
final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Test | ||
public void getValidatorContractAddressNormalization() { | ||
final ObjectNode objectNode = | ||
objectMapper.createObjectNode().put(VALIDATOR_CONTRACT_ADDRESS, "0xABC"); | ||
|
||
final JsonQbftConfigOptions configOptions = new JsonQbftConfigOptions(objectNode); | ||
|
||
assertThat(configOptions.getValidatorContractAddress()).hasValue("0xabc"); | ||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
...ain/java/org/hyperledger/besu/consensus/qbft/validator/ValidatorModeTransitionLogger.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,81 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.hyperledger.besu.consensus.qbft.validator; | ||
|
||
import org.hyperledger.besu.config.QbftConfigOptions; | ||
import org.hyperledger.besu.consensus.common.bft.BftForkSpec; | ||
import org.hyperledger.besu.consensus.common.bft.BftForksSchedule; | ||
import org.hyperledger.besu.ethereum.core.BlockHeader; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class ValidatorModeTransitionLogger { | ||
|
||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
private final BftForksSchedule<QbftConfigOptions> bftForksSchedule; | ||
private final Consumer<String> msgConsumer; | ||
|
||
public ValidatorModeTransitionLogger(final BftForksSchedule<QbftConfigOptions> bftForksSchedule) { | ||
this.bftForksSchedule = bftForksSchedule; | ||
this.msgConsumer = LOG::info; | ||
} | ||
|
||
@VisibleForTesting | ||
ValidatorModeTransitionLogger( | ||
final BftForksSchedule<QbftConfigOptions> bftForksSchedule, | ||
final Consumer<String> msgConsumer) { | ||
this.bftForksSchedule = bftForksSchedule; | ||
this.msgConsumer = msgConsumer; | ||
} | ||
|
||
public void logTransitionChange(final BlockHeader parentHeader) { | ||
final BftForkSpec<QbftConfigOptions> currentForkSpec = | ||
bftForksSchedule.getFork(parentHeader.getNumber()); | ||
final BftForkSpec<QbftConfigOptions> nextForkSpec = | ||
bftForksSchedule.getFork(parentHeader.getNumber() + 1L); | ||
|
||
final QbftConfigOptions currentConfigOptions = currentForkSpec.getConfigOptions(); | ||
final QbftConfigOptions nextConfigOptions = nextForkSpec.getConfigOptions(); | ||
|
||
if (hasChangedConfig(currentConfigOptions, nextConfigOptions)) { | ||
msgConsumer.accept( | ||
String.format( | ||
"Transitioning validator selection mode from %s to %s", | ||
parseConfigToLog(currentConfigOptions), parseConfigToLog(nextConfigOptions))); | ||
} | ||
} | ||
|
||
private boolean hasChangedConfig( | ||
final QbftConfigOptions currentConfig, final QbftConfigOptions nextConfig) { | ||
return !currentConfig | ||
.getValidatorContractAddress() | ||
.equals(nextConfig.getValidatorContractAddress()); | ||
} | ||
|
||
private String parseConfigToLog(final QbftConfigOptions configOptions) { | ||
if (configOptions.getValidatorContractAddress().isPresent()) { | ||
return String.format( | ||
"contract (address: %s)", configOptions.getValidatorContractAddress().get()); | ||
} else { | ||
return "blockheader"; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
.../qbft/src/test/java/org/hyperledger/besu/consensus/qbft/MutableQbftConfigOptionsTest.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,44 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.hyperledger.besu.consensus.qbft; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.hyperledger.besu.config.QbftConfigOptions; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.Test; | ||
|
||
public class MutableQbftConfigOptionsTest { | ||
|
||
private final QbftConfigOptions qbftConfigOptions = mock(QbftConfigOptions.class); | ||
|
||
@Test | ||
public void getValidatorContractAddressNormalization() { | ||
when(qbftConfigOptions.getValidatorContractAddress()).thenReturn(Optional.of("0xABC")); | ||
|
||
final MutableQbftConfigOptions mutableQbftConfigOptions = | ||
new MutableQbftConfigOptions(qbftConfigOptions); | ||
|
||
assertThat(mutableQbftConfigOptions.getValidatorContractAddress()).hasValue("0xabc"); | ||
} | ||
} |
Oops, something went wrong.