Skip to content

Commit

Permalink
don't burn part of the rewards that would go to treasury
Browse files Browse the repository at this point in the history
  • Loading branch information
bsuieric authored and dzajkowski committed May 13, 2021
1 parent 68275ae commit 048d591
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
Expand Up @@ -31,7 +31,6 @@ object ConsensusConfig extends Logger {
final val HeaderExtraData = "header-extra-data"
final val BlockCacheSize = "block-cashe-size"
final val MiningEnabled = "mining-enabled"
final val TreasuryOptOut = "treasury-opt-out"
}

final val AllowedProtocols = Set(
Expand Down Expand Up @@ -69,15 +68,16 @@ object ConsensusConfig extends Logger {
.take(BlockHeaderValidator.MaxExtraDataSize)
val blockCacheSize = config.getInt(Keys.BlockCacheSize)
val miningEnabled = config.getBoolean(Keys.MiningEnabled)
val optOut = config.getBoolean(Keys.TreasuryOptOut)
// treasuryOptOut should always be false(no funds burned), keep for backwards compatibility
val treasuryOptOut = false

new ConsensusConfig(
protocol = protocol,
coinbase = coinbase,
headerExtraData = headerExtraData,
blockCacheSize = blockCacheSize,
miningEnabled = miningEnabled,
treasuryOptOut = optOut
treasuryOptOut = treasuryOptOut
)
}
}
Expand Up @@ -49,6 +49,7 @@ object BlockHeaderError {
case class HeaderFieldNotEmptyError(msg: String) extends BlockHeaderError
case class HeaderNotMatchParentError(msg: String) extends BlockHeaderError
case object CheckpointHeaderTreasuryOptOutError extends BlockHeaderError
case object BlockHeaderTreasuryOptOutError extends BlockHeaderError

case class HeaderUnexpectedError(msg: String) extends BlockHeaderError
}
Expand Down
Expand Up @@ -85,6 +85,7 @@ abstract class BlockHeaderValidatorSkeleton(blockchainConfig: BlockchainConfig)
_ <- validateNumber(blockHeader, parentHeader)
_ <- validateExtraFields(blockHeader)
_ <- validateEvenMore(blockHeader)
_ <- validateExtraFieldsTreasuryOptOut(blockHeader)
} yield BlockHeaderValid
}

Expand Down Expand Up @@ -239,6 +240,24 @@ abstract class BlockHeaderValidatorSkeleton(blockchainConfig: BlockchainConfig)
}
}

/**
* Validates [[io.iohk.ethereum.domain.BlockHeader.extraFields]] match extrafields - treasuryOptOut when enabled always to false(not burning rewards)
*
* @param blockHeader BlockHeader to validate.
* @return BlockHeader if valid, an [[BlockHeaderTreasuryOptOutError]] otherwise
*/
private def validateExtraFieldsTreasuryOptOut(
blockHeader: BlockHeader
): Either[BlockHeaderError, BlockHeaderValid] = {

blockHeader.extraFields match {
case HefPostEcip1097(treasuryOptOut, _) if !treasuryOptOut => Right(BlockHeaderValid)
case HefPostEcip1098(treasuryOptOut) if !treasuryOptOut => Right(BlockHeaderValid)
case HefEmpty => Right(BlockHeaderValid)
case _ => Left(BlockHeaderTreasuryOptOutError)
}
}

