Skip to content

Commit

Permalink
Add blocks missing
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom committed Dec 5, 2024
1 parent aac3095 commit 365ebd0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
11 changes: 8 additions & 3 deletions service/tbc/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ func lastCheckpointHeight(height uint64, hhm map[chainhash.Hash]uint64) uint64 {
}

type HashHeight struct {
Hash chainhash.Hash
Height uint64
Hash chainhash.Hash
Height uint64
Timestamp int64 // optional
}

func (h HashHeight) String() string {
Expand All @@ -127,7 +128,11 @@ func (s *Server) mdHashHeight(ctx context.Context, key []byte) (*HashHeight, err
if err != nil {
return nil, fmt.Errorf("metadata block header: %w", err)
}
return &HashHeight{Hash: *ch, Height: bh.Height}, nil
return &HashHeight{
Hash: *ch,
Height: bh.Height,
Timestamp: bh.Timestamp().Unix(),
}, nil
}

// UtxoIndexHash returns the last hash that has been been UTxO indexed.
Expand Down
51 changes: 43 additions & 8 deletions service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,12 @@ func (s *Server) promRunning() float64 {
return 0
}

func (s *Server) promBlocksMissing() float64 {
s.mtx.Lock()
defer s.mtx.Unlock()
return float64(s.prom.syncInfo.AtLeastMissing)
}

func (s *Server) promSynced() float64 {
s.mtx.Lock()
defer s.mtx.Unlock()
Expand All @@ -614,8 +620,10 @@ func (s *Server) promBlockHeader(m *prometheus.GaugeVec) {
defer s.mtx.Unlock()

m.Reset()
m.With(prometheus.Labels{"hash": s.prom.syncInfo.BlockHeader.Hash.String()}).
Set(float64(s.prom.syncInfo.BlockHeader.Height))
m.With(prometheus.Labels{
"hash": s.prom.syncInfo.BlockHeader.Hash.String(),
"timestamp": strconv.Itoa(int(s.prom.syncInfo.BlockHeader.Timestamp)),
}).Set(float64(s.prom.syncInfo.BlockHeader.Height))
}

func (s *Server) promUtxo() float64 {
Expand Down Expand Up @@ -2012,10 +2020,11 @@ func (s *Server) SetUpstreamStateId(ctx context.Context, upstreamStateId *[32]by
}

type SyncInfo struct {
Synced bool // True when all indexing is caught up
BlockHeader HashHeight
Utxo HashHeight
Tx HashHeight
Synced bool // True when all indexing is caught up
AtLeastMissing int // Blocks missing 0-63 is counted, >=64 returns -1
BlockHeader HashHeight
Utxo HashHeight
Tx HashHeight
}

func (s *Server) synced(ctx context.Context) (si SyncInfo) {
Expand Down Expand Up @@ -2047,6 +2056,7 @@ func (s *Server) synced(ctx context.Context) (si SyncInfo) {
}
si.BlockHeader.Hash = bhb.Hash
si.BlockHeader.Height = bhb.Height
si.BlockHeader.Timestamp = bhb.Timestamp().Unix()

// utxo index
utxoHH, err := s.UtxoIndexHash(ctx)
Expand All @@ -2062,8 +2072,28 @@ func (s *Server) synced(ctx context.Context) (si SyncInfo) {
}
si.Tx = *txHH

// Find out how many blocks are missing.
var (
blksMissing bool = true
maxMissing int = 64
)
// expensive check
bm, err := s.db.BlocksMissing(ctx, maxMissing)
if err != nil {
panic(err)
}
if len(bm) >= maxMissing {
// -1 is sentinel meaning > 64
si.AtLeastMissing = -1
} else {
si.AtLeastMissing = len(bm)
if si.AtLeastMissing == 0 {
blksMissing = false
}
}

if utxoHH.Hash.IsEqual(&bhb.Hash) && txHH.Hash.IsEqual(&bhb.Hash) &&
!s.indexing && !s.blksMissing(ctx) {
!s.indexing && !blksMissing {
si.Synced = true
}
return
Expand Down Expand Up @@ -2247,7 +2277,12 @@ func (s *Server) Run(pctx context.Context) error {
Subsystem: s.cfg.PrometheusSubsystem,
Name: "blockheader_height",
Help: "Blockheader canonical height and hash.",
}, []string{"hash"}), s.promBlockHeader),
}, []string{"hash", "timestamp"}), s.promBlockHeader),
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Subsystem: s.cfg.PrometheusSubsystem,
Name: "blocks_missing",
Help: "How many blocks are missing >= 64 returns -1.",
}, s.promBlocksMissing),
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Subsystem: s.cfg.PrometheusSubsystem,
Name: "running",
Expand Down

0 comments on commit 365ebd0

Please sign in to comment.