Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

opt & improve sealing #68

Merged
merged 1 commit into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
SHELL=/usr/bin/env bash

CLEAN:=
BINS:=

github.com/filecoin-project/venus-sealer/build.CurrentCommit=+git.$(subst -,.,$(shell git describe --always --match=NeVeRmAtCh --dirty 2>/dev/null || git rev-parse --short HEAD 2>/dev/null))

## FFI

FFI_PATH:=extern/filecoin-ffi/

CLEAN+=build/.filecoin-install

build:
go build -o venus-sealer ./app/venus-sealer
go build -o venus-worker ./app/venus-worker
BINS+=venus-sealer
BINS+=venus-worker

deps:
git submodule update --init
Expand All @@ -9,3 +24,7 @@ deps:
lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint run

clean:
rm -rf $(CLEAN) $(BINS)
-$(MAKE) -C $(FFI_PATH) clean
.PHONY: clean
2 changes: 1 addition & 1 deletion api/impl/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (a *CommonAPI) AuthNew(ctx context.Context, perms []auth.Permission) ([]byt
func (a *CommonAPI) Version(context.Context) (api.Version, error) {
return api.Version{
Version: constants.MinerVersion.String(),
APIVersion: constants.MinerAPIVersion,
APIVersion: constants.MinerAPIVersion0,
BlockDelay: a.NetworkParams.BlockDelaySecs,
}, nil
}
Expand Down
81 changes: 64 additions & 17 deletions app/venus-sealer/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"context"
"fmt"
"github.com/filecoin-project/venus-sealer/constants"
types2 "github.com/filecoin-project/venus-sealer/types"
"math"
corebig "math/big"
"sort"
"time"

Expand All @@ -16,10 +16,14 @@ import (

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"

"github.com/filecoin-project/venus-sealer/api"
"github.com/filecoin-project/venus-sealer/constants"
"github.com/filecoin-project/venus-sealer/lib/blockstore"
"github.com/filecoin-project/venus-sealer/lib/bufbstore"
types2 "github.com/filecoin-project/venus-sealer/types"
"github.com/filecoin-project/venus/pkg/specactors/adt"
"github.com/filecoin-project/venus/pkg/specactors/builtin"
"github.com/filecoin-project/venus/pkg/specactors/builtin/miner"
"github.com/filecoin-project/venus/pkg/types"
)
Expand Down Expand Up @@ -60,6 +64,7 @@ func infoCmdAct(cctx *cli.Context) error {
if err != nil {
return err
}

fmt.Print("Chain: ")

head, err := nodeAPI.ChainHead(ctx)
Expand Down Expand Up @@ -122,18 +127,23 @@ func infoCmdAct(cctx *cli.Context) error {
return err
}

rpercI := types.BigDiv(types.BigMul(pow.MinerPower.RawBytePower, types.NewInt(1000000)), pow.TotalPower.RawBytePower)
qpercI := types.BigDiv(types.BigMul(pow.MinerPower.QualityAdjPower, types.NewInt(1000000)), pow.TotalPower.QualityAdjPower)

fmt.Printf("Power: %s / %s (%0.4f%%)\n",
color.GreenString(types.DeciStr(pow.MinerPower.QualityAdjPower)),
types.DeciStr(pow.TotalPower.QualityAdjPower),
float64(qpercI.Int64())/10000)
types.BigDivFloat(
types.BigMul(pow.MinerPower.QualityAdjPower, big.NewInt(100)),
pow.TotalPower.QualityAdjPower,
),
)

fmt.Printf("\tRaw: %s / %s (%0.4f%%)\n",
color.BlueString(types.SizeStr(pow.MinerPower.RawBytePower)),
types.SizeStr(pow.TotalPower.RawBytePower),
float64(rpercI.Int64())/10000)
types.BigDivFloat(
types.BigMul(pow.MinerPower.RawBytePower, big.NewInt(100)),
pow.TotalPower.RawBytePower,
),
)

