-
Notifications
You must be signed in to change notification settings - Fork 1k
[4844] Add encodingContext to TransactionEncoder and TransactionDecoder #5820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
feaab57
1499768
318bc16
59665a7
9f06b73
f31a783
7b3f751
b20f6d4
ed726a4
ede2408
a1402c8
792e735
3fdc9a9
63124a1
8594933
b453cf7
a877c99
b678893
8ee0c89
1f1fc12
88ba4bf
25a78ed
0d62138
ea4651d
c99cb7c
80a10b8
30eca31
987a4a5
b57c906
2084eff
386112e
ef7fc75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcRequestException; | ||
| import org.hyperledger.besu.ethereum.core.Transaction; | ||
| import org.hyperledger.besu.ethereum.core.encoding.EncodingContext; | ||
| import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder; | ||
| import org.hyperledger.besu.ethereum.rlp.RLPException; | ||
|
|
||
|
|
@@ -27,7 +28,7 @@ public static Transaction decodeRawTransaction(final String rawTransaction) | |
| throws InvalidJsonRpcRequestException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we rename this method or get rid of it? It does not reflect which format to decode now that we have the two different kinds ...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any suggestions for names? It is used to handle the serialization in the RPC apis |
||
| try { | ||
| Bytes txnBytes = Bytes.fromHexString(rawTransaction); | ||
| return TransactionDecoder.decodeOpaqueBytes(txnBytes); | ||
| return TransactionDecoder.decodeOpaqueBytes(txnBytes, EncodingContext.POOLED_TRANSACTION); | ||
| } catch (final IllegalArgumentException e) { | ||
| throw new InvalidJsonRpcRequestException("Invalid raw transaction hex", e); | ||
| } catch (final RLPException r) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,9 @@ | |
| import org.hyperledger.besu.datatypes.TransactionType; | ||
| import org.hyperledger.besu.datatypes.VersionedHash; | ||
| import org.hyperledger.besu.datatypes.Wei; | ||
| import org.hyperledger.besu.ethereum.core.encoding.AccessListTransactionEncoder; | ||
| import org.hyperledger.besu.ethereum.core.encoding.BlobTransactionEncoder; | ||
| import org.hyperledger.besu.ethereum.core.encoding.EncodingContext; | ||
| import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder; | ||
| import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder; | ||
| import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; | ||
|
|
@@ -126,7 +128,7 @@ public static Transaction readFrom(final Bytes rlpBytes) { | |
| } | ||
|
|
||
| public static Transaction readFrom(final RLPInput rlpInput) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can only decode one of the two encoding formats now. Should this be renamed? Should it decode both formats? Should we remove it and only rely on the TransactionDecoder? |
||
| return TransactionDecoder.decodeForWire(rlpInput); | ||
| return TransactionDecoder.decodeRLP(rlpInput, EncodingContext.BLOCK_BODY); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -613,7 +615,7 @@ private Bytes32 getOrComputeSenderRecoveryHash() { | |
| * @param out the output to write the transaction to | ||
| */ | ||
| public void writeTo(final RLPOutput out) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s.a. |
||
| TransactionEncoder.encodeForWire(this, out); | ||
| TransactionEncoder.encodeRLP(this, out, EncodingContext.BLOCK_BODY); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -676,18 +678,17 @@ public int getSize() { | |
| } | ||
|
|
||
| private void memoizeHashAndSize() { | ||
| final Bytes bytes = TransactionEncoder.encodeOpaqueBytes(this); | ||
| final Bytes bytes = TransactionEncoder.encodeOpaqueBytes(this, EncodingContext.BLOCK_BODY); | ||
| hash = Hash.hash(bytes); | ||
|
|
||
| if (transactionType.supportsBlob()) { | ||
| if (getBlobsWithCommitments().isPresent()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure that we always want the size including the blobs when they are available? Or should we have a size and a size method that takes the encoding context as an argument? |
||
| size = bytes.size(); | ||
| final Bytes pooledBytes = | ||
| TransactionEncoder.encodeOpaqueBytes(this, EncodingContext.POOLED_TRANSACTION); | ||
| size = pooledBytes.size(); | ||
| return; | ||
| } | ||
| } | ||
| final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); | ||
| TransactionEncoder.encodeForWire(transactionType, bytes, rlpOutput); | ||
| size = rlpOutput.encodedSize(); | ||
| size = bytes.size(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -968,7 +969,7 @@ private static void eip1559PreimageFields( | |
| rlpOutput.writeBytes(to.map(Bytes::copy).orElse(Bytes.EMPTY)); | ||
| rlpOutput.writeUInt256Scalar(value); | ||
| rlpOutput.writeBytes(payload); | ||
| TransactionEncoder.writeAccessList(rlpOutput, accessList); | ||
| AccessListTransactionEncoder.writeAccessList(rlpOutput, accessList); | ||
| } | ||
|
|
||
| private static Bytes blobPreimage( | ||
|
|
@@ -1019,7 +1020,7 @@ private static Bytes accessListPreimage( | |
| RLP.encode( | ||
| rlpOutput -> { | ||
| rlpOutput.startList(); | ||
| TransactionEncoder.encodeAccessListInner( | ||
| AccessListTransactionEncoder.encodeAccessListInner( | ||
| chainId, nonce, gasPrice, gasLimit, to, value, payload, accessList, rlpOutput); | ||
| rlpOutput.endList(); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright Hyperledger Besu Contributors. | ||
| * | ||
| * 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.core.encoding; | ||
|
|
||
| import org.hyperledger.besu.crypto.SignatureAlgorithm; | ||
| import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; | ||
| import org.hyperledger.besu.datatypes.AccessListEntry; | ||
| import org.hyperledger.besu.datatypes.Address; | ||
| import org.hyperledger.besu.datatypes.TransactionType; | ||
| import org.hyperledger.besu.datatypes.Wei; | ||
| import org.hyperledger.besu.ethereum.core.Transaction; | ||
| import org.hyperledger.besu.ethereum.rlp.RLPInput; | ||
|
|
||
| import java.math.BigInteger; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import com.google.common.base.Suppliers; | ||
|
|
||
| class AccessListTransactionDecoder { | ||
| private static final Supplier<SignatureAlgorithm> SIGNATURE_ALGORITHM = | ||
| Suppliers.memoize(SignatureAlgorithmFactory::getInstance); | ||
|
|
||
| public static Transaction decode(final RLPInput rlpInput) { | ||
| rlpInput.enterList(); | ||
| final Transaction.Builder preSignatureTransactionBuilder = | ||
| Transaction.builder() | ||
| .type(TransactionType.ACCESS_LIST) | ||
| .chainId(BigInteger.valueOf(rlpInput.readLongScalar())) | ||
| .nonce(rlpInput.readLongScalar()) | ||
| .gasPrice(Wei.of(rlpInput.readUInt256Scalar())) | ||
| .gasLimit(rlpInput.readLongScalar()) | ||
| .to( | ||
| rlpInput.readBytes( | ||
| addressBytes -> addressBytes.size() == 0 ? null : Address.wrap(addressBytes))) | ||
| .value(Wei.of(rlpInput.readUInt256Scalar())) | ||
| .payload(rlpInput.readBytes()) | ||
| .accessList( | ||
| rlpInput.readList( | ||
| accessListEntryRLPInput -> { | ||
Gabriel-Trintinalia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| accessListEntryRLPInput.enterList(); | ||
| final AccessListEntry accessListEntry = | ||
| new AccessListEntry( | ||
| Address.wrap(accessListEntryRLPInput.readBytes()), | ||
| accessListEntryRLPInput.readList(RLPInput::readBytes32)); | ||
| accessListEntryRLPInput.leaveList(); | ||
| return accessListEntry; | ||
| })); | ||
| final byte recId = (byte) rlpInput.readIntScalar(); | ||
| final Transaction transaction = | ||
| preSignatureTransactionBuilder | ||
| .signature( | ||
| SIGNATURE_ALGORITHM | ||
| .get() | ||
| .createSignature( | ||
| rlpInput.readUInt256Scalar().toUnsignedBigInteger(), | ||
| rlpInput.readUInt256Scalar().toUnsignedBigInteger(), | ||
| recId)) | ||
| .build(); | ||
| rlpInput.leaveList(); | ||
| return transaction; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.