Skip to content

Commit

Permalink
Merge pull request #11140 from filecoin-project/fix/wallet-balance-cmd
Browse files Browse the repository at this point in the history
fix: cli: Only display `warning` if behind sync
  • Loading branch information
magik6k authored Aug 16, 2023
2 parents 22f4901 + 3cec54f commit 8092858
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
19 changes: 13 additions & 6 deletions cli/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
7 changes: 6 additions & 1 deletion cli/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions cli/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8092858

Please sign in to comment.