Skip to content

Commit 67a9b7f

Browse files
committed
Build block before import in reference tests
Signed-off-by: Miroslav Kovar <[email protected]>
1 parent f1e4953 commit 67a9b7f

File tree

8 files changed

+291
-26
lines changed

8 files changed

+291
-26
lines changed

ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public BlockCreationResult createBlock(
178178
parentHeader);
179179
}
180180

181-
protected BlockCreationResult createBlock(
181+
public BlockCreationResult createBlock(
182182
final Optional<List<Transaction>> maybeTransactions,
183183
final Optional<List<BlockHeader>> maybeOmmers,
184184
final Optional<List<Withdrawal>> maybeWithdrawals,

ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockBody.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,13 @@ public boolean equals(final Object o) {
185185
BlockBody blockBody = (BlockBody) o;
186186
return Objects.equals(transactions, blockBody.transactions)
187187
&& Objects.equals(ommers, blockBody.ommers)
188-
&& Objects.equals(withdrawals, blockBody.withdrawals);
188+
&& Objects.equals(withdrawals, blockBody.withdrawals)
189+
&& Objects.equals(blockAccessList, blockBody.blockAccessList);
189190
}
190191

191192
@Override
192193
public int hashCode() {
193-
return Objects.hash(transactions, ommers, withdrawals);
194+
return Objects.hash(transactions, ommers, withdrawals, blockAccessList);
194195
}
195196

196197
public boolean isEmpty() {

ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockHeaderBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public class BlockHeaderBuilder {
6666
private long gasUsed = -1L;
6767

6868
private long timestamp = -1L;
69+
// Some reference tests are setting a negative timestamp so we can't use the sign to decide
70+
// whether a timestamp has been set
71+
private boolean timestampSet = false;
6972

7073
private Bytes extraData;
7174

@@ -260,7 +263,7 @@ public BlockHeader buildBlockHeader() {
260263
number,
261264
gasLimit,
262265
gasUsed,
263-
timestamp < 0 ? Instant.now().getEpochSecond() : timestamp,
266+
timestampSet ? timestamp : Instant.now().getEpochSecond(),
264267
extraData,
265268
baseFee,
266269
mixHashOrPrevRandao,
@@ -450,6 +453,7 @@ public BlockHeaderBuilder gasUsed(final long gasUsed) {
450453

451454
public BlockHeaderBuilder timestamp(final long timestamp) {
452455
this.timestamp = timestamp;
456+
this.timestampSet = true;
453457
return this;
454458
}
455459

ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/block/access/list/BlockAccessList.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.HashMap;
3030
import java.util.List;
3131
import java.util.Map;
32+
import java.util.Objects;
3233
import java.util.Optional;
3334
import java.util.Set;
3435
import java.util.TreeMap;
@@ -51,6 +52,23 @@ public List<AccountChanges> getAccountChanges() {
5152
return accountChanges;
5253
}
5354

55+
@Override
56+
public boolean equals(final Object o) {
57+
if (this == o) {
58+
return true;
59+
}
60+
if (!(o instanceof BlockAccessList)) {
61+
return false;
62+
}
63+
final BlockAccessList that = (BlockAccessList) o;
64+
return Objects.equals(accountChanges, that.accountChanges);
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return Objects.hash(accountChanges);
70+
}
71+
5472
public void writeTo(final RLPOutput out) {
5573
BlockAccessListEncoder.encode(this, out);
5674
}
@@ -319,7 +337,7 @@ void addStorageRead(final StorageSlotKey slot) {
319337
}
320338

321339
void addBalanceChange(final int txIndex, final Bytes postBalance) {
322-
balances.add(new BalanceChange(txIndex, postBalance));
340+
balances.add(new BalanceChange(txIndex, postBalance.trimLeadingZeros()));
323341
}
324342

325343
void addNonceChange(final int txIndex, final long newNonce) {

ethereum/referencetests/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ dependencies {
186186
implementation project(':crypto:algorithms')
187187
implementation project(':datatypes')
188188
implementation project(':ethereum:core')
189+
implementation project(':ethereum:eth')
190+
implementation project(':ethereum:blockcreation')
191+
implementation project(':consensus:merge')
189192
implementation project(':metrics:core')
190193
implementation project(':util')
191194
implementation project(path: ':ethereum:core', configuration: 'testSupportArtifacts')
@@ -202,6 +205,9 @@ dependencies {
202205
referenceTestImplementation project(path: ':config')
203206
referenceTestImplementation project(path: ':datatypes')
204207
referenceTestImplementation project(path: ':ethereum:core')
208+
referenceTestImplementation project(path: ':ethereum:eth')
209+
referenceTestImplementation project(path: ':ethereum:blockcreation')
210+
referenceTestImplementation project(path: ':consensus:merge')
205211
referenceTestImplementation project(path: ':metrics:core')
206212
referenceTestImplementation project(path: ':ethereum:core', configuration: 'testSupportArtifacts')
207213
referenceTestImplementation project(path: ':ethereum:rlp')

ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/ReferenceTestProtocolSchedules.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Locale;
3535
import java.util.Map;
3636
import java.util.Optional;
37+
import java.util.Set;
3738
import java.util.function.Function;
3839
import java.util.stream.Collectors;
3940

@@ -44,6 +45,17 @@ public class ReferenceTestProtocolSchedules {
4445
private static final List<String> SPECS_PRIOR_TO_DELETING_EMPTY_ACCOUNTS =
4546
Arrays.asList("Frontier", "Homestead", "EIP150");
4647

48+
private static final Set<String> FORKS_WITHOUT_BLOCK_BUILDING =
49+
Set.of(
50+
"frontier",
51+
"frontiertohomesteadat5",
52+
"homestead",
53+
"homesteartoeip150at5",
54+
"homesteadtodaoat5",
55+
"eip150",
56+
"eip158",
57+
"eip158tobyzantiumat5");
58+
4759
private static ReferenceTestProtocolSchedules instance;
4860

4961
public static ReferenceTestProtocolSchedules getInstance() {
@@ -159,4 +171,8 @@ private static ProtocolSchedule createSchedule(final GenesisConfigOptions option
159171
public static boolean shouldClearEmptyAccounts(final String fork) {
160172
return !SPECS_PRIOR_TO_DELETING_EMPTY_ACCOUNTS.contains(fork);
161173
}
174+
175+
public static boolean supportsBlockBuilding(final String fork) {
176+
return !FORKS_WITHOUT_BLOCK_BUILDING.contains(fork.toLowerCase(Locale.ROOT));
177+
}
162178
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright contributors to Hyperledger Besu.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
package org.hyperledger.besu.consensus.merge.blockcreation;
16+
17+
import org.hyperledger.besu.ethereum.ProtocolContext;
18+
import org.hyperledger.besu.ethereum.blockcreation.AbstractBlockCreator;
19+
import org.hyperledger.besu.ethereum.core.Block;
20+
import org.hyperledger.besu.ethereum.core.BlockHeader;
21+
import org.hyperledger.besu.ethereum.core.MiningConfiguration;
22+
import org.hyperledger.besu.ethereum.core.Transaction;
23+
import org.hyperledger.besu.ethereum.core.Withdrawal;
24+
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
25+
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
26+
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
27+
28+
import java.util.List;
29+
import java.util.Optional;
30+
31+
import org.apache.tuweni.bytes.Bytes32;
32+
33+
/** Utility class exposing {@link MergeBlockCreator} functionality for reference tests. */
34+
public final class ReferenceTestMergeBlockCreator {
35+
36+
private ReferenceTestMergeBlockCreator() {}
37+
38+
public static Block createBlock(
39+
final MiningConfiguration miningConfiguration,
40+
final AbstractBlockCreator.ExtraDataCalculator extraDataCalculator,
41+
final TransactionPool transactionPool,
42+
final ProtocolContext protocolContext,
43+
final ProtocolSchedule protocolSchedule,
44+
final BlockHeader parentHeader,
45+
final EthScheduler ethScheduler,
46+
final Optional<List<Transaction>> transactions,
47+
final Optional<List<BlockHeader>> ommers,
48+
final Bytes32 random,
49+
final long timestamp,
50+
final Optional<List<Withdrawal>> withdrawals,
51+
final Optional<Bytes32> parentBeaconBlockRoot) {
52+
53+
return new MergeBlockCreator(
54+
miningConfiguration,
55+
extraDataCalculator,
56+
transactionPool,
57+
protocolContext,
58+
protocolSchedule,
59+
parentHeader,
60+
ethScheduler)
61+
.createBlock(
62+
transactions,
63+
ommers,
64+
withdrawals,
65+
Optional.of(random),
66+
parentBeaconBlockRoot,
67+
timestamp,
68+
true,
69+
parentHeader)
70+
.getBlock();
71+
}
72+
}

0 commit comments

Comments
 (0)