Commit 22054dfb authored by Matthew Slipper's avatar Matthew Slipper

indexer: reduce DB load

parent d5e70866
...@@ -44,7 +44,7 @@ func NewMetrics(monitoredTokens map[string]string) *Metrics { ...@@ -44,7 +44,7 @@ func NewMetrics(monitoredTokens map[string]string) *Metrics {
return &Metrics{ return &Metrics{
SyncHeight: promauto.NewGaugeVec(prometheus.GaugeOpts{ SyncHeight: promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "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",
......
...@@ -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