Skip to content

Commit 83ce851

Browse files
authored
updates regarding blobtx serialization (#4065)
1 parent 65de3a8 commit 83ce851

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

packages/tx/README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ This is the recommended tx type starting with the activation of the `london` HF,
101101
// ./examples/londonTx.ts
102102

103103
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
104+
import type { FeeMarketEIP1559TxData } from '@ethereumjs/tx'
104105
import { createFeeMarket1559Tx } from '@ethereumjs/tx'
105106
import { bytesToHex } from '@ethereumjs/util'
106107

107108
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
108109

109-
const txData = {
110+
const txData: FeeMarketEIP1559TxData = {
110111
data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
111112
gasLimit: '0x02625a00',
112113
maxPriorityFeePerGas: '0x01',
@@ -124,6 +125,7 @@ const txData = {
124125

125126
const tx = createFeeMarket1559Tx(txData, { common })
126127
console.log(bytesToHex(tx.hash())) // 0x6f9ef69ccb1de1aea64e511efd6542541008ced321887937c95b03779358ec8a
128+
127129
```
128130

129131
### Access List Transactions (EIP-2930)
@@ -139,12 +141,13 @@ This transaction type has been introduced along the `berlin` HF. See the followi
139141
// ./examples/accessListTx.ts
140142

141143
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
144+
import type { AccessList2930TxData } from '@ethereumjs/tx'
142145
import { createAccessList2930Tx } from '@ethereumjs/tx'
143146
import { bytesToHex } from '@ethereumjs/util'
144147

145148
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Berlin })
146149

147-
const txData = {
150+
const txData: AccessList2930TxData = {
148151
data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
149152
gasLimit: '0x02625a00',
150153
gasPrice: '0x01',
@@ -169,6 +172,7 @@ const txData = {
169172

170173
const tx = createAccessList2930Tx(txData, { common })
171174
console.log(bytesToHex(tx.hash())) // 0x9150cdebad74e88b038e6c6b964d99af705f9c0883d7f0bbc0f3e072358f5b1d
175+
172176
```
173177

174178
For generating access lists from tx data based on a certain network state there is a `reportAccessList` option
@@ -191,26 +195,26 @@ See the following code snipped for an example on how to instantiate:
191195
// ./examples/blobTx.ts
192196

193197
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
198+
import type { BlobEIP4844TxData } from '@ethereumjs/tx'
194199
import { createBlob4844Tx } from '@ethereumjs/tx'
195-
import { bytesToHex } from '@ethereumjs/util'
200+
import { bytesToHex, randomBytes } from '@ethereumjs/util'
196201
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
197202
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
198203

199204
const main = async () => {
200205
const kzg = new microEthKZG(trustedSetup)
201206
const common = new Common({
202207
chain: Mainnet,
203-
hardfork: Hardfork.Shanghai,
204-
eips: [4844],
208+
hardfork: Hardfork.Cancun,
205209
customCrypto: { kzg },
206210
})
207211

208-
const txData = {
212+
const txData: BlobEIP4844TxData = {
209213
data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
210214
gasLimit: '0x02625a00',
211215
maxPriorityFeePerGas: '0x01',
212216
maxFeePerGas: '0xff',
213-
maxFeePerDataGas: '0xfff',
217+
maxFeePerBlobGas: '0xfff',
214218
nonce: '0x00',
215219
to: '0xcccccccccccccccccccccccccccccccccccccccc',
216220
value: '0x0186a0',
@@ -226,16 +230,27 @@ const main = async () => {
226230
const tx = createBlob4844Tx(txData, { common })
227231

228232
console.log(bytesToHex(tx.hash())) //0x3c3e7c5e09c250d2200bcc3530f4a9088d7e3fb4ea3f4fccfd09f535a3539e84
233+
234+
// To send a transaction via RPC, you can something like this:
235+
// const rawTx = tx.sign(privateKeyBytes).serializeNetworkWrapper()
236+
// myRPCClient.request('eth_sendRawTransaction', [rawTx]) // submits a transaction via RPC
229237
}
230238

231239
void main()
240+
232241
```
233242

234243
Note that `versionedHashes` and `kzgCommitments` have a real length of 32 bytes, `blobs` have a real length of `4096` bytes and values are trimmed here for brevity.
235244

236245
Alternatively, you can pass a `blobsData` property with an array of strings corresponding to a set of blobs and the `fromTxData` constructor will derive the corresponding `blobs`, `versionedHashes`, `kzgCommitments`, and `kzgProofs` for you.
237246

238-
See the [Blob Transaction Tests](./test/eip4844.spec.ts) for examples of usage in instantiating, serializing, and deserializing these transactions.
247+
#### Serialization
248+
249+
Blob transactions can be serialized in two ways.
250+
1) `tx.serialize()` - the standard serialization returns an RLP-encoded `uint8Array` that conforms to the transaction as represented after it is included in a block
251+
2) `tx.serializeNetworkWrapper()` - this serialization format includes the `blobs` in the encoded data and is the format specified for transactions that are being submitted to/gossipped around the mempool. If you are constructing a transaction to submit via JSON-RPC, use this format.
252+
253+
See the [Blob Transaction Tests](./test/eip4844.spec.ts) for additional examples of usage in instantiating, serializing, and deserializing these transactions.
239254

240255
### EOA Code Transaction (EIP-7702)
241256

@@ -293,10 +308,11 @@ See this [example script](./examples/transactions.ts) or the following code exam
293308
// ./examples/legacyTx.ts
294309

295310
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
311+
import type { LegacyTxData } from '@ethereumjs/tx'
296312
import { createLegacyTx } from '@ethereumjs/tx'
297313
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
298314

299-
const txParams = {
315+
const txData: LegacyTxData = {
300316
nonce: '0x0',
301317
gasPrice: '0x09184e72a000',
302318
gasLimit: '0x2710',
@@ -306,7 +322,7 @@ const txParams = {
306322
}
307323

308324
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
309-
const tx = createLegacyTx(txParams, { common })
325+
const tx = createLegacyTx(txData, { common })
310326

311327
const privateKey = hexToBytes('0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109')
312328

packages/tx/examples/blobTx.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
22
import type { BlobEIP4844TxData } from '@ethereumjs/tx'
33
import { createBlob4844Tx } from '@ethereumjs/tx'
4-
import { bytesToHex } from '@ethereumjs/util'
4+
import { bytesToHex, randomBytes } from '@ethereumjs/util'
55
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
66
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
77

@@ -34,6 +34,10 @@ const main = async () => {
3434
const tx = createBlob4844Tx(txData, { common })
3535

3636
console.log(bytesToHex(tx.hash())) //0x3c3e7c5e09c250d2200bcc3530f4a9088d7e3fb4ea3f4fccfd09f535a3539e84
37+
38+
// To send a transaction via RPC, you can something like this:
39+
// const rawTx = tx.sign(privateKeyBytes).serializeNetworkWrapper()
40+
// myRPCClient.request('eth_sendRawTransaction', [rawTx]) // submits a transaction via RPC
3741
}
3842

3943
void main()

packages/tx/src/4844/tx.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,9 @@ export class Blob4844Tx implements TransactionInterface<typeof TransactionType.B
317317
}
318318

319319
/**
320-
* @returns the serialized form of a blob transaction in the network wrapper format (used for gossipping mempool transactions over devp2p)
320+
* @returns the serialized form of a blob transaction in the network wrapper format
321+
* This format is used for gossipping mempool transactions over devp2p or when
322+
* submitting a transaction via RPC.
321323
*/
322324
serializeNetworkWrapper(): Uint8Array {
323325
if (

0 commit comments

Comments
 (0)