Merge pull request #5092 from zgfzgf/feat-shed-election

add shed election estimate command
This commit is contained in:
Łukasz Magiera 2021-04-01 17:05:09 +02:00 committed by GitHub
commit cf4128fc78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,30 +7,35 @@ import (
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"golang.org/x/xerrors" "golang.org/x/xerrors"
) )
var electionCmd = &cli.Command{ var electionCmd = &cli.Command{
Name: "election", Name: "election",
Usage: "commands related to leader election", Usage: "Commands related to leader election",
Subcommands: []*cli.Command{ Subcommands: []*cli.Command{
electionRunDummy, electionRunDummy,
electionEstimate,
}, },
} }
var electionRunDummy = &cli.Command{ var electionRunDummy = &cli.Command{
Name: "run-dummy", Name: "run-dummy",
Usage: "runs dummy elections with given power", Usage: "Runs dummy elections with given power",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "network-power", Name: "network-power",
Usage: "network storage power",
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "miner-power", Name: "miner-power",
Usage: "miner storage power",
}, },
&cli.Uint64Flag{ &cli.Uint64Flag{
Name: "seed", Name: "seed",
Usage: "rand number",
Value: 0, Value: 0,
}, },
}, },
@ -42,7 +47,7 @@ var electionRunDummy = &cli.Command{
} }
networkPow, err := types.BigFromString(cctx.String("network-power")) networkPow, err := types.BigFromString(cctx.String("network-power"))
if err != nil { if err != nil {
return xerrors.Errorf("decoding miner-power: %w", err) return xerrors.Errorf("decoding network-power: %w", err)
} }
ep := &types.ElectionProof{} ep := &types.ElectionProof{}
@ -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
},
}