diff --git a/chain/sub/incoming.go b/chain/sub/incoming.go index f21af601eb1..bf2cd391449 100644 --- a/chain/sub/incoming.go +++ b/chain/sub/incoming.go @@ -30,8 +30,10 @@ import ( "github.com/filecoin-project/lotus/metrics" ) -var log = logging.Logger("sub") -var DefaultHashFunction = uint64(mh.BLAKE2B_MIN + 31) +var ( + log = logging.Logger("sub") + DefaultHashFunction = uint64(mh.BLAKE2B_MIN + 31) +) var msgCidPrefix = cid.Prefix{ Version: 1, @@ -169,7 +171,6 @@ func fetchCids( cids []cid.Cid, cb func(int, blocks.Block) error, ) error { - ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -327,7 +328,7 @@ func (mv *MessageValidator) Validate(ctx context.Context, pid peer.ID, msg *pubs start := time.Now() defer func() { - ms := time.Now().Sub(start).Microseconds() + ms := time.Since(start).Microseconds() stats.Record(ctx, metrics.MessageValidationDuration.M(float64(ms)/1000)) }() @@ -394,7 +395,7 @@ func (mv *MessageValidator) validateLocalMessage(ctx context.Context, msg *pubsu start := time.Now() defer func() { - ms := time.Now().Sub(start).Microseconds() + ms := time.Since(start).Microseconds() stats.Record(ctx, metrics.MessageValidationDuration.M(float64(ms)/1000)) }() diff --git a/cli/info.go b/cli/info.go index 411a3169eb3..f011494665b 100644 --- a/cli/info.go +++ b/cli/info.go @@ -45,7 +45,7 @@ func infoCmdAct(cctx *cli.Context) error { } fmt.Printf("Network: %s\n", network.NetworkName) - fmt.Printf("StartTime: %s (started at %s)\n", time.Now().Sub(start).Truncate(time.Second), start.Truncate(time.Second)) + fmt.Printf("StartTime: %s (started at %s)\n", time.Since(start).Truncate(time.Second), start.Truncate(time.Second)) fmt.Print("Chain: ") err = SyncBasefeeCheck(ctx, fullapi) if err != nil { @@ -75,7 +75,7 @@ func infoCmdAct(cctx *cli.Context) error { fmt.Printf("%s (check %s)\n", color.RedString("⚠ %d Active alerts", len(activeAlerts)), color.YellowString("lotus log alerts")) } - //Chain health calculated as percentage: amount of blocks in last finality / very healthy amount of blocks in a finality (900 epochs * 5 blocks per tipset) + // Chain health calculated as percentage: amount of blocks in last finality / very healthy amount of blocks in a finality (900 epochs * 5 blocks per tipset) health := (100 * (900 * status.ChainStatus.BlocksPerTipsetLastFinality) / (900 * 5)) switch { case health > 85: @@ -173,7 +173,6 @@ func infoCmdAct(cctx *cli.Context) error { return err } return tw.Flush() - } func SyncBasefeeCheck(ctx context.Context, fullapi v1api.FullNode) error { @@ -186,9 +185,9 @@ func SyncBasefeeCheck(ctx context.Context, fullapi v1api.FullNode) error { case time.Now().Unix()-int64(head.MinTimestamp()) < int64(buildconstants.BlockDelaySecs*3/2): // within 1.5 epochs fmt.Printf("[%s]", color.GreenString("sync ok")) case time.Now().Unix()-int64(head.MinTimestamp()) < int64(buildconstants.BlockDelaySecs*5): // within 5 epochs - fmt.Printf("[%s]", color.YellowString("sync slow (%s behind)", time.Now().Sub(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second))) + fmt.Printf("[%s]", color.YellowString("sync slow (%s behind)", time.Since(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second))) default: - fmt.Printf("[%s]", color.RedString("sync behind! (%s behind)", time.Now().Sub(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second))) + fmt.Printf("[%s]", color.RedString("sync behind! (%s behind)", time.Since(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second))) } basefee := head.MinTicketBlock().ParentBaseFee gasCol := []color.Attribute{color.FgBlue} diff --git a/cmd/lotus-miner/info.go b/cmd/lotus-miner/info.go index 9548a4a771f..cdc0cba00bf 100644 --- a/cmd/lotus-miner/info.go +++ b/cmd/lotus-miner/info.go @@ -81,7 +81,7 @@ func infoCmdAct(cctx *cli.Context) error { if err != nil { return err } - fmt.Printf("StartTime: %s (started at %s)\n", time.Now().Sub(start).Truncate(time.Second), start.Truncate(time.Second)) + fmt.Printf("StartTime: %s (started at %s)\n", time.Since(start).Truncate(time.Second), start.Truncate(time.Second)) fmt.Print("Chain: ") @@ -135,7 +135,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full // Sector size mi, err := fullapi.StateMinerInfo(ctx, maddr, types.EmptyTSK) - if err != nil { return err } @@ -166,7 +165,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full ), ) secCounts, err := fullapi.StateMinerSectorCount(ctx, maddr, types.EmptyTSK) - if err != nil { return err } @@ -267,7 +265,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full colorTokenAmount(" Available: %s\n", availBalance) mb, err := fullapi.StateMarketBalance(ctx, maddr, types.EmptyTSK) - if err != nil { return xerrors.Errorf("getting market balance: %w", err) } @@ -278,7 +275,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full colorTokenAmount(" Available: %s\n", big.Sub(mb.Escrow, mb.Locked)) wb, err := fullapi.WalletBalance(ctx, mi.Worker) - if err != nil { return xerrors.Errorf("getting worker balance: %w", err) } diff --git a/cmd/lotus-miner/proving.go b/cmd/lotus-miner/proving.go index 79ad623d1ca..f5e4d59c0e2 100644 --- a/cmd/lotus-miner/proving.go +++ b/cmd/lotus-miner/proving.go @@ -239,12 +239,12 @@ It will not send any messages to the chain.`, start := time.Now() res, err := minerApi.ComputeWindowPoSt(ctx, dlIdx, types.EmptyTSK) - fmt.Printf("Took %s\n", time.Now().Sub(start)) + fmt.Printf("Took %s\n", time.Since(start)) if err != nil { return err } - //convert sector information into easily readable information + // convert sector information into easily readable information type PoStPartition struct { Index uint64 Skipped []uint64 diff --git a/cmd/lotus-miner/sealing.go b/cmd/lotus-miner/sealing.go index ed2b2d2948a..6d5f4ed7cbb 100644 --- a/cmd/lotus-miner/sealing.go +++ b/cmd/lotus-miner/sealing.go @@ -286,7 +286,7 @@ var sealingJobsCmd = &cli.Command{ } dur := "n/a" if !l.Start.IsZero() { - dur = time.Now().Sub(l.Start).Truncate(time.Millisecond * 100).String() + dur = time.Since(l.Start).Truncate(time.Millisecond * 100).String() } hostname, ok := workerHostnames[l.wid] diff --git a/cmd/lotus-miner/storage.go b/cmd/lotus-miner/storage.go index b39fe2bf750..609f2ead674 100644 --- a/cmd/lotus-miner/storage.go +++ b/cmd/lotus-miner/storage.go @@ -351,7 +351,7 @@ var storageListCmd = &cli.Command{ fmt.Printf("\t%s: %s:\n", color.RedString("Error"), err) continue } - ping := time.Now().Sub(pingStart) + ping := time.Since(pingStart) safeRepeat := func(s string, count int) string { if count < 0 { @@ -360,7 +360,7 @@ var storageListCmd = &cli.Command{ return strings.Repeat(s, count) } - var barCols = int64(50) + barCols := int64(50) // filesystem use bar { @@ -738,7 +738,6 @@ var storageListSectorsCmd = &cli.Command{ } for _, info := range si { - list = append(list, entry{ id: sector, storage: info.ID, diff --git a/miner/warmup.go b/miner/warmup.go index 46236784a72..1bfe79b6cf6 100644 --- a/miner/warmup.go +++ b/miner/warmup.go @@ -84,7 +84,7 @@ out: return xerrors.Errorf("failed to compute proof: %w", err) } - log.Infow("winning PoSt warmup successful", "took", time.Now().Sub(start)) + log.Infow("winning PoSt warmup successful", "took", time.Since(start)) return nil } diff --git a/storage/paths/fetch.go b/storage/paths/fetch.go index 6b87c0dd90d..5f57e3e14a2 100644 --- a/storage/paths/fetch.go +++ b/storage/paths/fetch.go @@ -37,7 +37,7 @@ func fetch(ctx context.Context, url, outname string, header http.Header) (rerr e start := time.Now() var bytes int64 defer func() { - took := time.Now().Sub(start) + took := time.Since(start) mibps := float64(bytes) / 1024 / 1024 * float64(time.Second) / float64(took) log.Infow("Fetch done", "url", url, "out", outname, "took", took.Round(time.Millisecond), "bytes", bytes, "MiB/s", mibps, "err", rerr) }() diff --git a/storage/paths/local.go b/storage/paths/local.go index 07223ad5317..ef206edb88e 100644 --- a/storage/paths/local.go +++ b/storage/paths/local.go @@ -144,7 +144,7 @@ func (p *path) stat(ls LocalStorage, newReserve ...statExistingSectorForReservat } if stat.Reserved < 0 { - //log.Warnf("negative reserved storage: p.reserved=%d, reserved: %d", p.reserved, stat.Reserved) + // log.Warnf("negative reserved storage: p.reserved=%d, reserved: %d", p.reserved, stat.Reserved) var jsonReservations []map[string]interface{} for id, res := range p.reservations { jsonReservations = append(jsonReservations, map[string]interface{}{ @@ -182,8 +182,8 @@ func (p *path) stat(ls LocalStorage, newReserve ...statExistingSectorForReservat } } - if time.Now().Sub(start) > 5*time.Second { - log.Warnw("slow storage stat", "took", time.Now().Sub(start), "reservations", len(p.reservations)) + if took := time.Since(start); took > 5*time.Second { + log.Warnw("slow storage stat", "took", took, "reservations", len(p.reservations)) } return stat, newReserveOnDisk, err @@ -463,7 +463,8 @@ func (st *Local) reportStorage(ctx context.Context) { } func (st *Local) Reserve(ctx context.Context, sid storiface.SectorRef, ft storiface.SectorFileType, - storageIDs storiface.SectorPaths, overheadTab map[storiface.SectorFileType]int, minFreePercentage float64) (func(), error) { + storageIDs storiface.SectorPaths, overheadTab map[storiface.SectorFileType]int, minFreePercentage float64, +) (func(), error) { ssize, err := sid.ProofType.SectorSize() if err != nil { return nil, err @@ -661,7 +662,6 @@ func (st *Local) AcquireSector(ctx context.Context, sid storiface.SectorRef, exi } alloc, err := allocPathOk(si.CanSeal, si.CanStore, si.AllowTypes, si.DenyTypes, si.AllowMiners, si.DenyMiners, fileType, sid.ID.Miner) - if err != nil { log.Debug(err) continue @@ -950,7 +950,7 @@ func (st *Local) GenerateSingleVanillaProof(ctx context.Context, minerID abi.Act case r := <-resCh: return r.Unwrap() case <-ctx.Done(): - log.Errorw("failed to generate valilla PoSt proof before context cancellation", "err", ctx.Err(), "duration", time.Now().Sub(start), "cache-id", cacheID, "sealed-id", sealedID, "cache", cache, "sealed", sealed) + log.Errorw("failed to generate valilla PoSt proof before context cancellation", "err", ctx.Err(), "duration", time.Since(start), "cache-id", cacheID, "sealed-id", sealedID, "cache", cache, "sealed", sealed) // this will leave the GenerateSingleVanillaProof goroutine hanging, but that's still less bad than failing PoSt return nil, xerrors.Errorf("failed to generate vanilla proof before context cancellation: %w", ctx.Err()) diff --git a/storage/paths/localstorage_cached.go b/storage/paths/localstorage_cached.go index 31e2528ffc0..f791384cb43 100644 --- a/storage/paths/localstorage_cached.go +++ b/storage/paths/localstorage_cached.go @@ -11,8 +11,10 @@ import ( "github.com/filecoin-project/lotus/storage/sealer/storiface" ) -var StatTimeout = 5 * time.Second -var MaxDiskUsageDuration = time.Second +var ( + StatTimeout = 5 * time.Second + MaxDiskUsageDuration = time.Second +) type cachedLocalStorage struct { base LocalStorage @@ -61,7 +63,7 @@ func (c *cachedLocalStorage) Stat(path string) (fsutil.FsStat, error) { c.statLk.Lock() defer c.statLk.Unlock() - if v, ok := c.stats.Get(path); ok && time.Now().Sub(v.time) < StatTimeout { + if v, ok := c.stats.Get(path); ok && time.Since(v.time) < StatTimeout { return v.stat, nil } @@ -87,7 +89,7 @@ func (c *cachedLocalStorage) DiskUsage(path string) (int64, error) { entry = v // if we have recent cached entry, use that - if time.Now().Sub(entry.last.time) < StatTimeout { + if time.Since(entry.last.time) < StatTimeout { return entry.last.usage, nil } } else { @@ -126,7 +128,7 @@ func (c *cachedLocalStorage) DiskUsage(path string) (int64, error) { log.Warnw("getting usage is slow, falling back to previous usage", "path", path, "fallback", entry.last.usage, - "age", time.Now().Sub(entry.last.time)) + "age", time.Since(entry.last.time)) } return entry.last.usage, nil diff --git a/storage/sealer/fsutil/filesize_unix.go b/storage/sealer/fsutil/filesize_unix.go index 3d80e0b8a24..8cc33961a78 100644 --- a/storage/sealer/fsutil/filesize_unix.go +++ b/storage/sealer/fsutil/filesize_unix.go @@ -39,8 +39,8 @@ func FileSize(path string) (SizeInfo, error) { return err }) - if time.Now().Sub(start) >= 3*time.Second { - log.Warnw("very slow file size check", "took", time.Now().Sub(start), "path", path) + if took := time.Since(start); took >= 3*time.Second { + log.Warnw("very slow file size check", "took", took, "path", path) } if err != nil { diff --git a/storage/wdpost/wdpost_run.go b/storage/wdpost/wdpost_run.go index efb43ddd247..11eff7ee3a2 100644 --- a/storage/wdpost/wdpost_run.go +++ b/storage/wdpost/wdpost_run.go @@ -117,7 +117,6 @@ func (s *WindowPoStScheduler) startSubmitPoST( posts []miner.SubmitWindowedPoStParams, completeSubmitPoST CompleteSubmitPoSTCb, ) context.CancelFunc { - ctx, abort := context.WithCancel(ctx) go func() { defer abort() @@ -276,7 +275,7 @@ func (s *WindowPoStScheduler) runPoStCycle(ctx context.Context, manual bool, di log := log.WithOptions(zap.Fields(zap.Time("cycle", start))) log.Infow("starting PoSt cycle", "manual", manual, "ts", ts, "deadline", di.Index) defer func() { - log.Infow("post cycle done", "took", time.Now().Sub(start)) + log.Infow("post cycle done", "took", time.Since(start)) }() if !manual { diff --git a/tools/stats/points/collect.go b/tools/stats/points/collect.go index 6f030fdb7c0..8ad698e739b 100644 --- a/tools/stats/points/collect.go +++ b/tools/stats/points/collect.go @@ -81,7 +81,7 @@ func (c *ChainPointCollector) Collect(ctx context.Context, tipset *types.TipSet) start := time.Now() done := metrics.Timer(ctx, metrics.TipsetCollectionDuration) defer func() { - log.Infow("record tipset", "elapsed", time.Now().Sub(start).Seconds()) + log.Infow("record tipset", "elapsed", time.Since(start).Seconds()) done() }() @@ -126,7 +126,7 @@ func (c *ChainPointCollector) collectBlockheaderPoints(ctx context.Context, pl * start := time.Now() done := metrics.Timer(ctx, metrics.TipsetCollectionBlockHeaderDuration) defer func() { - log.Infow("collect blockheader points", "elapsed", time.Now().Sub(start).Seconds()) + log.Infow("collect blockheader points", "elapsed", time.Since(start).Seconds()) done() }() @@ -218,7 +218,7 @@ func (c *ChainPointCollector) collectStaterootPoints(ctx context.Context, pl *in start := time.Now() done := metrics.Timer(ctx, metrics.TipsetCollectionStaterootDuration) defer func() { - log.Infow("collect stateroot points", "elapsed", time.Now().Sub(start).Seconds()) + log.Infow("collect stateroot points", "elapsed", time.Since(start).Seconds()) done() }() @@ -280,7 +280,7 @@ func (c *ChainPointCollector) collectMessagePoints(ctx context.Context, pl *infl start := time.Now() done := metrics.Timer(ctx, metrics.TipsetCollectionMessageDuration) defer func() { - log.Infow("collect message points", "elapsed", time.Now().Sub(start).Seconds()) + log.Infow("collect message points", "elapsed", time.Since(start).Seconds()) done() }()