2020-10-20 15:36:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2020-10-20 15:45:03 +00:00
|
|
|
"math/rand"
|
2020-10-20 15:36:23 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2020-12-02 04:57:57 +00:00
|
|
|
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
|
2020-10-20 15:36:23 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var electionCmd = &cli.Command{
|
|
|
|
Name: "election",
|
2020-12-02 04:36:05 +00:00
|
|
|
Usage: "Commands related to leader election",
|
2020-10-20 15:36:23 +00:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
electionRunDummy,
|
2020-12-02 04:57:57 +00:00
|
|
|
electionEstimate,
|
2020-10-20 15:36:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var electionRunDummy = &cli.Command{
|
|
|
|
Name: "run-dummy",
|
2020-12-02 04:36:05 +00:00
|
|
|
Usage: "Runs dummy elections with given power",
|
2020-10-20 15:36:23 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
2020-12-02 04:36:05 +00:00
|
|
|
Name: "network-power",
|
|
|
|
Usage: "network storage power",
|
2020-10-20 15:36:23 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-12-02 04:36:05 +00:00
|
|
|
Name: "miner-power",
|
|
|
|
Usage: "miner storage power",
|
2020-10-20 15:36:23 +00:00
|
|
|
},
|
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: "seed",
|
2020-12-02 04:36:05 +00:00
|
|
|
Usage: "rand number",
|
2020-10-20 15:45:03 +00:00
|
|
|
Value: 0,
|
2020-10-20 15:36:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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 {
|
2020-12-02 04:36:05 +00:00
|
|
|
return xerrors.Errorf("decoding network-power: %w", err)
|
2020-10-20 15:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ep := &types.ElectionProof{}
|
|
|
|
ep.VRFProof = make([]byte, 32)
|
2020-10-20 15:45:03 +00:00
|
|
|
seed := cctx.Uint64("seed")
|
|
|
|
if seed == 0 {
|
|
|
|
seed = rand.Uint64()
|
|
|
|
}
|
|
|
|
binary.BigEndian.PutUint64(ep.VRFProof, seed)
|
2020-10-20 15:36:23 +00:00
|
|
|
|
|
|
|
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++
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-12-02 04:57:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|