Skip to content

Commit cd1b793

Browse files
committed
fix: logic issues
1 parent d98a672 commit cd1b793

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

p2p/protocol.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
type BlockWriteState struct {
2727
HasHeader bool
2828
HasBody bool
29+
HasBlock bool
2930
}
3031

3132
// conn represents an individual connection with a peer.
@@ -321,7 +322,8 @@ func (c *conn) getParentBlock(ctx context.Context, header *types.Header) error {
321322
}
322323

323324
// Check cache first before querying the database
324-
if state, ok := c.conns.Blocks().Get(header.ParentHash); ok && state.HasHeader {
325+
state, ok := c.conns.Blocks().Get(header.ParentHash)
326+
if ok && state.HasHeader && state.HasBody {
325327
return nil
326328
}
327329

@@ -334,8 +336,6 @@ func (c *conn) getParentBlock(ctx context.Context, header *types.Header) error {
334336
Str("number", new(big.Int).Sub(header.Number, big.NewInt(1)).String()).
335337
Msg("Fetching missing parent block")
336338

337-
// Get current state from cache (will be zero value if not present)
338-
state, _ := c.conns.Blocks().Get(header.ParentHash)
339339
return c.getBlockData(header.ParentHash, state)
340340
}
341341

@@ -357,8 +357,7 @@ func (c *conn) handleNewBlockHashes(ctx context.Context, msg ethp2p.Msg) error {
357357

358358
// Check what parts of the block we already have
359359
state, ok := c.conns.Blocks().Get(hash)
360-
if ok && state.HasHeader && state.HasBody {
361-
// We already have the full block
360+
if ok {
362361
continue
363362
}
364363

@@ -367,11 +366,7 @@ func (c *conn) handleNewBlockHashes(ctx context.Context, msg ethp2p.Msg) error {
367366
return err
368367
}
369368

370-
// Update cache to track what we've requested (actual parts will be marked
371-
// when they're written to the database)
372-
if !ok {
373-
c.conns.Blocks().Add(hash, BlockWriteState{HasHeader: false, HasBody: false})
374-
}
369+
c.conns.Blocks().Add(hash, BlockWriteState{})
375370
uniqueHashes = append(uniqueHashes, hash)
376371
}
377372

@@ -514,19 +509,24 @@ func (c *conn) handleNewBlock(ctx context.Context, msg ethp2p.Msg) error {
514509
}
515510
c.headMutex.Unlock()
516511

517-
// Check if we already have the full block in the cache
518-
if state, ok := c.conns.Blocks().Get(hash); ok && state.HasHeader && state.HasBody {
519-
return nil
520-
}
521-
522512
if err := c.getParentBlock(ctx, block.Block.Header()); err != nil {
523513
return err
524514
}
525515

516+
// Check if we already have the full block in the cache
517+
if state, ok := c.conns.Blocks().Get(hash); ok && state.HasBlock {
518+
return nil
519+
}
520+
526521
c.db.WriteBlock(ctx, c.node, block.Block, block.TD, tfs)
527522

528523
// Update cache to mark both header and body as written
529-
c.conns.Blocks().Add(hash, BlockWriteState{HasHeader: true, HasBody: true})
524+
state := BlockWriteState{
525+
HasHeader: true,
526+
HasBody: true,
527+
HasBlock: true,
528+
}
529+
c.conns.Blocks().Add(hash, state)
530530

531531
return nil
532532
}

0 commit comments

Comments
 (0)