2020-10-20 15:36:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-03 03:52:30 +00:00
|
|
|
"context"
|
2020-10-20 15:36:23 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2020-10-20 15:45:03 +00:00
|
|
|
"math/rand"
|
2020-10-20 15:36:23 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/xerrors"
|
2021-07-11 18:02:15 +00:00
|
|
|
|
2021-04-03 03:52:30 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
2021-04-03 03:52:30 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/gen"
|
2020-10-20 15:36:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2021-04-03 03:52:30 +00:00
|
|
|
electionBacktest,
|
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
|
|
|
|
},
|
|
|
|
}
|
2021-04-03 03:52:30 +00:00
|
|
|
|
|
|
|
var electionBacktest = &cli.Command{
|
|
|
|
Name: "backtest",
|
|
|
|
Usage: "Backtest elections with given miner",
|
|
|
|
ArgsUsage: "[minerAddress]",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: "height",
|
|
|
|
Usage: "blockchain head height",
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "count",
|
2021-04-06 14:40:05 +00:00
|
|
|
Usage: "number of won elections to look for",
|
2021-04-03 03:52:30 +00:00
|
|
|
Value: 120,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := lcli.GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("GetFullNodeAPI: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer closer()
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
|
|
|
|
var head *types.TipSet
|
|
|
|
if cctx.IsSet("height") {
|
|
|
|
head, err = api.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(cctx.Uint64("height")), types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("ChainGetTipSetByHeight: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
head, err = api.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("ChainHead: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
miner, err := address.NewFromString(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("miner address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
count := cctx.Int("count")
|
|
|
|
if count < 1 {
|
|
|
|
return xerrors.Errorf("count: %d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("height, winCount")
|
|
|
|
roundEnd := head.Height() + abi.ChainEpoch(1)
|
|
|
|
for i := 0; i < count; {
|
|
|
|
for round := head.Height() + abi.ChainEpoch(1); round <= roundEnd; round++ {
|
|
|
|
i++
|
|
|
|
win, err := backTestWinner(ctx, miner, round, head, api)
|
|
|
|
if err == nil && win != nil {
|
|
|
|
fmt.Printf("%d, %d\n", round, win.WinCount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
roundEnd = head.Height()
|
|
|
|
head, err = api.ChainGetTipSet(ctx, head.Parents())
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-11 18:02:15 +00:00
|
|
|
func backTestWinner(ctx context.Context, miner address.Address, round abi.ChainEpoch, ts *types.TipSet, api v0api.FullNode) (*types.ElectionProof, error) {
|
2021-04-03 03:52:30 +00:00
|
|
|
mbi, err := api.MinerGetBaseInfo(ctx, miner, round, ts.Key())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to get mining base info: %w", err)
|
|
|
|
}
|
|
|
|
if mbi == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if !mbi.EligibleForMining {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
brand := mbi.PrevBeaconEntry
|
|
|
|
bvals := mbi.BeaconEntries
|
|
|
|
if len(bvals) > 0 {
|
|
|
|
brand = bvals[len(bvals)-1]
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:37:02 +00:00
|
|
|
winner, err := gen.IsRoundWinner(ctx, round, miner, brand, mbi, api)
|
2021-04-03 03:52:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to check if we win next round: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return winner, nil
|
|
|
|
}
|