-
Notifications
You must be signed in to change notification settings - Fork 45
/
minerprotocols.go
214 lines (182 loc) · 5.74 KB
/
minerprotocols.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package minerprotocols
import (
"context"
"fmt"
"os"
"strconv"
"time"
"github.com/gammazero/workerpool"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lily/model"
observed "github.com/filecoin-project/lily/model/surveyed"
lapi "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
)
var log = logging.Logger("lily/tasks/minerproto")
var (
resultBufferEnv = "LILY_SURVEY_MINER_PROTOCOL_BUFFER"
resultBufferSize = 50
workerPoolSizeEnv = "LILY_SURVEY_MINER_PROTOCOL_WORKERS"
workerPoolSize = 50
fetchTimeoutEnv = "LILY_SURVEY_MINER_PROTOCOL_TIMEOUT_SECONDS"
fetchTimeout = 30
)
func init() {
if s := os.Getenv(resultBufferEnv); s != "" {
v, err := strconv.ParseInt(s, 10, 64)
if err == nil {
resultBufferSize = int(v)
}
}
if s := os.Getenv(workerPoolSizeEnv); s != "" {
v, err := strconv.ParseInt(s, 10, 64)
if err == nil {
workerPoolSize = int(v)
}
}
if s := os.Getenv(fetchTimeoutEnv); s != "" {
v, err := strconv.ParseInt(s, 10, 64)
if err == nil {
fetchTimeout = int(v)
}
}
}
type API interface {
Host() host.Host
ChainHead(context.Context) (*types.TipSet, error)
StateMinerInfo(ctx context.Context, addr address.Address, tsk types.TipSetKey) (lapi.MinerInfo, error)
StateListMiners(ctx context.Context, tsk types.TipSetKey) ([]address.Address, error)
StateMinerPower(context.Context, address.Address, types.TipSetKey) (*lapi.MinerPower, error)
}
func NewTask(api API) *Task {
return &Task{api: api}
}
type Task struct {
api API
}
func (t *Task) Process(ctx context.Context) (model.Persistable, error) {
headTs, err := t.api.ChainHead(ctx)
if err != nil {
return nil, fmt.Errorf("getting chain head: %w", err)
}
miners, err := t.api.StateListMiners(ctx, headTs.Key())
if err != nil {
return nil, fmt.Errorf("listing miners: %w", err)
}
queriedCount := uint64(0)
start := time.Now()
out := make(observed.MinerProtocolList, 0, len(miners))
results := make(chan *observed.MinerProtocol, resultBufferSize)
pool := workerpool.New(workerPoolSize)
for _, miner := range miners {
select {
case <-ctx.Done():
pool.Stop()
return nil, ctx.Err()
default:
}
miner := miner
mpower, err := t.api.StateMinerPower(ctx, miner, headTs.Key())
if err != nil {
return nil, fmt.Errorf("getting miner %s power: %w", miner, err)
}
// don't process miners without min power
if !mpower.HasMinPower {
continue
}
// find the miner, if DNE abort as this indicates an error in the API as a miner was returned from StateListMiners that DNE in state tree.
minerInfo, err := t.api.StateMinerInfo(ctx, miner, headTs.Key())
if err != nil {
return nil, fmt.Errorf("getting miner %s info: %w", miner, err)
}
// don't process miners without multiaddresses set
if len(minerInfo.Multiaddrs) == 0 {
continue
}
queriedCount++
log.Debugw("fetching miner protocol info", "miner", miner, "count", queriedCount)
pool.Submit(func() {
fetchCtx, cancel := context.WithTimeout(ctx, time.Second*time.Duration(fetchTimeout))
defer cancel()
fetchMinerProtocolModel(fetchCtx, t.api, miner, minerInfo, start, results)
})
}
// wait for all workers to complete then close the results channel
go func() {
pool.StopWait()
close(results)
}()
// drain results until closed.
for res := range results {
log.Debugw("miner protocol result received", "miner", res.MinerID, "count", len(out))
out = append(out, res)
}
log.Infow("miner protocol survey complete", "duration", time.Since(start), "results", len(out), "queried", queriedCount)
return out, nil
}
func (t *Task) Close() error {
return nil
}
func fetchMinerProtocolModel(ctx context.Context, api API, addr address.Address, minerInfo lapi.MinerInfo, start time.Time, results chan *observed.MinerProtocol) {
// since miners may choose if their peerID is set in their info
var peerID string
if minerInfo.PeerId != nil {
peerID = minerInfo.PeerId.String()
}
// extract any multiaddresses the miner has set in their info, they may have none bail if that is the case.
minerPeerInfo, err := getMinerAddrInfo(minerInfo)
if err != nil {
log.Debugw("failed getting miner address info", "miner", addr, "error", err)
return
}
// attempt to connect to miner
if err := api.Host().Connect(ctx, *minerPeerInfo); err != nil {
log.Debugw("failed connecting to miner", "miner", addr, "error", err)
return
}
// get protocols supported by miner
protos, err := api.Host().Peerstore().GetProtocols(minerPeerInfo.ID)
if err != nil {
log.Debugw("failed getting miner protocols", "miner", addr, "error", err)
return
}
protosStrArray := []string{}
for _, v := range protos {
protosStrArray = append(protosStrArray, string(v))
}
// find miners agent version
agentVersionI, err := api.Host().Peerstore().Get(minerPeerInfo.ID, "AgentVersion")
if err != nil {
log.Debugw("failed getting miner agent", "miner", addr, "error", err)
return
}
// create the model we will export to storage
results <- &observed.MinerProtocol{
ObservedAt: start,
MinerID: addr.String(),
PeerID: peerID,
Agent: agentVersionI.(string),
Protocols: protosStrArray,
}
}
func getMinerAddrInfo(info lapi.MinerInfo) (*peer.AddrInfo, error) {
var maddrs []multiaddr.Multiaddr
for _, m := range info.Multiaddrs {
ma, err := multiaddr.NewMultiaddrBytes(m)
if err != nil {
return nil, fmt.Errorf("miner had invalid multiaddrs in their info: %w", err)
}
maddrs = append(maddrs, ma)
}
if len(maddrs) == 0 {
return nil, fmt.Errorf("miner has no multiaddrs set on-chain")
}
return &peer.AddrInfo{
ID: *info.PeerId,
Addrs: maddrs,
}, nil
}