2020-07-23 02:37:34 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2020-07-24 20:11:16 +00:00
|
|
|
func setupTopMinerByBaseRewardSchema(ctx context.Context, db *sql.DB) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tx.Exec(`
|
|
|
|
create materialized view if not exists top_miners_by_base_reward as
|
|
|
|
with total_rewards_by_miner as (
|
|
|
|
select
|
|
|
|
b.miner,
|
2020-07-24 20:19:22 +00:00
|
|
|
sum(bbr.base_block_reward * b.win_count) as total_reward
|
2020-07-24 20:11:16 +00:00
|
|
|
from blocks b
|
|
|
|
inner join base_block_rewards bbr on b.parentstateroot = bbr.state_root
|
|
|
|
group by 1
|
|
|
|
) select
|
|
|
|
rank() over (order by total_reward desc),
|
|
|
|
miner,
|
|
|
|
total_reward
|
|
|
|
from total_rewards_by_miner
|
|
|
|
group by 2, 3;
|
|
|
|
|
|
|
|
create index if not exists top_miners_by_base_reward_miner_index
|
|
|
|
on top_miners_by_base_reward (miner);
|
|
|
|
|
|
|
|
create materialized view if not exists top_miners_by_base_reward_max_height as
|
|
|
|
select
|
|
|
|
b."timestamp"as current_timestamp,
|
|
|
|
max(b.height) as current_height
|
|
|
|
from blocks b
|
|
|
|
join base_block_rewards bbr on b.parentstateroot = bbr.state_root
|
|
|
|
where bbr.base_block_reward is not null
|
|
|
|
group by 1
|
|
|
|
order by 1 desc
|
|
|
|
limit 1;
|
|
|
|
`); err != nil {
|
|
|
|
return xerrors.Errorf("create top_miner_by_base_reward views", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return xerrors.Errorf("commiting top_miner_by_base_reward views", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 02:37:34 +00:00
|
|
|
func refreshTopMinerByBaseReward(ctx context.Context, db *sql.DB) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
t := time.Now()
|
|
|
|
defer func() {
|
|
|
|
log.Debugw("refresh top_miners_by_base_reward", "duration", time.Since(t).String())
|
|
|
|
}()
|
|
|
|
|
2020-07-24 20:11:16 +00:00
|
|
|
_, err := db.Exec("refresh materialized view top_miners_by_base_reward;")
|
2020-07-23 02:37:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("refresh top_miners_by_base_reward: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-24 20:11:16 +00:00
|
|
|
_, err = db.Exec("refresh materialized view top_miners_by_base_reward_max_height;")
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("refresh top_miners_by_base_reward_max_height: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-23 02:37:34 +00:00
|
|
|
return nil
|
|
|
|
}
|