Skip to content
This repository was archived by the owner on Jul 1, 2025. It is now read-only.

Commit 5c7e6d6

Browse files
fab-10garyschulte
authored andcommitted
Precompute authorities when importing blocks (#8017)
* Precompute authorities when importing blocks Signed-off-by: Fabio Di Fabio <[email protected]> * Using Supplier to make the authorizer thread safe Signed-off-by: Fabio Di Fabio <[email protected]> * Process code delegation in parallel if there are more that one Signed-off-by: Fabio Di Fabio <[email protected]> --------- Signed-off-by: Fabio Di Fabio <[email protected]>
1 parent 3569554 commit 5c7e6d6

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/engine/AbstractEngineNewPayload.java

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.engine.WithdrawalsValidatorProvider.getWithdrawalsValidator;
2525

2626
import org.hyperledger.besu.consensus.merge.blockcreation.MergeMiningCoordinator;
27-
import org.hyperledger.besu.datatypes.Address;
2827
import org.hyperledger.besu.datatypes.BlobGas;
2928
import org.hyperledger.besu.datatypes.Hash;
3029
import org.hyperledger.besu.datatypes.RequestType;
@@ -222,20 +221,8 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
222221
blockParam.getTransactions().stream()
223222
.map(Bytes::fromHexString)
224223
.map(in -> TransactionDecoder.decodeOpaqueBytes(in, EncodingContext.BLOCK_BODY))
225-
.collect(Collectors.toList());
226-
transactions.forEach(
227-
transaction ->
228-
mergeCoordinator
229-
.getEthScheduler()
230-
.scheduleTxWorkerTask(
231-
() -> {
232-
Address sender = transaction.getSender();
233-
LOG.atTrace()
234-
.setMessage("The sender for transaction {} is calculated : {}")
235-
.addArgument(transaction::getHash)
236-
.addArgument(sender)
237-
.log();
238-
}));
224+
.toList();
225+
precomputeSenders(transactions);
239226
} catch (final RLPException | IllegalArgumentException e) {
240227
return respondWithInvalid(
241228
reqId,
@@ -392,6 +379,47 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
392379
}
393380
}
394381

382+
private void precomputeSenders(final List<Transaction> transactions) {
383+
transactions.forEach(
384+
transaction -> {
385+
mergeCoordinator
386+
.getEthScheduler()
387+
.scheduleTxWorkerTask(
388+
() -> {
389+
final var sender = transaction.getSender();
390+
LOG.atTrace()
391+
.setMessage("The sender for transaction {} is calculated : {}")
392+
.addArgument(transaction::getHash)
393+
.addArgument(sender)
394+
.log();
395+
});
396+
if (transaction.getType().supportsDelegateCode()) {
397+
precomputeAuthorities(transaction);
398+
}
399+
});
400+
}
401+
402+
private void precomputeAuthorities(final Transaction transaction) {
403+
final var codeDelegations = transaction.getCodeDelegationList().get();
404+
int index = 0;
405+
for (final var codeDelegation : codeDelegations) {
406+
final var constIndex = index++;
407+
mergeCoordinator
408+
.getEthScheduler()
409+
.scheduleTxWorkerTask(
410+
() -> {
411+
final var authority = codeDelegation.authorizer();
412+
LOG.atTrace()
413+
.setMessage(
414+
"The code delegation authority at index {} for transaction {} is calculated : {}")
415+
.addArgument(constIndex)
416+
.addArgument(transaction::getHash)
417+
.addArgument(authority)
418+
.log();
419+
});
420+
}
421+
}
422+
395423
JsonRpcResponse respondWith(
396424
final Object requestId,
397425
final EnginePayloadParameter param,

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public class CodeDelegation implements org.hyperledger.besu.datatypes.CodeDelega
4242
private final Address address;
4343
private final long nonce;
4444
private final SECPSignature signature;
45-
private Optional<Address> authorizer = Optional.empty();
46-
private boolean isAuthorityComputed = false;
45+
private final Supplier<Optional<Address>> authorizerSupplier =
46+
Suppliers.memoize(this::computeAuthority);
4747

4848
/**
4949
* An access list entry as defined in EIP-7702
@@ -107,12 +107,7 @@ public SECPSignature signature() {
107107

108108
@Override
109109
public Optional<Address> authorizer() {
110-
if (!isAuthorityComputed) {
111-
authorizer = computeAuthority();
112-
isAuthorityComputed = true;
113-
}
114-
115-
return authorizer;
110+
return authorizerSupplier.get();
116111
}
117112

118113
@Override

ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/PendingTransaction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ public interface MemorySize {
430430
int ACCESS_LIST_ENTRY_SHALLOW_SIZE = 248;
431431
int OPTIONAL_ACCESS_LIST_SHALLOW_SIZE = 40;
432432
int OPTIONAL_CODE_DELEGATION_LIST_SHALLOW_SIZE = 40;
433-
int CODE_DELEGATION_ENTRY_SIZE = 432;
433+
int CODE_DELEGATION_ENTRY_SIZE = 472;
434434
int VERSIONED_HASH_SIZE = 96;
435435
int LIST_SHALLOW_SIZE = 48;
436436
int OPTIONAL_SHALLOW_SIZE = 16;

0 commit comments

Comments
 (0)