Unify IsSyncDone

Unify IsSyncDone in cli/sync.go and cli/wallet.go
This commit is contained in:
Phi 2023-08-09 17:13:25 +02:00
parent 826ef40626
commit 18ae6bd18b
2 changed files with 15 additions and 24 deletions

View File

@ -273,11 +273,6 @@ func SyncWait(ctx context.Context, napi v0api.FullNode, watch bool) error {
continue continue
} }
head, err := napi.ChainHead(ctx)
if err != nil {
return err
}
working := -1 working := -1
for i, ss := range state.ActiveSyncs { for i, ss := range state.ActiveSyncs {
switch ss.Stage { switch ss.Stage {
@ -332,7 +327,11 @@ func SyncWait(ctx context.Context, napi v0api.FullNode, watch bool) error {
_ = target // todo: maybe print? (creates a bunch of line wrapping issues with most tipsets) _ = target // todo: maybe print? (creates a bunch of line wrapping issues with most tipsets)
if !watch && time.Now().Unix()-int64(head.MinTimestamp()) < int64(build.BlockDelaySecs) { isDone, err := IsSyncDone(ctx, napi)
if err != nil {
return err
}
if !watch && isDone {
fmt.Println("\nDone!") fmt.Println("\nDone!")
return nil return nil
} }
@ -347,3 +346,11 @@ func SyncWait(ctx context.Context, napi v0api.FullNode, watch bool) error {
i++ i++
} }
} }
func IsSyncDone(ctx context.Context, napi v0api.FullNode) (bool, error) {
head, err := napi.ChainHead(ctx)
if err != nil {
return false, err
}
return time.Now().Unix()-int64(head.MinTimestamp()) < int64(build.BlockDelaySecs), nil
}

View File

@ -3,13 +3,11 @@ package cli
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"context"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"golang.org/x/term" "golang.org/x/term"
@ -21,7 +19,6 @@ import (
"github.com/filecoin-project/go-state-types/crypto" "github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/network" "github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/tablewriter" "github.com/filecoin-project/lotus/lib/tablewriter"
@ -209,12 +206,12 @@ var walletBalance = &cli.Command{
return err return err
} }
behindSync, err := isSyncBehind(ctx, api) inSync, err := IsSyncDone(ctx, api)
if err != nil { if err != nil {
return err return err
} }
if balance.Equals(types.NewInt(0)) && behindSync { if balance.Equals(types.NewInt(0)) && !inSync {
afmt.Printf("%s (warning: may display 0 if chain sync in progress)\n", types.FIL(balance)) afmt.Printf("%s (warning: may display 0 if chain sync in progress)\n", types.FIL(balance))
} else { } else {
afmt.Printf("%s\n", types.FIL(balance)) afmt.Printf("%s\n", types.FIL(balance))
@ -762,16 +759,3 @@ var walletMarketAdd = &cli.Command{
return nil return nil
}, },
} }
func isSyncBehind(ctx context.Context, fullapi v0api.FullNode) (bool, error) {
head, err := fullapi.ChainHead(ctx)
if err != nil {
return false, err
}
if time.Now().Unix()-int64(head.MinTimestamp()) >= int64(build.BlockDelaySecs*5) { // if more than 5 epochs
return true, nil
}
return false, nil
}