Serialize CodeDelegation in RPCs (#8265)

Supports eth_getTransactionByHash

Signed-off-by: Simon Dudley <simon.dudley@consensys.net>
This commit is contained in:
Simon Dudley
2025-02-07 14:15:10 +10:00
committed by GitHub
parent a021feae03
commit 08e3427439
4 changed files with 114 additions and 4 deletions

View File

@@ -0,0 +1,70 @@
/*
* 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.ethereum.api.jsonrpc.internal.results;
import org.hyperledger.besu.datatypes.CodeDelegation;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.tuweni.units.bigints.UInt256;
@JsonPropertyOrder({"chainId", "address", "nonce", "yParity", "r", "s"})
public class CodeDelegationResult {
private final String chainId;
private final String address;
private final String nonce;
private final String yParity;
private final String r;
private final String s;
public CodeDelegationResult(final CodeDelegation codeDelegation) {
chainId = Quantity.create(UInt256.valueOf(codeDelegation.chainId()));
address = codeDelegation.address().toHexString();
nonce = Quantity.create(codeDelegation.nonce());
yParity = Quantity.create(codeDelegation.v());
r = Quantity.create(codeDelegation.r());
s = Quantity.create(codeDelegation.s());
}
@JsonProperty("chainId")
public String chainId() {
return chainId;
}
@JsonProperty("address")
public String address() {
return address;
}
@JsonProperty("nonce")
public String nonce() {
return nonce;
}
@JsonProperty("yParity")
public String yParity() {
return yParity;
}
@JsonProperty("r")
public String r() {
return r;
}
@JsonProperty("s")
public String s() {
return s;
}
}

View File

@@ -23,6 +23,7 @@ import org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata;
import org.hyperledger.besu.ethereum.core.Transaction;
import java.util.List;
import java.util.Optional;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -94,7 +95,7 @@ public class TransactionCompleteResult implements TransactionResult {
private final List<VersionedHash> versionedHashes;
@JsonInclude(JsonInclude.Include.NON_NULL)
private final List<CodeDelegation> authorizationList;
private final List<CodeDelegationResult> authorizationList;
public TransactionCompleteResult(final TransactionWithMetadata tx) {
final Transaction transaction = tx.getTransaction();
@@ -140,7 +141,11 @@ public class TransactionCompleteResult implements TransactionResult {
this.r = Quantity.create(transaction.getR());
this.s = Quantity.create(transaction.getS());
this.versionedHashes = transaction.getVersionedHashes().orElse(null);
this.authorizationList = transaction.getCodeDelegationList().orElse(null);
final Optional<List<CodeDelegation>> codeDelegationList = transaction.getCodeDelegationList();
this.authorizationList =
codeDelegationList
.map(cds -> cds.stream().map(CodeDelegationResult::new).toList())
.orElse(null);
}
@JsonGetter(value = "accessList")
@@ -256,7 +261,7 @@ public class TransactionCompleteResult implements TransactionResult {
}
@JsonGetter(value = "authorizationList")
public List<CodeDelegation> getAuthorizationList() {
public List<CodeDelegationResult> getAuthorizationList() {
return authorizationList;
}
}

View File

@@ -50,8 +50,9 @@ import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class EthGetTransactionByHashTest {
// EIP-7702 Set Code Transaction
private static final String VALID_TRANSACTION =
"0xf86d0485174876e800830222e0945aae326516b4f8fe08074b7e972e40a713048d62880de0b6b3a7640000801ba05d4e7998757264daab67df2ce6f7e7a0ae36910778a406ca73898c9899a32b9ea0674700d5c3d1d27f2e6b4469957dfd1a1c49bf92383d80717afc84eb05695d5b";
"0xb8c404f8c1018080078307a12094a94f5374fce5edbc8e2a8697c15331677e6ebf0b8080c0f85cf85a809400000000000000000000000000000000000010008080a0dbcff17ff6c249f13b334fa86bcbaa1afd9f566ca9b06e4ea5fab9bdde9a9202a05c34c9d8af5b20e4a425fc1daf2d9d484576857eaf1629145b4686bac733868e01a0d61673cd58ffa5fc605c3215aa4647fa3afbea1d1f577e08402442992526d980a0063068ca818025c7b8493d0623cb70ef3a2ba4b3e2ae0af1146d1c9b065c0aff";
@Mock private BlockchainQueries blockchainQueries;
private EthGetTransactionByHash method;

View File

@@ -19,10 +19,13 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hyperledger.besu.evm.account.Account.MAX_NONCE;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.TransactionType;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.core.CodeDelegation;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPException;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
@@ -44,6 +47,37 @@ class TransactionRLPDecoderTest {
"0xb8a902f8a686796f6c6f7632800285012a05f20082753094000000000000000000000000000000000000aaaa8080f838f794000000000000000000000000000000000000aaaae1a0000000000000000000000000000000000000000000000000000000000000000001a00c1d69648e348fe26155b45de45004f0e4195f6352d8f0935bc93e98a3e2a862a060064e5b9765c0ac74223b0cf49635c59ae0faf82044fd17bcc68a549ade6f95";
private static final String NONCE_64_BIT_MAX_MINUS_2_TX_RLP =
"0xf86788fffffffffffffffe0182520894095e7baea6a6c7c4c2dfeb977efac326af552d8780801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a01fffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804";
private static final String EIP7702_SET_CODE_TX_OPAQUE =
"0x04f8c1018080078307a12094a94f5374fce5edbc8e2a8697c15331677e6ebf0b8080c0f85cf85a809400000000000000000000000000000000000010008080a0dbcff17ff6c249f13b334fa86bcbaa1afd9f566ca9b06e4ea5fab9bdde9a9202a05c34c9d8af5b20e4a425fc1daf2d9d484576857eaf1629145b4686bac733868e01a0d61673cd58ffa5fc605c3215aa4647fa3afbea1d1f577e08402442992526d980a0063068ca818025c7b8493d0623cb70ef3a2ba4b3e2ae0af1146d1c9b065c0aff";
@Test
void decodeEIP7702SetCodeTransaction() {
final Transaction transaction =
TransactionDecoder.decodeOpaqueBytes(
Bytes.fromHexString(EIP7702_SET_CODE_TX_OPAQUE), EncodingContext.BLOCK_BODY);
final BytesValueRLPOutput rlpOut = new BytesValueRLPOutput();
TransactionEncoder.encodeRLP(transaction, rlpOut, EncodingContext.BLOCK_BODY);
assertThat(rlpOut.encoded())
.isEqualTo(
Bytes.fromHexString(
"0xb8c404f8c1018080078307a12094a94f5374fce5edbc8e2a8697c15331677e6ebf0b8080c0f85cf85a809400000000000000000000000000000000000010008080a0dbcff17ff6c249f13b334fa86bcbaa1afd9f566ca9b06e4ea5fab9bdde9a9202a05c34c9d8af5b20e4a425fc1daf2d9d484576857eaf1629145b4686bac733868e01a0d61673cd58ffa5fc605c3215aa4647fa3afbea1d1f577e08402442992526d980a0063068ca818025c7b8493d0623cb70ef3a2ba4b3e2ae0af1146d1c9b065c0aff"));
assertThat(transaction.getType()).isEqualTo(TransactionType.DELEGATE_CODE);
assertThat(transaction.getCodeDelegationList()).isPresent();
assertThat(transaction.getCodeDelegationList().get().size()).isEqualTo(1);
var expectedCodeDelegation =
CodeDelegation.createCodeDelegation(
BigInteger.ZERO,
Address.fromHexString("0x1000"),
"0x0",
"0x0",
"0xdbcff17ff6c249f13b334fa86bcbaa1afd9f566ca9b06e4ea5fab9bdde9a9202",
"0x5c34c9d8af5b20e4a425fc1daf2d9d484576857eaf1629145b4686bac733868e");
var actualCodeDelegation = transaction.getCodeDelegationList().get().getFirst();
assertThat(actualCodeDelegation.chainId()).isEqualTo(expectedCodeDelegation.chainId());
assertThat(actualCodeDelegation.address()).isEqualTo(expectedCodeDelegation.address());
assertThat(actualCodeDelegation.nonce()).isEqualTo(expectedCodeDelegation.nonce());
assertThat(actualCodeDelegation.signature()).isEqualTo(expectedCodeDelegation.signature());
}
@Test
void decodeFrontierNominalCase() {