Skip to content

Commit 8e4fb95

Browse files
committed
feat: store entire blocks
1 parent cd1b793 commit 8e4fb95

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

p2p/conns.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Conns struct {
1919

2020
// blocks tracks blocks written to the database across all peers
2121
// to avoid duplicate writes and requests.
22-
blocks *Cache[common.Hash, BlockWriteState]
22+
blocks *Cache[common.Hash, BlockCache]
2323
}
2424

2525
// ConnsOptions contains configuration options for creating a new Conns manager.
@@ -34,7 +34,7 @@ type ConnsOptions struct {
3434
func NewConns(opts ConnsOptions) *Conns {
3535
return &Conns{
3636
conns: make(map[string]*conn),
37-
blocks: NewCache[common.Hash, BlockWriteState](opts.MaxBlocks, opts.BlocksCacheTTL),
37+
blocks: NewCache[common.Hash, BlockCache](opts.MaxBlocks, opts.BlocksCacheTTL),
3838
}
3939
}
4040

@@ -108,6 +108,6 @@ func (c *Conns) GetPeerConnectedAt(peerID string) time.Time {
108108
}
109109

110110
// Blocks returns the global blocks cache.
111-
func (c *Conns) Blocks() *Cache[common.Hash, BlockWriteState] {
111+
func (c *Conns) Blocks() *Cache[common.Hash, BlockCache] {
112112
return c.blocks
113113
}

p2p/protocol.go

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import (
2222
"github.com/0xPolygon/polygon-cli/p2p/database"
2323
)
2424

25-
// BlockWriteState tracks what parts of a block have been written to the database.
26-
type BlockWriteState struct {
27-
HasHeader bool
28-
HasBody bool
29-
HasBlock bool
25+
// BlockCache stores the actual block data to avoid duplicate fetches and database queries.
26+
type BlockCache struct {
27+
Header *types.Header
28+
Body *eth.BlockBody
29+
TD *big.Int
3030
}
3131

3232
// conn represents an individual connection with a peer.
@@ -271,9 +271,9 @@ func (c *conn) readStatus(packet *eth.StatusPacket) error {
271271
// getBlockData will send GetBlockHeaders and/or GetBlockBodies requests to the
272272
// peer based on what parts of the block we already have. It will return an error
273273
// if sending either of the requests failed.
274-
func (c *conn) getBlockData(hash common.Hash, state BlockWriteState) error {
274+
func (c *conn) getBlockData(hash common.Hash, cache BlockCache) error {
275275
// Only request header if we don't have it
276-
if !state.HasHeader {
276+
if cache.Header == nil {
277277
headersRequest := &GetBlockHeaders{
278278
GetBlockHeadersRequest: &eth.GetBlockHeadersRequest{
279279
// Providing both the hash and number will result in a `both origin
@@ -290,7 +290,7 @@ func (c *conn) getBlockData(hash common.Hash, state BlockWriteState) error {
290290
}
291291

292292
// Only request body if we don't have it
293-
if !state.HasBody {
293+
if cache.Body == nil {
294294
c.requestNum++
295295
c.requests.Add(c.requestNum, hash)
296296

@@ -322,8 +322,8 @@ func (c *conn) getParentBlock(ctx context.Context, header *types.Header) error {
322322
}
323323

324324
// Check cache first before querying the database
325-
state, ok := c.conns.Blocks().Get(header.ParentHash)
326-
if ok && state.HasHeader && state.HasBody {
325+
cache, ok := c.conns.Blocks().Get(header.ParentHash)
326+
if ok && cache.Header != nil && cache.Body != nil {
327327
return nil
328328
}
329329

@@ -336,7 +336,7 @@ func (c *conn) getParentBlock(ctx context.Context, header *types.Header) error {
336336
Str("number", new(big.Int).Sub(header.Number, big.NewInt(1)).String()).
337337
Msg("Fetching missing parent block")
338338

339-
return c.getBlockData(header.ParentHash, state)
339+
return c.getBlockData(header.ParentHash, cache)
340340
}
341341

342342
func (c *conn) handleNewBlockHashes(ctx context.Context, msg ethp2p.Msg) error {
@@ -356,17 +356,17 @@ func (c *conn) handleNewBlockHashes(ctx context.Context, msg ethp2p.Msg) error {
356356
hash := entry.Hash
357357

358358
// Check what parts of the block we already have
359-
state, ok := c.conns.Blocks().Get(hash)
359+
cache, ok := c.conns.Blocks().Get(hash)
360360
if ok {
361361
continue
362362
}
363363

364364
// Request only the parts we don't have
365-
if err := c.getBlockData(hash, state); err != nil {
365+
if err := c.getBlockData(hash, cache); err != nil {
366366
return err
367367
}
368368

369-
c.conns.Blocks().Add(hash, BlockWriteState{})
369+
c.conns.Blocks().Add(hash, BlockCache{})
370370
uniqueHashes = append(uniqueHashes, hash)
371371
}
372372

@@ -425,12 +425,12 @@ func (c *conn) handleBlockHeaders(ctx context.Context, msg ethp2p.Msg) error {
425425

426426
c.db.WriteBlockHeaders(ctx, headers, tfs)
427427

428-
// Update cache to mark headers as written
428+
// Update cache to store headers
429429
for _, header := range headers {
430430
hash := header.Hash()
431-
state, _ := c.conns.Blocks().Get(hash)
432-
state.HasHeader = true
433-
c.conns.Blocks().Add(hash, state)
431+
cache, _ := c.conns.Blocks().Get(hash)
432+
cache.Header = header
433+
c.conns.Blocks().Add(hash, cache)
434434
}
435435

436436
return nil
@@ -471,16 +471,17 @@ func (c *conn) handleBlockBodies(ctx context.Context, msg ethp2p.Msg) error {
471471
c.requests.Remove(packet.RequestId)
472472

473473
// Check if we already have the body in the cache
474-
if state, ok := c.conns.Blocks().Get(hash); ok && state.HasBody {
474+
if cache, ok := c.conns.Blocks().Get(hash); ok && cache.Body != nil {
475475
return nil
476476
}
477477

478-
c.db.WriteBlockBody(ctx, packet.BlockBodiesResponse[0], hash, tfs)
478+
body := packet.BlockBodiesResponse[0]
479+
c.db.WriteBlockBody(ctx, body, hash, tfs)
479480

480-
// Update cache to mark body as written
481-
state, _ := c.conns.Blocks().Get(hash)
482-
state.HasBody = true
483-
c.conns.Blocks().Add(hash, state)
481+
// Update cache to store body
482+
cache, _ := c.conns.Blocks().Get(hash)
483+
cache.Body = body
484+
c.conns.Blocks().Add(hash, cache)
484485

485486
return nil
486487
}
@@ -514,19 +515,22 @@ func (c *conn) handleNewBlock(ctx context.Context, msg ethp2p.Msg) error {
514515
}
515516

516517
// Check if we already have the full block in the cache
517-
if state, ok := c.conns.Blocks().Get(hash); ok && state.HasBlock {
518+
if cache, ok := c.conns.Blocks().Get(hash); ok && cache.TD != nil {
518519
return nil
519520
}
520521

521522
c.db.WriteBlock(ctx, c.node, block.Block, block.TD, tfs)
522523

523-
// Update cache to mark both header and body as written
524-
state := BlockWriteState{
525-
HasHeader: true,
526-
HasBody: true,
527-
HasBlock: true,
528-
}
529-
c.conns.Blocks().Add(hash, state)
524+
// Update cache to store the full block
525+
c.conns.Blocks().Add(hash, BlockCache{
526+
Header: block.Block.Header(),
527+
Body: &eth.BlockBody{
528+
Transactions: block.Block.Transactions(),
529+
Uncles: block.Block.Uncles(),
530+
Withdrawals: block.Block.Withdrawals(),
531+
},
532+
TD: block.TD,
533+
})
530534

531535
return nil
532536
}

0 commit comments

Comments
 (0)