From ea6afe84db823a26e5c2ed85f03c2b0f16ef6049 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 20 Oct 2020 17:36:23 +0200 Subject: [PATCH 1/2] Add election run-dummy command Signed-off-by: Jakub Sztandera --- cmd/lotus-shed/election.go | 65 ++++++++++++++++++++++++++++++++++++++ cmd/lotus-shed/main.go | 1 + 2 files changed, 66 insertions(+) create mode 100644 cmd/lotus-shed/election.go diff --git a/cmd/lotus-shed/election.go b/cmd/lotus-shed/election.go new file mode 100644 index 000000000..bc053dea4 --- /dev/null +++ b/cmd/lotus-shed/election.go @@ -0,0 +1,65 @@ +package main + +import ( + "encoding/binary" + "fmt" + + "github.com/filecoin-project/lotus/chain/types" + lcli "github.com/filecoin-project/lotus/cli" + "github.com/urfave/cli/v2" + "golang.org/x/xerrors" +) + +var electionCmd = &cli.Command{ + Name: "election", + Usage: "commands related to leader election", + Subcommands: []*cli.Command{ + electionRunDummy, + }, +} + +var electionRunDummy = &cli.Command{ + Name: "run-dummy", + Usage: "runs dummy elections with given power", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "network-power", + }, + &cli.StringFlag{ + Name: "miner-power", + }, + &cli.Uint64Flag{ + Name: "seed", + Value: 4, + }, + }, + Action: func(cctx *cli.Context) error { + ctx := lcli.ReqContext(cctx) + 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 miner-power: %w", err) + } + + ep := &types.ElectionProof{} + ep.VRFProof = make([]byte, 32) + binary.BigEndian.PutUint64(ep.VRFProof, cctx.Uint64("seed")) + + i := uint64(0) + for { + if ctx.Err() != nil { + return ctx.Err() + } + binary.BigEndian.PutUint64(ep.VRFProof[8:], i) + j := ep.ComputeWinCount(minerPow, networkPow) + _, err := fmt.Printf("%t, %d\n", j != 0, j) + if err != nil { + return err + } + i++ + } + }, +} diff --git a/cmd/lotus-shed/main.go b/cmd/lotus-shed/main.go index 8201ec111..eef357596 100644 --- a/cmd/lotus-shed/main.go +++ b/cmd/lotus-shed/main.go @@ -46,6 +46,7 @@ func main() { ledgerCmd, sectorsCmd, msgCmd, + electionCmd, } app := &cli.App{ From cced3bf9ee9cc9c51e72c85b06aa14ba44ddbb22 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 20 Oct 2020 17:45:03 +0200 Subject: [PATCH 2/2] Randomize seed Signed-off-by: Jakub Sztandera --- cmd/lotus-shed/election.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd/lotus-shed/election.go b/cmd/lotus-shed/election.go index bc053dea4..ffe30d163 100644 --- a/cmd/lotus-shed/election.go +++ b/cmd/lotus-shed/election.go @@ -3,6 +3,7 @@ package main import ( "encoding/binary" "fmt" + "math/rand" "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" @@ -30,7 +31,7 @@ var electionRunDummy = &cli.Command{ }, &cli.Uint64Flag{ Name: "seed", - Value: 4, + Value: 0, }, }, Action: func(cctx *cli.Context) error { @@ -46,7 +47,11 @@ var electionRunDummy = &cli.Command{ ep := &types.ElectionProof{} ep.VRFProof = make([]byte, 32) - binary.BigEndian.PutUint64(ep.VRFProof, cctx.Uint64("seed")) + seed := cctx.Uint64("seed") + if seed == 0 { + seed = rand.Uint64() + } + binary.BigEndian.PutUint64(ep.VRFProof, seed) i := uint64(0) for {