override def validateHeaderOnly(blockHeader: BlockHeader): Either[BlockHeaderError, BlockHeaderValid] = {
for {
_ <- validateExtraData(blockHeader)
Expand Down
12 changes: 1 addition & 11 deletions src/main/scala/io/iohk/ethereum/ledger/BlockPreparator.scala
Expand Up @@ -44,10 +44,7 @@ class BlockPreparator(
* 1. Reward for block is distributed as:
* a. If treasury is disabled or it's has been selfdestructed:
* Pay 100% of it to the miner
* b. If a. isn't true and the miner opted out:
* Pay 80% of it to the miner
* Never generate the 20% else
* c. If a. isn't true and the miner opted in:
* b. If a. isn't true:
* Pay 80% of it to the miner
* Pay 20% of it to the treasury contract
* 2. Miner is payed a reward for the inclusion of ommers
Expand Down Expand Up @@ -76,13 +73,6 @@ class BlockPreparator(
val worldAfterMinerReward = increaseAccountBalance(minerAddress, UInt256(minerReward))(worldStateProxy)
log.debug(s"Paying block $blockNumber reward of $minerReward to miner with address $minerAddress")
worldAfterMinerReward
} else if (block.header.treasuryOptOut.get) {
val minerReward = minerRewardForOmmers + minerRewardForBlock * MinerRewardPercentageAfterECIP1098 / 100
val worldAfterMinerReward = increaseAccountBalance(minerAddress, UInt256(minerReward))(worldStateProxy)
log.debug(
s"Paying block $blockNumber reward of $minerReward to miner with address $minerAddress, miner opted-out of treasury"
)
worldAfterMinerReward
} else {
val minerReward = minerRewardForOmmers + minerRewardForBlock * MinerRewardPercentageAfterECIP1098 / 100
val worldAfterMinerReward = increaseAccountBalance(minerAddress, UInt256(minerReward))(worldStateProxy)
Expand Down
Expand Up @@ -188,14 +188,26 @@ class EthashBlockHeaderValidatorSpec
res shouldBe Right(BlockHeaderValid)
}

it should "mark as valid a post ecip1098 block opt-out with opt out defined" in new EphemBlockchainTestSetup {
it should "mark as invalid a post ecip1098 block opt-out with opt out defined" in new EphemBlockchainTestSetup {
val ecip1098BlockNumber = validBlockHeader.number / 2
val blockchainConfigWithECIP1098Enabled: BlockchainConfig =
blockchainConfig.copy(ecip1098BlockNumber = ecip1098BlockNumber)
val blockHeaderValidator = new BlockValidatorWithPowMocked(blockchainConfigWithECIP1098Enabled)

val validHeader = validBlockHeader.copy(extraFields = HefPostEcip1098(treasuryOptOut = true))

val validationResult = blockHeaderValidator.validate(validHeader, validParentBlockHeader)
validationResult shouldBe Left(BlockHeaderTreasuryOptOutError)
}

it should "mark as valid a post ecip1098 block opt-out with opt out undefined" in new EphemBlockchainTestSetup {
val ecip1098BlockNumber = validBlockHeader.number / 2
val blockchainConfigWithECIP1098Enabled: BlockchainConfig =
blockchainConfig.copy(ecip1098BlockNumber = ecip1098BlockNumber)
val blockHeaderValidator = new BlockValidatorWithPowMocked(blockchainConfigWithECIP1098Enabled)

val validHeader = validBlockHeader.copy(extraFields = HefPostEcip1098(treasuryOptOut = false))

val validationResult = blockHeaderValidator.validate(validHeader, validParentBlockHeader)
validationResult shouldBe Right(BlockHeaderValid)
}
Expand Down
8 changes: 6 additions & 2 deletions src/test/scala/io/iohk/ethereum/ledger/BlockRewardSpec.scala
Expand Up @@ -130,13 +130,17 @@ class BlockRewardSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyC
MinerRewardPercentageAfterECIP1098 * blockReward / 100,
TreasuryRewardPercentageAfterECIP1098 * blockReward / 100
),
(Some(true), true, MinerRewardPercentageAfterECIP1098 * blockReward / 100, 0)
(
Some(true),
true,
MinerRewardPercentageAfterECIP1098 * blockReward / 100,
TreasuryRewardPercentageAfterECIP1098 * blockReward / 100
)
)

forAll(table) { case (minerOptsOut, contractDeployed, minerReward, treasuryReward) =>
val minerAddress = validAccountAddress
val block = sampleBlock(minerAddress, Nil, minerOptsOut)

val worldBeforeExecution =
if (contractDeployed) worldState
else {
Expand Down

0 comments on commit 048d591

Please sign in to comment.