Skip to content

Commit

Permalink
Treat fee stats as a migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Sep 4, 2024
1 parent 8c5369d commit dce46af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
10 changes: 3 additions & 7 deletions cmd/soroban-rpc/internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,12 @@ func (d *Daemon) mustInitializeStorage(cfg *config.Config) *feewindow.FeeWindows
// means the final range is only the fee stat analysis range.
//
feeStatsRange, err := db.GetMigrationLedgerRange(readTxMetaCtx, d.db, maxFeeRetentionWindow)
dataMigrations.Append(feeWindows.AsMigration(feeStatsRange))
if err != nil {
d.logger.WithError(err).Fatal("could not get ledger range for fee stats")
}
ledgerSeqRange := dataMigrations.ApplicableRange().Merge(feeStatsRange)

ledgerSeqRange := dataMigrations.ApplicableRange()

//
// 5. Apply migration for events & transactions, and perform fee stat analysis.
Expand All @@ -389,12 +391,6 @@ func (d *Daemon) mustInitializeStorage(cfg *config.Config) *feewindow.FeeWindows
Debug("Still initializing in-memory store")
}

if feeStatsRange.IsLedgerIncluded(currentSeq) { // skip irrelevant ledgers
if err = feeWindows.IngestFees(txMeta); err != nil {
d.logger.WithError(err).Fatal("could not initialize fee stats")
}
}

if err := dataMigrations.Apply(readTxMetaCtx, txMeta); err != nil {
d.logger.WithError(err).Fatal("could not apply migration for ledger ", currentSeq)
}
Expand Down
32 changes: 32 additions & 0 deletions cmd/soroban-rpc/internal/feewindow/feewindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package feewindow

import (
"context"
"errors"
"io"
"slices"
Expand Down Expand Up @@ -194,3 +195,34 @@ func (fw *FeeWindows) IngestFees(meta xdr.LedgerCloseMeta) error {
}
return nil
}

func (fw *FeeWindows) AsMigration(seqRange db.LedgerSeqRange) db.Migration {
return &feeWindowMigration{
firstLedger: seqRange.First,
lastLedger: seqRange.Last,
windows: fw,
}
}

type feeWindowMigration struct {
firstLedger uint32
lastLedger uint32
windows *FeeWindows
}

func (fw *feeWindowMigration) ApplicableRange() db.LedgerSeqRange {
return db.LedgerSeqRange{
First: fw.firstLedger,
Last: fw.lastLedger,
}
}

func (fw *feeWindowMigration) Apply(_ context.Context, meta xdr.LedgerCloseMeta) error {
return fw.windows.IngestFees(meta)
}

func (fw *feeWindowMigration) Commit(_ context.Context) error {
return nil // no-op
}

var _ db.Migration = &feeWindowMigration{}

0 comments on commit dce46af

Please sign in to comment.