From 7d3e4c5d011c36d2ed8954b511ed24e499e8f4a6 Mon Sep 17 00:00:00 2001 From: zgfzgf <1901989065@qq.com> Date: Wed, 2 Dec 2020 12:36:05 +0800 Subject: [PATCH 1/2] feat election error miner-power to network-power --- cmd/lotus-shed/election.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/lotus-shed/election.go b/cmd/lotus-shed/election.go index ffe30d163..000df0c62 100644 --- a/cmd/lotus-shed/election.go +++ b/cmd/lotus-shed/election.go @@ -13,7 +13,7 @@ import ( var electionCmd = &cli.Command{ Name: "election", - Usage: "commands related to leader election", + Usage: "Commands related to leader election", Subcommands: []*cli.Command{ electionRunDummy, }, @@ -21,16 +21,19 @@ var electionCmd = &cli.Command{ 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, }, }, @@ -42,7 +45,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{} From 7c8ab6977a38a25cef67e7eca8b9e3e2137eee96 Mon Sep 17 00:00:00 2001 From: zgfzgf <1901989065@qq.com> Date: Wed, 2 Dec 2020 12:57:57 +0800 Subject: [PATCH 2/2] add estimate command --- cmd/lotus-shed/election.go | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/cmd/lotus-shed/election.go b/cmd/lotus-shed/election.go index 000df0c62..c844203d6 100644 --- a/cmd/lotus-shed/election.go +++ b/cmd/lotus-shed/election.go @@ -7,6 +7,7 @@ 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" ) @@ -16,6 +17,7 @@ var electionCmd = &cli.Command{ Usage: "Commands related to leader election", Subcommands: []*cli.Command{ electionRunDummy, + electionEstimate, }, } @@ -71,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 + }, +}