Skip to content

Commit e39cbf9

Browse files
committed
add blobTx
1 parent da6185c commit e39cbf9

23 files changed

+5648
-183
lines changed

core/rawdb/accessors_rollup_batch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func WriteRollupBatch(db ethdb.KeyValueWriter, batch types.RollupBatch) {
15-
bz, err := rlp.EncodeToBytes(&batch)
15+
bz, err := batch.Encode()
1616
if err != nil {
1717
log.Crit("failed to RLP encode batch", "batch index", batch.Index, "err", err)
1818
}
@@ -49,7 +49,7 @@ func ReadRollupBatch(db ethdb.Reader, batchIndex uint64) *types.RollupBatch {
4949
}
5050

5151
rb := new(types.RollupBatch)
52-
if err = rlp.Decode(bytes.NewReader(data), rb); err != nil {
52+
if err = rb.Decode(data); err != nil {
5353
log.Crit("Invalid RollupBatch RLP", "batch index", batchIndex, "data", data, "err", err)
5454
}
5555
return rb

core/tx_pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ func (pool *TxPool) local() map[common.Address]types.Transactions {
612612
// rules and adheres to some heuristic limits of the local node (price and size).
613613
func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
614614
// No unauthenticated deposits allowed in the transaction pool.
615-
if tx.IsL1MessageTx() {
615+
if tx.IsL1MessageTx() || tx.Type() == types.BlobTxType {
616616
return ErrTxTypeNotSupported
617617
}
618618

core/types/access_list_tx.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package types
1818

1919
import (
20+
"bytes"
2021
"math/big"
2122

2223
"github.com/scroll-tech/go-ethereum/common"
24+
"github.com/scroll-tech/go-ethereum/rlp"
2325
)
2426

2527
//go:generate gencodec -type AccessTuple -out gen_access_tuple.go
@@ -113,3 +115,11 @@ func (tx *AccessListTx) rawSignatureValues() (v, r, s *big.Int) {
113115
func (tx *AccessListTx) setSignatureValues(chainID, v, r, s *big.Int) {
114116
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
115117
}
118+
119+
func (tx *AccessListTx) encode(b *bytes.Buffer) error {
120+
return rlp.Encode(b, tx)
121+
}
122+
123+
func (tx *AccessListTx) decode(input []byte) error {
124+
return rlp.DecodeBytes(input, tx)
125+
}

core/types/dynamic_fee_tx.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package types
1818

1919
import (
20+
"bytes"
2021
"math/big"
2122

2223
"github.com/scroll-tech/go-ethereum/common"
24+
"github.com/scroll-tech/go-ethereum/rlp"
2325
)
2426

2527
type DynamicFeeTx struct {
@@ -101,3 +103,11 @@ func (tx *DynamicFeeTx) rawSignatureValues() (v, r, s *big.Int) {
101103
func (tx *DynamicFeeTx) setSignatureValues(chainID, v, r, s *big.Int) {
102104
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
103105
}
106+
107+
func (tx *DynamicFeeTx) encode(b *bytes.Buffer) error {
108+
return rlp.Encode(b, tx)
109+
}
110+
111+
func (tx *DynamicFeeTx) decode(input []byte) error {
112+
return rlp.DecodeBytes(input, tx)
113+
}

core/types/gen_batch.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/types/l1_message_tx.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package types
22

33
import (
4+
"bytes"
5+
"github.com/scroll-tech/go-ethereum/rlp"
46
"math/big"
57

68
"github.com/scroll-tech/go-ethereum/common"
@@ -52,3 +54,11 @@ func (tx *L1MessageTx) rawSignatureValues() (v, r, s *big.Int) {
5254
func (tx *L1MessageTx) setSignatureValues(chainID, v, r, s *big.Int) {
5355
// this is a noop for l1 message transactions
5456
}
57+
58+
func (tx *L1MessageTx) encode(b *bytes.Buffer) error {
59+
return rlp.Encode(b, tx)
60+
}
61+
62+
func (tx *L1MessageTx) decode(input []byte) error {
63+
return rlp.DecodeBytes(input, tx)
64+
}

core/types/legacy_tx.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package types
1818

1919
import (
20+
"bytes"
2021
"math/big"
2122

2223
"github.com/scroll-tech/go-ethereum/common"
@@ -110,3 +111,11 @@ func (tx *LegacyTx) rawSignatureValues() (v, r, s *big.Int) {
110111
func (tx *LegacyTx) setSignatureValues(chainID, v, r, s *big.Int) {
111112
tx.V, tx.R, tx.S = v, r, s
112113
}
114+
115+
func (tx *LegacyTx) encode(*bytes.Buffer) error {
116+
panic("encode called on LegacyTx")
117+
}
118+
119+
func (tx *LegacyTx) decode([]byte) error {
120+
panic("decode called on LegacyTx)")
121+
}

core/types/rollup_batch.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package types
33
import (
44
"github.com/scroll-tech/go-ethereum/common"
55
"github.com/scroll-tech/go-ethereum/common/hexutil"
6+
"github.com/scroll-tech/go-ethereum/crypto/kzg4844"
7+
"github.com/scroll-tech/go-ethereum/rlp"
68
)
79

810
//go:generate go run github.com/fjl/gencodec -type RollupBatch -field-override rollupBatchMarshaling -out gen_batch.go
@@ -17,6 +19,8 @@ type RollupBatch struct {
1719
PrevStateRoot common.Hash
1820
PostStateRoot common.Hash
1921
WithdrawRoot common.Hash
22+
23+
Sidecar *BlobTxSidecar `rlp:"-"`
2024
}
2125

2226
type rollupBatchMarshaling struct {
@@ -27,6 +31,54 @@ type rollupBatchMarshaling struct {
2731
SkippedL1MessageBitmap hexutil.Bytes
2832
}
2933

34+
// blobTxWithBlobs is used for encoding of transactions when blobs are present.
35+
type rollupBatchWithBlobs struct {
36+
Batch *RollupBatch
37+
Blobs []kzg4844.Blob
38+
Commitments []kzg4844.Commitment
39+
Proofs []kzg4844.Proof
40+
}
41+
42+
func (r *RollupBatch) Encode() ([]byte, error) {
43+
if r.Sidecar == nil {
44+
return rlp.EncodeToBytes(r)
45+
}
46+
inner := &rollupBatchWithBlobs{
47+
Batch: r,
48+
Blobs: r.Sidecar.Blobs,
49+
Commitments: r.Sidecar.Commitments,
50+
Proofs: r.Sidecar.Proofs,
51+
}
52+
return rlp.EncodeToBytes(inner)
53+
}
54+
55+
func (r *RollupBatch) Decode(input []byte) error {
56+
outerList, _, err := rlp.SplitList(input)
57+
if err != nil {
58+
return err
59+
}
60+
firstElemKind, _, _, err := rlp.Split(outerList)
61+
if err != nil {
62+
return err
63+
}
64+
65+
if firstElemKind != rlp.List {
66+
return rlp.DecodeBytes(input, r)
67+
}
68+
// It's a batch with blobs.
69+
var inner rollupBatchWithBlobs
70+
if err := rlp.DecodeBytes(input, &inner); err != nil {
71+
return err
72+
}
73+
*r = *inner.Batch
74+
r.Sidecar = &BlobTxSidecar{
75+
Blobs: inner.Blobs,
76+
Commitments: inner.Commitments,
77+
Proofs: inner.Proofs,
78+
}
79+
return nil
80+
}
81+
3082
//go:generate go run github.com/fjl/gencodec -type BatchSignature -field-override batchSignatureMarshaling -out gen_batch_sig.go
3183

3284
type BatchSignature struct {

core/types/transaction.go

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const (
4545
LegacyTxType = iota
4646
AccessListTxType
4747
DynamicFeeTxType
48+
BlobTxType
4849

4950
L1MessageTxType = 0x7E
5051
)
@@ -87,6 +88,9 @@ type TxData interface {
8788

8889
rawSignatureValues() (v, r, s *big.Int)
8990
setSignatureValues(chainID, v, r, s *big.Int)
91+
92+
encode(*bytes.Buffer) error
93+
decode([]byte) error
9094
}
9195

9296
// EncodeRLP implements rlp.Encoder
@@ -107,7 +111,7 @@ func (tx *Transaction) EncodeRLP(w io.Writer) error {
107111
// encodeTyped writes the canonical encoding of a typed transaction to w.
108112
func (tx *Transaction) encodeTyped(w *bytes.Buffer) error {
109113
w.WriteByte(tx.Type())
110-
return rlp.Encode(w, tx.inner)
114+
return tx.inner.encode(w)
111115
}
112116

113117
// MarshalBinary returns the canonical encoding of the transaction.
@@ -179,22 +183,21 @@ func (tx *Transaction) decodeTyped(b []byte) (TxData, error) {
179183
if len(b) == 0 {
180184
return nil, errEmptyTypedTx
181185
}
186+
var inner TxData
182187
switch b[0] {
183188
case AccessListTxType:
184-
var inner AccessListTx
185-
err := rlp.DecodeBytes(b[1:], &inner)
186-
return &inner, err
189+
inner = new(AccessListTx)
187190
case DynamicFeeTxType:
188-
var inner DynamicFeeTx
189-
err := rlp.DecodeBytes(b[1:], &inner)
190-
return &inner, err
191+
inner = new(DynamicFeeTx)
192+
case BlobTxType:
193+
inner = new(BlobTx)
191194
case L1MessageTxType:
192-
var inner L1MessageTx
193-
err := rlp.DecodeBytes(b[1:], &inner)
194-
return &inner, err
195+
inner = new(L1MessageTx)
195196
default:
196197
return nil, ErrTxTypeNotSupported
197198
}
199+
err := inner.decode(b[1:])
200+
return inner, err
198201
}
199202

200203
// setDecoded sets the inner transaction and size after decoding.
@@ -389,6 +392,68 @@ func (tx *Transaction) EffectiveGasTipIntCmp(other *big.Int, baseFee *big.Int) i
389392
return tx.EffectiveGasTipValue(baseFee).Cmp(other)
390393
}
391394

395+
// BlobGas returns the blob gas limit of the transaction for blob transactions, 0 otherwise.
396+
func (tx *Transaction) BlobGas() uint64 {
397+
if blobtx, ok := tx.inner.(*BlobTx); ok {
398+
return blobtx.blobGas()
399+
}
400+
return 0
401+
}
402+
403+
// BlobGasFeeCap returns the blob gas fee cap per blob gas of the transaction for blob transactions, nil otherwise.
404+
func (tx *Transaction) BlobGasFeeCap() *big.Int {
405+
if blobtx, ok := tx.inner.(*BlobTx); ok {
406+
return blobtx.BlobFeeCap.ToBig()
407+
}
408+
return nil
409+
}
410+
411+
// BlobHashes returns the hashes of the blob commitments for blob transactions, nil otherwise.
412+
func (tx *Transaction) BlobHashes() []common.Hash {
413+
if blobtx, ok := tx.inner.(*BlobTx); ok {
414+
return blobtx.BlobHashes
415+
}
416+
return nil
417+
}
418+
419+
// BlobTxSidecar returns the sidecar of a blob transaction, nil otherwise.
420+
func (tx *Transaction) BlobTxSidecar() *BlobTxSidecar {
421+
if blobtx, ok := tx.inner.(*BlobTx); ok {
422+
return blobtx.Sidecar
423+
}
424+
return nil
425+
}
426+
427+
// BlobGasFeeCapCmp compares the blob fee cap of two transactions.
428+
func (tx *Transaction) BlobGasFeeCapCmp(other *Transaction) int {
429+
return tx.BlobGasFeeCap().Cmp(other.BlobGasFeeCap())
430+
}
431+
432+
// BlobGasFeeCapIntCmp compares the blob fee cap of the transaction against the given blob fee cap.
433+
func (tx *Transaction) BlobGasFeeCapIntCmp(other *big.Int) int {
434+
return tx.BlobGasFeeCap().Cmp(other)
435+
}
436+
437+
// WithoutBlobTxSidecar returns a copy of tx with the blob sidecar removed.
438+
func (tx *Transaction) WithoutBlobTxSidecar() *Transaction {
439+
blobtx, ok := tx.inner.(*BlobTx)
440+
if !ok {
441+
return tx
442+
}
443+
cpy := &Transaction{
444+
inner: blobtx.withoutSidecar(),
445+
time: tx.time,
446+
}
447+
// Note: tx.size cache not carried over because the sidecar is included in size!
448+
if h := tx.hash.Load(); h != nil {
449+
cpy.hash.Store(h)
450+
}
451+
if f := tx.from.Load(); f != nil {
452+
cpy.from.Store(f)
453+
}
454+
return cpy
455+
}
456+
392457
// Hash returns the transaction hash.
393458
func (tx *Transaction) Hash() common.Hash {
394459
if hash := tx.hash.Load(); hash != nil {

0 commit comments

Comments
 (0)