secCounts, err := nodeAPI.StateMinerSectorCount(ctx, maddr, types.EmptyTSK)
if err != nil {
Expand All @@ -148,7 +158,7 @@ func infoCmdAct(cctx *cli.Context) error {
} else {
var faultyPercentage float64
if secCounts.Live != 0 {
faultyPercentage = float64(10000*nfaults/secCounts.Live) / 100.
faultyPercentage = float64(100*nfaults) / float64(secCounts.Live)
}
fmt.Printf("\tProving: %s (%s Faulty, %.2f%%)\n",
types.SizeStr(types.BigMul(types.NewInt(proving), types.NewInt(uint64(mi.SectorSize)))),
Expand All @@ -159,16 +169,53 @@ func infoCmdAct(cctx *cli.Context) error {
if !pow.HasMinPower {
fmt.Print("Below minimum power threshold, no blocks will be won")
} else {
expWinChance := float64(types.BigMul(qpercI, types.NewInt(constants.BlocksPerEpoch)).Int64()) / 1000000
if expWinChance > 0 {
if expWinChance > 1 {
expWinChance = 1
}
winRate := time.Duration(float64(time.Second*time.Duration(params.BlockDelaySecs)) / expWinChance)
winPerDay := float64(time.Hour*24) / float64(winRate)

fmt.Print("Expected block win rate: ")
color.Blue("%.4f/day (every %s)", winPerDay, winRate.Truncate(time.Second))
winRatio := new(corebig.Rat).SetFrac(
types.BigMul(pow.MinerPower.QualityAdjPower, types.NewInt(constants.BlocksPerEpoch)).Int,
pow.TotalPower.QualityAdjPower.Int,
)

if winRatioFloat, _ := winRatio.Float64(); winRatioFloat > 0 {

// if the corresponding poisson distribution isn't infinitely small then
// throw it into the mix as well, accounting for multi-wins
winRationWithPoissonFloat := -math.Expm1(-winRatioFloat)
winRationWithPoisson := new(corebig.Rat).SetFloat64(winRationWithPoissonFloat)
if winRationWithPoisson != nil {
winRatio = winRationWithPoisson
winRatioFloat = winRationWithPoissonFloat
}
weekly, _ := new(corebig.Rat).Mul(
winRatio,
new(corebig.Rat).SetInt64(7*builtin.EpochsInDay),
).Float64()

avgDuration, _ := new(corebig.Rat).Mul(
new(corebig.Rat).SetInt64(builtin.EpochDurationSeconds),
new(corebig.Rat).Inv(winRatio),
).Float64()

fmt.Print("Projected average block win rate: ")
color.Blue(
"%.02f/week (every %s)",
weekly,
(time.Second * time.Duration(avgDuration)).Truncate(time.Second).String(),
)

// Geometric distribution of P(Y < k) calculated as described in https://en.wikipedia.org/wiki/Geometric_distribution#Probability_Outcomes_Examples
// https://www.wolframalpha.com/input/?i=t+%3E+0%3B+p+%3E+0%3B+p+%3C+1%3B+c+%3E+0%3B+c+%3C1%3B+1-%281-p%29%5E%28t%29%3Dc%3B+solve+t
// t == how many dice-rolls (epochs) before win
// p == winRate == ( minerPower / netPower )
// c == target probability of win ( 99.9% in this case )
fmt.Print("Projected block win with ")
color.Green(
"99.9%% probability every %s",
(time.Second * time.Duration(
builtin.EpochDurationSeconds*math.Log(1-0.999)/
math.Log(1-winRatioFloat),
)).Truncate(time.Second).String(),
)
fmt.Println("(projections DO NOT account for future network and miner growth)")
}
}

Expand Down
60 changes: 36 additions & 24 deletions app/venus-sealer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ import (
"crypto/rand"
"encoding/json"
"fmt"
market2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/market"
types3 "github.com/filecoin-project/venus-messager/types"
"github.com/filecoin-project/venus-sealer/api"
"github.com/filecoin-project/venus-sealer/config"
"github.com/filecoin-project/venus-sealer/constants"
"github.com/filecoin-project/venus-sealer/models"
"github.com/filecoin-project/venus-sealer/service"
types2 "github.com/filecoin-project/venus-sealer/types"
"github.com/filecoin-project/venus/fixtures/asset"
"github.com/filecoin-project/venus/pkg/gen/genesis"
"io/ioutil"
"os"
"path/filepath"
"strconv"

"github.com/filecoin-project/go-address"
paramfetch "github.com/filecoin-project/go-paramfetch"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
market2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/market"
power2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/power"
"github.com/filecoin-project/venus/fixtures/asset"
"github.com/filecoin-project/venus/pkg/gen/genesis"

"github.com/docker/go-units"
"github.com/google/uuid"
"github.com/libp2p/go-libp2p-core/crypto"
Expand All @@ -29,17 +28,21 @@ import (
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
paramfetch "github.com/filecoin-project/go-paramfetch"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
power2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/power"
"github.com/filecoin-project/venus-sealer/sector-storage/stores"
actors "github.com/filecoin-project/venus/pkg/specactors"
"github.com/filecoin-project/venus/pkg/specactors/builtin/miner"
"github.com/filecoin-project/venus/pkg/specactors/builtin/power"
"github.com/filecoin-project/venus/pkg/specactors/policy"
"github.com/filecoin-project/venus/pkg/types"

types3 "github.com/filecoin-project/venus-messager/types"

"github.com/filecoin-project/venus-sealer/api"
"github.com/filecoin-project/venus-sealer/config"
"github.com/filecoin-project/venus-sealer/constants"
"github.com/filecoin-project/venus-sealer/models"
"github.com/filecoin-project/venus-sealer/sector-storage/stores"
"github.com/filecoin-project/venus-sealer/service"
types2 "github.com/filecoin-project/venus-sealer/types"
)

var initCmd = &cli.Command{
Expand All @@ -58,7 +61,7 @@ var initCmd = &cli.Command{
&cli.StringFlag{
Name: "worker",
Aliases: []string{"w"},
Usage: "worker key to use (overrides --create-worker-key)",
Usage: "worker key to use",
},
&cli.StringFlag{
Name: "owner",
Expand Down Expand Up @@ -101,7 +104,7 @@ var initCmd = &cli.Command{
},
&cli.StringFlag{
Name: "network",
Usage: "set network type mainnet calibration 2k",
Usage: "network type: one of mainnet,calibration,2k&nerpa",
Value: "mainnet",
DefaultText: "mainnet",
},
Expand Down Expand Up @@ -160,6 +163,7 @@ var initCmd = &cli.Command{
ctx := api.ReqContext(cctx)

log.Info("Checking proof parameters")

ps, err := asset.Asset("fixtures/_assets/proof-params/parameters.json")
if err != nil {
return err
Expand All @@ -170,10 +174,11 @@ var initCmd = &cli.Command{
}

if err := paramfetch.GetParams(ctx, ps, srs, uint64(ssize)); err != nil {
return xerrors.Errorf("get params: %w", err)
return xerrors.Errorf("fetching proof parameters: %w", err)
}

log.Info("Trying to connect to full node RPC")

setAuthToken(cctx)
fullNode, closer, err := api.GetFullNodeAPIV2(cctx) // TODO: consider storing full node address in config
if err != nil {
Expand All @@ -187,6 +192,7 @@ var initCmd = &cli.Command{
return err
}
parserFlag(defaultCfg, cctx)

log.Info("Checking full node sync status")

if !cctx.Bool("genesis-miner") && !cctx.Bool("nosync") {
Expand All @@ -196,18 +202,20 @@ var initCmd = &cli.Command{
}

log.Info("Checking if repo exists")
cfgPath := cctx.String("config")

cfgPath := cctx.String(FlagMinerRepo)
defaultCfg.ConfigPath = cfgPath

exit, err := config.ConfigExist(defaultCfg.DataDir)
if err != nil {
return err
}
if exit {
return xerrors.Errorf("data has exit in %s", defaultCfg.DataDir)
return xerrors.Errorf("repo is already initialized at %s", defaultCfg.DataDir)
}

log.Info("Checking full node version")

v, err := fullNode.Version(ctx)
if err != nil {
return err
Expand All @@ -224,6 +232,7 @@ var initCmd = &cli.Command{
defer closer()

log.Info("Initializing repo")

{
//write config
err = config.SaveConfig(cfgPath, defaultCfg)
Expand Down Expand Up @@ -301,9 +310,9 @@ var initCmd = &cli.Command{
func setAuthToken(cctx *cli.Context) {
if cctx.IsSet("auth-token") {
authToken := cctx.String("auth-token")
cctx.Set("node-token", authToken)
cctx.Set("messager-token", authToken)
cctx.Set("gateway-token", authToken)
_ = cctx.Set("node-token", authToken)
_ = cctx.Set("messager-token", authToken)
_ = cctx.Set("gateway-token", authToken)
}
}

Expand Down Expand Up @@ -331,12 +340,14 @@ func parserFlag(cfg *config.StorageMiner, cctx *cli.Context) {
if cctx.IsSet("messager-token") {
cfg.Messager.Token = cctx.String("messager-token")
}

if cctx.IsSet("gateway-token") {
cfg.RegisterProof.Token = cctx.String("gateway-token")
}
}
func storageMinerInit(ctx context.Context, cctx *cli.Context, api api.FullNode, messagerClient api.IMessager, cfg *config.StorageMiner, ssize abi.SectorSize, gasPrice types.BigInt) error {
log.Info("Initializing libp2p identity")

repo, err := models.SetDataBase(config.HomeDir(cfg.DataDir), &cfg.DB)
if err != nil {
return err
Expand Down Expand Up @@ -369,6 +380,7 @@ func storageMinerInit(ctx context.Context, cctx *cli.Context, api api.FullNode,
if err := metaDataService.SaveMinerAddress(a); err != nil {
return err
}

if pssb := cctx.String("pre-sealed-metadata"); pssb != "" {
pssb, err := homedir.Expand(pssb)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions app/venus-sealer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

var log = logging.Logger("main")

const FlagMinerRepo = "miner-repo"

func main() {
sealer.SetupLogLevels()

Expand Down
2 changes: 1 addition & 1 deletion app/venus-sealer/proving.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ var provingInfoCmd = &cli.Command{

var faultPerc float64
if proving > 0 {
faultPerc = float64(faults*10000/proving) / 100
faultPerc = float64(faults * 100 / proving)
}

fmt.Printf("Current Epoch: %d\n", cd.CurrentEpoch)
Expand Down
Loading