Skip to content

Commit

Permalink
Merge pull request #6107 from filecoin-project/fix/wallet-balance-cmd
Browse files Browse the repository at this point in the history
Fix/wallet balance cmd
  • Loading branch information
LinZexiao authored Aug 21, 2023
2 parents 64a8fcd + c6e3952 commit 9b2bf79
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
28 changes: 27 additions & 1 deletion cmd/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/howeyc/gopass"

Expand Down Expand Up @@ -261,10 +262,35 @@ var balanceCmd = &cmds.Command{
return err
}

return printOneString(re, (types.FIL)(balance).String())
isDone, err := isSyncDone(req.Context, env)
if err != nil {
return err
}

var balanceStr string
if balance.Equals(big.NewInt(0)) && !isDone {
balanceStr = fmt.Sprintf("%s (warning: may display 0 if chain sync in progress)", types.FIL(balance))
} else {
balanceStr = (types.FIL)(balance).String()
}

return printOneString(re, balanceStr)
},
}

func isSyncDone(ctx context.Context, env cmds.Environment) (bool, error) {
head, err := getEnv(env).ChainAPI.ChainHead(ctx)
if err != nil {
return false, err
}
params, err := getEnv(env).ChainAPI.StateGetNetworkParams(ctx)
if err != nil {
return false, err
}

return time.Now().Unix()-int64(head.MinTimestamp()) < int64(params.BlockDelaySecs), nil
}

// WalletSerializeResult is the type wallet export and import return and expect.
type WalletSerializeResult struct {
KeyInfo []*key.KeyInfo
Expand Down
4 changes: 2 additions & 2 deletions cmd/address_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestWalletBalance(t *testing.T) {

t.Log("[success] not found, zero")
balance := cmdClient.RunSuccess(ctx, "wallet", "balance", addr.String()).ReadStdout()
assert.Equal(t, "0 FIL\n", balance)
assert.Equal(t, "0 FIL (warning: may display 0 if chain sync in progress)\n", balance)

t.Log("[success] balance 1394000000000000000000000000")
balance = cmdClient.RunSuccess(ctx, "wallet", "balance", builtin.RewardActorAddr.String()).ReadStdout()
Expand All @@ -69,7 +69,7 @@ func TestWalletBalance(t *testing.T) {
var addrNew cmd.AddressResult
cmdClient.RunSuccessFirstLine(ctx, "wallet", "new")
balance = cmdClient.RunSuccess(ctx, "wallet", "balance", addrNew.Address.String()).ReadStdout()
assert.Equal(t, "0 FIL\n", balance)
assert.Equal(t, "0 FIL (warning: may display 0 if chain sync in progress)\n", balance)
}

func TestWalletLoadFromFile(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/events/filter/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ func (ei *EventIndex) migrateToVersion2(ctx context.Context, chainStore *chain.S
currTS := chainStore.GetHead()

for int64(currTS.Height()) >= minHeight.Int64 {
if currTS.Height() == 0 {
break
}

if currTS.Height()%1000 == 0 {
log.Infof("Migrating height %d (remaining %d)", currTS.Height(), int64(currTS.Height())-minHeight.Int64)
}
Expand Down

0 comments on commit 9b2bf79

Please sign in to comment.