Merge pull request #717 from filecoin-project/feat/sminer-sync-wait

storageminer: Wait for sync
This commit is contained in:
Łukasz Magiera 2019-12-04 02:26:30 +01:00 committed by GitHub
commit 42b59342ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 52 deletions

View File

@ -1,6 +1,7 @@
package cli package cli
import ( import (
"context"
"fmt" "fmt"
"time" "time"
@ -73,48 +74,52 @@ var syncWaitCmd = &cli.Command{
defer closer() defer closer()
ctx := ReqContext(cctx) ctx := ReqContext(cctx)
for { return SyncWait(ctx, napi)
state, err := napi.SyncState(ctx)
if err != nil {
return err
}
head, err := napi.ChainHead(ctx)
if err != nil {
return err
}
working := 0
for i, ss := range state.ActiveSyncs {
switch ss.Stage {
case api.StageSyncComplete:
default:
working = i
case api.StageIdle:
// not complete, not actively working
}
}
ss := state.ActiveSyncs[working]
var target []cid.Cid
if ss.Target != nil {
target = ss.Target.Cids()
}
fmt.Printf("\r\x1b[2KWorker %d: Target: %s\tState: %s\tHeight: %d", working, target, chain.SyncStageString(ss.Stage), ss.Height)
if time.Now().Unix()-int64(head.MinTimestamp()) < build.BlockDelay {
fmt.Println("\nDone!")
return nil
}
select {
case <-ctx.Done():
fmt.Println("\nExit by user")
return nil
case <-time.After(1 * time.Second):
}
}
}, },
} }
func SyncWait(ctx context.Context, napi api.FullNode) error {
for {
state, err := napi.SyncState(ctx)
if err != nil {
return err
}
head, err := napi.ChainHead(ctx)
if err != nil {
return err
}
working := 0
for i, ss := range state.ActiveSyncs {
switch ss.Stage {
case api.StageSyncComplete:
default:
working = i
case api.StageIdle:
// not complete, not actively working
}
}
ss := state.ActiveSyncs[working]
var target []cid.Cid
if ss.Target != nil {
target = ss.Target.Cids()
}
fmt.Printf("\r\x1b[2KWorker %d: Target: %s\tState: %s\tHeight: %d", working, target, chain.SyncStageString(ss.Stage), ss.Height)
if time.Now().Unix()-int64(head.MinTimestamp()) < build.BlockDelay {
fmt.Println("\nDone!")
return nil
}
select {
case <-ctx.Done():
fmt.Println("\nExit by user")
return nil
case <-time.After(1 * time.Second):
}
}
}

View File

@ -80,6 +80,23 @@ var initCmd = &cli.Command{
return xerrors.Errorf("fetching proof parameters: %w", err) return xerrors.Errorf("fetching proof parameters: %w", err)
} }
log.Info("Trying to connect to full node RPC")
api, closer, err := lcli.GetFullNodeAPI(cctx) // TODO: consider storing full node address in config
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
log.Info("Checking full node sync status")
if !cctx.Bool("genesis-miner") {
if err := lcli.SyncWait(ctx, api); err != nil {
return xerrors.Errorf("sync wait: %w", err)
}
}
log.Info("Checking if repo exists") log.Info("Checking if repo exists")
repoPath := cctx.String(FlagStorageRepo) repoPath := cctx.String(FlagStorageRepo)
@ -96,15 +113,6 @@ var initCmd = &cli.Command{
return xerrors.Errorf("repo at '%s' is already initialized", cctx.String(FlagStorageRepo)) return xerrors.Errorf("repo at '%s' is already initialized", cctx.String(FlagStorageRepo))
} }
log.Info("Trying to connect to full node RPC")
api, closer, err := lcli.GetFullNodeAPI(cctx) // TODO: consider storing full node address in config
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
log.Info("Checking full node version") log.Info("Checking full node version")
v, err := api.Version(ctx) v, err := api.Version(ctx)

View File

@ -35,6 +35,10 @@ var runCmd = &cli.Command{
Usage: "Enable use of GPU for mining operations", Usage: "Enable use of GPU for mining operations",
Value: true, Value: true,
}, },
&cli.BoolFlag{
Name: "nosync",
Usage: "Don't check full-node sync status",
},
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
if err := build.GetParams(true, false); err != nil { if err := build.GetParams(true, false); err != nil {
@ -61,6 +65,14 @@ var runCmd = &cli.Command{
return xerrors.Errorf("lotus-daemon API version doesn't match: local: ", api.Version{APIVersion: build.APIVersion}) return xerrors.Errorf("lotus-daemon API version doesn't match: local: ", api.Version{APIVersion: build.APIVersion})
} }
log.Info("Checking full node sync status")
if !cctx.Bool("nosync") {
if err := lcli.SyncWait(ctx, nodeApi); err != nil {
return xerrors.Errorf("sync wait: %w", err)
}
}
storageRepoPath := cctx.String(FlagStorageRepo) storageRepoPath := cctx.String(FlagStorageRepo)
r, err := repo.NewFS(storageRepoPath) r, err := repo.NewFS(storageRepoPath)
if err != nil { if err != nil {