From 95248916b0b24012c233202565b7caee8efcdcbb Mon Sep 17 00:00:00 2001 From: Sally MacFarlane Date: Thu, 19 Mar 2026 13:52:15 +1000 Subject: [PATCH 1/3] reduce key-pair generations Signed-off-by: Sally MacFarlane --- .../util/BlockchainUtilParameterizedTest.java | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java index af673d10d53..29976a03fac 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java @@ -33,6 +33,7 @@ import java.util.stream.Stream; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -44,7 +45,12 @@ public class BlockchainUtilParameterizedTest { private static final int chainHeight = 89; private static Block genesisBlock; - private static MutableBlockchain localBlockchain; + // Pre-generated canonical chain data (crypto happens once in @BeforeAll) + private static final List canonicalBlocks = new ArrayList<>(); + private static final List> canonicalReceipts = new ArrayList<>(); + + // Rebuilt cheaply from canonical data before each test — never accumulates fork chains + private MutableBlockchain localBlockchain; private MutableBlockchain remoteBlockchain; @@ -54,16 +60,27 @@ public class BlockchainUtilParameterizedTest { @BeforeAll public static void setupClass() { genesisBlock = blockDataGenerator.genesisBlock(); - localBlockchain = InMemoryKeyValueStorageProvider.createInMemoryBlockchain(genesisBlock); - // Setup local chain. + // Use a temporary chain only to thread parent hashes; store blocks for reuse + final MutableBlockchain tempChain = + InMemoryKeyValueStorageProvider.createInMemoryBlockchain(genesisBlock); for (int i = 1; i <= chainHeight; i++) { final BlockDataGenerator.BlockOptions options = new BlockDataGenerator.BlockOptions() .setBlockNumber(i) - .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); + .setParentHash(tempChain.getBlockHashByNumber(i - 1).get()); final Block block = blockDataGenerator.block(options); final List receipts = blockDataGenerator.receipts(block); - localBlockchain.appendBlock(block, receipts); + tempChain.appendBlock(block, receipts); + canonicalBlocks.add(block); + canonicalReceipts.add(receipts); + } + } + + @BeforeEach + public void setupInstance() { + localBlockchain = InMemoryKeyValueStorageProvider.createInMemoryBlockchain(genesisBlock); + for (int i = 0; i < canonicalBlocks.size(); i++) { + localBlockchain.appendBlock(canonicalBlocks.get(i), canonicalReceipts.get(i)); } } @@ -78,16 +95,9 @@ public void setup(final int commonAncestorHeight) { final BlockBody commonBody = localBlockchain.getBlockBody(commonHeader.getHash()).get(); remoteBlockchain.appendBlock(new Block(commonHeader, commonBody), receipts); } - // Remaining blocks are disparate. + // Remaining blocks are disparate on the remote chain only. + // Local canonical chain already has blocks up to chainHeight from @BeforeEach. for (long i = commonAncestorHeight + 1L; i <= chainHeight; i++) { - final BlockDataGenerator.BlockOptions localOptions = - new BlockDataGenerator.BlockOptions() - .setBlockNumber(i) - .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); - final Block localBlock = blockDataGenerator.block(localOptions); - final List localReceipts = blockDataGenerator.receipts(localBlock); - localBlockchain.appendBlock(localBlock, localReceipts); - final BlockDataGenerator.BlockOptions remoteOptions = new BlockDataGenerator.BlockOptions() .setDifficulty(Difficulty.ONE) // differentiator From c95d973f1c183a710afacda9a42a9813628a168f Mon Sep 17 00:00:00 2001 From: Sally MacFarlane Date: Sat, 21 Mar 2026 06:26:24 +1000 Subject: [PATCH 2/3] Pre-size ArrayList instances in BlockchainUtilParameterizedTest Co-Authored-By: Claude Sonnet 4.6 Signed-off-by: Sally MacFarlane --- .../besu/ethereum/util/BlockchainUtilParameterizedTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java index 29976a03fac..9a41072e672 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java @@ -46,8 +46,8 @@ public class BlockchainUtilParameterizedTest { private static final int chainHeight = 89; private static Block genesisBlock; // Pre-generated canonical chain data (crypto happens once in @BeforeAll) - private static final List canonicalBlocks = new ArrayList<>(); - private static final List> canonicalReceipts = new ArrayList<>(); + private static final List canonicalBlocks = new ArrayList<>(chainHeight); + private static final List> canonicalReceipts = new ArrayList<>(chainHeight); // Rebuilt cheaply from canonical data before each test — never accumulates fork chains private MutableBlockchain localBlockchain; @@ -107,7 +107,7 @@ public void setup(final int commonAncestorHeight) { final List remoteReceipts = blockDataGenerator.receipts(remoteBlock); remoteBlockchain.appendBlock(remoteBlock, remoteReceipts); } - headers = new ArrayList<>(); + headers = new ArrayList<>(chainHeight + 1); for (long i = 0L; i <= remoteBlockchain.getChainHeadBlockNumber(); i++) { headers.add(remoteBlockchain.getBlockHeader(i).get()); } From ffe1090bc7103d59105fd1e9d24e522c28f2da7d Mon Sep 17 00:00:00 2001 From: Sally MacFarlane Date: Sat, 21 Mar 2026 06:29:30 +1000 Subject: [PATCH 3/3] formatting Signed-off-by: Sally MacFarlane --- .../besu/ethereum/util/BlockchainUtilParameterizedTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java index 9a41072e672..5b4cf38a126 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java @@ -47,7 +47,8 @@ public class BlockchainUtilParameterizedTest { private static Block genesisBlock; // Pre-generated canonical chain data (crypto happens once in @BeforeAll) private static final List canonicalBlocks = new ArrayList<>(chainHeight); - private static final List> canonicalReceipts = new ArrayList<>(chainHeight); + private static final List> canonicalReceipts = + new ArrayList<>(chainHeight); // Rebuilt cheaply from canonical data before each test — never accumulates fork chains private MutableBlockchain localBlockchain;