Skip to content

Commit

Permalink
Merge pull request #5792 from filecoin-project/fix/block-delay
Browse files Browse the repository at this point in the history
fix: block delay is 0
  • Loading branch information
diwufeiwen authored Mar 7, 2023
2 parents 51e65a1 + 5fb89cb commit 7482d63
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cmd/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ var minerInfoCmd = &cmds.Command{
blockstoreAPI := env.(*node.Env).BlockStoreAPI
api := env.(*node.Env).ChainAPI

blockDelay, err := blockDelay(req)
blockDelay, err := getBlockDelay(ctx, env)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/miner_proving.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var provingInfoCmd = &cmds.Command{
}
ctx := req.Context

blockDelay, err := blockDelay(req)
blockDelay, err := getBlockDelay(ctx, env)
if err != nil {
return err
}
Expand Down Expand Up @@ -123,8 +123,8 @@ var provingInfoCmd = &cmds.Command{
writer.Printf("Current Epoch: %d\n", cd.CurrentEpoch)

writer.Printf("Proving Period Boundary: %d\n", cd.PeriodStart%cd.WPoStProvingPeriod)
writer.Printf("Proving Period Start: %s\n", EpochTime(cd.CurrentEpoch, cd.PeriodStart, blockDelay))
writer.Printf("Next Period Start: %s\n", EpochTime(cd.CurrentEpoch, cd.PeriodStart+cd.WPoStProvingPeriod, blockDelay))
writer.Printf("Proving Period Start: %s\n", EpochTimeTs(cd.CurrentEpoch, cd.PeriodStart, blockDelay, head))
writer.Printf("Next Period Start: %s\n", EpochTimeTs(cd.CurrentEpoch, cd.PeriodStart+cd.WPoStProvingPeriod, blockDelay, head))

writer.Println()
writer.Printf("Faults: %d (%.2f%%)\n", faults, faultPerc)
Expand Down
42 changes: 12 additions & 30 deletions cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import (
"encoding/json"
"fmt"
"io"
"path/filepath"
"strconv"

"github.com/filecoin-project/venus/app/paths"
"github.com/filecoin-project/venus/cmd/tablewriter"

"github.com/filecoin-project/venus/pkg/config"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
actorstypes "github.com/filecoin-project/go-state-types/actors"
Expand Down Expand Up @@ -257,7 +253,8 @@ var stateSectorCmd = &cmds.Command{
return err
}

ts, err := env.(*node.Env).ChainAPI.ChainHead(req.Context)
ctx := req.Context
ts, err := env.(*node.Env).ChainAPI.ChainHead(ctx)
if err != nil {
return err
}
Expand All @@ -267,12 +264,12 @@ var stateSectorCmd = &cmds.Command{
return err
}

blockDelay, err := blockDelay(req)
blockDelay, err := getBlockDelay(ctx, env)
if err != nil {
return err
}

si, err := env.(*node.Env).ChainAPI.StateSectorGetInfo(req.Context, maddr, abi.SectorNumber(sid), ts.Key())
si, err := env.(*node.Env).ChainAPI.StateSectorGetInfo(ctx, maddr, abi.SectorNumber(sid), ts.Key())
if err != nil {
return err
}
Expand All @@ -289,8 +286,8 @@ var stateSectorCmd = &cmds.Command{
writer.Println("SealedCID: ", si.SealedCID)
writer.Println("DealIDs: ", si.DealIDs)
writer.Println()
writer.Println("Activation: ", EpochTime(height, si.Activation, blockDelay))
writer.Println("Expiration: ", EpochTime(height, si.Expiration, blockDelay))
writer.Println("Activation: ", EpochTimeTs(height, si.Activation, blockDelay, ts))
writer.Println("Expiration: ", EpochTimeTs(height, si.Expiration, blockDelay, ts))
writer.Println()
writer.Println("DealWeight: ", si.DealWeight)
writer.Println("VerifiedDealWeight: ", si.VerifiedDealWeight)
Expand All @@ -311,22 +308,6 @@ var stateSectorCmd = &cmds.Command{
},
}

func blockDelay(req *cmds.Request) (uint64, error) {
var err error
repoDir, _ := req.Options[OptionRepoDir].(string)
repoDir, err = paths.GetRepoPath(repoDir)
if err != nil {
return 0, err
}
cfgPath := filepath.Join(repoDir, "config.json")
cfg, err := config.ReadFile(cfgPath)
if err != nil {
return 0, err
}

return cfg.NetworkParams.BlockDelay, nil
}

var stateGetActorCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Print actor information",
Expand Down Expand Up @@ -471,22 +452,23 @@ var stateMinerInfo = &cmds.Command{
return err
}

blockDelay, err := blockDelay(req)
ctx := req.Context
blockDelay, err := getBlockDelay(ctx, env)
if err != nil {
return err
}

ts, err := env.(*node.Env).ChainAPI.ChainHead(req.Context)
ts, err := env.(*node.Env).ChainAPI.ChainHead(ctx)
if err != nil {
return err
}

mi, err := env.(*node.Env).ChainAPI.StateMinerInfo(req.Context, addr, ts.Key())
mi, err := env.(*node.Env).ChainAPI.StateMinerInfo(ctx, addr, ts.Key())
if err != nil {
return err
}

availableBalance, err := env.(*node.Env).ChainAPI.StateMinerAvailableBalance(req.Context, addr, ts.Key())
availableBalance, err := env.(*node.Env).ChainAPI.StateMinerAvailableBalance(ctx, addr, ts.Key())
if err != nil {
return fmt.Errorf("getting miner available balance: %w", err)
}
Expand Down Expand Up @@ -535,7 +517,7 @@ var stateMinerInfo = &cmds.Command{

writer.Println()

cd, err := env.(*node.Env).ChainAPI.StateMinerProvingDeadline(req.Context, addr, ts.Key())
cd, err := env.(*node.Env).ChainAPI.StateMinerProvingDeadline(ctx, addr, ts.Key())
if err != nil {
return fmt.Errorf("getting miner info: %w", err)
}
Expand Down
29 changes: 29 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ func cidsFromSlice(args []string) ([]cid.Cid, error) {
return out, nil
}

func getBlockDelay(ctx context.Context, env cmds.Environment) (uint64, error) {
params, err := env.(*node.Env).ChainAPI.StateGetNetworkParams(ctx)
if err != nil {
return 0, err
}

return params.BlockDelaySecs, nil
}

func EpochTime(curr, e abi.ChainEpoch, blockDelay uint64) string {
switch {
case curr > e:
Expand All @@ -154,6 +163,26 @@ func EpochTime(curr, e abi.ChainEpoch, blockDelay uint64) string {
panic("math broke")
}

// EpochTimeTs is like EpochTime, but also outputs absolute time. `ts` is only
// used to provide a timestamp at some epoch to calculate time from. It can be
// a genesis tipset.
//
// Example output: `1944975 (01 Jul 22 08:07 CEST, 10 hours 29 minutes ago)`
func EpochTimeTs(curr, e abi.ChainEpoch, blockDelay uint64, ts *types.TipSet) string {
timeStr := time.Unix(int64(ts.MinTimestamp()+(uint64(e-ts.Height())*blockDelay)), 0).Format(time.RFC822)

switch {
case curr > e:
return fmt.Sprintf("%d (%s, %s ago)", e, timeStr, durafmt.Parse(time.Second*time.Duration(int64(blockDelay)*int64(curr-e))).LimitFirstN(2))
case curr == e:
return fmt.Sprintf("%d (%s, now)", e, timeStr)
case curr < e:
return fmt.Sprintf("%d (%s, in %s)", e, timeStr, durafmt.Parse(time.Second*time.Duration(int64(blockDelay)*int64(e-curr))).LimitFirstN(2))
}

panic("math broke")
}

func printOneString(re cmds.ResponseEmitter, str string) error {
buf := new(bytes.Buffer)
writer := NewSilentWriter(buf)
Expand Down

0 comments on commit 7482d63

Please sign in to comment.