From e9a94f3682d8f17784683a0332ac2a07116eabb8 Mon Sep 17 00:00:00 2001 From: daniellehrner Date: Wed, 22 Jan 2025 21:59:27 +0100 Subject: [PATCH] Devnet-5: bug fixes for 7702 (#8148) * * fix code hash for delegated accounts in ExtCodeHashOperation * move common code to AbstractExtCodeOperation class Signed-off-by: Daniel Lehrner * * rename DelegatedCode -> CodeDelegation, for consistency Signed-off-by: Daniel Lehrner * * rename authorization -> CodeDelegation, for consistency Signed-off-by: Daniel Lehrner * rename method to start with lower case Signed-off-by: Daniel Lehrner --------- Signed-off-by: Daniel Lehrner Co-authored-by: Simon Dudley --- ...deDelegationTransactionAcceptanceTest.java | 8 +- .../besu/datatypes/Transaction.java | 4 +- .../mainnet/CodeDelegationProcessor.java | 10 +-- .../mainnet/MainnetTransactionProcessor.java | 9 ++- .../mainnet/MainnetTransactionValidator.java | 4 +- .../encoding/CodeDelegationDecoderTest.java | 38 +++++---- .../mainnet/CodeDelegationProcessorTest.java | 34 ++++---- .../hyperledger/besu/evmtool/T8nExecutor.java | 35 +++++---- ...ava => AbstractCodeDelegationAccount.java} | 30 ++++---- .../hyperledger/besu/evm/account/Account.java | 4 +- .../besu/evm/account/AccountState.java | 8 +- ...ccount.java => CodeDelegationAccount.java} | 10 +-- ...tableCodeDelegationDelegationAccount.java} | 10 +-- .../evm/operation/AbstractCallOperation.java | 23 +++--- .../operation/AbstractExtCallOperation.java | 18 ++--- .../operation/AbstractExtCodeOperation.java | 77 +++++++++++++++++++ .../evm/operation/ExtCodeCopyOperation.java | 13 +--- .../evm/operation/ExtCodeHashOperation.java | 13 +--- .../evm/operation/ExtCodeSizeOperation.java | 13 +--- ....java => CodeDelegationGasCostHelper.java} | 20 ++--- ...eHelper.java => CodeDelegationHelper.java} | 14 ++-- ...ervice.java => CodeDelegationService.java} | 40 +++++----- .../besu/evm/worldstate/EVMWorldUpdater.java | 34 ++++---- 23 files changed, 261 insertions(+), 208 deletions(-) rename evm/src/main/java/org/hyperledger/besu/evm/account/{AbstractDelegatedCodeAccount.java => AbstractCodeDelegationAccount.java} (69%) rename evm/src/main/java/org/hyperledger/besu/evm/account/{DelegatedCodeAccount.java => CodeDelegationAccount.java} (92%) rename evm/src/main/java/org/hyperledger/besu/evm/account/{MutableDelegatedCodeAccount.java => MutableCodeDelegationDelegationAccount.java} (92%) create mode 100644 evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCodeOperation.java rename evm/src/main/java/org/hyperledger/besu/evm/worldstate/{DelegatedCodeGasCostHelper.java => CodeDelegationGasCostHelper.java} (81%) rename evm/src/main/java/org/hyperledger/besu/evm/worldstate/{DelegateCodeHelper.java => CodeDelegationHelper.java} (78%) rename evm/src/main/java/org/hyperledger/besu/evm/worldstate/{DelegatedCodeService.java => CodeDelegationService.java} (69%) diff --git a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/ethereum/CodeDelegationTransactionAcceptanceTest.java b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/ethereum/CodeDelegationTransactionAcceptanceTest.java index 72937afb59d..cb7b8d4158e 100644 --- a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/ethereum/CodeDelegationTransactionAcceptanceTest.java +++ b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/ethereum/CodeDelegationTransactionAcceptanceTest.java @@ -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) @@ -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( @@ -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( @@ -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()))); diff --git a/datatypes/src/main/java/org/hyperledger/besu/datatypes/Transaction.java b/datatypes/src/main/java/org/hyperledger/besu/datatypes/Transaction.java index e614901300b..570f4d2427f 100644 --- a/datatypes/src/main/java/org/hyperledger/besu/datatypes/Transaction.java +++ b/datatypes/src/main/java/org/hyperledger/besu/datatypes/Transaction.java @@ -243,9 +243,9 @@ default Optional getMaxFeePerBlobGas() { Optional> 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(); } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/CodeDelegationProcessor.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/CodeDelegationProcessor.java index 977b5031936..8d6fcb206a1 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/CodeDelegationProcessor.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/CodeDelegationProcessor.java @@ -70,7 +70,7 @@ public CodeDelegationResult process( .get() .forEach( codeDelegation -> - processAuthorization( + processCodeDelegation( evmWorldUpdater, (org.hyperledger.besu.ethereum.core.CodeDelegation) codeDelegation, result)); @@ -78,7 +78,7 @@ public CodeDelegationResult process( return result; } - private void processAuthorization( + private void processCodeDelegation( final EVMWorldUpdater evmWorldUpdater, final CodeDelegation codeDelegation, final CodeDelegationResult result) { @@ -130,7 +130,7 @@ private void processAuthorization( } else { authority = maybeAuthorityAccount.get(); - if (!evmWorldUpdater.authorizedCodeService().canSetDelegatedCode(authority)) { + if (!evmWorldUpdater.codeDelegationService().canSetCodeDelegation(authority)) { return; } @@ -150,8 +150,8 @@ private void processAuthorization( } evmWorldUpdater - .authorizedCodeService() - .processDelegatedCodeAuthorization(authority, codeDelegation.address()); + .codeDelegationService() + .processCodeDelegation(authority, codeDelegation.address()); authority.incrementNonce(); } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java index a2526707950..1609229fe36 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java @@ -427,8 +427,8 @@ public TransactionProcessingResult processTransaction( final Address to = transaction.getTo().get(); final Optional 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 = @@ -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( diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java index 0bef4d569f1..950c3d2b62d 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java @@ -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; @@ -333,7 +333,7 @@ public ValidationResult 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 validateTransactionSignature( diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/CodeDelegationDecoderTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/CodeDelegationDecoderTest.java index a0c7689bc71..da241d616fd 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/CodeDelegationDecoderTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/CodeDelegationDecoderTest.java @@ -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"); @@ -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"); @@ -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"); @@ -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)); } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/CodeDelegationProcessorTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/CodeDelegationProcessorTest.java index c4b45f9e08c..757fea0b5e4 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/CodeDelegationProcessorTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/CodeDelegationProcessorTest.java @@ -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; @@ -47,7 +47,7 @@ class CodeDelegationProcessorTest { @Mock private Transaction transaction; - @Mock private DelegatedCodeService authorizedCodeService; + @Mock private CodeDelegationService authorizedCodeService; @Mock private MutableAccount authority; @@ -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); @@ -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); @@ -129,17 +129,17 @@ 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); @@ -147,7 +147,7 @@ void shouldRejectDelegationWithInvalidNonce() { // Assert assertThat(result.alreadyExistingDelegators()).isZero(); verify(authority, never()).incrementNonce(); - verify(authorizedCodeService, never()).processDelegatedCodeAuthorization(any(), any()); + verify(authorizedCodeService, never()).processCodeDelegation(any(), any()); } @Test @@ -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 @@ -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 @@ -201,17 +201,17 @@ 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); @@ -219,7 +219,7 @@ void shouldRejectDelegationWhenCannotSetDelegatedCode() { // 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) { diff --git a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/T8nExecutor.java b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/T8nExecutor.java index 7053961a125..0768afd4f0f 100644 --- a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/T8nExecutor.java +++ b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/T8nExecutor.java @@ -221,43 +221,44 @@ protected static List extractTransactions( continue; } - List authorizations = new ArrayList<>(authorizationList.size()); + List 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")) { diff --git a/evm/src/main/java/org/hyperledger/besu/evm/account/AbstractDelegatedCodeAccount.java b/evm/src/main/java/org/hyperledger/besu/evm/account/AbstractCodeDelegationAccount.java similarity index 69% rename from evm/src/main/java/org/hyperledger/besu/evm/account/AbstractDelegatedCodeAccount.java rename to evm/src/main/java/org/hyperledger/besu/evm/account/AbstractCodeDelegationAccount.java index 2318d48650c..2cff9fadf9d 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/account/AbstractDelegatedCodeAccount.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/account/AbstractCodeDelegationAccount.java @@ -23,20 +23,20 @@ import org.apache.tuweni.bytes.Bytes; -abstract class AbstractDelegatedCodeAccount implements Account { +abstract class AbstractCodeDelegationAccount implements Account { private final WorldUpdater worldUpdater; private final GasCalculator gasCalculator; /** The address of the account that has delegated code to be loaded into it. */ - protected final Address delegatedCodeAddress; + protected final Address codeDelegationAddress; - protected AbstractDelegatedCodeAccount( + protected AbstractCodeDelegationAccount( final WorldUpdater worldUpdater, - final Address delegatedCodeAddress, + final Address codeDelegationAddress, final GasCalculator gasCalculator) { this.worldUpdater = worldUpdater; this.gasCalculator = gasCalculator; - this.delegatedCodeAddress = delegatedCodeAddress; + this.codeDelegationAddress = codeDelegationAddress; } /** @@ -45,8 +45,8 @@ protected AbstractDelegatedCodeAccount( * @return the delegated code. */ @Override - public Optional getDelegatedCode() { - return resolveDelegatedCode(); + public Optional getCodeDelegationTargetCode() { + return resolveCodeDelegationTargetCode(); } /** @@ -55,8 +55,8 @@ public Optional getDelegatedCode() { * @return the hash of the delegated code. */ @Override - public Optional getDelegatedCodeHash() { - return getDelegatedCode().map(Hash::hash); + public Optional getCodeDelegationTargetHash() { + return getCodeDelegationTargetCode().map(Hash::hash); } /** @@ -65,23 +65,23 @@ public Optional getDelegatedCodeHash() { * @return the address of the delegated code. */ @Override - public Optional
delegatedCodeAddress() { - return Optional.of(delegatedCodeAddress); + public Optional
codeDelegationAddress() { + return Optional.of(codeDelegationAddress); } @Override - public boolean hasDelegatedCode() { + public boolean hasCodeDelegation() { return true; } private Optional getDelegatedAccount() { - return Optional.ofNullable(worldUpdater.getAccount(delegatedCodeAddress)); + return Optional.ofNullable(worldUpdater.getAccount(codeDelegationAddress)); } - private Optional resolveDelegatedCode() { + private Optional resolveCodeDelegationTargetCode() { final Optional maybeDelegatedAccount = getDelegatedAccount(); - if (gasCalculator.isPrecompile(delegatedCodeAddress) || maybeDelegatedAccount.isEmpty()) { + if (gasCalculator.isPrecompile(codeDelegationAddress) || maybeDelegatedAccount.isEmpty()) { return Optional.of(Bytes.EMPTY); } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/account/Account.java b/evm/src/main/java/org/hyperledger/besu/evm/account/Account.java index f0196c82766..ab3c0ba8192 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/account/Account.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/account/Account.java @@ -57,7 +57,7 @@ public interface Account extends AccountState { * * @return the address of the delegated code account if it has one otherwise empty. */ - default Optional
delegatedCodeAddress() { + default Optional
codeDelegationAddress() { return Optional.empty(); } @@ -66,7 +66,7 @@ default Optional
delegatedCodeAddress() { * * @return true if the account has delegated code otherwise false. */ - default boolean hasDelegatedCode() { + default boolean hasCodeDelegation() { return false; } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/account/AccountState.java b/evm/src/main/java/org/hyperledger/besu/evm/account/AccountState.java index 235f61fbe40..514bb8254a6 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/account/AccountState.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/account/AccountState.java @@ -78,9 +78,9 @@ public interface AccountState { /** * The optional EVM bytecode if the account has set a 7702 code delegation. * - * @return the delegated code (which may be empty). + * @return the code of the target account (which may be empty). */ - default Optional getDelegatedCode() { + default Optional getCodeDelegationTargetCode() { return Optional.empty(); } @@ -94,9 +94,9 @@ default Optional getDelegatedCode() { /** * The optional hash of the delegated EVM bytecode if the account has set a 7702 code delegation. * - * @return the hash of the delegated code (which may be empty). + * @return the hash of the code of the target account (which may be empty). */ - default Optional getDelegatedCodeHash() { + default Optional getCodeDelegationTargetHash() { return Optional.empty(); } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/account/DelegatedCodeAccount.java b/evm/src/main/java/org/hyperledger/besu/evm/account/CodeDelegationAccount.java similarity index 92% rename from evm/src/main/java/org/hyperledger/besu/evm/account/DelegatedCodeAccount.java rename to evm/src/main/java/org/hyperledger/besu/evm/account/CodeDelegationAccount.java index 3252dab9c21..85511730b83 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/account/DelegatedCodeAccount.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/account/CodeDelegationAccount.java @@ -28,19 +28,19 @@ import org.apache.tuweni.units.bigints.UInt256; /** Wraps an EOA account and includes delegated code to be run on behalf of it. */ -public class DelegatedCodeAccount extends AbstractDelegatedCodeAccount implements Account { +public class CodeDelegationAccount extends AbstractCodeDelegationAccount implements Account { private final Account wrappedAccount; /** - * Creates a new AuthorizedCodeAccount. + * Creates a new CodeDelegationAccount. * * @param worldUpdater the world updater. * @param wrappedAccount the account that has delegated code to be loaded into it. * @param codeDelegationAddress the address of the delegated code. * @param gasCalculator the gas calculator to check for precompiles. */ - public DelegatedCodeAccount( + public CodeDelegationAccount( final WorldUpdater worldUpdater, final Account wrappedAccount, final Address codeDelegationAddress, @@ -60,8 +60,8 @@ public boolean isStorageEmpty() { } @Override - public Optional
delegatedCodeAddress() { - return super.delegatedCodeAddress(); + public Optional
codeDelegationAddress() { + return super.codeDelegationAddress(); } @Override diff --git a/evm/src/main/java/org/hyperledger/besu/evm/account/MutableDelegatedCodeAccount.java b/evm/src/main/java/org/hyperledger/besu/evm/account/MutableCodeDelegationDelegationAccount.java similarity index 92% rename from evm/src/main/java/org/hyperledger/besu/evm/account/MutableDelegatedCodeAccount.java rename to evm/src/main/java/org/hyperledger/besu/evm/account/MutableCodeDelegationDelegationAccount.java index 9b98d596430..e132974c021 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/account/MutableDelegatedCodeAccount.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/account/MutableCodeDelegationDelegationAccount.java @@ -29,20 +29,20 @@ import org.apache.tuweni.units.bigints.UInt256; /** Wraps an EOA account and includes delegated code to be run on behalf of it. */ -public class MutableDelegatedCodeAccount extends AbstractDelegatedCodeAccount +public class MutableCodeDelegationDelegationAccount extends AbstractCodeDelegationAccount implements MutableAccount { private final MutableAccount wrappedAccount; /** - * Creates a new MutableAuthorizedCodeAccount. + * Creates a new MutableCodeDelegationDelegationAccount. * * @param worldUpdater the world updater. * @param wrappedAccount the account that has delegated code to be loaded into it. * @param codeDelegationAddress the address of the delegated code. * @param gasCalculator the gas calculator to check for precompiles. */ - public MutableDelegatedCodeAccount( + public MutableCodeDelegationDelegationAccount( final WorldUpdater worldUpdater, final MutableAccount wrappedAccount, final Address codeDelegationAddress, @@ -62,8 +62,8 @@ public boolean isStorageEmpty() { } @Override - public Optional
delegatedCodeAddress() { - return super.delegatedCodeAddress(); + public Optional
codeDelegationAddress() { + return super.codeDelegationAddress(); } @Override diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCallOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCallOperation.java index d2cde85a62f..19310598468 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCallOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCallOperation.java @@ -26,7 +26,7 @@ import org.hyperledger.besu.evm.frame.MessageFrame; import org.hyperledger.besu.evm.frame.MessageFrame.State; import org.hyperledger.besu.evm.gascalculator.GasCalculator; -import org.hyperledger.besu.evm.worldstate.DelegatedCodeGasCostHelper; +import org.hyperledger.besu.evm.worldstate.CodeDelegationGasCostHelper; import org.apache.tuweni.bytes.Bytes; @@ -191,24 +191,24 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { final Account contract = frame.getWorldUpdater().get(to); - if (contract != null && contract.hasDelegatedCode()) { - if (contract.getDelegatedCode().isEmpty()) { + if (contract != null && contract.hasCodeDelegation()) { + if (contract.getCodeDelegationTargetCode().isEmpty()) { throw new RuntimeException("A delegated code account must have delegated code"); } - if (contract.getDelegatedCodeHash().isEmpty()) { + if (contract.getCodeDelegationTargetHash().isEmpty()) { throw new RuntimeException("A delegated code account must have a delegated code hash"); } - final long delegatedCodeResolutionGas = - DelegatedCodeGasCostHelper.delegatedCodeGasCost(frame, gasCalculator(), contract); + final long codeDelegationResolutionGas = + CodeDelegationGasCostHelper.codeDelegationGasCost(frame, gasCalculator(), contract); - if (frame.getRemainingGas() < delegatedCodeResolutionGas) { + if (frame.getRemainingGas() < codeDelegationResolutionGas) { return new Operation.OperationResult( - delegatedCodeResolutionGas, ExceptionalHaltReason.INSUFFICIENT_GAS); + codeDelegationResolutionGas, ExceptionalHaltReason.INSUFFICIENT_GAS); } - frame.decrementRemainingGas(delegatedCodeResolutionGas); + frame.decrementRemainingGas(codeDelegationResolutionGas); } final Account account = frame.getWorldUpdater().get(frame.getRecipientAddress()); @@ -357,8 +357,9 @@ protected static Code getCode(final EVM evm, final Account account) { return CodeV0.EMPTY_CODE; } - if (account.hasDelegatedCode()) { - return evm.getCode(account.getDelegatedCodeHash().get(), account.getDelegatedCode().get()); + if (account.hasCodeDelegation()) { + return evm.getCode( + account.getCodeDelegationTargetHash().get(), account.getCodeDelegationTargetCode().get()); } return evm.getCode(account.getCodeHash(), account.getCode()); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCallOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCallOperation.java index 1040cb1a86f..66e93700e53 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCallOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCallOperation.java @@ -25,7 +25,7 @@ import org.hyperledger.besu.evm.frame.MessageFrame; import org.hyperledger.besu.evm.gascalculator.GasCalculator; import org.hyperledger.besu.evm.internal.Words; -import org.hyperledger.besu.evm.worldstate.DelegatedCodeGasCostHelper; +import org.hyperledger.besu.evm.worldstate.CodeDelegationGasCostHelper; import javax.annotation.Nonnull; @@ -125,24 +125,24 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { Address to = Words.toAddress(toBytes); final Account contract = frame.getWorldUpdater().get(to); - if (contract != null && contract.hasDelegatedCode()) { - if (contract.getDelegatedCode().isEmpty()) { + if (contract != null && contract.hasCodeDelegation()) { + if (contract.getCodeDelegationTargetCode().isEmpty()) { throw new RuntimeException("A delegated code account must have delegated code"); } - if (contract.getDelegatedCodeHash().isEmpty()) { + if (contract.getCodeDelegationTargetHash().isEmpty()) { throw new RuntimeException("A delegated code account must have a delegated code hash"); } - final long delegatedCodeResolutionGas = - DelegatedCodeGasCostHelper.delegatedCodeGasCost(frame, gasCalculator(), contract); + final long codeDelegationResolutionGas = + CodeDelegationGasCostHelper.codeDelegationGasCost(frame, gasCalculator(), contract); - if (frame.getRemainingGas() < delegatedCodeResolutionGas) { + if (frame.getRemainingGas() < codeDelegationResolutionGas) { return new Operation.OperationResult( - delegatedCodeResolutionGas, ExceptionalHaltReason.INSUFFICIENT_GAS); + codeDelegationResolutionGas, ExceptionalHaltReason.INSUFFICIENT_GAS); } - frame.decrementRemainingGas(delegatedCodeResolutionGas); + frame.decrementRemainingGas(codeDelegationResolutionGas); } boolean accountCreation = (contract == null || contract.isEmpty()) && !zeroValue; diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCodeOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCodeOperation.java new file mode 100644 index 00000000000..523634671cd --- /dev/null +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCodeOperation.java @@ -0,0 +1,77 @@ +/* + * Copyright contributors to Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.evm.operation; + +import org.hyperledger.besu.datatypes.Hash; +import org.hyperledger.besu.evm.account.Account; +import org.hyperledger.besu.evm.gascalculator.GasCalculator; +import org.hyperledger.besu.evm.worldstate.CodeDelegationHelper; + +import org.apache.tuweni.bytes.Bytes; + +/** + * ExtCode* operations treat EOAs with delegated code differently than other operations. This + * abstract class contains common methods for this behaviour. + */ +abstract class AbstractExtCodeOperation extends AbstractOperation { + /** + * Instantiates a new Abstract operation. + * + * @param opcode the opcode + * @param name the name + * @param stackItemsConsumed the stack items consumed + * @param stackItemsProduced the stack items produced + * @param gasCalculator the gas calculator + */ + protected AbstractExtCodeOperation( + final int opcode, + final String name, + final int stackItemsConsumed, + final int stackItemsProduced, + final GasCalculator gasCalculator) { + super(opcode, name, stackItemsConsumed, stackItemsProduced, gasCalculator); + } + + /** + * Returns the code for standard accounts or a special designator for EOAs with delegated code + * + * @param account The account + * @return the code or the special 7702 designator + */ + protected Bytes getCode(final Account account) { + if (account == null) { + return Bytes.EMPTY; + } + + return account.hasCodeDelegation() + ? CodeDelegationHelper.getCodeDelegationForRead() + : account.getCode(); + } + + /** + * Returns the code hash for standard accounts or a special designator for EOAs with delegated + * code + * + * @param account The account + * @return the code hash or the hash of the special 7702 designator + */ + protected Hash getCodeHash(final Account account) { + if (account.hasCodeDelegation()) { + return Hash.hash(CodeDelegationHelper.getCodeDelegationForRead()); + } + + return account.getCodeHash(); + } +} diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeCopyOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeCopyOperation.java index 848cfaaeaa0..5ea719327d5 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeCopyOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeCopyOperation.java @@ -25,12 +25,11 @@ import org.hyperledger.besu.evm.frame.MessageFrame; import org.hyperledger.besu.evm.gascalculator.GasCalculator; import org.hyperledger.besu.evm.internal.Words; -import org.hyperledger.besu.evm.worldstate.DelegateCodeHelper; import org.apache.tuweni.bytes.Bytes; /** The Ext code copy operation. */ -public class ExtCodeCopyOperation extends AbstractOperation { +public class ExtCodeCopyOperation extends AbstractExtCodeOperation { /** This is the "code" legacy contracts see when copying code from an EOF contract. */ public static final Bytes EOF_REPLACEMENT_CODE = Bytes.fromHexString("0xef00"); @@ -108,14 +107,4 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { return new OperationResult(cost, null); } - - private static Bytes getCode(final Account account) { - if (account == null) { - return Bytes.EMPTY; - } - - return account.hasDelegatedCode() - ? DelegateCodeHelper.getDelegatedCodeForRead() - : account.getCode(); - } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeHashOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeHashOperation.java index 9157c55aa05..6611111ad11 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeHashOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeHashOperation.java @@ -25,12 +25,11 @@ import org.hyperledger.besu.evm.internal.OverflowException; import org.hyperledger.besu.evm.internal.UnderflowException; import org.hyperledger.besu.evm.internal.Words; -import org.hyperledger.besu.evm.worldstate.DelegateCodeHelper; import org.apache.tuweni.bytes.Bytes; /** The Ext code hash operation. */ -public class ExtCodeHashOperation extends AbstractOperation { +public class ExtCodeHashOperation extends AbstractExtCodeOperation { // // 0x9dbf3648db8210552e9c4f75c6a1c3057c0ca432043bd648be15fe7be05646f5 static final Hash EOF_REPLACEMENT_HASH = Hash.hash(ExtCodeCopyOperation.EOF_REPLACEMENT_CODE); @@ -93,7 +92,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { && code.get(1) == 0) { frame.pushStackItem(EOF_REPLACEMENT_HASH); } else { - frame.pushStackItem(account.getCodeHash()); + frame.pushStackItem(getCodeHash(account)); } } return new OperationResult(cost, null); @@ -104,12 +103,4 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { return new OperationResult(cost(true), ExceptionalHaltReason.TOO_MANY_STACK_ITEMS); } } - - private static Bytes getCode(final Account account) { - if (!account.hasDelegatedCode()) { - return account.getCode(); - } - - return DelegateCodeHelper.getDelegatedCodeForRead(); - } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeSizeOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeSizeOperation.java index 8b8ff172542..2e2c60d9d30 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeSizeOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeSizeOperation.java @@ -24,12 +24,11 @@ import org.hyperledger.besu.evm.internal.OverflowException; import org.hyperledger.besu.evm.internal.UnderflowException; import org.hyperledger.besu.evm.internal.Words; -import org.hyperledger.besu.evm.worldstate.DelegateCodeHelper; import org.apache.tuweni.bytes.Bytes; /** The Ext code size operation. */ -public class ExtCodeSizeOperation extends AbstractOperation { +public class ExtCodeSizeOperation extends AbstractExtCodeOperation { static final Bytes EOF_SIZE = Bytes.of(2); @@ -103,14 +102,4 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { return new OperationResult(cost(true), ExceptionalHaltReason.TOO_MANY_STACK_ITEMS); } } - - private static Bytes getCode(final Account account) { - if (account == null) { - return Bytes.EMPTY; - } - - return account.hasDelegatedCode() - ? DelegateCodeHelper.getDelegatedCodeForRead() - : account.getCode(); - } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/worldstate/DelegatedCodeGasCostHelper.java b/evm/src/main/java/org/hyperledger/besu/evm/worldstate/CodeDelegationGasCostHelper.java similarity index 81% rename from evm/src/main/java/org/hyperledger/besu/evm/worldstate/DelegatedCodeGasCostHelper.java rename to evm/src/main/java/org/hyperledger/besu/evm/worldstate/CodeDelegationGasCostHelper.java index 480dc8fcb67..5e822469b00 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/worldstate/DelegatedCodeGasCostHelper.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/worldstate/CodeDelegationGasCostHelper.java @@ -26,10 +26,10 @@ * be executed when a contract has delegated code. This process is necessary to determine the * contract that will be executed and to ensure that the contract is warm in the cache. */ -public class DelegatedCodeGasCostHelper { +public class CodeDelegationGasCostHelper { /** Private constructor to prevent instantiation. */ - private DelegatedCodeGasCostHelper() { + private CodeDelegationGasCostHelper() { // empty constructor } @@ -41,25 +41,25 @@ private DelegatedCodeGasCostHelper() { * @param account the account * @return the gas cost and result of the operation */ - public static long delegatedCodeGasCost( + public static long codeDelegationGasCost( final MessageFrame frame, final GasCalculator gasCalculator, final Account account) { - if (!account.hasDelegatedCode()) { + if (!account.hasCodeDelegation()) { return 0; } - if (account.delegatedCodeAddress().isEmpty()) { + if (account.codeDelegationAddress().isEmpty()) { throw new RuntimeException("A delegated code account must have a delegated code address"); } - return calculateDelegatedCodeResolutionGas( - frame, gasCalculator, account.delegatedCodeAddress().get()); + return calculateCodeDelegationResolutionGas( + frame, gasCalculator, account.codeDelegationAddress().get()); } - private static long calculateDelegatedCodeResolutionGas( + private static long calculateCodeDelegationResolutionGas( final MessageFrame frame, final GasCalculator gasCalculator, final Address delegateeAddress) { - final boolean delegatedCodeIsWarm = + final boolean isWarm = frame.warmUpAddress(delegateeAddress) || gasCalculator.isPrecompile(delegateeAddress); - return delegatedCodeIsWarm + return isWarm ? gasCalculator.getWarmStorageReadCost() : gasCalculator.getColdAccountAccessCost(); } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/worldstate/DelegateCodeHelper.java b/evm/src/main/java/org/hyperledger/besu/evm/worldstate/CodeDelegationHelper.java similarity index 78% rename from evm/src/main/java/org/hyperledger/besu/evm/worldstate/DelegateCodeHelper.java rename to evm/src/main/java/org/hyperledger/besu/evm/worldstate/CodeDelegationHelper.java index 5331bdd2757..b9794abebe0 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/worldstate/DelegateCodeHelper.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/worldstate/CodeDelegationHelper.java @@ -19,20 +19,20 @@ import org.apache.tuweni.bytes.Bytes; /** Helper class for 7702 delegated code interactions */ -public class DelegateCodeHelper { +public class CodeDelegationHelper { /** * The designator that is returned when a ExtCode* operation calls a contract with delegated code */ public static final Bytes DELEGATED_CODE_DESIGNATOR = Bytes.fromHexString("ef01"); /** The prefix that is used to identify delegated code */ - public static final Bytes DELEGATED_CODE_PREFIX = Bytes.fromHexString("ef0100"); + public static final Bytes CODE_DELEGATION_PREFIX = Bytes.fromHexString("ef0100"); /** The size of the delegated code */ - public static final int DELEGATED_CODE_SIZE = DELEGATED_CODE_PREFIX.size() + Address.SIZE; + public static final int DELEGATED_CODE_SIZE = CODE_DELEGATION_PREFIX.size() + Address.SIZE; /** create a new DelegateCodeHelper */ - public DelegateCodeHelper() { + public CodeDelegationHelper() { // empty } @@ -42,10 +42,10 @@ public DelegateCodeHelper() { * @param code the code to check. * @return {@code true} if the code is delegated code, {@code false} otherwise. */ - public static boolean hasDelegatedCode(final Bytes code) { + public static boolean hasCodeDelegation(final Bytes code) { return code != null && code.size() == DELEGATED_CODE_SIZE - && code.slice(0, DELEGATED_CODE_PREFIX.size()).equals(DELEGATED_CODE_PREFIX); + && code.slice(0, CODE_DELEGATION_PREFIX.size()).equals(CODE_DELEGATION_PREFIX); } /** @@ -53,7 +53,7 @@ public static boolean hasDelegatedCode(final Bytes code) { * * @return the hardcoded designator for delegated code: ef01 */ - public static Bytes getDelegatedCodeForRead() { + public static Bytes getCodeDelegationForRead() { return DELEGATED_CODE_DESIGNATOR; } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/worldstate/DelegatedCodeService.java b/evm/src/main/java/org/hyperledger/besu/evm/worldstate/CodeDelegationService.java similarity index 69% rename from evm/src/main/java/org/hyperledger/besu/evm/worldstate/DelegatedCodeService.java rename to evm/src/main/java/org/hyperledger/besu/evm/worldstate/CodeDelegationService.java index d919b9dc691..3b838895eda 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/worldstate/DelegatedCodeService.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/worldstate/CodeDelegationService.java @@ -14,29 +14,29 @@ */ package org.hyperledger.besu.evm.worldstate; -import static org.hyperledger.besu.evm.worldstate.DelegateCodeHelper.DELEGATED_CODE_PREFIX; -import static org.hyperledger.besu.evm.worldstate.DelegateCodeHelper.hasDelegatedCode; +import static org.hyperledger.besu.evm.worldstate.CodeDelegationHelper.CODE_DELEGATION_PREFIX; +import static org.hyperledger.besu.evm.worldstate.CodeDelegationHelper.hasCodeDelegation; import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.evm.account.Account; -import org.hyperledger.besu.evm.account.DelegatedCodeAccount; +import org.hyperledger.besu.evm.account.CodeDelegationAccount; import org.hyperledger.besu.evm.account.MutableAccount; -import org.hyperledger.besu.evm.account.MutableDelegatedCodeAccount; +import org.hyperledger.besu.evm.account.MutableCodeDelegationDelegationAccount; import org.hyperledger.besu.evm.gascalculator.GasCalculator; import org.apache.tuweni.bytes.Bytes; /** A service that manages the code injection of delegated code. */ -public class DelegatedCodeService { +public class CodeDelegationService { private final GasCalculator gasCalculator; /** - * Creates a new DelegatedCodeService. + * Creates a new CodeDelegationService. * * @param gasCalculator the gas calculator to check for pre compiles. */ - public DelegatedCodeService(final GasCalculator gasCalculator) { + public CodeDelegationService(final GasCalculator gasCalculator) { this.gasCalculator = gasCalculator; } @@ -45,17 +45,17 @@ public DelegatedCodeService(final GasCalculator gasCalculator) { * address. If the address is 0, it will set the code to empty. * * @param account the account to which the delegated code is added. - * @param delegatedCodeAddress the address of the target of the authorization. + * @param codeDelegationAddress the address of the target of the authorization. */ - public void processDelegatedCodeAuthorization( - final MutableAccount account, final Address delegatedCodeAddress) { - // authorization to zero address removes any delegated code - if (delegatedCodeAddress.equals(Address.ZERO)) { + public void processCodeDelegation( + final MutableAccount account, final Address codeDelegationAddress) { + // code delegation to zero address removes any delegated code + if (codeDelegationAddress.equals(Address.ZERO)) { account.setCode(Bytes.EMPTY); return; } - account.setCode(Bytes.concatenate(DELEGATED_CODE_PREFIX, delegatedCodeAddress)); + account.setCode(Bytes.concatenate(CODE_DELEGATION_PREFIX, codeDelegationAddress)); } /** @@ -64,8 +64,8 @@ public void processDelegatedCodeAuthorization( * @param account the account to check. * @return {@code true} if the account can set delegated code, {@code false} otherwise. */ - public boolean canSetDelegatedCode(final Account account) { - return account.getCode().isEmpty() || hasDelegatedCode(account.getCode()); + public boolean canSetCodeDelegation(final Account account) { + return account.getCode().isEmpty() || hasCodeDelegation(account.getCode()); } /** @@ -77,11 +77,11 @@ public boolean canSetDelegatedCode(final Account account) { * otherwise. */ public Account processAccount(final WorldUpdater worldUpdater, final Account account) { - if (account == null || !hasDelegatedCode(account.getCode())) { + if (account == null || !hasCodeDelegation(account.getCode())) { return account; } - return new DelegatedCodeAccount( + return new CodeDelegationAccount( worldUpdater, account, resolveDelegatedAddress(account.getCode()), gasCalculator); } @@ -95,15 +95,15 @@ public Account processAccount(final WorldUpdater worldUpdater, final Account acc */ public MutableAccount processMutableAccount( final WorldUpdater worldUpdater, final MutableAccount account) { - if (account == null || !hasDelegatedCode(account.getCode())) { + if (account == null || !hasCodeDelegation(account.getCode())) { return account; } - return new MutableDelegatedCodeAccount( + return new MutableCodeDelegationDelegationAccount( worldUpdater, account, resolveDelegatedAddress(account.getCode()), gasCalculator); } private Address resolveDelegatedAddress(final Bytes code) { - return Address.wrap(code.slice(DELEGATED_CODE_PREFIX.size())); + return Address.wrap(code.slice(CODE_DELEGATION_PREFIX.size())); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/worldstate/EVMWorldUpdater.java b/evm/src/main/java/org/hyperledger/besu/evm/worldstate/EVMWorldUpdater.java index f6e45f4555f..8659270f94e 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/worldstate/EVMWorldUpdater.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/worldstate/EVMWorldUpdater.java @@ -26,11 +26,11 @@ /** * The EVM world updater. This class is a wrapper around a WorldUpdater that provides an - * AuthorizedCodeService to manage the authorized code for accounts. + * CodeDelegationService to manage the code delegations for accounts. */ public class EVMWorldUpdater implements WorldUpdater { private final WorldUpdater rootWorldUpdater; - private final DelegatedCodeService delegatedCodeService; + private final CodeDelegationService codeDelegationService; /** * Instantiates a new EVM world updater. @@ -39,49 +39,49 @@ public class EVMWorldUpdater implements WorldUpdater { * @param gasCalculator the gas calculator to check for precompiles. */ public EVMWorldUpdater(final WorldUpdater rootWorldUpdater, final GasCalculator gasCalculator) { - this(rootWorldUpdater, new DelegatedCodeService(gasCalculator)); + this(rootWorldUpdater, new CodeDelegationService(gasCalculator)); } private EVMWorldUpdater( - final WorldUpdater rootWorldUpdater, final DelegatedCodeService delegatedCodeService) { + final WorldUpdater rootWorldUpdater, final CodeDelegationService codeDelegationService) { this.rootWorldUpdater = rootWorldUpdater; - this.delegatedCodeService = delegatedCodeService; + this.codeDelegationService = codeDelegationService; } /** - * Authorized code service. + * Code delegation service. * - * @return the authorized code service + * @return the code delegation service */ - public DelegatedCodeService authorizedCodeService() { - return delegatedCodeService; + public CodeDelegationService codeDelegationService() { + return codeDelegationService; } @Override public MutableAccount createAccount(final Address address, final long nonce, final Wei balance) { - return delegatedCodeService.processMutableAccount( + return codeDelegationService.processMutableAccount( this, rootWorldUpdater.createAccount(address, nonce, balance)); } @Override public MutableAccount getAccount(final Address address) { - return delegatedCodeService.processMutableAccount(this, rootWorldUpdater.getAccount(address)); + return codeDelegationService.processMutableAccount(this, rootWorldUpdater.getAccount(address)); } @Override public MutableAccount getOrCreate(final Address address) { - return delegatedCodeService.processMutableAccount(this, rootWorldUpdater.getOrCreate(address)); + return codeDelegationService.processMutableAccount(this, rootWorldUpdater.getOrCreate(address)); } @Override public MutableAccount getOrCreateSenderAccount(final Address address) { - return delegatedCodeService.processMutableAccount( + return codeDelegationService.processMutableAccount( this, rootWorldUpdater.getOrCreateSenderAccount(address)); } @Override public MutableAccount getSenderAccount(final MessageFrame frame) { - return delegatedCodeService.processMutableAccount( + return codeDelegationService.processMutableAccount( this, rootWorldUpdater.getSenderAccount(frame)); } @@ -114,17 +114,17 @@ public void commit() { public Optional parentUpdater() { return rootWorldUpdater.parentUpdater().isPresent() ? Optional.of( - new EVMWorldUpdater(rootWorldUpdater.parentUpdater().get(), delegatedCodeService)) + new EVMWorldUpdater(rootWorldUpdater.parentUpdater().get(), codeDelegationService)) : Optional.empty(); } @Override public WorldUpdater updater() { - return new EVMWorldUpdater(rootWorldUpdater.updater(), delegatedCodeService); + return new EVMWorldUpdater(rootWorldUpdater.updater(), codeDelegationService); } @Override public Account get(final Address address) { - return delegatedCodeService.processAccount(this, rootWorldUpdater.get(address)); + return codeDelegationService.processAccount(this, rootWorldUpdater.get(address)); } }