Skip to content

Commit

Permalink
Yikes, fix an ugly early stop in indexer bug and add some debug
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom committed Nov 1, 2024
1 parent 5396990 commit 30919b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
15 changes: 12 additions & 3 deletions service/tbc/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func (s *Server) isCanonical(ctx context.Context, bh *tbcd.BlockHeader) (bool, e
}
// Move best block header backwards until we find bh.
for {
log.Infof("isCanonical %v @ %v bh %v", bhb.Height, bhb, bh.Height)
// log.Debugf("isCanonical %v @ %v bh %v", bhb.Height, bhb, bh.Height)
if height, ok := s.checkpoints[bhb.Hash]; ok && bh.Height <= height {
return true, nil
Expand All @@ -213,6 +214,7 @@ func (s *Server) isCanonical(ctx context.Context, bh *tbcd.BlockHeader) (bool, e
}

func (s *Server) findCanonicalParent(ctx context.Context, bh *tbcd.BlockHeader) (*tbcd.BlockHeader, error) {
log.Infof("findCanonicalParent %v", bh)
log.Tracef("findCanonicalParent %v", bh)

// Genesis is always canonical.
Expand All @@ -230,6 +232,7 @@ func (s *Server) findCanonicalParent(ctx context.Context, bh *tbcd.BlockHeader)
return nil, err
}
if canonical {
log.Infof("findCanonicalParent exit %v", bh)
log.Tracef("findCanonicalParent exit %v", bh)
return bh, nil
}
Expand Down Expand Up @@ -1358,6 +1361,8 @@ func (s *Server) syncIndexersToBest(ctx context.Context) error {
return err
}

log.Infof("Sync indexers to best: %v @ %v", bhb, bhb.Height)

// Index Utxo
utxoHH, err := s.UtxoIndexHash(ctx)
if err != nil {
Expand Down Expand Up @@ -1405,9 +1410,13 @@ func (s *Server) syncIndexersToBest(ctx context.Context) error {
if err != nil {
return err
}
cp, err = s.findCanonicalParent(ctx, txBH)
if err != nil {
return err
// We can short circuit looking up canonical parent if txBH == utxoBH.
ptxHash := &txBH.Hash
if !ptxHash.IsEqual(&utxoBH.Hash) {
cp, err = s.findCanonicalParent(ctx, txBH)
if err != nil {
return err
}
}
if !cp.Hash.IsEqual(&txBH.Hash) {
log.Infof("Syncing tx index to: %v from: %v via: %v",
Expand Down
21 changes: 15 additions & 6 deletions service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,14 +853,11 @@ func (s *Server) syncBlocks(ctx context.Context) {
go func() {
if err = s.SyncIndexersToBest(ctx); err != nil && !errors.Is(err, ErrAlreadyIndexing) && !errors.Is(err, context.Canceled) {
// XXX this is probably not a panic.
panic(fmt.Errorf("sync blocks: %w", err))
panic(fmt.Errorf("sync blocks: %T %w", err, err))
}

// Get block headers
if s.Synced(ctx).Synced {
// Flush out blocks we saw during quiece.
log.Infof("flush missed")

s.mtx.Lock()
ib := make([]*chainhash.Hash, 0, len(s.invBlocks))
for k := range s.invBlocks {
Expand All @@ -869,6 +866,9 @@ func (s *Server) syncBlocks(ctx context.Context) {
clear(s.invBlocks)
s.mtx.Unlock()

// Flush out blocks we saw during quiece.
log.Infof("flush missed headers %v", len(ib))

if len(ib) == 0 {
log.Infof("nothing to do")
return
Expand Down Expand Up @@ -917,10 +917,18 @@ func (s *Server) handleHeaders(ctx context.Context, p *peer, msg *wire.MsgHeader
log.Tracef("handleHeaders (%v): %v %v", p, len(msg.Headers))
defer log.Tracef("handleHeaders exit (%v): %v", p, len(msg.Headers))

// When quiesced do not handle headers
// When quiesced do not handle headers but do cache them.
s.mtx.Lock()
if s.indexing {
log.Debugf("handleHeaders indexing %v", s.indexing)
for k := range msg.Headers {
if _, ok := s.invBlocks[msg.Headers[k].BlockHash()]; !ok {
s.invBlocks[msg.Headers[k].BlockHash()] = struct{}{}
}
}
if len(s.invBlocks) != 0 {
log.Infof("handleHeaders indexing %v %v",
len(msg.Headers), len(s.invBlocks))
}
s.mtx.Unlock()
return ErrAlreadyIndexing
}
Expand Down Expand Up @@ -1158,6 +1166,7 @@ func (s *Server) handleBlock(ctx context.Context, p *peer, msg *wire.MsgBlock, r
func (s *Server) handleInv(ctx context.Context, p *peer, msg *wire.MsgInv, raw []byte) error {
switch msg.InvList[0].Type {
case wire.InvTypeTx:
case wire.InvTypeBlock:
default:
// log.Infof("%v: %T", p, m)
log.Infof("handleInv (%v) %v", p, msg.InvList[0].Type)
Expand Down

0 comments on commit 30919b6

Please sign in to comment.