Skip to content

Commit

Permalink
Merge pull request #5092 from zgfzgf/feat-shed-election
Browse files Browse the repository at this point in the history
add shed election estimate command
  • Loading branch information
magik6k committed Apr 1, 2021
2 parents 49be1f3 + 7c8ab69 commit cf4128f
Showing 1 changed file with 61 additions and 5 deletions.
66 changes: 61 additions & 5 deletions cmd/lotus-shed/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,35 @@ import (

"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
)

var electionCmd = &cli.Command{
Name: "election",
Usage: "commands related to leader election",
Usage: "Commands related to leader election",
Subcommands: []*cli.Command{
electionRunDummy,
electionEstimate,
},
}

var electionRunDummy = &cli.Command{
Name: "run-dummy",
Usage: "runs dummy elections with given power",
Usage: "Runs dummy elections with given power",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "network-power",
Name: "network-power",
Usage: "network storage power",
},
&cli.StringFlag{
Name: "miner-power",
Name: "miner-power",
Usage: "miner storage power",
},
&cli.Uint64Flag{
Name: "seed",
Usage: "rand number",
Value: 0,
},
},
Expand All @@ -42,7 +47,7 @@ var electionRunDummy = &cli.Command{
}
networkPow, err := types.BigFromString(cctx.String("network-power"))
if err != nil {
return xerrors.Errorf("decoding miner-power: %w", err)
return xerrors.Errorf("decoding network-power: %w", err)
}

ep := &types.ElectionProof{}
Expand All @@ -68,3 +73,54 @@ var electionRunDummy = &cli.Command{
}
},
}

var electionEstimate = &cli.Command{
Name: "estimate",
Usage: "Estimate elections with given power",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "network-power",
Usage: "network storage power",
},
&cli.StringFlag{
Name: "miner-power",
Usage: "miner storage power",
},
&cli.Uint64Flag{
Name: "seed",
Usage: "rand number",
Value: 0,
},
},
Action: func(cctx *cli.Context) error {
minerPow, err := types.BigFromString(cctx.String("miner-power"))
if err != nil {
return xerrors.Errorf("decoding miner-power: %w", err)
}
networkPow, err := types.BigFromString(cctx.String("network-power"))
if err != nil {
return xerrors.Errorf("decoding network-power: %w", err)
}

ep := &types.ElectionProof{}
ep.VRFProof = make([]byte, 32)
seed := cctx.Uint64("seed")
if seed == 0 {
seed = rand.Uint64()
}
binary.BigEndian.PutUint64(ep.VRFProof, seed)

winYear := int64(0)
for i := 0; i < builtin2.EpochsInYear; i++ {
binary.BigEndian.PutUint64(ep.VRFProof[8:], uint64(i))
j := ep.ComputeWinCount(minerPow, networkPow)
winYear += j
}
winHour := winYear * builtin2.EpochsInHour / builtin2.EpochsInYear
winDay := winYear * builtin2.EpochsInDay / builtin2.EpochsInYear
winMonth := winYear * builtin2.EpochsInDay * 30 / builtin2.EpochsInYear
fmt.Println("winInHour, winInDay, winInMonth, winInYear")
fmt.Printf("%d, %d, %d, %d\n", winHour, winDay, winMonth, winYear)
return nil
},
}

0 comments on commit cf4128f

Please sign in to comment.