2020-07-03 14:52:40 +00:00
|
|
|
package stats
|
2019-10-12 00:13:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-05 18:12:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
2019-10-12 00:13:16 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-05-20 17:43:22 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-08-25 23:46:31 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2019-10-12 00:13:16 +00:00
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-10-18 11:51:35 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/api/client"
|
2019-11-17 21:29:56 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-18 11:51:35 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2019-10-12 00:13:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getAPI(path string) (string, http.Header, error) {
|
|
|
|
r, err := repo.NewFS(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ma, err := r.APIEndpoint()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, xerrors.Errorf("failed to get api endpoint: %w", err)
|
|
|
|
}
|
|
|
|
_, addr, err := manet.DialArgs(ma)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
var headers http.Header
|
|
|
|
token, err := r.APIToken()
|
|
|
|
if err != nil {
|
2019-12-19 14:58:26 +00:00
|
|
|
log.Warnw("Couldn't load CLI token, capabilities may be limited", "error", err)
|
2019-10-12 00:13:16 +00:00
|
|
|
} else {
|
|
|
|
headers = http.Header{}
|
|
|
|
headers.Add("Authorization", "Bearer "+string(token))
|
|
|
|
}
|
|
|
|
|
|
|
|
return "ws://" + addr + "/rpc/v0", headers, nil
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:12:47 +00:00
|
|
|
func WaitForSyncComplete(ctx context.Context, napi v0api.FullNode) error {
|
2019-12-19 14:58:26 +00:00
|
|
|
sync_complete:
|
2019-10-12 00:13:16 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2020-07-10 14:43:14 +00:00
|
|
|
case <-build.Clock.After(5 * time.Second):
|
2019-12-19 14:58:26 +00:00
|
|
|
state, err := napi.SyncState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, w := range state.ActiveSyncs {
|
|
|
|
if w.Target == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.Stage == api.StageSyncErrored {
|
|
|
|
log.Errorw(
|
|
|
|
"Syncing",
|
|
|
|
"worker", i,
|
|
|
|
"base", w.Base.Key(),
|
|
|
|
"target", w.Target.Key(),
|
|
|
|
"target_height", w.Target.Height(),
|
|
|
|
"height", w.Height,
|
|
|
|
"error", w.Message,
|
2020-09-08 18:42:20 +00:00
|
|
|
"stage", w.Stage.String(),
|
2019-12-19 14:58:26 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
log.Infow(
|
|
|
|
"Syncing",
|
|
|
|
"worker", i,
|
|
|
|
"base", w.Base.Key(),
|
|
|
|
"target", w.Target.Key(),
|
|
|
|
"target_height", w.Target.Height(),
|
|
|
|
"height", w.Height,
|
2020-09-08 18:42:20 +00:00
|
|
|
"stage", w.Stage.String(),
|
2019-12-19 14:58:26 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.Stage == api.StageSyncComplete {
|
|
|
|
break sync_complete
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2020-07-10 14:43:14 +00:00
|
|
|
case <-build.Clock.After(5 * time.Second):
|
2019-11-17 21:29:56 +00:00
|
|
|
head, err := napi.ChainHead(ctx)
|
2019-10-12 00:13:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:43:14 +00:00
|
|
|
timestampDelta := build.Clock.Now().Unix() - int64(head.MinTimestamp())
|
2019-12-19 14:58:26 +00:00
|
|
|
|
|
|
|
log.Infow(
|
|
|
|
"Waiting for reasonable head height",
|
|
|
|
"height", head.Height(),
|
|
|
|
"timestamp_delta", timestampDelta,
|
|
|
|
)
|
2019-10-12 00:13:16 +00:00
|
|
|
|
2019-12-19 14:58:26 +00:00
|
|
|
// If we get within 20 blocks of the current exected block height we
|
|
|
|
// consider sync complete. Block propagation is not always great but we still
|
|
|
|
// want to be recording stats as soon as we can
|
2020-06-30 13:22:48 +00:00
|
|
|
if timestampDelta < int64(build.BlockDelaySecs)*20 {
|
2019-10-12 00:13:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:12:47 +00:00
|
|
|
func GetTips(ctx context.Context, api v0api.FullNode, lastHeight abi.ChainEpoch, headlag int) (<-chan *types.TipSet, error) {
|
2019-10-12 00:13:16 +00:00
|
|
|
chmain := make(chan *types.TipSet)
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
hb := newHeadBuffer(headlag)
|
2020-02-28 02:59:43 +00:00
|
|
|
|
2019-10-12 00:13:16 +00:00
|
|
|
notif, err := api.ChainNotify(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(chmain)
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
ticker := time.NewTicker(30 * time.Second)
|
|
|
|
defer ticker.Stop()
|
2019-10-17 21:36:08 +00:00
|
|
|
|
2019-10-12 00:13:16 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case changes := <-notif:
|
|
|
|
for _, change := range changes {
|
2019-12-19 14:58:26 +00:00
|
|
|
log.Infow("Head event", "height", change.Val.Height(), "type", change.Type)
|
2019-10-12 00:13:16 +00:00
|
|
|
|
|
|
|
switch change.Type {
|
|
|
|
case store.HCCurrent:
|
|
|
|
tipsets, err := loadTipsets(ctx, api, change.Val, lastHeight)
|
|
|
|
if err != nil {
|
2019-12-19 14:58:26 +00:00
|
|
|
log.Info(err)
|
2019-10-12 00:13:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tipset := range tipsets {
|
|
|
|
chmain <- tipset
|
|
|
|
}
|
|
|
|
case store.HCApply:
|
2020-08-20 04:49:10 +00:00
|
|
|
if out := hb.push(change); out != nil {
|
2020-02-28 02:59:43 +00:00
|
|
|
chmain <- out.Val
|
|
|
|
}
|
|
|
|
case store.HCRevert:
|
2020-08-20 04:49:10 +00:00
|
|
|
hb.pop()
|
2019-10-12 00:13:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-20 04:49:10 +00:00
|
|
|
case <-ticker.C:
|
2019-12-19 14:58:26 +00:00
|
|
|
log.Info("Running health check")
|
2019-10-17 21:36:08 +00:00
|
|
|
|
|
|
|
cctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
2020-03-06 06:46:07 +00:00
|
|
|
|
2019-10-17 21:36:08 +00:00
|
|
|
if _, err := api.ID(cctx); err != nil {
|
2019-12-19 14:58:26 +00:00
|
|
|
log.Error("Health check failed")
|
2020-03-06 06:46:07 +00:00
|
|
|
cancel()
|
2019-10-17 21:36:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
2019-12-19 14:58:26 +00:00
|
|
|
log.Info("Node online")
|
2019-10-12 00:13:16 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return chmain, nil
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:12:47 +00:00
|
|
|
func loadTipsets(ctx context.Context, api v0api.FullNode, curr *types.TipSet, lowestHeight abi.ChainEpoch) ([]*types.TipSet, error) {
|
2019-10-12 00:13:16 +00:00
|
|
|
tipsets := []*types.TipSet{}
|
|
|
|
for {
|
|
|
|
if curr.Height() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if curr.Height() <= lowestHeight {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-12-19 14:58:26 +00:00
|
|
|
log.Infow("Walking back", "height", curr.Height())
|
2019-10-12 00:13:16 +00:00
|
|
|
tipsets = append(tipsets, curr)
|
|
|
|
|
2019-12-16 19:22:56 +00:00
|
|
|
tsk := curr.Parents()
|
2019-11-19 15:53:00 +00:00
|
|
|
prev, err := api.ChainGetTipSet(ctx, tsk)
|
2019-10-12 00:13:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return tipsets, err
|
|
|
|
}
|
|
|
|
|
|
|
|
curr = prev
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, j := 0, len(tipsets)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
tipsets[i], tipsets[j] = tipsets[j], tipsets[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return tipsets, nil
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:12:47 +00:00
|
|
|
func GetFullNodeAPI(ctx context.Context, repo string) (v0api.FullNode, jsonrpc.ClientCloser, error) {
|
2019-10-12 00:13:16 +00:00
|
|
|
addr, headers, err := getAPI(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:12:47 +00:00
|
|
|
return client.NewFullNodeRPCV0(ctx, addr, headers)
|
2019-10-12 00:13:16 +00:00
|
|
|
}
|