2019-11-15 16:38:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-16 12:34:52 +00:00
|
|
|
"bytes"
|
2019-11-15 16:38:56 +00:00
|
|
|
"container/list"
|
|
|
|
"context"
|
2020-01-19 16:18:47 +00:00
|
|
|
"encoding/json"
|
2020-06-25 00:57:51 +00:00
|
|
|
"fmt"
|
2020-01-08 16:29:46 +00:00
|
|
|
"math"
|
2020-07-09 21:18:19 +00:00
|
|
|
"sort"
|
2019-11-22 16:20:56 +00:00
|
|
|
"sync"
|
2020-06-25 00:57:51 +00:00
|
|
|
"time"
|
2020-02-26 22:54:34 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-06-26 00:03:28 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2020-07-08 22:30:24 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-06-26 00:03:28 +00:00
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-02-26 22:54:34 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
2020-06-26 00:03:28 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
2020-07-09 21:18:19 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/market"
|
2020-06-26 00:03:28 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/power"
|
2020-07-08 22:30:24 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/reward"
|
2020-06-26 00:03:28 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
2019-11-15 16:38:56 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-05-28 00:06:29 +00:00
|
|
|
parmap "github.com/filecoin-project/lotus/lib/parmap"
|
2019-11-15 16:38:56 +00:00
|
|
|
)
|
|
|
|
|
2020-02-21 18:49:49 +00:00
|
|
|
func runSyncer(ctx context.Context, api api.FullNode, st *storage, maxBatch int) {
|
2019-11-15 16:38:56 +00:00
|
|
|
notifs, err := api.ChainNotify(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for notif := range notifs {
|
|
|
|
for _, change := range notif {
|
|
|
|
switch change.Type {
|
|
|
|
case store.HCCurrent:
|
|
|
|
fallthrough
|
|
|
|
case store.HCApply:
|
2020-02-21 18:49:49 +00:00
|
|
|
syncHead(ctx, api, st, change.Val, maxBatch)
|
2019-11-15 16:38:56 +00:00
|
|
|
case store.HCRevert:
|
|
|
|
log.Warnf("revert todo")
|
|
|
|
}
|
2019-12-13 14:47:51 +00:00
|
|
|
|
|
|
|
if change.Type == store.HCCurrent {
|
|
|
|
go subMpool(ctx, api, st)
|
|
|
|
go subBlocks(ctx, api, st)
|
|
|
|
}
|
2019-11-15 16:38:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-07-08 22:30:24 +00:00
|
|
|
type rewardStateInfo struct {
|
|
|
|
stateroot cid.Cid
|
|
|
|
baselinePower big.Int
|
|
|
|
}
|
|
|
|
|
2020-06-30 20:22:58 +00:00
|
|
|
type minerStateInfo struct {
|
2020-06-25 00:57:51 +00:00
|
|
|
// common
|
2019-11-16 12:34:52 +00:00
|
|
|
addr address.Address
|
|
|
|
act types.Actor
|
|
|
|
stateroot cid.Cid
|
|
|
|
|
2020-07-02 04:08:45 +00:00
|
|
|
// calculating changes
|
|
|
|
tsKey types.TipSetKey
|
|
|
|
parentTsKey types.TipSetKey
|
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// miner specific
|
2020-02-25 21:09:22 +00:00
|
|
|
state miner.State
|
2020-07-01 11:50:50 +00:00
|
|
|
info *miner.MinerInfo
|
2019-12-11 23:31:59 +00:00
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// tracked by power actor
|
|
|
|
rawPower big.Int
|
|
|
|
qalPower big.Int
|
|
|
|
ssize uint64
|
|
|
|
psize uint64
|
2019-11-16 12:34:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 01:13:27 +00:00
|
|
|
type marketStateInfo struct {
|
|
|
|
// common
|
|
|
|
act types.Actor
|
|
|
|
stateroot cid.Cid
|
|
|
|
|
|
|
|
// calculating changes
|
|
|
|
// calculating changes
|
|
|
|
tsKey types.TipSetKey
|
|
|
|
parentTsKey types.TipSetKey
|
|
|
|
|
|
|
|
// market actor specific
|
|
|
|
state market.State
|
|
|
|
}
|
|
|
|
|
2020-01-19 16:18:47 +00:00
|
|
|
type actorInfo struct {
|
2020-07-02 04:08:45 +00:00
|
|
|
stateroot cid.Cid
|
|
|
|
tsKey types.TipSetKey
|
|
|
|
parentTsKey types.TipSetKey
|
|
|
|
state string
|
2020-01-19 16:18:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 21:18:19 +00:00
|
|
|
type tipsetKeyHeight struct {
|
|
|
|
height abi.ChainEpoch
|
|
|
|
tsKey types.TipSetKey
|
|
|
|
}
|
|
|
|
|
2020-06-24 17:39:45 +00:00
|
|
|
func syncHead(ctx context.Context, api api.FullNode, st *storage, headTs *types.TipSet, maxBatch int) {
|
2019-11-15 18:37:57 +00:00
|
|
|
var alk sync.Mutex
|
|
|
|
|
2019-12-11 22:17:44 +00:00
|
|
|
log.Infof("Getting synced block list")
|
|
|
|
|
|
|
|
hazlist := st.hasList()
|
|
|
|
|
2019-11-15 18:37:57 +00:00
|
|
|
log.Infof("Getting headers / actors")
|
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// global list of all blocks that need to be synced
|
2020-01-08 16:29:46 +00:00
|
|
|
allToSync := map[cid.Cid]*types.BlockHeader{}
|
2020-06-25 00:57:51 +00:00
|
|
|
// a stack
|
2019-11-15 16:38:56 +00:00
|
|
|
toVisit := list.New()
|
|
|
|
|
2020-06-24 17:39:45 +00:00
|
|
|
for _, header := range headTs.Blocks() {
|
2019-11-15 16:38:56 +00:00
|
|
|
toVisit.PushBack(header)
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// TODO consider making a db query to check where syncing left off at in the case of a restart and avoid reprocessing
|
|
|
|
// those entries, or write value to file on shutdown
|
|
|
|
// walk the entire chain starting from headTS
|
2019-11-15 16:38:56 +00:00
|
|
|
for toVisit.Len() > 0 {
|
|
|
|
bh := toVisit.Remove(toVisit.Back()).(*types.BlockHeader)
|
2019-12-11 22:17:44 +00:00
|
|
|
_, has := hazlist[bh.Cid()]
|
2020-01-08 16:29:46 +00:00
|
|
|
if _, seen := allToSync[bh.Cid()]; seen || has {
|
2019-11-15 18:37:57 +00:00
|
|
|
continue
|
2019-11-15 16:38:56 +00:00
|
|
|
}
|
2019-11-15 18:37:57 +00:00
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
allToSync[bh.Cid()] = bh
|
|
|
|
if len(allToSync)%500 == 10 {
|
2020-06-30 17:26:41 +00:00
|
|
|
log.Debugf("to visit: (%d) %s @%d", len(allToSync), bh.Cid(), bh.Height)
|
2019-11-15 16:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(bh.Parents) == 0 {
|
2019-11-15 18:37:57 +00:00
|
|
|
continue
|
2019-11-15 16:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pts, err := api.ChainGetTipSet(ctx, types.NewTipSetKey(bh.Parents...))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, header := range pts.Blocks() {
|
|
|
|
toVisit.PushBack(header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// Main worker loop, this loop runs until all tipse from headTS to genesis have been processed.
|
2020-01-08 16:29:46 +00:00
|
|
|
for len(allToSync) > 0 {
|
2020-06-25 00:57:51 +00:00
|
|
|
// first map is addresses -> common actors states (head, code, balance, nonce)
|
|
|
|
// second map common actor states -> chain state (tipset, stateroot) & unique actor state (deserialization of their head CID) represented as json.
|
2020-02-20 03:06:41 +00:00
|
|
|
actors := map[address.Address]map[types.Actor]actorInfo{}
|
2020-06-25 00:57:51 +00:00
|
|
|
|
|
|
|
// map of actor public key address to ID address
|
2020-06-24 17:39:45 +00:00
|
|
|
addressToID := map[address.Address]address.Address{}
|
2020-02-26 09:05:22 +00:00
|
|
|
minH := abi.ChainEpoch(math.MaxInt64)
|
2019-11-15 16:38:56 +00:00
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// find the blockheader with the lowest height
|
2020-01-08 16:29:46 +00:00
|
|
|
for _, header := range allToSync {
|
|
|
|
if header.Height < minH {
|
|
|
|
minH = header.Height
|
|
|
|
}
|
2019-11-15 18:37:57 +00:00
|
|
|
}
|
2019-11-16 12:34:52 +00:00
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// toSync maps block cids to their headers and contains all block headers that will be synced in this batch
|
|
|
|
// `maxBatch` is a tunable parameter to control how many blocks we sync per iteration.
|
2020-01-08 16:29:46 +00:00
|
|
|
toSync := map[cid.Cid]*types.BlockHeader{}
|
|
|
|
for c, header := range allToSync {
|
2020-02-26 09:05:22 +00:00
|
|
|
if header.Height < minH+abi.ChainEpoch(maxBatch) {
|
2020-01-08 16:29:46 +00:00
|
|
|
toSync[c] = header
|
2020-06-24 17:39:45 +00:00
|
|
|
addressToID[header.Miner] = address.Undef
|
2020-01-08 16:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-25 00:57:51 +00:00
|
|
|
// remove everything we are syncing this round from the global list of blocks to sync
|
2020-01-08 16:29:46 +00:00
|
|
|
for c := range toSync {
|
|
|
|
delete(allToSync, c)
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
log.Infow("Starting Sync", "height", minH, "numBlocks", len(toSync), "maxBatch", maxBatch)
|
2020-01-08 16:29:46 +00:00
|
|
|
|
2020-07-09 21:18:19 +00:00
|
|
|
// relate tipset keys to height so they may be processed in ascending order.
|
|
|
|
var tipHeights []tipsetKeyHeight
|
|
|
|
tipsSeen := make(map[types.TipSetKey]struct{})
|
2020-06-26 00:00:22 +00:00
|
|
|
// map of addresses to changed actors
|
|
|
|
var changes map[string]types.Actor
|
2020-06-25 00:57:51 +00:00
|
|
|
// collect all actor state that has changes between block headers
|
2020-01-08 16:29:46 +00:00
|
|
|
paDone := 0
|
2020-05-28 00:06:29 +00:00
|
|
|
parmap.Par(50, parmap.MapArr(toSync), func(bh *types.BlockHeader) {
|
2020-01-08 16:29:46 +00:00
|
|
|
paDone++
|
|
|
|
if paDone%100 == 0 {
|
|
|
|
log.Infof("pa: %d %d%%", paDone, (paDone*100)/len(toSync))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bh.Parents) == 0 { // genesis case
|
2020-06-24 17:39:45 +00:00
|
|
|
genesisTs, _ := types.NewTipSet([]*types.BlockHeader{bh})
|
2020-07-02 04:08:45 +00:00
|
|
|
st.genesisTs = genesisTs
|
|
|
|
|
2020-06-24 17:39:45 +00:00
|
|
|
aadrs, err := api.StateListActors(ctx, genesisTs.Key())
|
2020-01-08 16:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// TODO suspicious there is not a lot to be gained by doing this in parallel since the genesis state
|
|
|
|
// is unlikely to contain a lot of actors, why not for loop here?
|
2020-05-28 00:06:29 +00:00
|
|
|
parmap.Par(50, aadrs, func(addr address.Address) {
|
2020-06-24 17:39:45 +00:00
|
|
|
act, err := api.StateGetActor(ctx, addr, genesisTs.Key())
|
2020-01-08 16:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-17 05:10:29 +00:00
|
|
|
|
|
|
|
ast, err := api.StateReadState(ctx, addr, genesisTs.Key())
|
2020-01-19 16:18:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
state, err := json.Marshal(ast.State)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
alk.Lock()
|
|
|
|
_, ok := actors[addr]
|
|
|
|
if !ok {
|
2020-01-19 16:18:47 +00:00
|
|
|
actors[addr] = map[types.Actor]actorInfo{}
|
|
|
|
}
|
|
|
|
actors[addr][*act] = actorInfo{
|
2020-07-02 04:08:45 +00:00
|
|
|
stateroot: bh.ParentStateRoot,
|
|
|
|
tsKey: genesisTs.Key(),
|
|
|
|
parentTsKey: genesisTs.Key(),
|
|
|
|
state: string(state),
|
2020-01-08 16:29:46 +00:00
|
|
|
}
|
2020-06-24 17:39:45 +00:00
|
|
|
addressToID[addr] = address.Undef
|
2020-01-08 16:29:46 +00:00
|
|
|
alk.Unlock()
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pts, err := api.ChainGetTipSet(ctx, types.NewTipSetKey(bh.Parents...))
|
2019-11-16 12:34:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// TODO Does this return actors that have been deleted between states?
|
|
|
|
// collect all actors that had state changes between the blockheader parent-state and its grandparent-state.
|
2020-06-26 00:00:22 +00:00
|
|
|
changes, err = api.StateChangedActors(ctx, pts.ParentState(), bh.ParentStateRoot)
|
2020-01-08 16:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// record the state of all actors that have changed
|
2020-01-08 16:29:46 +00:00
|
|
|
for a, act := range changes {
|
2020-05-27 20:53:20 +00:00
|
|
|
act := act
|
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
addr, err := address.NewFromString(a)
|
2019-11-16 12:34:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-24 17:39:45 +00:00
|
|
|
|
2020-06-17 05:10:29 +00:00
|
|
|
ast, err := api.StateReadState(ctx, addr, pts.Key())
|
2020-01-19 16:18:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-24 17:39:45 +00:00
|
|
|
|
2020-01-19 16:18:47 +00:00
|
|
|
state, err := json.Marshal(ast.State)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2020-01-08 16:29:46 +00:00
|
|
|
|
2019-11-16 12:34:52 +00:00
|
|
|
alk.Lock()
|
|
|
|
_, ok := actors[addr]
|
|
|
|
if !ok {
|
2020-01-19 16:18:47 +00:00
|
|
|
actors[addr] = map[types.Actor]actorInfo{}
|
|
|
|
}
|
2020-06-25 00:57:51 +00:00
|
|
|
// a change occurred for the actor with address `addr` and state `act` at tipset `pts`.
|
2020-01-19 16:18:47 +00:00
|
|
|
actors[addr][act] = actorInfo{
|
2020-07-02 04:08:45 +00:00
|
|
|
stateroot: bh.ParentStateRoot,
|
|
|
|
state: string(state),
|
|
|
|
tsKey: pts.Key(),
|
|
|
|
parentTsKey: pts.Parents(),
|
2019-11-16 12:34:52 +00:00
|
|
|
}
|
2020-06-24 17:39:45 +00:00
|
|
|
addressToID[addr] = address.Undef
|
2020-07-09 21:18:19 +00:00
|
|
|
if _, ok := tipsSeen[pts.Key()]; !ok {
|
|
|
|
tipHeights = append(tipHeights, tipsetKeyHeight{
|
|
|
|
height: pts.Height(),
|
|
|
|
tsKey: pts.Key(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
tipsSeen[pts.Key()] = struct{}{}
|
2019-11-16 12:34:52 +00:00
|
|
|
alk.Unlock()
|
2020-01-08 16:29:46 +00:00
|
|
|
}
|
|
|
|
})
|
2020-07-09 21:18:19 +00:00
|
|
|
// sort tipHeights in ascending order.
|
|
|
|
sort.Slice(tipHeights, func(i, j int) bool {
|
|
|
|
return tipHeights[i].height < tipHeights[j].height
|
|
|
|
})
|
2019-11-16 12:34:52 +00:00
|
|
|
|
2020-07-08 22:30:24 +00:00
|
|
|
// map of tipset to reward state
|
|
|
|
rewardTips := make(map[types.TipSetKey]*rewardStateInfo, len(changes))
|
2020-06-26 00:00:22 +00:00
|
|
|
// map of tipset to all miners that had a head-change at that tipset.
|
2020-06-30 20:22:58 +00:00
|
|
|
minerTips := make(map[types.TipSetKey][]*minerStateInfo, len(changes))
|
2020-06-26 00:00:22 +00:00
|
|
|
// heads we've seen, im being paranoid
|
|
|
|
headsSeen := make(map[cid.Cid]struct{}, len(actors))
|
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
log.Infof("Getting messages")
|
2019-11-16 12:34:52 +00:00
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
msgs, incls := fetchMessages(ctx, api, toSync)
|
2019-11-16 12:34:52 +00:00
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
log.Infof("Resolving addresses")
|
|
|
|
|
|
|
|
for _, message := range msgs {
|
2020-06-24 17:39:45 +00:00
|
|
|
addressToID[message.To] = address.Undef
|
|
|
|
addressToID[message.From] = address.Undef
|
2019-11-15 18:37:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:39:45 +00:00
|
|
|
parmap.Par(50, parmap.KMapArr(addressToID), func(addr address.Address) {
|
|
|
|
// FIXME: cannot use EmptyTSK here since actorID's can change during reorgs, need to use the corresponding tipset.
|
|
|
|
// TODO: figure out a way to get the corresponding tipset...
|
2020-02-11 23:29:45 +00:00
|
|
|
raddr, err := api.StateLookupID(ctx, addr, types.EmptyTSK)
|
2019-11-15 18:37:57 +00:00
|
|
|
if err != nil {
|
2020-01-08 16:29:46 +00:00
|
|
|
log.Warn(err)
|
2019-11-15 18:37:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
alk.Lock()
|
2020-06-24 17:39:45 +00:00
|
|
|
addressToID[addr] = raddr
|
2019-11-15 18:37:57 +00:00
|
|
|
alk.Unlock()
|
2020-01-08 16:29:46 +00:00
|
|
|
})
|
2019-12-12 18:34:28 +00:00
|
|
|
|
2020-07-08 22:30:24 +00:00
|
|
|
log.Infof("Getting actor change info")
|
2019-12-12 18:34:28 +00:00
|
|
|
|
2020-07-09 01:13:27 +00:00
|
|
|
// highly likely that the market actor will change at every epoch
|
|
|
|
marketActorChanges := make(map[types.TipSetKey]*marketStateInfo, len(changes))
|
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
minerChanges := 0
|
2020-01-08 16:29:46 +00:00
|
|
|
for addr, m := range actors {
|
|
|
|
for actor, c := range m {
|
2020-07-09 01:13:27 +00:00
|
|
|
// only want actors with head change events
|
|
|
|
if _, found := headsSeen[actor.Head]; found {
|
2020-01-08 16:29:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-07-09 01:13:27 +00:00
|
|
|
headsSeen[actor.Head] = struct{}{}
|
2019-11-15 18:37:57 +00:00
|
|
|
|
2020-07-09 01:13:27 +00:00
|
|
|
switch actor.Code {
|
|
|
|
case builtin.StorageMarketActorCodeID:
|
|
|
|
marketActorChanges[c.tsKey] = &marketStateInfo{
|
|
|
|
act: actor,
|
|
|
|
stateroot: c.stateroot,
|
|
|
|
tsKey: c.tsKey,
|
|
|
|
parentTsKey: c.parentTsKey,
|
|
|
|
state: market.State{},
|
2020-07-08 22:30:24 +00:00
|
|
|
}
|
2020-07-09 01:13:27 +00:00
|
|
|
case builtin.StorageMinerActorCodeID:
|
2020-07-08 22:30:24 +00:00
|
|
|
minerChanges++
|
2020-06-25 00:57:51 +00:00
|
|
|
|
2020-07-08 22:30:24 +00:00
|
|
|
minerTips[c.tsKey] = append(minerTips[c.tsKey], &minerStateInfo{
|
|
|
|
addr: addr,
|
|
|
|
act: actor,
|
|
|
|
stateroot: c.stateroot,
|
2019-12-12 18:34:28 +00:00
|
|
|
|
2020-07-08 22:30:24 +00:00
|
|
|
tsKey: c.tsKey,
|
|
|
|
parentTsKey: c.parentTsKey,
|
2020-07-02 04:08:45 +00:00
|
|
|
|
2020-07-08 22:30:24 +00:00
|
|
|
state: miner.State{},
|
2020-07-10 19:50:38 +00:00
|
|
|
info: nil,
|
2019-11-16 12:34:52 +00:00
|
|
|
|
2020-07-08 22:30:24 +00:00
|
|
|
rawPower: big.Zero(),
|
|
|
|
qalPower: big.Zero(),
|
|
|
|
})
|
2020-07-09 01:13:27 +00:00
|
|
|
// reward actor
|
|
|
|
case builtin.RewardActorCodeID:
|
|
|
|
rewardTips[c.tsKey] = &rewardStateInfo{
|
|
|
|
stateroot: c.stateroot,
|
|
|
|
baselinePower: big.Zero(),
|
|
|
|
}
|
2020-07-08 22:30:24 +00:00
|
|
|
}
|
2020-07-09 01:13:27 +00:00
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
}
|
2020-06-25 00:57:51 +00:00
|
|
|
}
|
2019-11-16 12:34:52 +00:00
|
|
|
|
2020-07-08 22:30:24 +00:00
|
|
|
rewardProcessingStartedAt := time.Now()
|
|
|
|
parmap.Par(50, parmap.KVMapArr(rewardTips), func(it func() (types.TipSetKey, *rewardStateInfo)) {
|
|
|
|
tsKey, rewardInfo := it()
|
|
|
|
// get reward actor states at each tipset once for all updates
|
|
|
|
rewardActor, err := api.StateGetActor(ctx, builtin.RewardActorAddr, tsKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(xerrors.Errorf("get reward state (@ %s): %w", rewardInfo.stateroot.String(), err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rewardStateRaw, err := api.ChainReadObj(ctx, rewardActor.Head)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(xerrors.Errorf("read state obj (@ %s): %w", rewardInfo.stateroot.String(), err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rewardActorState reward.State
|
|
|
|
if err := rewardActorState.UnmarshalCBOR(bytes.NewReader(rewardStateRaw)); err != nil {
|
|
|
|
log.Error(xerrors.Errorf("unmarshal state (@ %s): %w", rewardInfo.stateroot.String(), err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:30:21 +00:00
|
|
|
panic("TODO")
|
|
|
|
//rewardInfo.baselinePower = rewardActorState.BaselinePower
|
2020-07-08 22:30:24 +00:00
|
|
|
})
|
|
|
|
log.Infow("Completed Reward Processing", "duration", time.Since(rewardProcessingStartedAt).String(), "processed", len(rewardTips))
|
|
|
|
|
2020-06-26 00:00:22 +00:00
|
|
|
minerProcessingStartedAt := time.Now()
|
2020-06-25 00:57:51 +00:00
|
|
|
log.Infow("Processing miners", "numTips", len(minerTips), "numMinerChanges", minerChanges)
|
|
|
|
// extract the power actor state at each tipset, loop over all miners that changed at said tipset and extract their
|
|
|
|
// claims from the power actor state. This ensures we only fetch the power actors state once for each tipset.
|
2020-06-30 20:22:58 +00:00
|
|
|
parmap.Par(50, parmap.KVMapArr(minerTips), func(it func() (types.TipSetKey, []*minerStateInfo)) {
|
2020-06-25 00:57:51 +00:00
|
|
|
tsKey, minerInfo := it()
|
2019-11-16 12:34:52 +00:00
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// get the power actors claims map
|
|
|
|
mp, err := getPowerActorClaimsMap(ctx, api, tsKey)
|
2020-01-08 16:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
2019-11-16 12:34:52 +00:00
|
|
|
}
|
2020-06-25 00:57:51 +00:00
|
|
|
// Get miner raw and quality power
|
|
|
|
for _, mi := range minerInfo {
|
|
|
|
var claim power.Claim
|
|
|
|
// get miner claim from power actors claim map and store if found, else the miner had no claim at
|
|
|
|
// this tipset
|
|
|
|
found, err := mp.Get(adt.AddrKey(mi.addr), &claim)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
mi.qalPower = claim.QualityAdjPower
|
|
|
|
mi.rawPower = claim.RawBytePower
|
|
|
|
}
|
2019-11-16 12:34:52 +00:00
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// Get the miner state info
|
|
|
|
astb, err := api.ChainReadObj(ctx, mi.act.Head)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := mi.state.UnmarshalCBOR(bytes.NewReader(astb)); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2020-07-01 11:50:50 +00:00
|
|
|
mi.info, err = mi.state.GetInfo(&apiIpldStore{ctx, api})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2020-01-08 16:29:46 +00:00
|
|
|
}
|
2019-11-16 12:34:52 +00:00
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
// TODO Get the Sector Count
|
|
|
|
// FIXME this is returning a lot of "address not found" errors, which is strange given that StateChangedActors
|
|
|
|
// retruns all actors that had a state change at tipset `k.tsKey`, maybe its returning deleted miners too??
|
|
|
|
/*
|
|
|
|
sszs, err := api.StateMinerSectorCount(ctx, k.addr, k.tsKey)
|
|
|
|
if err != nil {
|
|
|
|
info.psize = 0
|
|
|
|
info.ssize = 0
|
|
|
|
} else {
|
|
|
|
info.psize = sszs.Pset
|
|
|
|
info.ssize = sszs.Sset
|
|
|
|
}
|
|
|
|
*/
|
2020-01-08 16:29:46 +00:00
|
|
|
})
|
2020-06-26 00:00:22 +00:00
|
|
|
log.Infow("Completed Miner Processing", "duration", time.Since(minerProcessingStartedAt).String(), "processed", minerChanges)
|
2020-01-08 16:29:46 +00:00
|
|
|
|
2020-07-09 01:13:27 +00:00
|
|
|
log.Info("Getting market actor info")
|
|
|
|
// TODO: consider taking the min of the array length and using that for concurrency param, e.g:
|
|
|
|
// concurrency := math.Min(len(marketActorChanges), 50)
|
|
|
|
parmap.Par(50, parmap.MapArr(marketActorChanges), func(mrktInfo *marketStateInfo) {
|
|
|
|
astb, err := api.ChainReadObj(ctx, mrktInfo.act.Head)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := mrktInfo.state.UnmarshalCBOR(bytes.NewReader(astb)); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
log.Info("Getting receipts")
|
|
|
|
|
|
|
|
receipts := fetchParentReceipts(ctx, api, toSync)
|
|
|
|
|
|
|
|
log.Info("Storing headers")
|
|
|
|
|
|
|
|
if err := st.storeHeaders(toSync, true); err != nil {
|
|
|
|
log.Errorf("%+v", err)
|
2019-12-11 23:31:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
log.Info("Storing address mapping")
|
|
|
|
|
2020-06-24 17:39:45 +00:00
|
|
|
if err := st.storeAddressMap(addressToID); err != nil {
|
2019-11-16 12:34:52 +00:00
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
log.Info("Storing actors")
|
|
|
|
if err := st.storeActors(actors); err != nil {
|
2019-11-16 12:34:52 +00:00
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-08 22:30:24 +00:00
|
|
|
chainPowerStartedAt := time.Now()
|
|
|
|
if err := st.storeChainPower(rewardTips); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Infow("Stored chain power", "duration", time.Since(chainPowerStartedAt).String())
|
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
log.Info("Storing miners")
|
2020-06-30 17:26:41 +00:00
|
|
|
if err := st.storeMiners(minerTips); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2020-01-08 16:29:46 +00:00
|
|
|
|
2020-07-08 22:30:24 +00:00
|
|
|
minerPowerStartedAt := time.Now()
|
|
|
|
if err := st.storeMinerPower(minerTips); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Infow("Stored miner power", "duration", time.Since(minerPowerStartedAt).String())
|
|
|
|
|
2020-06-25 00:57:51 +00:00
|
|
|
sectorStart := time.Now()
|
|
|
|
if err := st.storeSectors(minerTips, api); err != nil {
|
2019-11-16 12:34:52 +00:00
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2020-07-08 22:30:24 +00:00
|
|
|
log.Infow("Stored miner sectors", "duration", time.Since(sectorStart).String())
|
2019-11-16 12:34:52 +00:00
|
|
|
|
2020-06-30 20:22:58 +00:00
|
|
|
log.Info("Storing miner sectors heads")
|
2020-07-01 04:26:46 +00:00
|
|
|
if err := st.storeMinerSectorsHeads(minerTips, api); err != nil {
|
2019-11-16 12:34:52 +00:00
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-02 04:08:45 +00:00
|
|
|
log.Info("updating miner sectors heads")
|
|
|
|
if err := st.updateMinerSectors(minerTips, api); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-10 23:55:39 +00:00
|
|
|
log.Info("Storing market actor deal proposal info")
|
2020-07-09 21:18:19 +00:00
|
|
|
if err := st.storeMarketActorDealProposals(marketActorChanges, tipHeights, api); err != nil {
|
2020-07-09 01:13:27 +00:00
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-10 23:55:39 +00:00
|
|
|
log.Info("Storing market actor deal state info")
|
2020-07-09 21:18:19 +00:00
|
|
|
if err := st.storeMarketActorDealStates(marketActorChanges, tipHeights, api); err != nil {
|
2020-07-09 01:13:27 +00:00
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-10 23:55:39 +00:00
|
|
|
log.Info("Updating market actor deal proposal info")
|
|
|
|
if err := st.updateMarketActorDealProposals(marketActorChanges, tipHeights, api); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
log.Infof("Storing messages")
|
|
|
|
|
|
|
|
if err := st.storeMessages(msgs); err != nil {
|
2019-11-16 12:34:52 +00:00
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2019-12-12 18:34:28 +00:00
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
log.Info("Storing message inclusions")
|
2019-12-12 18:34:28 +00:00
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
if err := st.storeMsgInclusions(incls); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2019-12-10 23:42:36 +00:00
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
log.Infof("Storing parent receipts")
|
2019-12-10 23:42:36 +00:00
|
|
|
|
2020-01-08 16:29:46 +00:00
|
|
|
if err := st.storeReceipts(receipts); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Infof("Sync stage done")
|
2019-12-10 23:42:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 00:49:52 +00:00
|
|
|
log.Infof("Get deals")
|
|
|
|
|
|
|
|
// TODO: incremental, gather expired
|
2020-06-24 17:39:45 +00:00
|
|
|
deals, err := api.StateMarketDeals(ctx, headTs.Key())
|
2020-01-20 00:49:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Store deals")
|
|
|
|
|
|
|
|
if err := st.storeDeals(deals); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-22 15:10:22 +00:00
|
|
|
log.Infof("Refresh views")
|
|
|
|
|
|
|
|
if err := st.refreshViews(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-15 16:38:56 +00:00
|
|
|
log.Infof("Sync done")
|
|
|
|
}
|
|
|
|
|
2019-11-15 18:37:57 +00:00
|
|
|
func fetchMessages(ctx context.Context, api api.FullNode, toSync map[cid.Cid]*types.BlockHeader) (map[cid.Cid]*types.Message, map[cid.Cid][]cid.Cid) {
|
2019-11-15 16:38:56 +00:00
|
|
|
var lk sync.Mutex
|
|
|
|
messages := map[cid.Cid]*types.Message{}
|
|
|
|
inclusions := map[cid.Cid][]cid.Cid{} // block -> msgs
|
|
|
|
|
2020-05-28 00:06:29 +00:00
|
|
|
parmap.Par(50, parmap.MapArr(toSync), func(header *types.BlockHeader) {
|
2019-11-15 18:37:57 +00:00
|
|
|
msgs, err := api.ChainGetBlockMessages(ctx, header.Cid())
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
2019-11-15 16:38:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 18:37:57 +00:00
|
|
|
vmm := make([]*types.Message, 0, len(msgs.Cids))
|
|
|
|
for _, m := range msgs.BlsMessages {
|
|
|
|
vmm = append(vmm, m)
|
|
|
|
}
|
2019-11-15 16:38:56 +00:00
|
|
|
|
2019-11-15 18:37:57 +00:00
|
|
|
for _, m := range msgs.SecpkMessages {
|
|
|
|
vmm = append(vmm, &m.Message)
|
|
|
|
}
|
2019-11-15 16:38:56 +00:00
|
|
|
|
2019-11-15 18:37:57 +00:00
|
|
|
lk.Lock()
|
|
|
|
for _, message := range vmm {
|
|
|
|
messages[message.Cid()] = message
|
|
|
|
inclusions[header.Cid()] = append(inclusions[header.Cid()], message.Cid())
|
|
|
|
}
|
|
|
|
lk.Unlock()
|
|
|
|
})
|
2019-11-15 16:38:56 +00:00
|
|
|
|
|
|
|
return messages, inclusions
|
|
|
|
}
|
2019-12-03 11:05:12 +00:00
|
|
|
|
|
|
|
type mrec struct {
|
|
|
|
msg cid.Cid
|
|
|
|
state cid.Cid
|
|
|
|
idx int
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchParentReceipts(ctx context.Context, api api.FullNode, toSync map[cid.Cid]*types.BlockHeader) map[mrec]*types.MessageReceipt {
|
|
|
|
var lk sync.Mutex
|
|
|
|
out := map[mrec]*types.MessageReceipt{}
|
|
|
|
|
2020-05-28 00:06:29 +00:00
|
|
|
parmap.Par(50, parmap.MapArr(toSync), func(header *types.BlockHeader) {
|
2019-12-03 11:05:12 +00:00
|
|
|
recs, err := api.ChainGetParentReceipts(ctx, header.Cid())
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msgs, err := api.ChainGetParentMessages(ctx, header.Cid())
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lk.Lock()
|
|
|
|
for i, r := range recs {
|
|
|
|
out[mrec{
|
|
|
|
msg: msgs[i].Cid,
|
|
|
|
state: header.ParentStateRoot,
|
|
|
|
idx: i,
|
|
|
|
}] = r
|
|
|
|
}
|
|
|
|
lk.Unlock()
|
|
|
|
})
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
2020-06-25 00:57:51 +00:00
|
|
|
|
|
|
|
// load the power actor state clam as an adt.Map at the tipset `ts`.
|
|
|
|
func getPowerActorClaimsMap(ctx context.Context, api api.FullNode, ts types.TipSetKey) (*adt.Map, error) {
|
|
|
|
powerActor, err := api.StateGetActor(ctx, builtin.StoragePowerActorAddr, ts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
powerRaw, err := api.ChainReadObj(ctx, powerActor.Head)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var powerActorState power.State
|
|
|
|
if err := powerActorState.UnmarshalCBOR(bytes.NewReader(powerRaw)); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal power actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &apiIpldStore{ctx, api}
|
|
|
|
return adt.AsMap(s, powerActorState.Claims)
|
|
|
|
}
|
|
|
|
|
|
|
|
// require for AMT and HAMT access
|
2020-06-26 00:00:22 +00:00
|
|
|
// TODO extract this to a common location in lotus and reuse the code
|
2020-06-25 00:57:51 +00:00
|
|
|
type apiIpldStore struct {
|
|
|
|
ctx context.Context
|
|
|
|
api api.FullNode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *apiIpldStore) Context() context.Context {
|
|
|
|
return ht.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *apiIpldStore) Get(ctx context.Context, c cid.Cid, out interface{}) error {
|
|
|
|
raw, err := ht.api.ChainReadObj(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cu, ok := out.(cbg.CBORUnmarshaler)
|
|
|
|
if ok {
|
|
|
|
if err := cu.UnmarshalCBOR(bytes.NewReader(raw)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-26 00:00:22 +00:00
|
|
|
return fmt.Errorf("Object does not implement CBORUnmarshaler: %T", out)
|
2020-06-25 00:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *apiIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error) {
|
|
|
|
return cid.Undef, fmt.Errorf("Put is not implemented on apiIpldStore")
|
|
|
|
}
|