|
| 1 | +package minerprotocols |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/filecoin-project/go-address" |
| 11 | + lapi "github.com/filecoin-project/lotus/api" |
| 12 | + "github.com/filecoin-project/lotus/chain/types" |
| 13 | + "github.com/gammazero/workerpool" |
| 14 | + logging "github.com/ipfs/go-log/v2" |
| 15 | + "github.com/libp2p/go-libp2p-core/host" |
| 16 | + "github.com/libp2p/go-libp2p-core/peer" |
| 17 | + "github.com/multiformats/go-multiaddr" |
| 18 | + |
| 19 | + "github.com/filecoin-project/lily/model" |
| 20 | + observed "github.com/filecoin-project/lily/model/surveyed" |
| 21 | +) |
| 22 | + |
| 23 | +var log = logging.Logger("lily/tasks/minerproto") |
| 24 | + |
| 25 | +var ( |
| 26 | + resultBufferEnv = "LILY_SURVEY_MINER_PROTOCOL_BUFFER" |
| 27 | + resultBufferSize = 50 |
| 28 | + |
| 29 | + workerPoolSizeEnv = "LILY_SURVEY_MINER_PROTOCOL_WORKERS" |
| 30 | + workerPoolSize = 50 |
| 31 | + |
| 32 | + fetchTimeoutEnv = "LILY_SURVEY_MINER_PROTOCOL_TIMEOUT_SECONDS" |
| 33 | + fetchTimeout = 30 |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + if s := os.Getenv(resultBufferEnv); s != "" { |
| 38 | + v, err := strconv.ParseInt(s, 10, 64) |
| 39 | + if err == nil { |
| 40 | + resultBufferSize = int(v) |
| 41 | + } |
| 42 | + } |
| 43 | + if s := os.Getenv(workerPoolSizeEnv); s != "" { |
| 44 | + v, err := strconv.ParseInt(s, 10, 64) |
| 45 | + if err == nil { |
| 46 | + workerPoolSize = int(v) |
| 47 | + } |
| 48 | + } |
| 49 | + if s := os.Getenv(fetchTimeoutEnv); s != "" { |
| 50 | + v, err := strconv.ParseInt(s, 10, 64) |
| 51 | + if err == nil { |
| 52 | + fetchTimeout = int(v) |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +type API interface { |
| 58 | + Host() host.Host |
| 59 | + ChainHead(context.Context) (*types.TipSet, error) |
| 60 | + StateMinerInfo(ctx context.Context, addr address.Address, tsk types.TipSetKey) (lapi.MinerInfo, error) |
| 61 | + StateListMiners(ctx context.Context, tsk types.TipSetKey) ([]address.Address, error) |
| 62 | + StateMinerPower(context.Context, address.Address, types.TipSetKey) (*lapi.MinerPower, error) |
| 63 | +} |
| 64 | + |
| 65 | +func NewTask(api API) *Task { |
| 66 | + return &Task{api: api} |
| 67 | +} |
| 68 | + |
| 69 | +type Task struct { |
| 70 | + api API |
| 71 | +} |
| 72 | + |
| 73 | +func (t *Task) Process(ctx context.Context) (model.Persistable, error) { |
| 74 | + headTs, err := t.api.ChainHead(ctx) |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("getting chain head: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + miners, err := t.api.StateListMiners(ctx, headTs.Key()) |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("listing miners: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + queriedCount := uint64(0) |
| 85 | + start := time.Now() |
| 86 | + out := make(observed.MinerProtocolList, 0, len(miners)) |
| 87 | + results := make(chan *observed.MinerProtocol, resultBufferSize) |
| 88 | + pool := workerpool.New(workerPoolSize) |
| 89 | + |
| 90 | + for _, miner := range miners { |
| 91 | + select { |
| 92 | + case <-ctx.Done(): |
| 93 | + pool.Stop() |
| 94 | + return nil, ctx.Err() |
| 95 | + default: |
| 96 | + } |
| 97 | + miner := miner |
| 98 | + |
| 99 | + mpower, err := t.api.StateMinerPower(ctx, miner, headTs.Key()) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("getting miner %s power: %w", miner, err) |
| 102 | + } |
| 103 | + // don't process miners without min power |
| 104 | + if !mpower.HasMinPower { |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + // 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. |
| 109 | + minerInfo, err := t.api.StateMinerInfo(ctx, miner, headTs.Key()) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("getting miner %s info: %w", miner, err) |
| 112 | + } |
| 113 | + |
| 114 | + // don't process miners without multiaddresses set |
| 115 | + if len(minerInfo.Multiaddrs) == 0 { |
| 116 | + continue |
| 117 | + } |
| 118 | + |
| 119 | + queriedCount++ |
| 120 | + log.Debugw("fetching miner protocol info", "miner", miner, "count", queriedCount) |
| 121 | + pool.Submit(func() { |
| 122 | + fetchCtx, cancel := context.WithTimeout(ctx, time.Second*time.Duration(fetchTimeout)) |
| 123 | + defer cancel() |
| 124 | + fetchMinerProtocolModel(fetchCtx, t.api, miner, minerInfo, start, results) |
| 125 | + }) |
| 126 | + } |
| 127 | + |
| 128 | + // wait for all workers to complete then close the results channel |
| 129 | + go func() { |
| 130 | + pool.StopWait() |
| 131 | + close(results) |
| 132 | + }() |
| 133 | + |
| 134 | + // drain results until closed. |
| 135 | + for res := range results { |
| 136 | + log.Debugw("miner protocol result received", "miner", res.MinerID, "count", len(out)) |
| 137 | + out = append(out, res) |
| 138 | + } |
| 139 | + log.Infow("miner protocol survey complete", "duration", time.Since(start), "results", len(out), "queried", queriedCount) |
| 140 | + return out, nil |
| 141 | +} |
| 142 | + |
| 143 | +func (t *Task) Close() error { |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func fetchMinerProtocolModel(ctx context.Context, api API, addr address.Address, minerInfo lapi.MinerInfo, start time.Time, results chan *observed.MinerProtocol) { |
| 148 | + // since miners may choose if their peerID is set in their info |
| 149 | + var peerID string |
| 150 | + if minerInfo.PeerId != nil { |
| 151 | + peerID = minerInfo.PeerId.String() |
| 152 | + } |
| 153 | + |
| 154 | + // extract any multiaddresses the miner has set in their info, they may have none bail if that is the case. |
| 155 | + minerPeerInfo, err := getMinerAddrInfo(minerInfo) |
| 156 | + if err != nil { |
| 157 | + log.Debugw("failed getting miner address info", "miner", addr, "error", err) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + // attempt to connect to miner |
| 162 | + if err := api.Host().Connect(ctx, *minerPeerInfo); err != nil { |
| 163 | + log.Debugw("failed connecting to miner", "miner", addr, "error", err) |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + // get protocols supported by miner |
| 168 | + protos, err := api.Host().Peerstore().GetProtocols(minerPeerInfo.ID) |
| 169 | + if err != nil { |
| 170 | + log.Debugw("failed getting miner protocols", "miner", addr, "error", err) |
| 171 | + return |
| 172 | + } |
| 173 | + |
| 174 | + // find miners agent version |
| 175 | + agentVersionI, err := api.Host().Peerstore().Get(minerPeerInfo.ID, "AgentVersion") |
| 176 | + if err != nil { |
| 177 | + log.Debugw("failed getting miner agent", "miner", addr, "error", err) |
| 178 | + return |
| 179 | + } |
| 180 | + |
| 181 | + // create the model we will export to storage |
| 182 | + results <- &observed.MinerProtocol{ |
| 183 | + ObservedAt: start, |
| 184 | + MinerID: addr.String(), |
| 185 | + PeerID: peerID, |
| 186 | + Agent: agentVersionI.(string), |
| 187 | + Protocols: protos, |
| 188 | + } |
| 189 | + |
| 190 | +} |
| 191 | + |
| 192 | +func getMinerAddrInfo(info lapi.MinerInfo) (*peer.AddrInfo, error) { |
| 193 | + var maddrs []multiaddr.Multiaddr |
| 194 | + for _, m := range info.Multiaddrs { |
| 195 | + ma, err := multiaddr.NewMultiaddrBytes(m) |
| 196 | + if err != nil { |
| 197 | + return nil, fmt.Errorf("miner had invalid multiaddrs in their info: %w", err) |
| 198 | + } |
| 199 | + maddrs = append(maddrs, ma) |
| 200 | + } |
| 201 | + if len(maddrs) == 0 { |
| 202 | + return nil, fmt.Errorf("miner has no multiaddrs set on-chain") |
| 203 | + } |
| 204 | + return &peer.AddrInfo{ |
| 205 | + ID: *info.PeerId, |
| 206 | + Addrs: maddrs, |
| 207 | + }, nil |
| 208 | +} |
0 commit comments