Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
Signed-off-by: Karim Taam <[email protected]>
  • Loading branch information
matkt committed Jul 10, 2024
1 parent 7ec17b6 commit 46c7e3a
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class CliqueProtocolSchedule {
* @param evmConfiguration the evm configuration
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol schedule
*/
public static ProtocolSchedule create(
Expand Down Expand Up @@ -126,6 +127,7 @@ public static ProtocolSchedule create(
* @param evmConfiguration the evm configuration
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol schedule
*/
@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class MigratingProtocolContext extends ProtocolContext {
* @param worldStateArchive the world state archive
* @param consensusContextSchedule the consensus context schedule
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
*/
public MigratingProtocolContext(
final MutableBlockchain blockchain,
Expand All @@ -52,6 +53,7 @@ public MigratingProtocolContext(
* @param protocolSchedule the protocol schedule
* @param consensusContextFactory the consensus context factory
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol context
*/
public static ProtocolContext init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected BaseBftProtocolScheduleBuilder() {}
* @param evmConfiguration the evm configuration
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol schedule
*/
public BftProtocolSchedule createProtocolSchedule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ private BftProtocolSchedule createProtocolSchedule(
false,
EvmConfiguration.DEFAULT,
MiningParameters.MINING_DISABLED,
new BadBlockManager());
new BadBlockManager(),
false);

return new BftProtocolSchedule(
(DefaultProtocolSchedule) protocolScheduleBuilder.createProtocolSchedule());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected IbftProtocolScheduleBuilder() {}
* @param evmConfiguration the evm configuration
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol schedule
*/
public static BftProtocolSchedule create(
Expand Down Expand Up @@ -80,6 +81,7 @@ public static BftProtocolSchedule create(
* @param evmConfiguration the evm configuration
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol schedule
*/
public static BftProtocolSchedule create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class MergeProtocolSchedule {
* @param isRevertReasonEnabled the is revert reason enabled
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol schedule
*/
public static ProtocolSchedule create(
Expand All @@ -74,6 +75,7 @@ public static ProtocolSchedule create(
* @param isRevertReasonEnabled the is revert reason enabled
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol schedule
*/
public static ProtocolSchedule create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public TransitionProtocolSchedule(
* milestone starting points
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return an initialised TransitionProtocolSchedule using post-merge defaults
*/
public static TransitionProtocolSchedule fromConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class QbftProtocolScheduleBuilder extends BaseBftProtocolScheduleBuilder
* @param evmConfiguration the evm configuration
* @param miningParameters The mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol schedule
*/
public static BftProtocolSchedule create(
Expand Down Expand Up @@ -84,6 +85,7 @@ public static BftProtocolSchedule create(
* @param evmConfiguration the evm configuration
* @param miningParameters The mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol schedule
*/
public static BftProtocolSchedule create(
Expand Down Expand Up @@ -115,6 +117,7 @@ public static BftProtocolSchedule create(
* @param bftExtraDataCodec the bft extra data codec
* @param miningParameters The mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return the protocol schedule
*/
public static ProtocolSchedule create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class MainnetProtocolSchedule {
* @param evmConfiguration how to configure the EVMs jumpdest cache
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return A configured mainnet protocol schedule
*/
public static ProtocolSchedule fromConfig(
Expand Down Expand Up @@ -82,6 +83,7 @@ public static ProtocolSchedule fromConfig(
* @param evmConfiguration how to configure the EVMs jumpdest cache
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return A configured mainnet protocol schedule
*/
public static ProtocolSchedule fromConfig(
Expand Down Expand Up @@ -109,6 +111,7 @@ public static ProtocolSchedule fromConfig(
* @param evmConfiguration size of
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return A configured mainnet protocol schedule
*/
public static ProtocolSchedule fromConfig(
Expand All @@ -134,6 +137,7 @@ public static ProtocolSchedule fromConfig(
* starting points
* @param miningParameters the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxEnabled indicates whether parallel transaction is enabled.
* @return A configured mainnet protocol schedule
*/
public static ProtocolSchedule fromConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public ParallelizedConcurrentTransactionProcessor(
this.transactionCollisionDetector = new TransactionCollisionDetector();
}

@VisibleForTesting
public ParallelizedConcurrentTransactionProcessor(
final MainnetTransactionProcessor transactionProcessor,
final TransactionCollisionDetector transactionCollisionDetector) {
this.transactionProcessor = transactionProcessor;
this.transactionCollisionDetector = transactionCollisionDetector;
}

/**
* Initiates the parallel and optimistic execution of transactions within a block by creating a
* copy of the world state for each transaction. This method processes transactions in a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static class Builder {
private DiffBasedWorldStateUpdateAccumulator<?> transactionAccumulator;
private TransactionProcessingResult transactionProcessingResult;
private boolean isMiningBeneficiaryTouchedPreRewardByTransaction;
private Wei miningBeneficiaryReward;
private Wei miningBeneficiaryReward = Wei.ZERO;

public Builder transactionAccumulator(
final DiffBasedWorldStateUpdateAccumulator<?> transactionAccumulator) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* 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.ethereum.mainnet.parallelization;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.ethereum.privacy.storage.PrivateMetadataUpdater;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.NoOpBonsaiCachedWorldStorageManager;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.NoopBonsaiCachedMerkleTrieLoader;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.common.trielog.NoOpTrieLogManager;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.DiffBasedWorldStateConfig;
import org.hyperledger.besu.ethereum.trie.diffbased.common.worldview.accumulator.DiffBasedWorldStateUpdateAccumulator;
import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.evm.operation.BlockHashOperation;
import org.hyperledger.besu.evm.tracing.OperationTracer;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;

import java.util.Collections;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class ParallelizedConcurrentTransactionProcessorTest {

@Mock private MainnetTransactionProcessor transactionProcessor;
@Mock private BlockHeader blockHeader;
@Mock private Transaction transaction;
@Mock private PrivateMetadataUpdater privateMetadataUpdater;
@Mock private TransactionCollisionDetector transactionCollisionDetector;

private BonsaiWorldState worldState;

private ParallelizedConcurrentTransactionProcessor processor;

@BeforeEach
void setUp() {
processor =
new ParallelizedConcurrentTransactionProcessor(
transactionProcessor, transactionCollisionDetector);
final BonsaiWorldStateKeyValueStorage bonsaiWorldStateKeyValueStorage =
new BonsaiWorldStateKeyValueStorage(
new InMemoryKeyValueStorageProvider(),
new NoOpMetricsSystem(),
DataStorageConfiguration.DEFAULT_BONSAI_CONFIG);
worldState =
new BonsaiWorldState(
bonsaiWorldStateKeyValueStorage,
new NoopBonsaiCachedMerkleTrieLoader(),
new NoOpBonsaiCachedWorldStorageManager(bonsaiWorldStateKeyValueStorage),
new NoOpTrieLogManager(),
EvmConfiguration.DEFAULT,
new DiffBasedWorldStateConfig());
when(transactionCollisionDetector.hasCollision(any(), any(), any(), any())).thenReturn(false);
}

@Test
void testRunTransaction() {
Address miningBeneficiary = Address.fromHexString("0x1");
Wei blobGasPrice = Wei.ZERO;

Mockito.when(
transactionProcessor.processTransaction(
any(), any(), any(), any(), any(), any(), anyBoolean(), any(), any(), any()))
.thenReturn(
TransactionProcessingResult.successful(
Collections.emptyList(), 0, 0, Bytes.EMPTY, ValidationResult.valid()));

processor.runTransaction(
worldState,
blockHeader,
0,
transaction,
miningBeneficiary,
(blockNumber) -> Hash.EMPTY,
blobGasPrice,
privateMetadataUpdater);

verify(transactionProcessor, times(1))
.processTransaction(
any(DiffBasedWorldStateUpdateAccumulator.class),
eq(blockHeader),
eq(transaction),
eq(miningBeneficiary),
any(OperationTracer.class),
any(BlockHashOperation.BlockHashLookup.class),
eq(true),
eq(TransactionValidationParams.processingBlock()),
eq(privateMetadataUpdater),
eq(blobGasPrice));

assertTrue(
processor
.applyParallelizedTransactionResult(worldState, miningBeneficiary, transaction, 0)
.isPresent(),
"Expected the transaction context to be stored");
}

@Test
void testRunTransactionWithFailure() {
Address miningBeneficiary = Address.fromHexString("0x1");
Wei blobGasPrice = Wei.ZERO;

when(transactionProcessor.processTransaction(
any(), any(), any(), any(), any(), any(), anyBoolean(), any(), any(), any()))
.thenReturn(
TransactionProcessingResult.failed(
0,
0,
ValidationResult.invalid(
TransactionInvalidReason.BLOB_GAS_PRICE_BELOW_CURRENT_BLOB_BASE_FEE),
Optional.of(Bytes.EMPTY)));

processor.runTransaction(
worldState,
blockHeader,
0,
transaction,
miningBeneficiary,
(blockNumber) -> Hash.EMPTY,
blobGasPrice,
privateMetadataUpdater);

Optional<TransactionProcessingResult> result =
processor.applyParallelizedTransactionResult(worldState, miningBeneficiary, transaction, 0);
assertTrue(result.isEmpty(), "Expected the transaction result to indicate a failure");
}

@Test
void testRunTransactionWithConflict() {

Address miningBeneficiary = Address.fromHexString("0x1");
Wei blobGasPrice = Wei.ZERO;

Mockito.when(
transactionProcessor.processTransaction(
any(), any(), any(), any(), any(), any(), anyBoolean(), any(), any(), any()))
.thenReturn(
TransactionProcessingResult.successful(
Collections.emptyList(), 0, 0, Bytes.EMPTY, ValidationResult.valid()));

processor.runTransaction(
worldState,
blockHeader,
0,
transaction,
miningBeneficiary,
(blockNumber) -> Hash.EMPTY,
blobGasPrice,
privateMetadataUpdater);

verify(transactionProcessor, times(1))
.processTransaction(
any(DiffBasedWorldStateUpdateAccumulator.class),
eq(blockHeader),
eq(transaction),
eq(miningBeneficiary),
any(OperationTracer.class),
any(BlockHashOperation.BlockHashLookup.class),
eq(true),
eq(TransactionValidationParams.processingBlock()),
eq(privateMetadataUpdater),
eq(blobGasPrice));

// simulate a conflict
when(transactionCollisionDetector.hasCollision(any(), any(), any(), any())).thenReturn(true);

Optional<TransactionProcessingResult> result =
processor.applyParallelizedTransactionResult(worldState, miningBeneficiary, transaction, 0);
assertTrue(result.isEmpty(), "Expected no transaction result to be applied due to conflict");
}
}
Loading

0 comments on commit 46c7e3a

Please sign in to comment.