Skip to content

Commit

Permalink
tbc: check block does not exist prior to inserting into blocksmissing…
Browse files Browse the repository at this point in the history
… db (#382)

* Verify block does not exist prior to inserting into blocksmissing db

This is needed to support synthetic blockheader inserts in headers only
mode.

* Eat Has call on hash height as well

* Provide max requested BlockHeadersInsert
  • Loading branch information
marcopeereboom authored Jan 20, 2025
1 parent 370ecac commit 0062118
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 17 additions & 3 deletions database/tbcd/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,9 @@ func (l *ldb) BlockHeadersInsert(ctx context.Context, bhs *wire.MsgHeaders, batc
}
defer hhDiscard()

// blocks
blocksDB := l.rawPool[level.BlocksDB]

// retrieve best/canonical block header
bbh, err := bhsTx.Get([]byte(bhsCanonicalTipKey), nil)
if err != nil {
Expand Down Expand Up @@ -1111,12 +1114,23 @@ func (l *ldb) BlockHeadersInsert(ctx context.Context, bhs *wire.MsgHeaders, batc

// Store height_hash for future reference
hhKey := heightHashToKey(height, bhash[:])
hhBatch.Put(hhKey, []byte{}) // XXX nil?
ok, err := hhTx.Has(hhKey, nil)
if err != nil {
return tbcd.ITInvalid, nil, nil, 0,
fmt.Errorf("height hash has: %w", err)
} else if !ok {
hhBatch.Put(hhKey, []byte{})
}

// Insert a synthesized height_hash key that serves as an index
// to see which blocks are missing.
// XXX should we always insert or should we verify prior to insert?
bmBatch.Put(hhKey, []byte{})
ok, err = blocksDB.Has(hhKey)
if err != nil {
return tbcd.ITInvalid, nil, nil, 0,
fmt.Errorf("blocks has: %w", err)
} else if !ok {
bmBatch.Put(hhKey, []byte{})
}

// XXX reason about pre encoding. Due to the caller code being
// heavily reentrant the odds are not good that encoding would
Expand Down
4 changes: 4 additions & 0 deletions service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,10 @@ func (s *Server) BlockInsert(ctx context.Context, blk *wire.MsgBlock) (int64, er
return s.db.BlockInsert(ctx, btcutil.NewBlock(blk))
}

func (s *Server) BlockHeadersInsert(ctx context.Context, headers *wire.MsgHeaders) (tbcd.InsertType, *tbcd.BlockHeader, *tbcd.BlockHeader, int, error) {
return s.db.BlockHeadersInsert(ctx, headers, nil)
}

func (s *Server) handleBlock(ctx context.Context, p *rawpeer.RawPeer, msg *wire.MsgBlock, raw []byte) error {
log.Tracef("handleBlock (%v)", p)
defer log.Tracef("handleBlock exit (%v)", p)
Expand Down

0 comments on commit 0062118

Please sign in to comment.