Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move deposits-aggregation from exporter to statistics and r… #1235

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions backend/cmd/statistics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ import (
)

type options struct {
configPath string
statisticsDayToExport int64
statisticsDaysToExport string
statisticsValidatorToggle bool
statisticsChartToggle bool
statisticsGraffitiToggle bool
resetStatus bool
configPath string
statisticsDayToExport int64
statisticsDaysToExport string
statisticsValidatorToggle bool
statisticsChartToggle bool
statisticsGraffitiToggle bool
statisticsDepositsToggle bool
statisticsDepositsInterval time.Duration
resetStatus bool
}

var opt = &options{}
Expand All @@ -46,6 +48,8 @@ func Run() {
fs.BoolVar(&opt.statisticsValidatorToggle, "validators.enabled", false, "Toggle exporting validator statistics")
fs.BoolVar(&opt.statisticsChartToggle, "charts.enabled", false, "Toggle exporting chart series")
fs.BoolVar(&opt.statisticsGraffitiToggle, "graffiti.enabled", false, "Toggle exporting graffiti statistics")
fs.BoolVar(&opt.statisticsDepositsToggle, "deposits.enabled", false, "Toggle aggregating deposits")
fs.DurationVar(&opt.statisticsDepositsInterval, "deposits.interval", time.Hour*24, "Duration to wait between deposit aggregation")
fs.BoolVar(&opt.resetStatus, "validators.reset", false, "Export stats independent if they have already been exported previously")

versionFlag := fs.Bool("version", false, "Show version and exit")
Expand Down Expand Up @@ -248,6 +252,10 @@ func Run() {

go statisticsLoop(rpcClient)

if opt.statisticsDepositsToggle {
go depositsLoop()
}

utils.WaitForCtrlC()

log.Infof("exiting...")
Expand Down Expand Up @@ -287,6 +295,7 @@ func statisticsLoop(client rpc.Client) {
if lastExportedDayValidator != 0 {
lastExportedDayValidator++
}

if lastExportedDayValidator <= previousDay || lastExportedDayValidator == 0 {
for day := lastExportedDayValidator; day <= previousDay; day++ {
err := db.WriteValidatorStatisticsForDay(day, client)
Expand Down Expand Up @@ -358,6 +367,25 @@ func statisticsLoop(client rpc.Client) {
}
}

func depositsLoop() {
if opt.statisticsDepositsInterval < time.Minute {
log.Fatal(nil, "deposits.interval must be at least 1 minute", 0)
}
time.Sleep(time.Minute) // wait in case the process is in crashloop
for {
start := time.Now()
err := db.AggregateDeposits()
if err != nil {
log.Error(err, "error aggregating deposits", 0)
services.ReportStatus("deposits_aggregator", err.Error(), nil)
} else {
log.InfoWithFields(log.Fields{"duration": time.Since(start)}, "aggregated deposits")
services.ReportStatus("deposits_aggregator", "Running", nil)
}
time.Sleep(opt.statisticsDepositsInterval)
}
}

func clearStatsStatusTable(day uint64) {
log.Infof("deleting validator_stats_status for day %v", day)
_, err := db.WriterDb.Exec("DELETE FROM validator_stats_status WHERE day = $1", day)
Expand Down
44 changes: 44 additions & 0 deletions backend/pkg/commons/db/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -1843,3 +1843,47 @@ func CheckIfDayIsFinalized(day uint64) error {

return nil
}

func AggregateDeposits() error {
start := time.Now()
defer func() {
metrics.TaskDuration.WithLabelValues("statistics_aggregate_eth1_deposits").Observe(time.Since(start).Seconds())
}()
_, err := WriterDb.Exec(`
INSERT INTO eth1_deposits_aggregated (from_address, amount, validcount, invalidcount, slashedcount, totalcount, activecount, pendingcount, voluntary_exit_count)
SELECT
eth1.from_address,
SUM(eth1.amount) as amount,
SUM(eth1.validcount) AS validcount,
SUM(eth1.invalidcount) AS invalidcount,
COUNT(CASE WHEN v.status = 'slashed' THEN 1 END) AS slashedcount,
COUNT(v.pubkey) AS totalcount,
COUNT(CASE WHEN v.status = 'active_online' OR v.status = 'active_offline' THEN 1 END) as activecount,
COUNT(CASE WHEN v.status = 'deposited' THEN 1 END) AS pendingcount,
COUNT(CASE WHEN v.status = 'exited' THEN 1 END) AS voluntary_exit_count
FROM (
SELECT
from_address,
publickey,
SUM(amount) AS amount,
COUNT(CASE WHEN valid_signature = 't' THEN 1 END) AS validcount,
COUNT(CASE WHEN valid_signature = 'f' THEN 1 END) AS invalidcount
FROM eth1_deposits
GROUP BY from_address, publickey
) eth1
LEFT JOIN (SELECT pubkey, status FROM validators) v ON v.pubkey = eth1.publickey
GROUP BY eth1.from_address
ON CONFLICT (from_address) DO UPDATE SET
amount = excluded.amount,
validcount = excluded.validcount,
invalidcount = excluded.invalidcount,
slashedcount = excluded.slashedcount,
totalcount = excluded.totalcount,
activecount = excluded.activecount,
pendingcount = excluded.pendingcount,
voluntary_exit_count = excluded.voluntary_exit_count`)
if err != nil && err != sql.ErrNoRows {
return err
}
return nil
}
53 changes: 0 additions & 53 deletions backend/pkg/exporter/modules/execution_deposits_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/gobitfly/beaconchain/pkg/commons/contracts/deposit_contract"
"github.com/gobitfly/beaconchain/pkg/commons/db"
"github.com/gobitfly/beaconchain/pkg/commons/log"
"github.com/gobitfly/beaconchain/pkg/commons/metrics"
"github.com/gobitfly/beaconchain/pkg/commons/rpc"
"github.com/gobitfly/beaconchain/pkg/commons/services"
"github.com/gobitfly/beaconchain/pkg/commons/types"
Expand Down Expand Up @@ -282,13 +281,6 @@ func (d *executionDepositsExporter) export() (err error) {

log.Debugf("updating cached deposits view took %v", time.Since(start))

if len(depositsToSave) > 0 {
err = d.aggregateDeposits()
if err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -676,51 +668,6 @@ func (d *executionDepositsExporter) getDepositTraces(txsToTrace []string) (filte
return filteredTraces, nil
}

func (d *executionDepositsExporter) aggregateDeposits() error {
/// this could be a materialized view
start := time.Now()
defer func() {
metrics.TaskDuration.WithLabelValues("exporter_aggregate_eth1_deposits").Observe(time.Since(start).Seconds())
}()
_, err := db.WriterDb.Exec(`
INSERT INTO eth1_deposits_aggregated (from_address, amount, validcount, invalidcount, slashedcount, totalcount, activecount, pendingcount, voluntary_exit_count)
SELECT
eth1.from_address,
SUM(eth1.amount) as amount,
SUM(eth1.validcount) AS validcount,
SUM(eth1.invalidcount) AS invalidcount,
COUNT(CASE WHEN v.status = 'slashed' THEN 1 END) AS slashedcount,
COUNT(v.pubkey) AS totalcount,
COUNT(CASE WHEN v.status = 'active_online' OR v.status = 'active_offline' THEN 1 END) as activecount,
COUNT(CASE WHEN v.status = 'deposited' THEN 1 END) AS pendingcount,
COUNT(CASE WHEN v.status = 'exited' THEN 1 END) AS voluntary_exit_count
FROM (
SELECT
from_address,
publickey,
SUM(amount) AS amount,
COUNT(CASE WHEN valid_signature = 't' THEN 1 END) AS validcount,
COUNT(CASE WHEN valid_signature = 'f' THEN 1 END) AS invalidcount
FROM eth1_deposits
GROUP BY from_address, publickey
) eth1
LEFT JOIN (SELECT pubkey, status FROM validators) v ON v.pubkey = eth1.publickey
GROUP BY eth1.from_address
ON CONFLICT (from_address) DO UPDATE SET
amount = excluded.amount,
validcount = excluded.validcount,
invalidcount = excluded.invalidcount,
slashedcount = excluded.slashedcount,
totalcount = excluded.totalcount,
activecount = excluded.activecount,
pendingcount = excluded.pendingcount,
voluntary_exit_count = excluded.voluntary_exit_count`)
if err != nil && err != sql.ErrNoRows {
return nil
}
return err
}

func (d *executionDepositsExporter) updateCachedView() error {
err := db.CacheQuery(`
SELECT
Expand Down
Loading