From 826ef4062641009d9c37fa0f62f1c822dc00eb27 Mon Sep 17 00:00:00 2001 From: Phi Date: Tue, 8 Aug 2023 10:06:19 +0200 Subject: [PATCH 1/3] 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 2afe8617b95..e6786648e4b 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 +} From 18ae6bd18b1aff6abc71868f6a6ec70a7f223d9a Mon Sep 17 00:00:00 2001 From: Phi Date: Wed, 9 Aug 2023 17:13:25 +0200 Subject: [PATCH 2/3] Unify IsSyncDone Unify IsSyncDone in cli/sync.go and cli/wallet.go --- cli/sync.go | 19 +++++++++++++------ cli/wallet.go | 20 ++------------------ 2 files changed, 15 insertions(+), 24 deletions(-) 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 e6786648e4b..61946a46382 100644 --- a/cli/wallet.go +++ b/cli/wallet.go @@ -3,13 +3,11 @@ package cli import ( "bufio" "bytes" - "context" "encoding/hex" "encoding/json" "fmt" "os" "strings" - "time" "github.com/urfave/cli/v2" "golang.org/x/term" @@ -21,7 +19,6 @@ 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" @@ -209,12 +206,12 @@ var walletBalance = &cli.Command{ return err } - behindSync, err := isSyncBehind(ctx, api) + inSync, err := IsSyncDone(ctx, api) if err != nil { 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)) } else { afmt.Printf("%s\n", types.FIL(balance)) @@ -762,16 +759,3 @@ 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 -} From 3cec54f774ba0bc0d08717abe971a048e1ef38cc Mon Sep 17 00:00:00 2001 From: Phi Date: Wed, 9 Aug 2023 21:25:52 +0200 Subject: [PATCH 3/3] Make TestWalletBalance happy Set up an expected call to ChainHead --- cli/wallet_test.go | 6 ++++++ 1 file changed, 6 insertions(+) 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