From 9499beabfc70b03677e9aa496ad0568cedaa040c Mon Sep 17 00:00:00 2001 From: Fabio Di Fabio Date: Tue, 3 Oct 2023 13:27:59 +0200 Subject: [PATCH] Only use the buider to create transactions Signed-off-by: Fabio Di Fabio --- .../internal/methods/EthGasPriceTest.java | 17 +- .../eea/BaseEeaSendRawTransaction.java | 109 ++++++------ .../besu/ethereum/core/Transaction.java | 156 ------------------ .../eth/messages/MessageWrapperTest.java | 77 +++++---- .../besu/evmtool/EvmToolCommand.java | 21 +-- 5 files changed, 111 insertions(+), 269 deletions(-) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGasPriceTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGasPriceTest.java index 06acb91a317..eef2b4422e8 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGasPriceTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGasPriceTest.java @@ -169,17 +169,12 @@ private Object createFakeBlock(final Long height) { null), new BlockBody( List.of( - new Transaction( - 0, - Wei.of(height * 1000000L), - 0, - Optional.empty(), - Wei.ZERO, - null, - Bytes.EMPTY, - Address.ZERO, - Optional.empty(), - Optional.empty())), + new Transaction.Builder() + .nonce(0) + .gasPrice(Wei.of(height * 1000000L)) + .gasLimit(0) + .value(Wei.ZERO) + .build()), List.of()))); } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/BaseEeaSendRawTransaction.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/BaseEeaSendRawTransaction.java index d6f204a79fa..c54ba0107b3 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/BaseEeaSendRawTransaction.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/privacy/methods/eea/BaseEeaSendRawTransaction.java @@ -38,7 +38,6 @@ import java.math.BigInteger; import java.util.List; -import java.util.Optional; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; @@ -68,64 +67,64 @@ public class BaseEeaSendRawTransaction { final GasCalculator gasCalculator = new BerlinGasCalculator(); final Transaction PUBLIC_FLEXIBLE_TRANSACTION = - new Transaction( - 0L, - Wei.of(1), - 21000L, - Optional.of(FLEXIBLE_PRIVACY), - Wei.ZERO, - SIGNATURE_ALGORITHM_SUPPLIER - .get() - .createSignature( - new BigInteger( - "104310573331543561412661001400556426894275857431274618344686100036716947434951"), - new BigInteger( - "33080506591748900530090726168809539464160321639149722208454899701475015405641"), - Byte.parseByte("1")), - Bytes.fromBase64String(MOCK_ORION_KEY), - Address.wrap(Bytes.fromHexString("0x8411b12666f68ef74cace3615c9d5a377729d03f")), - Optional.empty(), - Optional.empty()); + new Transaction.Builder() + .nonce(0) + .gasPrice(Wei.ONE) + .gasLimit(21000) + .value(Wei.ZERO) + .to(FLEXIBLE_PRIVACY) + .signature( + SIGNATURE_ALGORITHM_SUPPLIER + .get() + .createSignature( + new BigInteger( + "104310573331543561412661001400556426894275857431274618344686100036716947434951"), + new BigInteger( + "33080506591748900530090726168809539464160321639149722208454899701475015405641"), + Byte.parseByte("1"))) + .payload(Bytes.fromBase64String(MOCK_ORION_KEY)) + .sender(Address.wrap(Bytes.fromHexString("0x8411b12666f68ef74cace3615c9d5a377729d03f"))) + .build(); final Transaction PUBLIC_PLUGIN_TRANSACTION = - new Transaction( - 0L, - Wei.of(1), - 21112L, - Optional.of(PLUGIN_PRIVACY), - Wei.ZERO, - SIGNATURE_ALGORITHM_SUPPLIER - .get() - .createSignature( - new BigInteger( - "111331907905663242841915789134040957461022579868467291368609335839524284474080"), - new BigInteger( - "16338460226177675602590882211136457396059831699034102939076916361204709826919"), - Byte.parseByte("0")), - Bytes.fromBase64String(MOCK_ORION_KEY), - Address.wrap(Bytes.fromHexString("0x8411b12666f68ef74cace3615c9d5a377729d03f")), - Optional.empty(), - Optional.empty()); + new Transaction.Builder() + .nonce(0) + .gasPrice(Wei.ONE) + .gasLimit(21112) + .value(Wei.ZERO) + .to(PLUGIN_PRIVACY) + .signature( + SIGNATURE_ALGORITHM_SUPPLIER + .get() + .createSignature( + new BigInteger( + "111331907905663242841915789134040957461022579868467291368609335839524284474080"), + new BigInteger( + "16338460226177675602590882211136457396059831699034102939076916361204709826919"), + Byte.parseByte("0"))) + .payload(Bytes.fromBase64String(MOCK_ORION_KEY)) + .sender(Address.wrap(Bytes.fromHexString("0x8411b12666f68ef74cace3615c9d5a377729d03f"))) + .build(); final Transaction PUBLIC_OFF_CHAIN_TRANSACTION = - new Transaction( - 0L, - Wei.of(1), - 21000L, - Optional.of(DEFAULT_PRIVACY), - Wei.ZERO, - SIGNATURE_ALGORITHM_SUPPLIER - .get() - .createSignature( - new BigInteger( - "45331864585825234947874751069766983839005678711670143534492294352090223768785"), - new BigInteger( - "32813839561238589140263096892921088101761344639911577803805398248765156383629"), - Byte.parseByte("1")), - Bytes.fromBase64String(MOCK_ORION_KEY), - Address.wrap(Bytes.fromHexString("0x8411b12666f68ef74cace3615c9d5a377729d03f")), - Optional.empty(), - Optional.empty()); + new Transaction.Builder() + .nonce(0) + .gasPrice(Wei.ONE) + .gasLimit(21000) + .value(Wei.ZERO) + .to(DEFAULT_PRIVACY) + .signature( + SIGNATURE_ALGORITHM_SUPPLIER + .get() + .createSignature( + new BigInteger( + "45331864585825234947874751069766983839005678711670143534492294352090223768785"), + new BigInteger( + "32813839561238589140263096892921088101761344639911577803805398248765156383629"), + Byte.parseByte("1"))) + .payload(Bytes.fromBase64String(MOCK_ORION_KEY)) + .sender(Address.wrap(Bytes.fromHexString("0x8411b12666f68ef74cace3615c9d5a377729d03f"))) + .build(); final JsonRpcRequestContext validPrivateForTransactionRequest = new JsonRpcRequestContext( diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java index a7cad4b8d85..2da83096f4a 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java @@ -226,162 +226,6 @@ public Transaction( } } - public Transaction( - final long nonce, - final Optional gasPrice, - final Optional maxPriorityFeePerGas, - final Optional maxFeePerGas, - final Optional maxFeePerBlobGas, - final long gasLimit, - final Optional
to, - final Wei value, - final SECPSignature signature, - final Bytes payload, - final Address sender, - final Optional chainId, - final Optional> versionedHashes, - final Optional blobsWithCommitments) { - this( - TransactionType.FRONTIER, - nonce, - gasPrice, - maxPriorityFeePerGas, - maxFeePerGas, - maxFeePerBlobGas, - gasLimit, - to, - value, - signature, - payload, - Optional.empty(), - sender, - chainId, - versionedHashes, - blobsWithCommitments); - } - - public Transaction( - final long nonce, - final Wei gasPrice, - final long gasLimit, - final Address to, - final Wei value, - final SECPSignature signature, - final Bytes payload, - final Optional chainId, - final Optional> versionedHashes, - final Optional blobsWithCommitments) { - this( - TransactionType.FRONTIER, - nonce, - Optional.of(gasPrice), - Optional.empty(), - Optional.empty(), - Optional.empty(), - gasLimit, - Optional.of(to), - value, - signature, - payload, - Optional.empty(), - null, - chainId, - versionedHashes, - blobsWithCommitments); - } - - /** - * Instantiates a transaction instance. - * - * @param nonce the nonce - * @param gasPrice the gas price - * @param gasLimit the gas limit - * @param to the transaction recipient - * @param value the value being transferred to the recipient - * @param signature the signature - * @param payload the payload - * @param sender the transaction sender - * @param chainId the chain id to apply the transaction to - *

The {@code to} will be an {@code Optional.empty()} for a contract creation transaction; - * otherwise it should contain an address. - *

The {@code chainId} must be greater than 0 to be applied to a specific chain; otherwise - * it will default to any chain. - */ - public Transaction( - final long nonce, - final Wei gasPrice, - final long gasLimit, - final Optional

to, - final Wei value, - final SECPSignature signature, - final Bytes payload, - final Address sender, - final Optional chainId, - final Optional> versionedHashes) { - this( - nonce, - Optional.of(gasPrice), - Optional.empty(), - Optional.empty(), - Optional.empty(), - gasLimit, - to, - value, - signature, - payload, - sender, - chainId, - versionedHashes, - Optional.empty()); - } - - /** - * Instantiates a transaction instance. - * - * @param nonce the nonce - * @param gasPrice the gas price - * @param gasLimit the gas limit - * @param to the transaction recipient - * @param value the value being transferred to the recipient - * @param signature the signature - * @param payload the payload - * @param sender the transaction sender - * @param chainId the chain id to apply the transaction to - *

The {@code to} will be an {@code Optional.empty()} for a contract creation transaction; - * otherwise it should contain an address. - *

The {@code chainId} must be greater than 0 to be applied to a specific chain; otherwise - * it will default to any chain. - */ - public Transaction( - final long nonce, - final Wei gasPrice, - final long gasLimit, - final Optional

to, - final Wei value, - final SECPSignature signature, - final Bytes payload, - final Address sender, - final Optional chainId, - final Optional maxFeePerBlobGas, - final Optional> versionedHashes, - final Optional blobsWithCommitments) { - this( - nonce, - Optional.of(gasPrice), - Optional.empty(), - Optional.empty(), - maxFeePerBlobGas, - gasLimit, - to, - value, - signature, - payload, - sender, - chainId, - versionedHashes, - blobsWithCommitments); - } - /** * Returns the transaction nonce. * diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/MessageWrapperTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/MessageWrapperTest.java index 8d5ebb96de5..78fcbb1c085 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/MessageWrapperTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/MessageWrapperTest.java @@ -52,14 +52,24 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.fasterxml.jackson.databind.module.SimpleModule; import org.apache.tuweni.bytes.Bytes; import org.junit.jupiter.api.Test; public class MessageWrapperTest { private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final SimpleModule module = new SimpleModule(); + + static { + module.addDeserializer(Transaction.class, new TransactionDeserializer()); + objectMapper.registerModule(module); + } @Test public void GetBlockHeaders() throws IOException { @@ -228,8 +238,7 @@ public void PooledTransactions() throws IOException { PooledTransactionsMessage.create( Arrays.asList( objectMapper.treeToValue( - testJson.get("data").get("PooledTransactionsPacket"), - TestTransaction[].class))); + testJson.get("data").get("PooledTransactionsPacket"), Transaction[].class))); final Bytes actual = pooledTransactionsMessage.wrapMessageData(BigInteger.valueOf(1111)).getData(); assertThat(actual).isEqualTo(expected); @@ -291,48 +300,46 @@ public void readBodyFieldsThrowsIfThereIsListWrappingBodyFields() { () -> BlockBody.readFrom(bytesValueRLPInput, new MainnetBlockHeaderFunctions())); } - private static class TestTransaction extends Transaction { + private static class TransactionDeserializer extends StdDeserializer { + protected TransactionDeserializer() { + this(null); + } - @JsonCreator - public TestTransaction( - @JsonProperty("nonce") final String nonce, - @JsonProperty("gasPrice") final String gasPrice, - @JsonProperty("gas") final String gasLimit, - @JsonProperty("to") final String to, - @JsonProperty("value") final String value, - @JsonProperty("input") final String data, - @JsonProperty("v") final String v, - @JsonProperty("r") final String r, - @JsonProperty("s") final String s, - @JsonProperty("hash") final String __) { + protected TransactionDeserializer(final Class vc) { + super(vc); + } - super( - Bytes.fromHexStringLenient(nonce).toLong(), - Wei.fromHexString(gasPrice), - Long.decode(gasLimit), - Address.fromHexString(to), - Wei.fromHexString(value), - new SECP256K1() - .createSignature( - new BigInteger(r.substring(2), 16), - new BigInteger(s.substring(2), 16), - recIdAndChainId(Byte.decode(v)).getKey()), - Bytes.fromHexString(data), - recIdAndChainId(Byte.decode(v)).getValue(), - Optional.empty(), - Optional.empty()); + @Override + public Transaction deserialize(final JsonParser p, final DeserializationContext ctxt) + throws IOException { + JsonNode node = p.getCodec().readTree(p); + return new Transaction.Builder() + .nonce(Bytes.fromHexStringLenient(node.get("nonce").asText()).toLong()) + .gasPrice(Wei.fromHexString(node.get("gasPrice").asText())) + .gasLimit(Long.decode(node.get("gas").asText())) + .to(Address.fromHexString(node.get("to").asText())) + .value(Wei.fromHexString(node.get("value").asText())) + .signature( + new SECP256K1() + .createSignature( + new BigInteger(node.get("r").asText().substring(2), 16), + new BigInteger(node.get("s").asText().substring(2), 16), + recIdAndChainId(Byte.decode(node.get("v").asText())).getKey())) + .payload(Bytes.fromHexString(node.get("input").asText())) + .chainId(recIdAndChainId(Byte.decode(node.get("v").asText())).getValue()) + .build(); } } - private static Map.Entry> recIdAndChainId(final Byte vByte) { + private static Map.Entry recIdAndChainId(final Byte vByte) { final BigInteger v = BigInteger.valueOf(vByte); final byte recId; - Optional chainId = Optional.empty(); + BigInteger chainId = null; if (v.equals(REPLAY_UNPROTECTED_V_BASE) || v.equals(REPLAY_UNPROTECTED_V_BASE_PLUS_1)) { recId = v.subtract(REPLAY_UNPROTECTED_V_BASE).byteValueExact(); } else if (v.compareTo(REPLAY_PROTECTED_V_MIN) > 0) { - chainId = Optional.of(v.subtract(REPLAY_PROTECTED_V_BASE).divide(TWO)); - recId = v.subtract(TWO.multiply(chainId.get()).add(REPLAY_PROTECTED_V_BASE)).byteValueExact(); + chainId = v.subtract(REPLAY_PROTECTED_V_BASE).divide(TWO); + recId = v.subtract(TWO.multiply(chainId).add(REPLAY_PROTECTED_V_BASE)).byteValueExact(); } else { throw new RuntimeException( String.format("An unsupported encoded `v` value of %s was found", v)); @@ -343,7 +350,7 @@ private static Map.Entry> recIdAndChainId(final Byte public static class TestBlockBody extends BlockBody { @JsonCreator public TestBlockBody( - @JsonProperty("Transactions") final List transactions, + @JsonProperty("Transactions") final List transactions, @JsonProperty("Uncles") final List uncles) { super( transactions.stream().collect(toUnmodifiableList()), diff --git a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java index 0e2daf96eb6..e2b9d69a846 100644 --- a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java +++ b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java @@ -58,7 +58,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.NavigableMap; -import java.util.Optional; import java.util.stream.Collectors; import com.google.common.base.Joiner; @@ -338,17 +337,15 @@ public void run() { final ProtocolSpec protocolSpec = component.getProtocolSpec().apply(BlockHeaderBuilder.createDefault().buildBlockHeader()); final Transaction tx = - new Transaction( - 0, - Wei.ZERO, - Long.MAX_VALUE, - Optional.ofNullable(receiver), - Wei.ZERO, - null, - callData, - sender, - Optional.empty(), - Optional.empty()); + new Transaction.Builder() + .nonce(0) + .gasPrice(Wei.ZERO) + .gasLimit(Long.MAX_VALUE) + .to(receiver) + .value(Wei.ZERO) + .payload(callData) + .sender(sender) + .build(); final long intrinsicGasCost = protocolSpec