2020-07-14 20:02:44 +00:00
|
|
|
package processor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2020-07-14 20:02:44 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/reward"
|
2020-08-19 20:34:58 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/smoothing"
|
2020-07-27 23:55:21 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-07-14 20:02:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type rewardActorInfo struct {
|
|
|
|
common actorInfo
|
|
|
|
|
2020-08-28 00:09:33 +00:00
|
|
|
cumSumBaselinePower big.Int
|
|
|
|
cumSumRealizedPower big.Int
|
2020-07-16 03:52:21 +00:00
|
|
|
|
2020-08-28 00:09:33 +00:00
|
|
|
effectiveNetworkTime int64
|
|
|
|
effectiveBaselinePower big.Int
|
2020-08-19 19:55:53 +00:00
|
|
|
|
2020-08-28 00:09:33 +00:00
|
|
|
newBaselinePower big.Int
|
|
|
|
newBaseReward big.Int
|
|
|
|
newSmoothingEstimate *smoothing.FilterEstimate
|
|
|
|
|
|
|
|
totalMinedReward big.Int
|
2020-07-14 20:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Processor) setupRewards() error {
|
|
|
|
tx, err := p.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := tx.Exec(`
|
2020-07-16 03:52:21 +00:00
|
|
|
/* captures chain-specific power state for any given stateroot */
|
2020-08-28 00:09:33 +00:00
|
|
|
create table if not exists chain_reward
|
2020-07-14 20:02:44 +00:00
|
|
|
(
|
|
|
|
state_root text not null
|
2020-08-28 00:09:33 +00:00
|
|
|
constraint chain_reward_pk
|
2020-07-14 20:02:44 +00:00
|
|
|
primary key,
|
2020-08-28 00:09:33 +00:00
|
|
|
cum_sum_baseline text not null,
|
|
|
|
cum_sum_realized text not null,
|
|
|
|
effective_network_time int not null,
|
|
|
|
effective_baseline_power text not null,
|
2020-08-19 19:55:53 +00:00
|
|
|
|
2020-08-28 00:09:33 +00:00
|
|
|
new_baseline_power text not null,
|
|
|
|
new_reward numeric not null,
|
|
|
|
new_reward_smoothed_position_estimate text not null,
|
|
|
|
new_reward_smoothed_velocity_estimate text not null,
|
|
|
|
|
|
|
|
total_mined_reward text not null
|
2020-08-19 19:55:53 +00:00
|
|
|
);
|
2020-07-14 20:02:44 +00:00
|
|
|
`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2020-07-27 23:55:21 +00:00
|
|
|
func (p *Processor) HandleRewardChanges(ctx context.Context, rewardTips ActorTips, nullRounds []types.TipSetKey) error {
|
|
|
|
rewardChanges, err := p.processRewardActors(ctx, rewardTips, nullRounds)
|
2020-07-14 20:02:44 +00:00
|
|
|
if err != nil {
|
2020-08-19 20:34:58 +00:00
|
|
|
return xerrors.Errorf("Failed to process reward actors: %w", err)
|
2020-07-14 20:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.persistRewardActors(ctx, rewardChanges); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-27 23:55:21 +00:00
|
|
|
func (p *Processor) processRewardActors(ctx context.Context, rewardTips ActorTips, nullRounds []types.TipSetKey) ([]rewardActorInfo, error) {
|
2020-07-14 20:02:44 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
2020-07-16 14:45:31 +00:00
|
|
|
log.Debugw("Processed Reward Actors", "duration", time.Since(start).String())
|
2020-07-14 20:02:44 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
var out []rewardActorInfo
|
|
|
|
for tipset, rewards := range rewardTips {
|
|
|
|
for _, act := range rewards {
|
|
|
|
var rw rewardActorInfo
|
|
|
|
rw.common = act
|
|
|
|
|
|
|
|
// get reward actor states at each tipset once for all updates
|
|
|
|
rewardActor, err := p.node.StateGetActor(ctx, builtin.RewardActorAddr, tipset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("get reward state (@ %s): %w", rw.common.stateroot.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rewardStateRaw, err := p.node.ChainReadObj(ctx, rewardActor.Head)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("read state obj (@ %s): %w", rw.common.stateroot.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rewardActorState reward.State
|
|
|
|
if err := rewardActorState.UnmarshalCBOR(bytes.NewReader(rewardStateRaw)); err != nil {
|
|
|
|
return nil, xerrors.Errorf("unmarshal state (@ %s): %w", rw.common.stateroot.String(), err)
|
|
|
|
}
|
|
|
|
|
2020-08-28 00:09:33 +00:00
|
|
|
rw.cumSumBaselinePower = rewardActorState.CumsumBaseline
|
|
|
|
rw.cumSumRealizedPower = rewardActorState.CumsumRealized
|
|
|
|
rw.effectiveNetworkTime = int64(rewardActorState.EffectiveNetworkTime)
|
|
|
|
rw.effectiveBaselinePower = rewardActorState.EffectiveBaselinePower
|
|
|
|
rw.newBaselinePower = rewardActorState.ThisEpochBaselinePower
|
|
|
|
rw.newBaseReward = rewardActorState.ThisEpochReward
|
|
|
|
rw.newSmoothingEstimate = rewardActorState.ThisEpochRewardSmoothed
|
|
|
|
rw.totalMinedReward = rewardActorState.TotalMined
|
2020-07-14 20:02:44 +00:00
|
|
|
out = append(out, rw)
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 23:55:21 +00:00
|
|
|
for _, tsKey := range nullRounds {
|
|
|
|
var rw rewardActorInfo
|
2020-07-28 14:37:29 +00:00
|
|
|
tipset, err := p.node.ChainGetTipSet(ctx, tsKey)
|
2020-07-27 23:55:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rw.common.tsKey = tipset.Key()
|
|
|
|
rw.common.height = tipset.Height()
|
|
|
|
rw.common.stateroot = tipset.ParentState()
|
|
|
|
rw.common.parentTsKey = tipset.Parents()
|
|
|
|
// get reward actor states at each tipset once for all updates
|
|
|
|
rewardActor, err := p.node.StateGetActor(ctx, builtin.RewardActorAddr, tsKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rewardStateRaw, err := p.node.ChainReadObj(ctx, rewardActor.Head)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var rewardActorState reward.State
|
|
|
|
if err := rewardActorState.UnmarshalCBOR(bytes.NewReader(rewardStateRaw)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-28 00:09:33 +00:00
|
|
|
rw.cumSumBaselinePower = rewardActorState.CumsumBaseline
|
|
|
|
rw.cumSumRealizedPower = rewardActorState.CumsumRealized
|
|
|
|
rw.effectiveNetworkTime = int64(rewardActorState.EffectiveNetworkTime)
|
|
|
|
rw.effectiveBaselinePower = rewardActorState.EffectiveBaselinePower
|
|
|
|
rw.newBaselinePower = rewardActorState.ThisEpochBaselinePower
|
|
|
|
rw.newBaseReward = rewardActorState.ThisEpochReward
|
|
|
|
rw.newSmoothingEstimate = rewardActorState.ThisEpochRewardSmoothed
|
|
|
|
rw.totalMinedReward = rewardActorState.TotalMined
|
2020-07-27 23:55:21 +00:00
|
|
|
out = append(out, rw)
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:02:44 +00:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Processor) persistRewardActors(ctx context.Context, rewards []rewardActorInfo) error {
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
2020-07-16 14:45:31 +00:00
|
|
|
log.Debugw("Persisted Reward Actors", "duration", time.Since(start).String())
|
2020-07-14 20:02:44 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
tx, err := p.db.Begin()
|
|
|
|
if err != nil {
|
2020-08-28 00:09:33 +00:00
|
|
|
return xerrors.Errorf("begin chain_reward tx: %w", err)
|
2020-07-14 20:02:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 00:09:33 +00:00
|
|
|
if _, err := tx.Exec(`create temp table cr (like chain_reward excluding constraints) on commit drop`); err != nil {
|
|
|
|
return xerrors.Errorf("prep chain_reward temp: %w", err)
|
2020-07-14 20:02:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 00:09:33 +00:00
|
|
|
stmt, err := tx.Prepare(`copy cr ( state_root, cum_sum_baseline, cum_sum_realized, effective_network_time, effective_baseline_power, new_baseline_power, new_reward, new_reward_smoothed_position_estimate, new_reward_smoothed_velocity_estimate, total_mined_reward) from STDIN`)
|
2020-07-14 20:02:44 +00:00
|
|
|
if err != nil {
|
2020-08-28 00:09:33 +00:00
|
|
|
return xerrors.Errorf("prepare tmp chain_reward: %w", err)
|
2020-07-14 20:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, rewardState := range rewards {
|
|
|
|
if _, err := stmt.Exec(
|
|
|
|
rewardState.common.stateroot.String(),
|
2020-08-28 00:09:33 +00:00
|
|
|
rewardState.cumSumBaselinePower.String(),
|
|
|
|
rewardState.cumSumRealizedPower.String(),
|
|
|
|
rewardState.effectiveNetworkTime,
|
|
|
|
rewardState.effectiveBaselinePower.String(),
|
|
|
|
rewardState.newBaselinePower.String(),
|
|
|
|
rewardState.newBaseReward.String(),
|
|
|
|
rewardState.newSmoothingEstimate.PositionEstimate.String(),
|
|
|
|
rewardState.newSmoothingEstimate.VelocityEstimate.String(),
|
|
|
|
rewardState.totalMinedReward.String(),
|
2020-07-14 20:02:44 +00:00
|
|
|
); err != nil {
|
|
|
|
log.Errorw("failed to store chain power", "state_root", rewardState.common.stateroot, "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := stmt.Close(); err != nil {
|
2020-08-28 00:09:33 +00:00
|
|
|
return xerrors.Errorf("close prepared chain_reward: %w", err)
|
2020-08-19 19:55:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 00:09:33 +00:00
|
|
|
if _, err := tx.Exec(`insert into chain_reward select * from cr on conflict do nothing`); err != nil {
|
|
|
|
return xerrors.Errorf("insert chain_reward from tmp: %w", err)
|
2020-08-19 19:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2020-08-28 00:09:33 +00:00
|
|
|
return xerrors.Errorf("commit chain_reward tx: %w", err)
|
2020-08-19 19:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|