Commit 7f42e184 authored by Matthew Slipper's avatar Matthew Slipper Committed by GitHub

Merge pull request #2272 from mslipper/bugfix/metrics

indexer: fix metrics, reduce DB load
parents 29799d9d f02b940e
...@@ -43,8 +43,8 @@ func NewMetrics(monitoredTokens map[string]string) *Metrics { ...@@ -43,8 +43,8 @@ func NewMetrics(monitoredTokens map[string]string) *Metrics {
return &Metrics{ return &Metrics{
SyncHeight: promauto.NewGaugeVec(prometheus.GaugeOpts{ SyncHeight: promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "l1_sync_height", Name: "sync_height",
Help: "The max height of the indexer's last batch of L1 blocks.", Help: "The max height of the indexer's last batch of L1/L1 blocks.",
Namespace: metricsNamespace, Namespace: metricsNamespace,
}, []string{ }, []string{
"chain", "chain",
...@@ -66,7 +66,7 @@ func NewMetrics(monitoredTokens map[string]string) *Metrics { ...@@ -66,7 +66,7 @@ func NewMetrics(monitoredTokens map[string]string) *Metrics {
"symbol", "symbol",
}), }),
StateBatchesCount: prometheus.NewCounter(prometheus.CounterOpts{ StateBatchesCount: promauto.NewCounter(prometheus.CounterOpts{
Name: "state_batches_count", Name: "state_batches_count",
Help: "The number of state batches indexed.", Help: "The number of state batches indexed.",
Namespace: metricsNamespace, Namespace: metricsNamespace,
...@@ -101,7 +101,7 @@ func NewMetrics(monitoredTokens map[string]string) *Metrics { ...@@ -101,7 +101,7 @@ func NewMetrics(monitoredTokens map[string]string) *Metrics {
"chain", "chain",
}), }),
CachedTokensCount: prometheus.NewCounterVec(prometheus.CounterOpts{ CachedTokensCount: promauto.NewCounterVec(prometheus.CounterOpts{
Name: "cached_tokens_count", Name: "cached_tokens_count",
Help: "How many tokens are in the cache", Help: "How many tokens are in the cache",
Namespace: metricsNamespace, Namespace: metricsNamespace,
...@@ -118,7 +118,7 @@ func (m *Metrics) SetL1SyncHeight(height uint64) { ...@@ -118,7 +118,7 @@ func (m *Metrics) SetL1SyncHeight(height uint64) {
} }
func (m *Metrics) SetL2SyncHeight(height uint64) { func (m *Metrics) SetL2SyncHeight(height uint64) {
m.SyncHeight.WithLabelValues("l1").Set(float64(height)) m.SyncHeight.WithLabelValues("l2").Set(float64(height))
} }
func (m *Metrics) RecordDeposit(addr common.Address) { func (m *Metrics) RecordDeposit(addr common.Address) {
......
...@@ -94,9 +94,9 @@ type Service struct { ...@@ -94,9 +94,9 @@ type Service struct {
latestHeader uint64 latestHeader uint64
headerSelector *ConfirmedHeaderSelector headerSelector *ConfirmedHeaderSelector
metrics *metrics.Metrics metrics *metrics.Metrics
tokenCache map[common.Address]*db.Token tokenCache map[common.Address]*db.Token
wg sync.WaitGroup wg sync.WaitGroup
} }
type IndexerStatus struct { type IndexerStatus struct {
...@@ -289,10 +289,15 @@ func (s *Service) Update(newHeader *types.Header) error { ...@@ -289,10 +289,15 @@ func (s *Service) Update(newHeader *types.Header) error {
logger.Error("Error querying state batches", "err", err) logger.Error("Error querying state batches", "err", err)
} }
for _, header := range headers { for i, header := range headers {
blockHash := header.Hash blockHash := header.Hash
number := header.Number.Uint64() number := header.Number.Uint64()
deposits := depositsByBlockHash[blockHash] deposits := depositsByBlockHash[blockHash]
batches := stateBatches[blockHash]
if len(deposits) == 0 && len(batches) == 0 && i != len(headers)-1 {
continue
}
block := &db.IndexedL1Block{ block := &db.IndexedL1Block{
Hash: blockHash, Hash: blockHash,
...@@ -313,7 +318,6 @@ func (s *Service) Update(newHeader *types.Header) error { ...@@ -313,7 +318,6 @@ func (s *Service) Update(newHeader *types.Header) error {
return err return err
} }
batches := stateBatches[blockHash]
err = s.cfg.DB.AddStateBatch(batches) err = s.cfg.DB.AddStateBatch(batches)
if err != nil { if err != nil {
logger.Error( logger.Error(
......
...@@ -69,15 +69,15 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { ...@@ -69,15 +69,15 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
} }
type ServiceConfig struct { type ServiceConfig struct {
Context context.Context Context context.Context
Metrics *metrics.Metrics Metrics *metrics.Metrics
L2Client *l2ethclient.Client L2Client *l2ethclient.Client
ChainID *big.Int ChainID *big.Int
ConfDepth uint64 ConfDepth uint64
MaxHeaderBatchSize uint64 MaxHeaderBatchSize uint64
StartBlockNumber uint64 StartBlockNumber uint64
StartBlockHash string StartBlockHash string
DB *db.Database DB *db.Database
} }
type Service struct { type Service struct {
...@@ -89,9 +89,9 @@ type Service struct { ...@@ -89,9 +89,9 @@ type Service struct {
latestHeader uint64 latestHeader uint64
headerSelector *ConfirmedHeaderSelector headerSelector *ConfirmedHeaderSelector
metrics *metrics.Metrics metrics *metrics.Metrics
tokenCache map[common.Address]*db.Token tokenCache map[common.Address]*db.Token
wg sync.WaitGroup wg sync.WaitGroup
} }
type IndexerStatus struct { type IndexerStatus struct {
...@@ -267,11 +267,15 @@ func (s *Service) Update(newHeader *types.Header) error { ...@@ -267,11 +267,15 @@ func (s *Service) Update(newHeader *types.Header) error {
} }
} }
for _, header := range headers { for i, header := range headers {
blockHash := header.Hash() blockHash := header.Hash()
number := header.Number.Uint64() number := header.Number.Uint64()
withdrawals := withdrawalsByBlockHash[blockHash] withdrawals := withdrawalsByBlockHash[blockHash]
if len(withdrawals) == 0 && i != len(headers)-1 {
continue
}
block := &db.IndexedL2Block{ block := &db.IndexedL2Block{
Hash: blockHash, Hash: blockHash,
ParentHash: header.ParentHash, ParentHash: header.ParentHash,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment