Skip to content

Commit

Permalink
Merge branch 'main' into mattWhiteheadMaintainer
Browse files Browse the repository at this point in the history
  • Loading branch information
macfarla authored Sep 26, 2023
2 parents 1d02aa8 + 77b34f5 commit 190f2b8
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,9 @@ void fireNewUnverifiedForkchoiceEvent(
/**
* Put payload by Identifier.
*
* @param payloadId the payload identifier
* @param blockWithReceipts the block with receipts
* @param payloadWrapper payload wrapper
*/
void putPayloadById(final PayloadIdentifier payloadId, final BlockWithReceipts blockWithReceipts);
void putPayloadById(final PayloadWrapper payloadWrapper);

/**
* Retrieve block by id.
Expand All @@ -173,7 +172,7 @@ void fireNewUnverifiedForkchoiceEvent(
/**
* Sets is chain pruning enabled.
*
* @param isChainPruningEnabled the is chain pruning enabled
* @param isChainPruningEnabled whether chain pruning is enabled
*/
default void setIsChainPruningEnabled(final boolean isChainPruningEnabled) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.merge;

import org.hyperledger.besu.consensus.merge.blockcreation.PayloadIdentifier;
import org.hyperledger.besu.ethereum.core.BlockWithReceipts;

/** Wrapper for payload plus extra info. */
public record PayloadWrapper(
PayloadIdentifier payloadIdentifier, BlockWithReceipts blockWithReceipts) {}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class PostMergeContext implements MergeContext {
private final Subscribers<UnverifiedForkchoiceListener>
newUnverifiedForkchoiceCallbackSubscribers = Subscribers.create();

private final EvictingQueue<PayloadTuple> blocksInProgress =
private final EvictingQueue<PayloadWrapper> blocksInProgress =
EvictingQueue.create(MAX_BLOCKS_IN_PROGRESS);

// latest finalized block
Expand Down Expand Up @@ -232,27 +232,35 @@ public boolean validateCandidateHead(final BlockHeader candidateHeader) {
}

@Override
public void putPayloadById(
final PayloadIdentifier payloadId, final BlockWithReceipts newBlockWithReceipts) {
public void putPayloadById(final PayloadWrapper payloadWrapper) {
synchronized (blocksInProgress) {
final Optional<BlockWithReceipts> maybeCurrBestBlock = retrieveBlockById(payloadId);
final Optional<BlockWithReceipts> maybeCurrBestBlock =
retrieveBlockById(payloadWrapper.payloadIdentifier());

maybeCurrBestBlock.ifPresentOrElse(
currBestBlock -> {
if (compareByGasUsedDesc.compare(newBlockWithReceipts, currBestBlock) < 0) {
if (compareByGasUsedDesc.compare(payloadWrapper.blockWithReceipts(), currBestBlock)
< 0) {
LOG.atDebug()
.setMessage("New proposal for payloadId {} {} is better than the previous one {}")
.addArgument(payloadId)
.addArgument(() -> logBlockProposal(newBlockWithReceipts.getBlock()))
.addArgument(payloadWrapper.payloadIdentifier())
.addArgument(
() -> logBlockProposal(payloadWrapper.blockWithReceipts().getBlock()))
.addArgument(() -> logBlockProposal(currBestBlock.getBlock()))
.log();
blocksInProgress.removeAll(
retrieveTuplesById(payloadId).collect(Collectors.toUnmodifiableList()));
blocksInProgress.add(new PayloadTuple(payloadId, newBlockWithReceipts));
logCurrentBestBlock(newBlockWithReceipts);
retrievePayloadsById(payloadWrapper.payloadIdentifier())
.collect(Collectors.toUnmodifiableList()));
blocksInProgress.add(
new PayloadWrapper(
payloadWrapper.payloadIdentifier(), payloadWrapper.blockWithReceipts()));
logCurrentBestBlock(payloadWrapper.blockWithReceipts());
}
},
() -> blocksInProgress.add(new PayloadTuple(payloadId, newBlockWithReceipts)));
() ->
blocksInProgress.add(
new PayloadWrapper(
payloadWrapper.payloadIdentifier(), payloadWrapper.blockWithReceipts())));
}
}

Expand All @@ -276,15 +284,15 @@ private void logCurrentBestBlock(final BlockWithReceipts blockWithReceipts) {
@Override
public Optional<BlockWithReceipts> retrieveBlockById(final PayloadIdentifier payloadId) {
synchronized (blocksInProgress) {
return retrieveTuplesById(payloadId)
.map(tuple -> tuple.blockWithReceipts)
return retrievePayloadsById(payloadId)
.map(payloadWrapper -> payloadWrapper.blockWithReceipts())
.sorted(compareByGasUsedDesc)
.findFirst();
}
}

private Stream<PayloadTuple> retrieveTuplesById(final PayloadIdentifier payloadId) {
return blocksInProgress.stream().filter(z -> z.payloadIdentifier.equals(payloadId));
private Stream<PayloadWrapper> retrievePayloadsById(final PayloadIdentifier payloadId) {
return blocksInProgress.stream().filter(z -> z.payloadIdentifier().equals(payloadId));
}

private String logBlockProposal(final Block block) {
Expand All @@ -296,25 +304,6 @@ private String logBlockProposal(final Block block) {
+ block.getBody().getTransactions().size();
}

private static class PayloadTuple {
/** The Payload identifier. */
final PayloadIdentifier payloadIdentifier;
/** The Block with receipts. */
final BlockWithReceipts blockWithReceipts;

/**
* Instantiates a new Payload tuple.
*
* @param payloadIdentifier the payload identifier
* @param blockWithReceipts the block with receipts
*/
PayloadTuple(
final PayloadIdentifier payloadIdentifier, final BlockWithReceipts blockWithReceipts) {
this.payloadIdentifier = payloadIdentifier;
this.blockWithReceipts = blockWithReceipts;
}
}

@Override
public void setIsChainPruningEnabled(final boolean isChainPruningEnabled) {
this.isChainPruningEnabled = isChainPruningEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ public boolean validateCandidateHead(final BlockHeader candidateHeader) {
}

@Override
public void putPayloadById(
final PayloadIdentifier payloadId, final BlockWithReceipts blockWithReceipts) {
postMergeContext.putPayloadById(payloadId, blockWithReceipts);
public void putPayloadById(final PayloadWrapper payloadWrapper) {
postMergeContext.putPayloadById(payloadWrapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator.ForkchoiceResult.Status.INVALID;

import org.hyperledger.besu.consensus.merge.MergeContext;
import org.hyperledger.besu.consensus.merge.PayloadWrapper;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
Expand Down Expand Up @@ -289,7 +290,8 @@ public PayloadIdentifier preparePayload(
BlockProcessingResult result = validateProposedBlock(emptyBlock);
if (result.isSuccessful()) {
mergeContext.putPayloadById(
payloadIdentifier, new BlockWithReceipts(emptyBlock, result.getReceipts()));
new PayloadWrapper(
payloadIdentifier, new BlockWithReceipts(emptyBlock, result.getReceipts())));
LOG.info(
"Start building proposals for block {} identified by {}",
emptyBlock.getHeader().getNumber(),
Expand Down Expand Up @@ -444,7 +446,8 @@ private void evaluateNewBlock(
if (isBlockCreationCancelled(payloadIdentifier)) return;

mergeContext.putPayloadById(
payloadIdentifier, new BlockWithReceipts(bestBlock, resultBest.getReceipts()));
new PayloadWrapper(
payloadIdentifier, new BlockWithReceipts(bestBlock, resultBest.getReceipts())));
LOG.atDebug()
.setMessage(
"Successfully built block {} for proposal identified by {}, with {} transactions, in {}ms")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void putAndRetrieveFirstPayload() {
BlockWithReceipts mockBlockWithReceipts = createBlockWithReceipts(1, 21000, 1);

PayloadIdentifier firstPayloadId = new PayloadIdentifier(1L);
postMergeContext.putPayloadById(firstPayloadId, mockBlockWithReceipts);
postMergeContext.putPayloadById(new PayloadWrapper(firstPayloadId, mockBlockWithReceipts));

assertThat(postMergeContext.retrieveBlockById(firstPayloadId)).contains(mockBlockWithReceipts);
}
Expand All @@ -149,8 +149,8 @@ public void puttingTwoBlocksWithTheSamePayloadIdWeRetrieveTheBest() {
BlockWithReceipts betterBlockWithReceipts = createBlockWithReceipts(2, 11, 1);

PayloadIdentifier payloadId = new PayloadIdentifier(1L);
postMergeContext.putPayloadById(payloadId, zeroTxBlockWithReceipts);
postMergeContext.putPayloadById(payloadId, betterBlockWithReceipts);
postMergeContext.putPayloadById(new PayloadWrapper(payloadId, zeroTxBlockWithReceipts));
postMergeContext.putPayloadById(new PayloadWrapper(payloadId, betterBlockWithReceipts));

assertThat(postMergeContext.retrieveBlockById(payloadId)).contains(betterBlockWithReceipts);
}
Expand All @@ -162,9 +162,9 @@ public void puttingABlockWithTheSamePayloadIdSmallerThanAnExistingOneWeRetrieveT
BlockWithReceipts smallBlockWithReceipts = createBlockWithReceipts(3, 5, 1);

PayloadIdentifier payloadId = new PayloadIdentifier(1L);
postMergeContext.putPayloadById(payloadId, zeroTxBlockWithReceipts);
postMergeContext.putPayloadById(payloadId, betterBlockWithReceipts);
postMergeContext.putPayloadById(payloadId, smallBlockWithReceipts);
postMergeContext.putPayloadById(new PayloadWrapper(payloadId, zeroTxBlockWithReceipts));
postMergeContext.putPayloadById(new PayloadWrapper(payloadId, betterBlockWithReceipts));
postMergeContext.putPayloadById(new PayloadWrapper(payloadId, smallBlockWithReceipts));

assertThat(postMergeContext.retrieveBlockById(payloadId)).contains(betterBlockWithReceipts);
}
Expand Down
Loading

0 comments on commit 190f2b8

Please sign in to comment.