Skip to content

Commit

Permalink
Merge branch 'main' into bug/issue-8135/engine_api_milestones_zero
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellehrner committed Jan 22, 2025
2 parents 2a47bfb + e9a94f3 commit e7833b8
Show file tree
Hide file tree
Showing 23 changed files with 261 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void tearDown() {
public void shouldTransferAllEthOfAuthorizerToSponsor() throws IOException {

// 7702 transaction
final CodeDelegation authorization =
final CodeDelegation codeDelegation =
org.hyperledger.besu.ethereum.core.CodeDelegation.builder()
.chainId(BigInteger.valueOf(20211))
.address(SEND_ALL_ETH_CONTRACT_ADDRESS)
Expand All @@ -108,7 +108,7 @@ public void shouldTransferAllEthOfAuthorizerToSponsor() throws IOException {
.value(Wei.ZERO)
.payload(Bytes32.leftPad(Bytes.fromHexString(transactionSponsor.getAddress())))
.accessList(List.of())
.codeDelegations(List.of(authorization))
.codeDelegations(List.of(codeDelegation))
.signAndBuild(
secp256k1.createKeyPair(
secp256k1.createPrivateKey(
Expand Down Expand Up @@ -143,7 +143,7 @@ public void shouldCheckNonceAfterNonceIncreaseOfSender() throws IOException {
final long GAS_LIMIT = 1_000_000L;
cluster.verify(authorizer.balanceEquals(Amount.ether(90_000)));

final CodeDelegation authorization =
final CodeDelegation codeDelegation =
org.hyperledger.besu.ethereum.core.CodeDelegation.builder()
.chainId(BigInteger.valueOf(20211))
.nonce(
Expand All @@ -166,7 +166,7 @@ public void shouldCheckNonceAfterNonceIncreaseOfSender() throws IOException {
.value(Wei.ZERO)
.payload(Bytes32.leftPad(Bytes.fromHexString(otherAccount.getAddress())))
.accessList(List.of())
.codeDelegations(List.of(authorization))
.codeDelegations(List.of(codeDelegation))
.signAndBuild(
secp256k1.createKeyPair(
secp256k1.createPrivateKey(AUTHORIZER_PRIVATE_KEY.toUnsignedBigInteger())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ default Optional<? extends Quantity> getMaxFeePerBlobGas() {
Optional<List<CodeDelegation>> getCodeDelegationList();

/**
* Returns the size of the authorization list.
* Returns the size of the code delegation list.
*
* @return the size of the authorization list
* @return the size of the code delegation list
*/
int codeDelegationListSize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ public CodeDelegationResult process(
.get()
.forEach(
codeDelegation ->
processAuthorization(
processCodeDelegation(
evmWorldUpdater,
(org.hyperledger.besu.ethereum.core.CodeDelegation) codeDelegation,
result));

return result;
}

private void processAuthorization(
private void processCodeDelegation(
final EVMWorldUpdater evmWorldUpdater,
final CodeDelegation codeDelegation,
final CodeDelegationResult result) {
Expand Down Expand Up @@ -130,7 +130,7 @@ private void processAuthorization(
} else {
authority = maybeAuthorityAccount.get();

if (!evmWorldUpdater.authorizedCodeService().canSetDelegatedCode(authority)) {
if (!evmWorldUpdater.codeDelegationService().canSetCodeDelegation(authority)) {
return;
}

Expand All @@ -150,8 +150,8 @@ private void processAuthorization(
}

evmWorldUpdater
.authorizedCodeService()
.processDelegatedCodeAuthorization(authority, codeDelegation.address());
.codeDelegationService()
.processCodeDelegation(authority, codeDelegation.address());
authority.incrementNonce();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ public TransactionProcessingResult processTransaction(
final Address to = transaction.getTo().get();
final Optional<Account> maybeContract = Optional.ofNullable(evmWorldUpdater.get(to));

if (maybeContract.isPresent() && maybeContract.get().hasDelegatedCode()) {
warmAddressList.add(maybeContract.get().delegatedCodeAddress().get());
if (maybeContract.isPresent() && maybeContract.get().hasCodeDelegation()) {
warmAddressList.add(maybeContract.get().codeDelegationAddress().get());
}

initialFrame =
Expand All @@ -441,9 +441,10 @@ public TransactionProcessingResult processTransaction(
maybeContract
.map(
c -> {
if (c.hasDelegatedCode()) {
if (c.hasCodeDelegation()) {
return messageCallProcessor.getCodeFromEVM(
c.getDelegatedCodeHash().get(), c.getDelegatedCode().get());
c.getCodeDelegationTargetHash().get(),
c.getCodeDelegationTargetCode().get());
}

return messageCallProcessor.getCodeFromEVM(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import static org.hyperledger.besu.evm.account.Account.MAX_NONCE;
import static org.hyperledger.besu.evm.internal.Words.clampedAdd;
import static org.hyperledger.besu.evm.worldstate.DelegateCodeHelper.hasDelegatedCode;
import static org.hyperledger.besu.evm.worldstate.CodeDelegationHelper.hasCodeDelegation;

import org.hyperledger.besu.crypto.SECPSignature;
import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
Expand Down Expand Up @@ -333,7 +333,7 @@ public ValidationResult<TransactionInvalidReason> validateForSender(
}

private static boolean canSendTransaction(final Account sender, final Hash codeHash) {
return codeHash.equals(Hash.EMPTY) || hasDelegatedCode(sender.getCode());
return codeHash.equals(Hash.EMPTY) || hasCodeDelegation(sender.getCode());
}

private ValidationResult<TransactionInvalidReason> validateTransactionSignature(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ void shouldDecodeInnerPayloadWithNonce() {
Bytes.fromHexString(
"0xf85a0194633688abc3ccf8b0c03088d2d1c6ae4958c2fa562a80a0840798fa67118e034c1eb7e42fe89e28d7cd5006dc813d5729e5f75b0d1a7ec5a03b1dbace38ceb862a65bf2eac0637693b5c3493bcb2a022dd614c0a74cce0b99"),
true);
final CodeDelegation authorization = CodeDelegationTransactionDecoder.decodeInnerPayload(input);
final CodeDelegation codeDelegation =
CodeDelegationTransactionDecoder.decodeInnerPayload(input);

assertThat(authorization.chainId()).isEqualTo(BigInteger.ONE);
assertThat(authorization.address())
assertThat(codeDelegation.chainId()).isEqualTo(BigInteger.ONE);
assertThat(codeDelegation.address())
.isEqualTo(Address.fromHexStringStrict("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56"));
assertThat(authorization.nonce()).isEqualTo(42);
assertThat(codeDelegation.nonce()).isEqualTo(42);

final SECPSignature signature = authorization.signature();
final SECPSignature signature = codeDelegation.signature();
assertThat(signature.getRecId()).isEqualTo((byte) 0);
assertThat(signature.getR().toString(16))
.isEqualTo("840798fa67118e034c1eb7e42fe89e28d7cd5006dc813d5729e5f75b0d1a7ec5");
Expand All @@ -61,14 +62,15 @@ void shouldDecodeInnerPayloadWithNonceZero() {
Bytes.fromHexString(
"0xf85a0194633688abc3ccf8b0c03088d2d1c6ae4958c2fa568001a0dd6b24048be1b7d7fe5bbbb73ffc37eb2ce1997ecb4ae5b6096532ef19363148a025b58a1ff8ad00bddbbfa1d5c2411961cbb6d08dcdc8ae88303db3c6cf983031"),
true);
final CodeDelegation authorization = CodeDelegationTransactionDecoder.decodeInnerPayload(input);
final CodeDelegation codeDelegation =
CodeDelegationTransactionDecoder.decodeInnerPayload(input);

assertThat(authorization.chainId()).isEqualTo(BigInteger.ONE);
assertThat(authorization.address())
assertThat(codeDelegation.chainId()).isEqualTo(BigInteger.ONE);
assertThat(codeDelegation.address())
.isEqualTo(Address.fromHexStringStrict("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56"));
assertThat(authorization.nonce()).isEqualTo(0);
assertThat(codeDelegation.nonce()).isEqualTo(0);

final SECPSignature signature = authorization.signature();
final SECPSignature signature = codeDelegation.signature();
assertThat(signature.getRecId()).isEqualTo((byte) 1);
assertThat(signature.getR().toString(16))
.isEqualTo("dd6b24048be1b7d7fe5bbbb73ffc37eb2ce1997ecb4ae5b6096532ef19363148");
Expand All @@ -85,14 +87,15 @@ void shouldDecodeInnerPayloadWithChainIdZero() {
Bytes.fromHexString(
"0xf85a8094633688abc3ccf8b0c03088d2d1c6ae4958c2fa560501a0025c1240d7ffec0daeedb752d3357aff2e3cd58468f0c2d43ee0ee999e02ace2a03c8a25b2becd6e666f69803d1ae3322f2e137b7745c2c7f19da80f993ffde4df"),
true);
final CodeDelegation authorization = CodeDelegationTransactionDecoder.decodeInnerPayload(input);
final CodeDelegation codeDelegation =
CodeDelegationTransactionDecoder.decodeInnerPayload(input);

assertThat(authorization.chainId()).isEqualTo(BigInteger.ZERO);
assertThat(authorization.address())
assertThat(codeDelegation.chainId()).isEqualTo(BigInteger.ZERO);
assertThat(codeDelegation.address())
.isEqualTo(Address.fromHexStringStrict("0x633688abc3cCf8B0C03088D2d1C6ae4958c2fA56"));
assertThat(authorization.nonce()).isEqualTo(5);
assertThat(codeDelegation.nonce()).isEqualTo(5);

final SECPSignature signature = authorization.signature();
final SECPSignature signature = codeDelegation.signature();
assertThat(signature.getRecId()).isEqualTo((byte) 1);
assertThat(signature.getR().toString(16))
.isEqualTo("25c1240d7ffec0daeedb752d3357aff2e3cd58468f0c2d43ee0ee999e02ace2");
Expand All @@ -107,8 +110,9 @@ void shouldDecodeInnerPayloadWhenSignatureIsZero() {
Bytes.fromHexString(
"0xdf8501a1f0ff5a947a40026a3b9a41754a95eec8c92c6b99886f440c5b808080"),
true);
final CodeDelegation authorization = CodeDelegationTransactionDecoder.decodeInnerPayload(input);
final CodeDelegation codeDelegation =
CodeDelegationTransactionDecoder.decodeInnerPayload(input);

assertThat(authorization.chainId()).isEqualTo(new BigInteger("01a1f0ff5a", 16));
assertThat(codeDelegation.chainId()).isEqualTo(new BigInteger("01a1f0ff5a", 16));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.worldstate.DelegatedCodeService;
import org.hyperledger.besu.evm.worldstate.CodeDelegationService;
import org.hyperledger.besu.evm.worldstate.EVMWorldUpdater;

import java.math.BigInteger;
Expand All @@ -47,7 +47,7 @@ class CodeDelegationProcessorTest {

@Mock private Transaction transaction;

@Mock private DelegatedCodeService authorizedCodeService;
@Mock private CodeDelegationService authorizedCodeService;

@Mock private MutableAccount authority;

Expand Down Expand Up @@ -95,7 +95,7 @@ void shouldRejectMaxNonce() {
@Test
void shouldProcessValidDelegationForNewAccount() {
// Arrange
when(worldUpdater.authorizedCodeService()).thenReturn(authorizedCodeService);
when(worldUpdater.codeDelegationService()).thenReturn(authorizedCodeService);
CodeDelegation codeDelegation = createCodeDelegation(CHAIN_ID, 0L);
when(transaction.getCodeDelegationList()).thenReturn(Optional.of(List.of(codeDelegation)));
when(worldUpdater.getAccount(any())).thenReturn(null);
Expand All @@ -109,18 +109,18 @@ void shouldProcessValidDelegationForNewAccount() {
assertThat(result.alreadyExistingDelegators()).isZero();
verify(worldUpdater).createAccount(any());
verify(authority).incrementNonce();
verify(authorizedCodeService).processDelegatedCodeAuthorization(authority, DELEGATE_ADDRESS);
verify(authorizedCodeService).processCodeDelegation(authority, DELEGATE_ADDRESS);
}

@Test
void shouldProcessValidDelegationForExistingAccount() {
// Arrange
when(worldUpdater.authorizedCodeService()).thenReturn(authorizedCodeService);
when(worldUpdater.codeDelegationService()).thenReturn(authorizedCodeService);
CodeDelegation codeDelegation = createCodeDelegation(CHAIN_ID, 1L);
when(transaction.getCodeDelegationList()).thenReturn(Optional.of(List.of(codeDelegation)));
when(worldUpdater.getAccount(any())).thenReturn(authority);
when(authority.getNonce()).thenReturn(1L);
when(authorizedCodeService.canSetDelegatedCode(any())).thenReturn(true);
when(authorizedCodeService.canSetCodeDelegation(any())).thenReturn(true);

// Act
CodeDelegationResult result = processor.process(worldUpdater, transaction);
Expand All @@ -129,25 +129,25 @@ void shouldProcessValidDelegationForExistingAccount() {
assertThat(result.alreadyExistingDelegators()).isEqualTo(1);
verify(worldUpdater, never()).createAccount(any());
verify(authority).incrementNonce();
verify(authorizedCodeService).processDelegatedCodeAuthorization(authority, DELEGATE_ADDRESS);
verify(authorizedCodeService).processCodeDelegation(authority, DELEGATE_ADDRESS);
}

@Test
void shouldRejectDelegationWithInvalidNonce() {
// Arrange
when(worldUpdater.authorizedCodeService()).thenReturn(authorizedCodeService);
when(worldUpdater.codeDelegationService()).thenReturn(authorizedCodeService);
CodeDelegation codeDelegation = createCodeDelegation(CHAIN_ID, 2L);
when(transaction.getCodeDelegationList()).thenReturn(Optional.of(List.of(codeDelegation)));
when(worldUpdater.getAccount(any())).thenReturn(authority);
when(authorizedCodeService.canSetDelegatedCode(any())).thenReturn(true);
when(authorizedCodeService.canSetCodeDelegation(any())).thenReturn(true);

// Act
CodeDelegationResult result = processor.process(worldUpdater, transaction);

// Assert
assertThat(result.alreadyExistingDelegators()).isZero();
verify(authority, never()).incrementNonce();
verify(authorizedCodeService, never()).processDelegatedCodeAuthorization(any(), any());
verify(authorizedCodeService, never()).processCodeDelegation(any(), any());
}

@Test
Expand All @@ -163,7 +163,7 @@ void shouldRejectDelegationWithSGreaterThanHalfCurveOrder() {
// Assert
assertThat(result.alreadyExistingDelegators()).isZero();
verify(authority, never()).incrementNonce();
verify(authorizedCodeService, never()).processDelegatedCodeAuthorization(any(), any());
verify(authorizedCodeService, never()).processCodeDelegation(any(), any());
}

@Test
Expand All @@ -181,7 +181,7 @@ void shouldRejectDelegationWithRecIdNeitherZeroNorOne() {
// Assert
assertThat(result.alreadyExistingDelegators()).isZero();
verify(authority, never()).incrementNonce();
verify(authorizedCodeService, never()).processDelegatedCodeAuthorization(any(), any());
verify(authorizedCodeService, never()).processCodeDelegation(any(), any());
}

@Test
Expand All @@ -201,25 +201,25 @@ void shouldRejectDelegationWithInvalidSignature() {
// Assert
assertThat(result.alreadyExistingDelegators()).isZero();
verify(authority, never()).incrementNonce();
verify(authorizedCodeService, never()).processDelegatedCodeAuthorization(any(), any());
verify(authorizedCodeService, never()).processCodeDelegation(any(), any());
}

@Test
void shouldRejectDelegationWhenCannotSetDelegatedCode() {
void shouldRejectDelegationWhenCannotSetCodeDelegation() {
// Arrange
when(worldUpdater.authorizedCodeService()).thenReturn(authorizedCodeService);
when(worldUpdater.codeDelegationService()).thenReturn(authorizedCodeService);
CodeDelegation codeDelegation = createCodeDelegation(CHAIN_ID, 1L);
when(transaction.getCodeDelegationList()).thenReturn(Optional.of(List.of(codeDelegation)));
when(worldUpdater.getAccount(any())).thenReturn(authority);
when(authorizedCodeService.canSetDelegatedCode(any())).thenReturn(false);
when(authorizedCodeService.canSetCodeDelegation(any())).thenReturn(false);

// Act
CodeDelegationResult result = processor.process(worldUpdater, transaction);

// Assert
assertThat(result.alreadyExistingDelegators()).isZero();
verify(authority, never()).incrementNonce();
verify(authorizedCodeService, never()).processDelegatedCodeAuthorization(any(), any());
verify(authorizedCodeService, never()).processCodeDelegation(any(), any());
}

private CodeDelegation createCodeDelegation(final BigInteger chainId, final long nonce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,43 +221,44 @@ protected static List<Transaction> extractTransactions(
continue;
}

List<CodeDelegation> authorizations = new ArrayList<>(authorizationList.size());
List<CodeDelegation> codeDelegations = new ArrayList<>(authorizationList.size());
for (JsonNode entryAsJson : authorizationList) {
final BigInteger authorizationChainId =
final BigInteger codeDelegationChainId =
Bytes.fromHexStringLenient(entryAsJson.get("chainId").textValue())
.toUnsignedBigInteger();
final Address authorizationAddress =
final Address codeDelegationAddress =
Address.fromHexString(entryAsJson.get("address").textValue());

final long authorizationNonce =
final long codeDelegationNonce =
Bytes.fromHexStringLenient(entryAsJson.get("nonce").textValue()).toLong();

final BigInteger authorizationV =
final BigInteger codeDelegationV =
Bytes.fromHexStringLenient(entryAsJson.get("v").textValue())
.toUnsignedBigInteger();
if (authorizationV.compareTo(BigInteger.valueOf(256)) >= 0) {
if (codeDelegationV.compareTo(BigInteger.valueOf(256)) >= 0) {
throw new IllegalArgumentException(
"Invalid authorizationV value. Must be less than 256");
"Invalid codeDelegationV value. Must be less than 256");
}

final BigInteger authorizationR =
final BigInteger codeDelegationR =
Bytes.fromHexStringLenient(entryAsJson.get("r").textValue())
.toUnsignedBigInteger();
final BigInteger authorizationS =
final BigInteger codeDelegationS =
Bytes.fromHexStringLenient(entryAsJson.get("s").textValue())
.toUnsignedBigInteger();

final SECPSignature authorizationSignature =
new SECPSignature(authorizationR, authorizationS, authorizationV.byteValue());
final SECPSignature codeDelegationSignature =
new SECPSignature(
codeDelegationR, codeDelegationS, codeDelegationV.byteValue());

authorizations.add(
codeDelegations.add(
new org.hyperledger.besu.ethereum.core.CodeDelegation(
authorizationChainId,
authorizationAddress,
authorizationNonce,
authorizationSignature));
codeDelegationChainId,
codeDelegationAddress,
codeDelegationNonce,
codeDelegationSignature));
}
builder.codeDelegations(authorizations);
builder.codeDelegations(codeDelegations);
}

if (txNode.has("blobVersionedHashes")) {
Expand Down
Loading

0 comments on commit e7833b8

Please sign in to comment.