Skip to content

Commit

Permalink
merge of Blockheader changes to support random field (#3104)
Browse files Browse the repository at this point in the history
* merge of Blockheader changes to support random field
Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte committed Nov 25, 2021
1 parent 183cdaf commit 2ed6c61
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.google.common.base.Suppliers;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;

/** A mined Ethereum block header. */
public class BlockHeader extends SealableBlockHeader
Expand All @@ -35,8 +36,6 @@ public class BlockHeader extends SealableBlockHeader

public static final long GENESIS_BLOCK_NUMBER = 0L;

private final Hash mixHash;

private final long nonce;

private final Supplier<Hash> hash;
Expand All @@ -60,7 +59,7 @@ public BlockHeader(
final long timestamp,
final Bytes extraData,
final Long baseFee,
final Hash mixHash,
final Bytes32 mixHashOrRandom,
final long nonce,
final BlockHeaderFunctions blockHeaderFunctions,
final Optional<LogsBloomFilter> privateLogsBloom) {
Expand All @@ -78,8 +77,8 @@ public BlockHeader(
gasUsed,
timestamp,
extraData,
baseFee);
this.mixHash = mixHash;
baseFee,
mixHashOrRandom);
this.nonce = nonce;
this.hash = Suppliers.memoize(() -> blockHeaderFunctions.hash(this));
this.parsedExtraData = Suppliers.memoize(() -> blockHeaderFunctions.parseExtraData(this));
Expand All @@ -101,7 +100,7 @@ public BlockHeader(
final long timestamp,
final Bytes extraData,
final Long baseFee,
final Hash mixHash,
final Bytes32 mixHashOrRandom,
final long nonce,
final BlockHeaderFunctions blockHeaderFunctions) {
super(
Expand All @@ -118,8 +117,8 @@ public BlockHeader(
gasUsed,
timestamp,
extraData,
baseFee);
this.mixHash = mixHash;
baseFee,
mixHashOrRandom);
this.nonce = nonce;
this.hash = Suppliers.memoize(() -> blockHeaderFunctions.hash(this));
this.parsedExtraData = Suppliers.memoize(() -> blockHeaderFunctions.parseExtraData(this));
Expand All @@ -133,7 +132,7 @@ public BlockHeader(
*/
@Override
public Hash getMixHash() {
return mixHash;
return Hash.wrap(mixHashOrRandom);
}

/**
Expand Down Expand Up @@ -215,7 +214,7 @@ public void writeTo(final RLPOutput out) {
out.writeLongScalar(gasUsed);
out.writeLongScalar(timestamp);
out.writeBytes(extraData);
out.writeBytes(mixHash);
out.writeBytes(mixHashOrRandom);
out.writeLong(nonce);
if (baseFee != null) {
out.writeLongScalar(baseFee);
Expand All @@ -239,7 +238,7 @@ public static BlockHeader readFrom(
final long gasUsed = input.readLongScalar();
final long timestamp = input.readLongScalar();
final Bytes extraData = input.readBytes();
final Hash mixHash = Hash.wrap(input.readBytes32());
final Bytes32 mixHashOrRandom = input.readBytes32();
final long nonce = input.readLong();
final Long baseFee = !input.isEndOfCurrentList() ? input.readLongScalar() : null;
input.leaveList();
Expand All @@ -258,7 +257,7 @@ public static BlockHeader readFrom(
timestamp,
extraData,
baseFee,
mixHash,
mixHashOrRandom,
nonce,
blockHeaderFunctions);
}
Expand Down Expand Up @@ -299,7 +298,7 @@ public String toString() {
sb.append("timestamp=").append(timestamp).append(", ");
sb.append("extraData=").append(extraData).append(", ");
sb.append("baseFee=").append(baseFee).append(", ");
sb.append("mixHash=").append(mixHash).append(", ");
sb.append("mixHashOrRandom=").append(mixHashOrRandom).append(", ");
sb.append("nonce=").append(nonce);
return sb.append("}").toString();
}
Expand All @@ -322,7 +321,7 @@ public static org.hyperledger.besu.ethereum.core.BlockHeader convertPluginBlockH
pluginBlockHeader.getTimestamp(),
pluginBlockHeader.getExtraData(),
pluginBlockHeader.getBaseFee().orElse(null),
Hash.fromHexString(pluginBlockHeader.getMixHash().toHexString()),
pluginBlockHeader.getRandom().orElse(null),
pluginBlockHeader.getNonce(),
blockHeaderFunctions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.OptionalLong;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;

/** A utility class for building block headers. */
public class BlockHeaderBuilder {
Expand Down Expand Up @@ -58,7 +59,7 @@ public class BlockHeaderBuilder {

private Long baseFee = null;

private Hash mixHash;
private Bytes32 mixHashOrRandom = null;

private BlockHeaderFunctions blockHeaderFunctions;

Expand Down Expand Up @@ -87,7 +88,8 @@ public static BlockHeaderBuilder fromHeader(final BlockHeader header) {
.extraData(header.getExtraData())
.baseFee(header.getBaseFee().orElse(null))
.mixHash(header.getMixHash())
.nonce(header.getNonce());
.nonce(header.getNonce())
.random(header.getRandom().orElse(null));
}

public static BlockHeaderBuilder fromBuilder(final BlockHeaderBuilder fromBuilder) {
Expand All @@ -106,8 +108,8 @@ public static BlockHeaderBuilder fromBuilder(final BlockHeaderBuilder fromBuilde
.gasUsed(fromBuilder.gasUsed)
.timestamp(fromBuilder.timestamp)
.extraData(fromBuilder.extraData)
.mixHash(fromBuilder.mixHash)
.baseFee(fromBuilder.baseFee)
.random(fromBuilder.mixHashOrRandom)
.blockHeaderFunctions(fromBuilder.blockHeaderFunctions);
toBuilder.nonce = fromBuilder.nonce;
return toBuilder;
Expand All @@ -131,7 +133,7 @@ public BlockHeader buildBlockHeader() {
timestamp < 0 ? Instant.now().getEpochSecond() : timestamp,
extraData,
baseFee,
mixHash,
mixHashOrRandom,
nonce.getAsLong(),
blockHeaderFunctions);
}
Expand All @@ -140,7 +142,7 @@ public ProcessableBlockHeader buildProcessableBlockHeader() {
validateProcessableBlockHeader();

return new ProcessableBlockHeader(
parentHash, coinbase, difficulty, number, gasLimit, timestamp, baseFee);
parentHash, coinbase, difficulty, number, gasLimit, timestamp, baseFee, mixHashOrRandom);
}

public SealableBlockHeader buildSealableBlockHeader() {
Expand All @@ -160,12 +162,13 @@ public SealableBlockHeader buildSealableBlockHeader() {
gasUsed,
timestamp,
extraData,
baseFee);
baseFee,
mixHashOrRandom);
}

private void validateBlockHeader() {
validateSealableBlockHeader();
checkState(this.mixHash != null, "Missing mixHash");
checkState(this.mixHashOrRandom != null, "Missing mixHash or random");
checkState(this.nonce.isPresent(), "Missing nonce");
checkState(this.blockHeaderFunctions != null, "Missing blockHeaderFunctions");
}
Expand Down Expand Up @@ -199,6 +202,7 @@ public BlockHeaderBuilder populateFrom(final ProcessableBlockHeader processableB
gasLimit(processableBlockHeader.getGasLimit());
timestamp(processableBlockHeader.getTimestamp());
baseFee(processableBlockHeader.getBaseFee().orElse(null));
processableBlockHeader.getRandom().ifPresent(this::random);
return this;
}

Expand All @@ -218,6 +222,7 @@ public BlockHeaderBuilder populateFrom(final SealableBlockHeader sealableBlockHe
timestamp(sealableBlockHeader.getTimestamp());
extraData(sealableBlockHeader.getExtraData());
baseFee(sealableBlockHeader.getBaseFee().orElse(null));
sealableBlockHeader.getRandom().ifPresent(this::random);
return this;
}

Expand Down Expand Up @@ -302,7 +307,7 @@ public BlockHeaderBuilder extraData(final Bytes data) {

public BlockHeaderBuilder mixHash(final Hash mixHash) {
checkNotNull(mixHash);
this.mixHash = mixHash;
this.mixHashOrRandom = mixHash;
return this;
}

Expand All @@ -320,4 +325,11 @@ public BlockHeaderBuilder baseFee(final Long baseFee) {
this.baseFee = baseFee;
return this;
}

public BlockHeaderBuilder random(final Bytes32 random) {
if (random != null) {
this.mixHashOrRandom = random;
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;

/** A block header capable of being processed. */
public class ProcessableBlockHeader implements BlockValues {
Expand All @@ -39,6 +40,8 @@ public class ProcessableBlockHeader implements BlockValues {
protected final long timestamp;
// base fee is included for post EIP-1559 blocks
protected final Long baseFee;
// random is included for post-merge blocks
protected final Bytes32 mixHashOrRandom;

protected ProcessableBlockHeader(
final Hash parentHash,
Expand All @@ -47,14 +50,16 @@ protected ProcessableBlockHeader(
final long number,
final long gasLimit,
final long timestamp,
final Long baseFee) {
final Long baseFee,
final Bytes32 mixHashOrRandom) {
this.parentHash = parentHash;
this.coinbase = coinbase;
this.difficulty = difficulty;
this.number = number;
this.gasLimit = gasLimit;
this.timestamp = timestamp;
this.baseFee = baseFee;
this.mixHashOrRandom = mixHashOrRandom;
}

/**
Expand Down Expand Up @@ -127,10 +132,19 @@ public long getTimestamp() {
/**
* Returns the basefee of the block.
*
* @return the raw bytes of the extra data field
* @return the optional long value for base fee
*/
@Override
public Optional<Long> getBaseFee() {
return Optional.ofNullable(baseFee);
}

/**
* Returns the random of the block.
*
* @return the raw bytes of the random field
*/
public Optional<Bytes32> getRandom() {
return Optional.ofNullable(mixHashOrRandom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hyperledger.besu.evm.log.LogsBloomFilter;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;

/** A block header capable of being sealed. */
public class SealableBlockHeader extends ProcessableBlockHeader {
Expand Down Expand Up @@ -50,8 +51,9 @@ protected SealableBlockHeader(
final long gasUsed,
final long timestamp,
final Bytes extraData,
final Long baseFee) {
super(parentHash, coinbase, difficulty, number, gasLimit, timestamp, baseFee);
final Long baseFee,
final Bytes32 mixHashOrRandom) {
super(parentHash, coinbase, difficulty, number, gasLimit, timestamp, baseFee, mixHashOrRandom);
this.ommersHash = ommersHash;
this.stateRoot = stateRoot;
this.transactionsRoot = transactionsRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.hyperledger.besu.evm.log.LogsBloomFilter;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.Test;

public class KeccakHasherTest {
Expand All @@ -43,7 +44,8 @@ protected KeccakSealableBlockHeader(
final long gasUsed,
final long timestamp,
final Bytes extraData,
final Long baseFee) {
final Long baseFee,
final Bytes32 random) {
super(
parentHash,
ommersHash,
Expand All @@ -58,7 +60,8 @@ protected KeccakSealableBlockHeader(
gasUsed,
timestamp,
extraData,
baseFee);
baseFee,
random);
}
}

Expand Down Expand Up @@ -103,6 +106,7 @@ public void testHasherFromBlock() {
7987824L,
1538483791L,
Bytes.fromHexString("0xd88301080f846765746888676f312e31302e31856c696e7578"),
null,
null);

PoWSolution result =
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Calculated : ${currentHash}
tasks.register('checkAPIChanges', FileStateChecker) {
description = "Checks that the API for the Plugin-API project does not change without deliberate thought"
files = sourceSets.main.allJava.files
knownHash = 'Z78NXPRIv33CBOfuUaAfgEr55ZrmxDWqVT8DVJ+FxA4='
knownHash = '/M23BAhwqtMpjhcoHDMJvbHyhJ4z39z2jJMWUGOBO14='
}
check.dependsOn('checkAPIChanges')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;

/**
* The minimum set of data for a BlockHeader, as defined in the <a href=
Expand Down Expand Up @@ -162,10 +163,19 @@ public interface BlockHeader {
/**
* The BASEFEE of this header.
*
* @return TheBASEFEE of this header.
* @return The BASEFEE of this header.
*/
@Unstable
default Optional<Long> getBaseFee() {
return Optional.empty();
}

/**
* Optional 32 bytes of random data.
*
* @return Optional random bytes from this header.
*/
default Optional<Bytes32> getRandom() {
return Optional.empty();
}
}

0 comments on commit 2ed6c61

Please sign in to comment.