Skip to content

Commit 2ba73e3

Browse files
committed
fix blob tx gossip encoding in mempool
1 parent 13c3f67 commit 2ba73e3

File tree

6 files changed

+73
-13
lines changed

6 files changed

+73
-13
lines changed

core/types/block.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,25 +232,25 @@ func (tx *minimalTx) EncodeRLP(w io.Writer) error {
232232
// a view over a regular transactions slice, to RLP decode/encode the transactions all the minimal way
233233
type extBlockTxs []*Transaction
234234

235-
func (txs *extBlockTxs) DecodeRLP(s *rlp.Stream) error {
235+
func (txs extBlockTxs) DecodeRLP(s *rlp.Stream) error {
236236
// we need generics to do this nicely...
237237
var out []*minimalTx
238-
for i, tx := range *txs {
238+
for i, tx := range txs {
239239
out[i] = (*minimalTx)(tx)
240240
}
241241
if err := s.Decode(&out); err != nil {
242242
return fmt.Errorf("failed to decode list of minimal txs: %v", err)
243243
}
244-
*txs = make([]*Transaction, len(out))
244+
txs = make([]*Transaction, len(out))
245245
for i, tx := range out {
246-
(*txs)[i] = (*Transaction)(tx)
246+
txs[i] = (*Transaction)(tx)
247247
}
248248
return nil
249249
}
250250

251-
func (txs *extBlockTxs) EncodeRLP(w io.Writer) error {
252-
out := make([]*minimalTx, len(*txs))
253-
for i, tx := range *txs {
251+
func (txs extBlockTxs) EncodeRLP(w io.Writer) error {
252+
out := make([]*minimalTx, len(txs))
253+
for i, tx := range txs {
254254
out[i] = (*minimalTx)(tx)
255255
}
256256
return rlp.Encode(w, &out)

core/types/transaction.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ func (tx *Transaction) EncodeRLP(w io.Writer) error {
137137
buf := encodeBufferPool.Get().(*bytes.Buffer)
138138
defer encodeBufferPool.Put(buf)
139139
buf.Reset()
140-
// TODO(EIP-4844): Ensure that we use encodeTyped when gossiping transactions
141140
if err := tx.encodeTypedMinimal(buf); err != nil {
142141
return err
143142
}
@@ -817,3 +816,63 @@ func copyAddressPtr(a *common.Address) *common.Address {
817816
cpy := *a
818817
return &cpy
819818
}
819+
820+
// NetworkTransaction is a Transaction wrapper that encodes its maximal representation
821+
type NetworkTransaction struct {
822+
Tx *Transaction
823+
}
824+
825+
func NewNetworkTransaction(tx *Transaction) *NetworkTransaction {
826+
return &NetworkTransaction{Tx: tx}
827+
}
828+
829+
// EncodeRLP implements rlp.Encoder
830+
func (tx *NetworkTransaction) EncodeRLP(w io.Writer) error {
831+
if tx.Tx.Type() == LegacyTxType {
832+
return rlp.Encode(w, tx.Tx)
833+
}
834+
// It's an EIP-2718 typed TX envelope.
835+
buf := encodeBufferPool.Get().(*bytes.Buffer)
836+
defer encodeBufferPool.Put(buf)
837+
buf.Reset()
838+
if err := tx.Tx.encodeTyped(buf); err != nil {
839+
return err
840+
}
841+
return rlp.Encode(w, buf.Bytes())
842+
}
843+
844+
// DecodeRLP implements rlp.Decoder
845+
func (tx *NetworkTransaction) DecodeRLP(s *rlp.Stream) error {
846+
kind, size, err := s.Kind()
847+
switch {
848+
case err != nil:
849+
return err
850+
case kind == rlp.List:
851+
// It's a legacy transaction.
852+
var inner LegacyTx
853+
err := s.Decode(&inner)
854+
if err == nil {
855+
tx.Tx.setDecoded(&inner, int(rlp.ListSize(size)))
856+
}
857+
return err
858+
default:
859+
// It's an EIP-2718 typed TX envelope.
860+
var b []byte
861+
if b, err = s.Bytes(); err != nil {
862+
return err
863+
}
864+
inner, wrapData, err := tx.Tx.decodeTyped(b)
865+
if err == nil {
866+
tx.Tx.setDecoded(inner, len(b))
867+
tx.Tx.wrapData = wrapData
868+
}
869+
return err
870+
}
871+
}
872+
873+
// Hash returns the transaction hash.
874+
func (tx *NetworkTransaction) Hash() common.Hash {
875+
return tx.Tx.Hash()
876+
}
877+
878+
type NetworkTransactions []*NetworkTransaction

eth/handler_eth_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ func testRecvTransactions(t *testing.T, protocol uint) {
278278
tx := types.NewTransaction(0, common.Address{}, big.NewInt(0), 100000, big.NewInt(0), nil)
279279
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
280280

281-
if err := src.SendTransactions([]*types.Transaction{tx}); err != nil {
281+
ntx := types.NewNetworkTransaction(tx)
282+
if err := src.SendTransactions([]*types.NetworkTransaction{ntx}); err != nil {
282283
t.Fatalf("failed to send transaction: %v", err)
283284
}
284285
select {

eth/protocols/eth/broadcast.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ func (p *Peer) broadcastTransactions() {
7676
// Pile transaction until we reach our allowed network limit
7777
var (
7878
hashesCount uint64
79-
txs []*types.Transaction
79+
txs []*types.NetworkTransaction
8080
size common.StorageSize
8181
)
8282
for i := 0; i < len(queue) && size < maxTxPacketSize; i++ {
8383
if tx := p.txpool.Get(queue[i]); tx != nil {
84-
txs = append(txs, tx)
84+
txs = append(txs, types.NewNetworkTransaction(tx))
8585
size += tx.Size()
8686
}
8787
hashesCount++

eth/protocols/eth/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func answerGetPooledTransactions(backend Backend, query GetPooledTransactionsPac
474474
continue
475475
}
476476
// If known, encode and queue for response packet
477-
if encoded, err := rlp.EncodeToBytes(tx); err != nil {
477+
if encoded, err := rlp.EncodeToBytes(types.NewNetworkTransaction(tx)); err != nil {
478478
log.Error("Failed to encode transaction", "err", err)
479479
} else {
480480
hashes = append(hashes, hash)

eth/protocols/eth/peer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (p *Peer) markTransaction(hash common.Hash) {
189189
//
190190
// The reasons this is public is to allow packages using this protocol to write
191191
// tests that directly send messages without having to do the asyn queueing.
192-
func (p *Peer) SendTransactions(txs types.Transactions) error {
192+
func (p *Peer) SendTransactions(txs types.NetworkTransactions) error {
193193
// Mark all the transactions as known, but ensure we don't overflow our limits
194194
for _, tx := range txs {
195195
p.knownTxs.Add(tx.Hash())

0 commit comments

Comments
 (0)