Prevents EIP1559 blocks from being mined before fork (#2226)

* add check

Signed-off-by: Karim TAAM <karim.t2am@gmail.com>

* update test

Signed-off-by: Karim TAAM <karim.t2am@gmail.com>

* clean code

Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
pull/2234/head
matkt 4 years ago committed by GitHub
parent f6d50dcab2
commit 1643e0b5d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/ProofOfWorkValidationRule.java
  2. 62
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/ProofOfWorkValidationRuleTest.java

@ -57,6 +57,9 @@ public final class ProofOfWorkValidationRule implements DetachedBlockHeaderValid
LOG.info("Invalid block header: missing mandatory base fee."); LOG.info("Invalid block header: missing mandatory base fee.");
return false; return false;
} }
} else if (header.getBaseFee().isPresent()) {
LOG.info("Invalid block header: presence of basefee in a non-eip1559 block");
return false;
} }
final Hash headerHash = hashHeader(header); final Hash headerHash = hashHeader(header);

@ -16,6 +16,7 @@ package org.hyperledger.besu.ethereum.mainnet.headervalidationrules;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import org.hyperledger.besu.config.experimental.ExperimentalEIPs;
import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderBuilder; import org.hyperledger.besu.ethereum.core.BlockHeaderBuilder;
import org.hyperledger.besu.ethereum.core.BlockHeaderFunctions; import org.hyperledger.besu.ethereum.core.BlockHeaderFunctions;
@ -35,6 +36,7 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import org.apache.tuweni.units.bigints.UInt256; import org.apache.tuweni.units.bigints.UInt256;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
@ -68,6 +70,11 @@ public class ProofOfWorkValidationRuleTest {
}); });
} }
@After
public void reset() {
ExperimentalEIPs.eip1559Enabled = false;
}
@Test @Test
public void validatesValidBlocks() { public void validatesValidBlocks() {
assertThat(validationRule.validate(blockHeader, parentHeader)).isTrue(); assertThat(validationRule.validate(blockHeader, parentHeader)).isTrue();
@ -139,6 +146,61 @@ public class ProofOfWorkValidationRuleTest {
assertThat(validationRule.validate(header, parentHeader)).isFalse(); assertThat(validationRule.validate(header, parentHeader)).isFalse();
} }
@Test
public void failsWithNonEip1559BlockAfterFork() {
final ProofOfWorkValidationRule proofOfWorkValidationRule =
new ProofOfWorkValidationRule(
new EpochCalculator.DefaultEpochCalculator(), true, PoWHasher.ETHASH_LIGHT);
final BlockHeaderBuilder headerBuilder =
BlockHeaderBuilder.fromHeader(blockHeader)
.difficulty(Difficulty.ONE)
.blockHeaderFunctions(mainnetBlockHashFunction())
.timestamp(1);
final BlockHeader preHeader = headerBuilder.buildBlockHeader();
final Hash headerHash = validationRule.hashHeader(preHeader);
PoWSolution solution =
PoWHasher.ETHASH_LIGHT.hash(
preHeader.getNonce(),
preHeader.getNumber(),
new EpochCalculator.DefaultEpochCalculator(),
headerHash);
final BlockHeader header = headerBuilder.mixHash(solution.getMixHash()).buildBlockHeader();
ExperimentalEIPs.eip1559Enabled = true;
assertThat(proofOfWorkValidationRule.validate(header, parentHeader)).isFalse();
}
@Test
public void failsWithEip1559BlockBeforeFork() {
final ProofOfWorkValidationRule proofOfWorkValidationRule =
new ProofOfWorkValidationRule(
new EpochCalculator.DefaultEpochCalculator(), false, PoWHasher.ETHASH_LIGHT);
final BlockHeaderBuilder headerBuilder =
BlockHeaderBuilder.fromHeader(blockHeader)
.difficulty(Difficulty.ONE)
.baseFee(10L)
.blockHeaderFunctions(mainnetBlockHashFunction())
.timestamp(1);
final BlockHeader preHeader = headerBuilder.buildBlockHeader();
final Hash headerHash = validationRule.hashHeader(preHeader);
PoWSolution solution =
PoWHasher.ETHASH_LIGHT.hash(
preHeader.getNonce(),
preHeader.getNumber(),
new EpochCalculator.DefaultEpochCalculator(),
headerHash);
final BlockHeader header = headerBuilder.mixHash(solution.getMixHash()).buildBlockHeader();
assertThat(proofOfWorkValidationRule.validate(header, parentHeader)).isFalse();
}
private BlockHeaderFunctions mainnetBlockHashFunction() { private BlockHeaderFunctions mainnetBlockHashFunction() {
final ProtocolSchedule protocolSchedule = ProtocolScheduleFixture.MAINNET; final ProtocolSchedule protocolSchedule = ProtocolScheduleFixture.MAINNET;
return ScheduleBasedBlockHeaderFunctions.create(protocolSchedule); return ScheduleBasedBlockHeaderFunctions.create(protocolSchedule);

Loading…
Cancel
Save