diff --git a/cli/sync.go b/cli/sync.go index 02e4e381ff7..89d2d94f039 100644 --- a/cli/sync.go +++ b/cli/sync.go @@ -273,11 +273,6 @@ func SyncWait(ctx context.Context, napi v0api.FullNode, watch bool) error { continue } - head, err := napi.ChainHead(ctx) - if err != nil { - return err - } - working := -1 for i, ss := range state.ActiveSyncs { 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) - 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!") return nil } @@ -347,3 +346,11 @@ func SyncWait(ctx context.Context, napi v0api.FullNode, watch bool) error { 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 +} diff --git a/cli/wallet.go b/cli/wallet.go index 2afe8617b95..61946a46382 100644 --- a/cli/wallet.go +++ b/cli/wallet.go @@ -206,7 +206,12 @@ var walletBalance = &cli.Command{ return err } - if balance.Equals(types.NewInt(0)) { + inSync, err := IsSyncDone(ctx, api) + if err != nil { + return err + } + + if balance.Equals(types.NewInt(0)) && !inSync { afmt.Printf("%s (warning: may display 0 if chain sync in progress)\n", types.FIL(balance)) } else { afmt.Printf("%s\n", types.FIL(balance)) diff --git a/cli/wallet_test.go b/cli/wallet_test.go index dee26018b28..eb2c544f0a6 100644 --- a/cli/wallet_test.go +++ b/cli/wallet_test.go @@ -21,6 +21,7 @@ import ( "github.com/filecoin-project/lotus/api" apitypes "github.com/filecoin-project/lotus/api/types" "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/lotus/chain/types/mock" ) func TestWalletNew(t *testing.T) { @@ -133,6 +134,11 @@ func TestWalletBalance(t *testing.T) { balance := big.NewInt(1234) + // add blocks to the chain + first := mock.TipSet(mock.MkBlock(nil, 5, 4)) + head := mock.TipSet(mock.MkBlock(first, 15, 7)) + + mockApi.EXPECT().ChainHead(ctx).Return(head, nil) mockApi.EXPECT().WalletBalance(ctx, addr).Return(balance, nil) //stm: @CLI_WALLET_BALANCE_001