Skip to content

Commit 955bf9e

Browse files
macfarlaclaude
andauthored
BlockchainUtilParameterizedTest - reduce key-pair generations (#10072)
* reduce key-pair generations Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com> * Pre-size ArrayList instances in BlockchainUtilParameterizedTest Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com> --------- Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f48338c commit 955bf9e

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/BlockchainUtilParameterizedTest.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.stream.Stream;
3434

3535
import org.junit.jupiter.api.BeforeAll;
36+
import org.junit.jupiter.api.BeforeEach;
3637
import org.junit.jupiter.api.Test;
3738
import org.junit.jupiter.params.ParameterizedTest;
3839
import org.junit.jupiter.params.provider.Arguments;
@@ -44,7 +45,13 @@ public class BlockchainUtilParameterizedTest {
4445

4546
private static final int chainHeight = 89;
4647
private static Block genesisBlock;
47-
private static MutableBlockchain localBlockchain;
48+
// Pre-generated canonical chain data (crypto happens once in @BeforeAll)
49+
private static final List<Block> canonicalBlocks = new ArrayList<>(chainHeight);
50+
private static final List<List<TransactionReceipt>> canonicalReceipts =
51+
new ArrayList<>(chainHeight);
52+
53+
// Rebuilt cheaply from canonical data before each test — never accumulates fork chains
54+
private MutableBlockchain localBlockchain;
4855

4956
private MutableBlockchain remoteBlockchain;
5057

@@ -54,16 +61,27 @@ public class BlockchainUtilParameterizedTest {
5461
@BeforeAll
5562
public static void setupClass() {
5663
genesisBlock = blockDataGenerator.genesisBlock();
57-
localBlockchain = InMemoryKeyValueStorageProvider.createInMemoryBlockchain(genesisBlock);
58-
// Setup local chain.
64+
// Use a temporary chain only to thread parent hashes; store blocks for reuse
65+
final MutableBlockchain tempChain =
66+
InMemoryKeyValueStorageProvider.createInMemoryBlockchain(genesisBlock);
5967
for (int i = 1; i <= chainHeight; i++) {
6068
final BlockDataGenerator.BlockOptions options =
6169
new BlockDataGenerator.BlockOptions()
6270
.setBlockNumber(i)
63-
.setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get());
71+
.setParentHash(tempChain.getBlockHashByNumber(i - 1).get());
6472
final Block block = blockDataGenerator.block(options);
6573
final List<TransactionReceipt> receipts = blockDataGenerator.receipts(block);
66-
localBlockchain.appendBlock(block, receipts);
74+
tempChain.appendBlock(block, receipts);
75+
canonicalBlocks.add(block);
76+
canonicalReceipts.add(receipts);
77+
}
78+
}
79+
80+
@BeforeEach
81+
public void setupInstance() {
82+
localBlockchain = InMemoryKeyValueStorageProvider.createInMemoryBlockchain(genesisBlock);
83+
for (int i = 0; i < canonicalBlocks.size(); i++) {
84+
localBlockchain.appendBlock(canonicalBlocks.get(i), canonicalReceipts.get(i));
6785
}
6886
}
6987

@@ -78,16 +96,9 @@ public void setup(final int commonAncestorHeight) {
7896
final BlockBody commonBody = localBlockchain.getBlockBody(commonHeader.getHash()).get();
7997
remoteBlockchain.appendBlock(new Block(commonHeader, commonBody), receipts);
8098
}
81-
// Remaining blocks are disparate.
99+
// Remaining blocks are disparate on the remote chain only.
100+
// Local canonical chain already has blocks up to chainHeight from @BeforeEach.
82101
for (long i = commonAncestorHeight + 1L; i <= chainHeight; i++) {
83-
final BlockDataGenerator.BlockOptions localOptions =
84-
new BlockDataGenerator.BlockOptions()
85-
.setBlockNumber(i)
86-
.setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get());
87-
final Block localBlock = blockDataGenerator.block(localOptions);
88-
final List<TransactionReceipt> localReceipts = blockDataGenerator.receipts(localBlock);
89-
localBlockchain.appendBlock(localBlock, localReceipts);
90-
91102
final BlockDataGenerator.BlockOptions remoteOptions =
92103
new BlockDataGenerator.BlockOptions()
93104
.setDifficulty(Difficulty.ONE) // differentiator
@@ -97,7 +108,7 @@ public void setup(final int commonAncestorHeight) {
97108
final List<TransactionReceipt> remoteReceipts = blockDataGenerator.receipts(remoteBlock);
98109
remoteBlockchain.appendBlock(remoteBlock, remoteReceipts);
99110
}
100-
headers = new ArrayList<>();
111+
headers = new ArrayList<>(chainHeight + 1);
101112
for (long i = 0L; i <= remoteBlockchain.getChainHeadBlockNumber(); i++) {
102113
headers.add(remoteBlockchain.getBlockHeader(i).get());
103114
}

0 commit comments

Comments
 (0)