Skip to content

Commit 7dba16c

Browse files
MariusVanDerWijdenlightclient
authored andcommitted
all: 4844 devnet 6 integration
1 parent cde462c commit 7dba16c

File tree

20 files changed

+281
-22
lines changed

20 files changed

+281
-22
lines changed

consensus/beacon/consensus.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
379379
return nil, errors.New("withdrawals set before Shanghai activation")
380380
}
381381
}
382+
if chain.Config().IsCancun(header.Number, header.Time) {
383+
var blobs int
384+
for _, tx := range txs {
385+
blobs += len(tx.BlobHashes())
386+
}
387+
blobGasUsed := uint64(blobs * params.BlobTxBlobGasPerBlob)
388+
header.BlobGasUsed = &blobGasUsed
389+
}
382390
// Finalize and assemble the block.
383391
beacon.Finalize(chain, header, state, txs, uncles, withdrawals)
384392

consensus/misc/eip4844/eip4844_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func TestCalcExcessBlobGas(t *testing.T) {
4545
// The excess blob gas should decrease by however much the target was
4646
// under-shot, capped at zero.
4747
{params.BlobTxTargetBlobGasPerBlock, params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob, params.BlobTxTargetBlobGasPerBlock},
48-
{params.BlobTxTargetBlobGasPerBlock, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) - 1, params.BlobTxBlobGasPerBlob},
49-
{params.BlobTxTargetBlobGasPerBlock, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) - 2, 0},
48+
{params.BlobTxTargetBlobGasPerBlock, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) - 1, 2 * params.BlobTxBlobGasPerBlob},
49+
{params.BlobTxTargetBlobGasPerBlock, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) - 2, params.BlobTxBlobGasPerBlob},
5050
{params.BlobTxBlobGasPerBlob - 1, (params.BlobTxTargetBlobGasPerBlock / params.BlobTxBlobGasPerBlob) - 1, 0},
5151
}
5252
for _, tt := range tests {
@@ -64,8 +64,9 @@ func TestCalcBlobFee(t *testing.T) {
6464
}{
6565
{0, 1},
6666
{1542706, 1},
67-
{1542707, 2},
68-
{10 * 1024 * 1024, 111},
67+
{1542707, 1},
68+
{3085414, 2},
69+
{10 * 1024 * 1024, 23},
6970
}
7071
for i, tt := range tests {
7172
have := CalcBlobFee(tt.excessBlobGas)

core/genesis.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,14 @@ func (g *Genesis) ToBlock() *types.Block {
496496
}
497497
}
498498
}
499+
if g.Config != nil && g.Config.IsCancun(big.NewInt(int64(g.Number)), g.Timestamp) {
500+
if g.ExcessBlobGas == nil {
501+
var excessBlobGas uint64
502+
head.ExcessBlobGas = &excessBlobGas
503+
} else {
504+
head.ExcessBlobGas = g.ExcessBlobGas
505+
}
506+
}
499507
return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil)).WithWithdrawals(withdrawals)
500508
}
501509

core/txpool/blobpool/blobpool.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,8 @@ func (p *BlobPool) reinject(addr common.Address, txhash common.Hash) {
954954
}
955955
p.lookup[meta.hash] = meta.id
956956
p.stored += uint64(meta.size)
957+
958+
p.eventFeed.Send(core.NewTxsEvent{Txs: types.Transactions{tx}})
957959
}
958960

