-
Notifications
You must be signed in to change notification settings - Fork 892
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added configurable block reward and recipient for IBFT2
Signed-off-by: Trent Mohay <trent.mohay@consensys.net>
- Loading branch information
Trent Mohay
committed
Jun 24, 2020
1 parent
89f37e4
commit 5770d7a
Showing
10 changed files
with
243 additions
and
24 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...ts/src/test/java/org/hyperledger/besu/tests/acceptance/ibft2/Ibft2BlockRewardPayment.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 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.tests.acceptance.ibft2; | ||
|
||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
import org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBase; | ||
import org.hyperledger.besu.tests.acceptance.dsl.account.Account; | ||
import org.hyperledger.besu.tests.acceptance.dsl.blockchain.Amount; | ||
import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode; | ||
import org.junit.Test; | ||
|
||
public class Ibft2BlockRewardPayment extends AcceptanceTestBase { | ||
|
||
@Test | ||
public void validatorsArePaidBlockReward() throws IOException { | ||
final String[] validators = {"validator1"}; | ||
final BesuNode validator1 = besu.createIbft2NodeWithValidators("validator1", validators); | ||
final BesuNode validator2 = besu.createIbft2NodeWithValidators("validator2", validators); | ||
cluster.start(validator1, validator2); | ||
final Account validator1Account = Account.create(ethTransactions, validator1.getAddress()); | ||
|
||
final int blockRewardEth = 5; | ||
final int blockToCheck = 2; | ||
|
||
cluster.verify(validator1Account.balanceAtBlockEquals(Amount.ether(0), BigInteger.ZERO)); | ||
cluster.verify(validator1Account | ||
.balanceAtBlockEquals(Amount.ether(blockRewardEth * blockToCheck), | ||
BigInteger.valueOf(blockToCheck))); | ||
} | ||
|
||
} |
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
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
99 changes: 99 additions & 0 deletions
99
...nsus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftProtocolScheduleTest.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,99 @@ | ||
package org.hyperledger.besu.consensus.ibft;/* | ||
* Copyright 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Optional; | ||
import org.hyperledger.besu.config.GenesisConfigOptions; | ||
import org.hyperledger.besu.config.IbftConfigOptions; | ||
import org.hyperledger.besu.ethereum.core.Address; | ||
import org.hyperledger.besu.ethereum.core.BlockHeader; | ||
import org.hyperledger.besu.ethereum.core.Wei; | ||
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; | ||
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec; | ||
import org.junit.Test; | ||
|
||
public class IbftProtocolScheduleTest { | ||
|
||
GenesisConfigOptions genesisConfig = mock(GenesisConfigOptions.class); | ||
|
||
@Test | ||
public void ensureBlockRewardAndMiningBeneficiaryInProtocolSpecMatchConfig() { | ||
final long arbitraryBlockRewardEth = 5; | ||
final String miningBeneficiary = Address.fromHexString("0x1").toString(); | ||
final IbftConfigOptions ibftConfig = mock(IbftConfigOptions.class); | ||
when(ibftConfig.getMiningBeneficiary()).thenReturn(Optional.of(miningBeneficiary)); | ||
when(ibftConfig.getBlockRewardEth()).thenReturn(arbitraryBlockRewardEth); | ||
|
||
when(genesisConfig.getIbft2ConfigOptions()).thenReturn(ibftConfig); | ||
|
||
final ProtocolSchedule schedule = IbftProtocolSchedule.create(genesisConfig); | ||
final ProtocolSpec spec = schedule.getByBlockNumber(1); | ||
|
||
spec.getBlockReward(); | ||
|
||
assertThat(spec.getBlockReward()).isEqualTo(Wei.fromEth(arbitraryBlockRewardEth)); | ||
assertThat(spec.getMiningBeneficiaryCalculator().calculateBeneficiary(null)) | ||
.isEqualTo(Address.fromHexString(miningBeneficiary)); | ||
} | ||
|
||
@Test | ||
public void illegalMiningBeneficiaryStringThrowsException() { | ||
final String miningBeneficiary = "notHexStringOfTwentyBytes"; | ||
final IbftConfigOptions ibftConfig = mock(IbftConfigOptions.class); | ||
when(ibftConfig.getMiningBeneficiary()).thenReturn(Optional.of(miningBeneficiary)); | ||
when(genesisConfig.getIbft2ConfigOptions()).thenReturn(ibftConfig); | ||
|
||
assertThatThrownBy(() -> IbftProtocolSchedule.create(genesisConfig)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Mining beneficiary in config is not a valid ethereum address"); | ||
} | ||
|
||
@Test | ||
public void missingMiningBeneficiaryInConfigWillPayCoinbaseInHeader() { | ||
final long arbitraryBlockRewardEth = 5; | ||
final IbftConfigOptions ibftConfig = mock(IbftConfigOptions.class); | ||
when(ibftConfig.getMiningBeneficiary()).thenReturn(Optional.empty()); | ||
when(ibftConfig.getBlockRewardEth()).thenReturn(arbitraryBlockRewardEth); | ||
when(genesisConfig.getIbft2ConfigOptions()).thenReturn(ibftConfig); | ||
|
||
final ProtocolSchedule schedule = IbftProtocolSchedule.create(genesisConfig); | ||
final ProtocolSpec spec = schedule.getByBlockNumber(1); | ||
|
||
final Address headerCoinbase = Address.fromHexString("0x123"); | ||
final BlockHeader header = mock(BlockHeader.class); | ||
when(header.getCoinbase()).thenReturn(headerCoinbase); | ||
|
||
assertThat(spec.getBlockReward()).isEqualTo(Wei.fromEth(arbitraryBlockRewardEth)); | ||
assertThat(spec.getMiningBeneficiaryCalculator().calculateBeneficiary(header)) | ||
.isEqualTo(headerCoinbase); | ||
} | ||
|
||
@Test | ||
public void negativeBlockRewardThrowsException() { | ||
final long arbitraryBlockRewardEth = -5; | ||
final IbftConfigOptions ibftConfig = mock(IbftConfigOptions.class); | ||
when(ibftConfig.getMiningBeneficiary()).thenReturn(Optional.empty()); | ||
when(ibftConfig.getBlockRewardEth()).thenReturn(arbitraryBlockRewardEth); | ||
when(genesisConfig.getIbft2ConfigOptions()).thenReturn(ibftConfig); | ||
|
||
assertThatThrownBy(() -> IbftProtocolSchedule.create(genesisConfig)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Ibft2 Block reward in config cannot be negative"); | ||
} | ||
} |
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
Oops, something went wrong.