-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
postfind.go
129 lines (119 loc) · 3.4 KB
/
postfind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
lapi "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/specs-actors/v2/actors/builtin"
"github.com/urfave/cli/v2"
)
var postFindCmd = &cli.Command{
Name: "post-find",
Description: "return addresses of all miners who have over zero power and have posted in the last day",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tipset",
Usage: "specify tipset state to search on",
},
&cli.BoolFlag{
Name: "verbose",
Usage: "get more frequent print updates",
},
&cli.BoolFlag{
Name: "withpower",
Usage: "only print addrs of miners with more than zero power",
},
&cli.IntFlag{
Name: "lookback",
Usage: "number of past epochs to search for post",
Value: 2880, //default 1 day
},
},
Action: func(c *cli.Context) error {
api, acloser, err := lcli.GetFullNodeAPI(c)
if err != nil {
return err
}
defer acloser()
ctx := lcli.ReqContext(c)
verbose := c.Bool("verbose")
withpower := c.Bool("withpower")
startTs, err := lcli.LoadTipSet(ctx, c, api)
if err != nil {
return err
}
if startTs == nil {
startTs, err = api.ChainHead(ctx)
if err != nil {
return err
}
}
stopEpoch := startTs.Height() - abi.ChainEpoch(c.Int("lookback"))
if verbose {
fmt.Printf("Collecting messages between %d and %d\n", startTs.Height(), stopEpoch)
}
// Get all messages over the last day
ts := startTs
msgs := make([]*types.Message, 0)
for ts.Height() > stopEpoch {
// Get messages on ts parent
next, err := api.ChainGetParentMessages(ctx, ts.Cids()[0])
if err != nil {
return err
}
msgs = append(msgs, messagesFromAPIMessages(next)...)
// Next ts
ts, err = api.ChainGetTipSet(ctx, ts.Parents())
if err != nil {
return err
}
if verbose && int64(ts.Height())%100 == 0 {
fmt.Printf("Collected messages back to height %d\n", ts.Height())
}
}
fmt.Printf("Loaded messages to height %d\n", ts.Height())
mAddrs, err := api.StateListMiners(ctx, startTs.Key())
if err != nil {
return err
}
minersToCheck := make(map[address.Address]struct{})
for _, mAddr := range mAddrs {
// if they have no power ignore. This filters out 14k inactive miners
// so we can do 100x fewer expensive message queries
if withpower {
power, err := api.StateMinerPower(ctx, mAddr, startTs.Key())
if err != nil {
return err
}
if power.MinerPower.RawBytePower.GreaterThan(big.Zero()) {
minersToCheck[mAddr] = struct{}{}
}
} else {
minersToCheck[mAddr] = struct{}{}
}
}
fmt.Printf("Loaded %d miners to check\n", len(minersToCheck))
postedMiners := make(map[address.Address]struct{})
for _, msg := range msgs {
_, shouldCheck := minersToCheck[msg.To]
_, seenBefore := postedMiners[msg.To]
if shouldCheck && !seenBefore {
if msg.Method == builtin.MethodsMiner.SubmitWindowedPoSt {
fmt.Printf("%s\n", msg.To)
postedMiners[msg.To] = struct{}{}
}
}
}
return nil
},
}
func messagesFromAPIMessages(apiMessages []lapi.Message) []*types.Message {
messages := make([]*types.Message, len(apiMessages))
for i, apiMessage := range apiMessages {
messages[i] = apiMessage.Message
}
return messages
}