From 826ef4062641009d9c37fa0f62f1c822dc00eb27 Mon Sep 17 00:00:00 2001 From: Phi Date: Tue, 8 Aug 2023 10:06:19 +0200 Subject: [PATCH] Only display `chain sync in progress` if behind sync Only display `chain sync in progress` if behind sync --- cli/wallet.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/cli/wallet.go b/cli/wallet.go index 2afe8617b..e6786648e 100644 --- a/cli/wallet.go +++ b/cli/wallet.go @@ -3,11 +3,13 @@ package cli import ( "bufio" "bytes" + "context" "encoding/hex" "encoding/json" "fmt" "os" "strings" + "time" "github.com/urfave/cli/v2" "golang.org/x/term" @@ -19,6 +21,7 @@ import ( "github.com/filecoin-project/go-state-types/crypto" "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/chain/types" "github.com/filecoin-project/lotus/lib/tablewriter" @@ -206,7 +209,12 @@ var walletBalance = &cli.Command{ return err } - if balance.Equals(types.NewInt(0)) { + behindSync, err := isSyncBehind(ctx, api) + if err != nil { + return err + } + + if balance.Equals(types.NewInt(0)) && behindSync { afmt.Printf("%s (warning: may display 0 if chain sync in progress)\n", types.FIL(balance)) } else { afmt.Printf("%s\n", types.FIL(balance)) @@ -754,3 +762,16 @@ var walletMarketAdd = &cli.Command{ 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 +}