959961
// SetGasTip implements txpool.SubPool, allowing the blob pool's gas requirements
@@ -1036,7 +1038,6 @@ func (p *BlobPool) validateTx(tx *types.Transaction) error {
10361038
// Ensure the transaction adheres to the stateful pool filters (nonce, balance)
10371039
stateOpts := &txpool.ValidationOptionsWithState{
10381040
State: p.state,
1039-
10401041
FirstNonceGap: func(addr common.Address) uint64 {
10411042
// Nonce gaps are not permitted in the blob pool, the first gap will
10421043
// be the next nonce shifted by however many transactions we already
@@ -1297,6 +1298,7 @@ func (p *BlobPool) add(tx *types.Transaction) (err error) {
12971298
}
12981299
p.updateStorageMetrics()
12991300

1301+
p.eventFeed.Send(core.NewTxsEvent{Txs: types.Transactions{tx}})
13001302
return nil
13011303
}
13021304

core/txpool/blobpool/slotter_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func TestNewSlotter(t *testing.T) {
4646
10*blobSize + txAvgSize, // 1-4 blobs + unexpectedly large tx infos < 4 blobs + max tx metadata size
4747
11*blobSize + txAvgSize, // 1-4 blobs + unexpectedly large tx infos < 4 blobs + max tx metadata size
4848
12*blobSize + txAvgSize, // 1-4 blobs + unexpectedly large tx infos >= 4 blobs + max tx metadata size
49+
13*blobSize + txAvgSize, // 1-4 blobs + unexpectedly large tx infos >= 4 blobs + max tx metadata size
50+
14*blobSize + txAvgSize, // 1-4 blobs + unexpectedly large tx infos >= 4 blobs + max tx metadata size
4951
}
5052
if len(shelves) != len(want) {
5153
t.Errorf("shelves count mismatch: have %d, want %d", len(shelves), len(want))

core/types/types_test.go

Lines changed: 57 additions & 1 deletion
Large diffs are not rendered by default.

eth/fetcher/tx_fetcher_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,11 @@ func testTransactionFetcher(t *testing.T, tt txFetcherTest) {
12871287
}
12881288

12891289
case doTxEnqueue:
1290-
if err := fetcher.Enqueue(step.peer, step.txs, step.direct); err != nil {
1290+
var txs []*types.BlobTxWithBlobs
1291+
for _, tx := range step.txs {
1292+
txs = append(txs, types.NewBlobTxWithBlobs(tx, nil, nil, nil))
1293+
}
1294+
if err := fetcher.Enqueue(step.peer, txs, step.direct); err != nil {
12911295
t.Errorf("step %d: %v", i, err)
12921296
}
12931297
<-wait // Fetcher needs to process this, wait until it's done

eth/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ func (h *handler) BroadcastTransactions(txs types.Transactions) {
625625
peers := h.peers.peersWithoutTransaction(tx.Hash())
626626

627627
var numDirect int
628-
if tx.Size() <= txMaxBroadcastSize {
628+
if tx.Size() <= txMaxBroadcastSize && tx.Type() != types.BlobTxType {
629629
numDirect = int(math.Sqrt(float64(len(peers))))
630630
}
631631
// Send the tx unconditionally to a subset of our peers

eth/handler_eth_test.go

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ import (
3030
"github.com/ethereum/go-ethereum/core/rawdb"
3131
"github.com/ethereum/go-ethereum/core/types"
3232
"github.com/ethereum/go-ethereum/core/vm"
33+
"github.com/ethereum/go-ethereum/crypto/kzg4844"
3334
"github.com/ethereum/go-ethereum/eth/downloader"
3435
"github.com/ethereum/go-ethereum/eth/protocols/eth"
3536
"github.com/ethereum/go-ethereum/event"
3637
"github.com/ethereum/go-ethereum/p2p"
3738
"github.com/ethereum/go-ethereum/p2p/enode"
3839
"github.com/ethereum/go-ethereum/params"
40+
"github.com/holiman/uint256"
3941
)
4042

4143
// testEthHandler is a mock event handler to listen for inbound network requests
@@ -71,7 +73,11 @@ func (h *testEthHandler) Handle(peer *eth.Peer, packet eth.Packet) error {
7173
return nil
7274

7375
case *eth.PooledTransactionsPacket:
74-
h.txBroadcasts.Send(([]*types.Transaction)(*packet))
76+
var txs []*types.Transaction
77+
for _, tx := range *packet {
78+
txs = append(txs, tx.Transaction)
79+
}
80+
h.txBroadcasts.Send(txs)
7581
return nil
7682

7783
default:
@@ -454,6 +460,87 @@ func testTransactionPropagation(t *testing.T, protocol uint) {
454460
}
455461
}
456462

463+
func TestBlobTransactionPropagation68(t *testing.T) { testBlobTransactionPropagation(t, eth.ETH68) }
464+
465+
func testBlobTransactionPropagation(t *testing.T, protocol uint) {
466+
t.Parallel()
467+
468+
// Create a source handler to send transactions from and a number of sinks
469+
// to receive them. We need multiple sinks since a one-to-one peering would
470+
// broadcast all transactions without announcement.
471+
source := newTestHandler()
472+
source.handler.snapSync.Store(false) // Avoid requiring snap, otherwise some will be dropped below
473+
defer source.close()
474+
475+
sinks := make([]*testHandler, 10)
476+
for i := 0; i < len(sinks); i++ {
477+
sinks[i] = newTestHandler()
478+
defer sinks[i].close()
479+
480+
sinks[i].handler.acceptTxs.Store(true) // mark synced to accept transactions
481+
}
482+
// Interconnect all the sink handlers with the source handler
483+
for i, sink := range sinks {
484+
sink := sink // Closure for gorotuine below
485+
486+
sourcePipe, sinkPipe := p2p.MsgPipe()
487+
defer sourcePipe.Close()
488+
defer sinkPipe.Close()
489+
490+
sourcePeer := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{byte(i + 1)}, "", nil, sourcePipe), sourcePipe, source.txpool)
491+
sinkPeer := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{0}, "", nil, sinkPipe), sinkPipe, sink.txpool)
492+
defer sourcePeer.Close()
493+
defer sinkPeer.Close()
494+
495+
go source.handler.runEthPeer(sourcePeer, func(peer *eth.Peer) error {
496+
return eth.Handle((*ethHandler)(source.handler), peer)
497+
})
498+
go sink.handler.runEthPeer(sinkPeer, func(peer *eth.Peer) error {
499+
return eth.Handle((*ethHandler)(sink.handler), peer)
500+
})
501+
}
502+
// Subscribe to all the transaction pools
503+
txChs := make([]chan core.NewTxsEvent, len(sinks))
504+
for i := 0; i < len(sinks); i++ {
505+
txChs[i] = make(chan core.NewTxsEvent, 1024)
506+
507+
sub := sinks[i].txpool.SubscribeNewTxsEvent(txChs[i])
508+
defer sub.Unsubscribe()
509+
}
510+
// Fill the source pool with transactions and wait for them at the sinks
511+
txs := make([]*txpool.Transaction, 1024)
512+
for nonce := range txs {
513+
tx := types.NewTx(&types.BlobTx{
514+
Nonce: uint64(nonce),
515+
To: common.Address{},
516+
GasTipCap: uint256.NewInt(0),
517+
GasFeeCap: uint256.NewInt(0),
518+
Gas: 100000,
519+
Value: uint256.NewInt(0),
520+
BlobHashes: make([]common.Hash, 1),
521+
ChainID: uint256.NewInt(0),
522+
Data: nil,
523+
})
524+
tx, _ = types.SignTx(tx, types.NewCancunSigner(common.Big0), testKey)
525+
526+
txs[nonce] = &txpool.Transaction{Tx: tx, BlobTxBlobs: make([]kzg4844.Blob, 1), BlobTxCommits: make([]kzg4844.Commitment, 1), BlobTxProofs: make([]kzg4844.Proof, 1)}
527+
}
528+
source.txpool.Add(txs, false, false)
529+
530+
// Iterate through all the sinks and ensure they all got the transactions
531+
for i := range sinks {
532+
for arrived, timeout := 0, false; arrived < len(txs) && !timeout; {
533+
select {
534+
case event := <-txChs[i]:
535+
arrived += len(event.Txs)
536+
case <-time.After(2 * time.Second):
537+
t.Errorf("sink %d: transaction propagation timed out: have %d, want %d", i, arrived, len(txs))
538+
timeout = true
539+
}
540+
}
541+
}
542+
}
543+
457544
// Tests that blocks are broadcast to a sqrt number of peers only.
458545
func TestBroadcastBlock1Peer(t *testing.T) { testBroadcastBlock(t, 1, 1) }
459546
func TestBroadcastBlock2Peers(t *testing.T) { testBroadcastBlock(t, 2, 1) }

eth/protocols/eth/broadcast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (p *Peer) broadcastTransactions() {
8080
size common.StorageSize
8181
)
8282
for i := 0; i < len(queue) && size < maxTxPacketSize; i++ {
83+
// Do not broadcast blob transactions
8384
if tx := p.txpool.Get(queue[i]); tx != nil {
8485
txs = append(txs, tx)
8586
size += common.StorageSize(tx.Size())

0 commit comments

Comments
 (0)