Skip to content

Commit

Permalink
Merge pull request #4314 from filecoin-project/feat/lotus-pcr-block-list
Browse files Browse the repository at this point in the history
Add block list to pcr
  • Loading branch information
magik6k authored Oct 22, 2020
2 parents 6b9a4c4 + ee8d8bf commit 6fc8550
Showing 1 changed file with 96 additions and 2 deletions.
98 changes: 96 additions & 2 deletions cmd/lotus-pcr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"context"
"encoding/csv"
"fmt"
"io"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/filecoin-project/lotus/chain/actors/builtin"
Expand Down Expand Up @@ -249,6 +251,15 @@ var recoverMinersCmd = &cli.Command{
}
defer closer()

r, err := NewRepo(cctx.String("repo"))
if err != nil {
return err
}

if err := r.Open(); err != nil {
return err
}

from, err := address.NewFromString(cctx.String("from"))
if err != nil {
return xerrors.Errorf("parsing source address (provide correct --from flag!): %w", err)
Expand All @@ -265,13 +276,20 @@ var recoverMinersCmd = &cli.Command{
minerRecoveryCutoff := uint64(cctx.Int("miner-recovery-cutoff"))
minerRecoveryBonus := uint64(cctx.Int("miner-recovery-bonus"))

blockmap := make(map[address.Address]struct{})

for _, addr := range r.Blocklist() {
blockmap[addr] = struct{}{}
}

rf := &refunder{
api: api,
wallet: from,
dryRun: dryRun,
minerRecoveryRefundPercent: minerRecoveryRefundPercent,
minerRecoveryCutoff: types.FromFil(minerRecoveryCutoff),
minerRecoveryBonus: types.FromFil(minerRecoveryBonus),
blockmap: blockmap,
}

refundTipset, err := api.ChainHead(ctx)
Expand Down Expand Up @@ -464,6 +482,12 @@ var runCmd = &cli.Command{
return err
}

blockmap := make(map[address.Address]struct{})

for _, addr := range r.Blocklist() {
blockmap[addr] = struct{}{}
}

rf := &refunder{
api: api,
wallet: from,
Expand All @@ -478,13 +502,18 @@ var runCmd = &cli.Command{
publishStorageDealsEnabled: publishStorageDealsEnabled,
preFeeCapMax: types.BigInt(preFeeCapMax),
proveFeeCapMax: types.BigInt(proveFeeCapMax),
blockmap: blockmap,
}

var refunds *MinersRefund = NewMinersRefund()
var rounds int = 0
var refunds = NewMinersRefund()
var rounds = 0
nextMinerRecovery := r.MinerRecoveryHeight() + minerRecoveryPeriod

for tipset := range tipsetsCh {
for k := range rf.blockmap {
fmt.Printf("%s\n", k)
}

refunds, err = rf.ProcessTipset(ctx, tipset, refunds)
if err != nil {
return err
Expand Down Expand Up @@ -632,6 +661,7 @@ type refunder struct {
windowedPoStEnabled bool
publishStorageDealsEnabled bool
threshold big.Int
blockmap map[address.Address]struct{}

preFeeCapMax big.Int
proveFeeCapMax big.Int
Expand Down Expand Up @@ -736,6 +766,11 @@ func (r *refunder) EnsureMinerMinimums(ctx context.Context, tipset *types.TipSet
}

for _, maddr := range miners {
if _, found := r.blockmap[maddr]; found {
log.Debugw("skipping blocked miner", "height", tipset.Height(), "key", tipset.Key(), "miner", maddr)
continue
}

mact, err := r.api.StateGetActor(ctx, maddr, types.EmptyTSK)
if err != nil {
log.Errorw("failed", "err", err, "height", tipset.Height(), "key", tipset.Key(), "miner", maddr)
Expand Down Expand Up @@ -895,6 +930,11 @@ func (r *refunder) processTipsetStorageMinerActor(ctx context.Context, tipset *t
refundValue := types.NewInt(0)
var messageMethod string

if _, found := r.blockmap[m.To]; found {
log.Debugw("skipping blocked miner", "height", tipset.Height(), "key", tipset.Key(), "miner", m.To)
return false, messageMethod, types.NewInt(0), nil
}

switch m.Method {
case miner.Methods.SubmitWindowedPoSt:
if !r.windowedPoStEnabled {
Expand Down Expand Up @@ -1175,6 +1215,7 @@ type Repo struct {
lastHeight abi.ChainEpoch
lastMinerRecoveryHeight abi.ChainEpoch
path string
blocklist []address.Address
}

func NewRepo(path string) (*Repo, error) {
Expand Down Expand Up @@ -1230,6 +1271,10 @@ func (r *Repo) Open() error {
return err
}

if err := r.loadBlockList(); err != nil {
return err
}

return nil
}

Expand All @@ -1255,6 +1300,51 @@ func loadChainEpoch(fn string) (abi.ChainEpoch, error) {
return abi.ChainEpoch(height), nil
}

func (r *Repo) loadBlockList() error {
var err error
fpath := filepath.Join(r.path, "blocklist")
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
defer func() {
err = f.Close()
}()

blocklist := []address.Address{}
input := bufio.NewReader(f)
for {
stra, errR := input.ReadString('\n')
stra = strings.TrimSpace(stra)

if len(stra) == 0 {
if errR == io.EOF {
break
}
continue
}

addr, err := address.NewFromString(stra)
if err != nil {
return err
}

blocklist = append(blocklist, addr)

if errR != nil && errR != io.EOF {
return err
}

if errR == io.EOF {
break
}
}

r.blocklist = blocklist

return nil
}

func (r *Repo) loadHeight() error {
var err error
r.lastHeight, err = loadChainEpoch(filepath.Join(r.path, "height"))
Expand All @@ -1267,6 +1357,10 @@ func (r *Repo) loadMinerRecoveryHeight() error {
return err
}

func (r *Repo) Blocklist() []address.Address {
return r.blocklist
}

func (r *Repo) Height() abi.ChainEpoch {
return r.lastHeight
}
Expand Down

0 comments on commit 6fc8550

Please sign in to comment.