|
| 1 | +package discv4 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/ethereum/go-ethereum/p2p/enode" |
| 12 | + log "github.com/sirupsen/logrus" |
| 13 | + "go.uber.org/atomic" |
| 14 | + "golang.org/x/sync/errgroup" |
| 15 | + |
| 16 | + "github.com/dennis-tra/nebula-crawler/config" |
| 17 | + "github.com/dennis-tra/nebula-crawler/core" |
| 18 | + "github.com/dennis-tra/nebula-crawler/db" |
| 19 | + "github.com/dennis-tra/nebula-crawler/discvx" |
| 20 | +) |
| 21 | + |
| 22 | +type CrawlerConfig struct { |
| 23 | + DialTimeout time.Duration |
| 24 | + AddrDialType config.AddrType |
| 25 | + LogErrors bool |
| 26 | +} |
| 27 | + |
| 28 | +type Crawler struct { |
| 29 | + id string |
| 30 | + cfg *CrawlerConfig |
| 31 | + listener *discvx.UDPv4 |
| 32 | + crawledPeers int |
| 33 | + done chan struct{} |
| 34 | +} |
| 35 | + |
| 36 | +var _ core.Worker[PeerInfo, core.CrawlResult[PeerInfo]] = (*Crawler)(nil) |
| 37 | + |
| 38 | +func (c *Crawler) Work(ctx context.Context, task PeerInfo) (core.CrawlResult[PeerInfo], error) { |
| 39 | + logEntry := log.WithFields(log.Fields{ |
| 40 | + "crawlerID": c.id, |
| 41 | + "remoteID": task.peerID.ShortString(), |
| 42 | + "crawlCount": c.crawledPeers, |
| 43 | + }) |
| 44 | + logEntry.Debugln("Crawling peer") |
| 45 | + defer logEntry.Debugln("Crawled peer") |
| 46 | + |
| 47 | + crawlStart := time.Now() |
| 48 | + |
| 49 | + result := c.crawlDiscV4(ctx, task) |
| 50 | + |
| 51 | + cr := core.CrawlResult[PeerInfo]{ |
| 52 | + CrawlerID: c.id, |
| 53 | + Info: task, |
| 54 | + CrawlStartTime: crawlStart, |
| 55 | + RoutingTableFromAPI: false, |
| 56 | + RoutingTable: result.RoutingTable, |
| 57 | + // Agent: libp2pResult.Agent, |
| 58 | + // Protocols: libp2pResult.Protocols, |
| 59 | + // ConnectError: libp2pResult.ConnectError, |
| 60 | + // ConnectErrorStr: libp2pResult.ConnectErrorStr, |
| 61 | + CrawlError: result.Error, |
| 62 | + CrawlErrorStr: result.ErrorStr, |
| 63 | + CrawlEndTime: time.Now(), |
| 64 | + // ConnectStartTime: libp2pResult.ConnectStartTime, |
| 65 | + // ConnectEndTime: libp2pResult.ConnectEndTime, |
| 66 | + // Properties: data, |
| 67 | + LogErrors: c.cfg.LogErrors, |
| 68 | + } |
| 69 | + |
| 70 | + // We've now crawled this peer, so increment |
| 71 | + c.crawledPeers++ |
| 72 | + |
| 73 | + return cr, nil |
| 74 | +} |
| 75 | + |
| 76 | +type DiscV4Result struct { |
| 77 | + // The time we received the first successful response |
| 78 | + RespondedAt *time.Time |
| 79 | + |
| 80 | + // The updated ethereum node record |
| 81 | + ENR *enode.Node |
| 82 | + |
| 83 | + // The neighbors of the crawled peer |
| 84 | + RoutingTable *core.RoutingTable[PeerInfo] |
| 85 | + |
| 86 | + // The time the draining of bucket entries was finished |
| 87 | + DoneAt time.Time |
| 88 | + |
| 89 | + // The combined error of crawling the peer's buckets |
| 90 | + Error error |
| 91 | + |
| 92 | + // The above error mapped to a known string |
| 93 | + ErrorStr string |
| 94 | +} |
| 95 | + |
| 96 | +func (c *Crawler) crawlDiscV4(ctx context.Context, pi PeerInfo) DiscV4Result { |
| 97 | + // mutex to guard access to result and allNeighbors |
| 98 | + mu := sync.RWMutex{} |
| 99 | + |
| 100 | + // the final result struct |
| 101 | + result := DiscV4Result{} |
| 102 | + |
| 103 | + // all neighbors of pi. We're using a map to deduplicate. |
| 104 | + allNeighbors := map[string]PeerInfo{} |
| 105 | + |
| 106 | + // errorBits tracks at which CPL errors have occurred. |
| 107 | + // 0000 0000 0000 0000 - No error |
| 108 | + // 0000 0000 0000 0001 - An error has occurred at CPL 0 |
| 109 | + // 1000 0000 0000 0001 - An error has occurred at CPL 0 and 15 |
| 110 | + errorBits := atomic.NewUint32(0) |
| 111 | + |
| 112 | + enr, err := c.listener.RequestENR(pi.Node) |
| 113 | + if err != nil { |
| 114 | + result.ENR = pi.Node |
| 115 | + } else { |
| 116 | + result.ENR = enr |
| 117 | + now := time.Now() |
| 118 | + result.RespondedAt = &now |
| 119 | + } |
| 120 | + |
| 121 | + errg := errgroup.Group{} |
| 122 | + for i := 0; i <= 15; i++ { // 15 is maximum |
| 123 | + count := i // Copy value |
| 124 | + errg.Go(func() error { |
| 125 | + pubKey, err := discvx.GenRandomPublicKey(pi.Node.ID(), count) |
| 126 | + if err != nil { |
| 127 | + log.WithError(err).WithField("enr", pi.Node.String()).Warnln("Failed generating public key") |
| 128 | + errorBits.Add(1 << count) |
| 129 | + return fmt.Errorf("generating random public key with CPL %d: %w", count, err) |
| 130 | + } |
| 131 | + |
| 132 | + udpAddr := &net.UDPAddr{IP: pi.Node.IP(), Port: pi.Node.UDP()} |
| 133 | + |
| 134 | + var neighbors []*enode.Node |
| 135 | + for retry := 0; retry < 2; retry++ { |
| 136 | + neighbors, err = c.listener.FindNode(pi.Node.ID(), udpAddr, pubKey) |
| 137 | + if err == nil { |
| 138 | + break |
| 139 | + } |
| 140 | + |
| 141 | + errorBits.Add(1 << count) |
| 142 | + |
| 143 | + if errors.Is(err, discvx.ErrTimeout) { |
| 144 | + sleepDur := time.Second * time.Duration(5*(retry+1)) |
| 145 | + select { |
| 146 | + case <-ctx.Done(): |
| 147 | + return ctx.Err() |
| 148 | + case <-time.After(sleepDur): // may add jitter here |
| 149 | + continue |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + errorBits.Add(1 << count) |
| 154 | + |
| 155 | + return fmt.Errorf("getting closest peer with CPL %d: %w", count, err) |
| 156 | + } |
| 157 | + |
| 158 | + mu.Lock() |
| 159 | + defer mu.Unlock() |
| 160 | + |
| 161 | + if result.RespondedAt == nil { |
| 162 | + now := time.Now() |
| 163 | + result.RespondedAt = &now |
| 164 | + } |
| 165 | + |
| 166 | + for _, n := range neighbors { |
| 167 | + npi, err := NewPeerInfo(n) |
| 168 | + if err != nil { |
| 169 | + log.WithError(err).Warnln("Failed parsing ethereum node neighbor") |
| 170 | + continue |
| 171 | + } |
| 172 | + allNeighbors[string(npi.peerID)] = npi |
| 173 | + } |
| 174 | + |
| 175 | + if err != nil { |
| 176 | + errorBits.Add(1 << count) |
| 177 | + return err |
| 178 | + } |
| 179 | + |
| 180 | + return nil |
| 181 | + }) |
| 182 | + } |
| 183 | + |
| 184 | + // wait for go routines to finish |
| 185 | + err = errg.Wait() |
| 186 | + |
| 187 | + // track done timestamp and error |
| 188 | + result.DoneAt = time.Now() |
| 189 | + result.Error = err |
| 190 | + |
| 191 | + result.RoutingTable = &core.RoutingTable[PeerInfo]{ |
| 192 | + PeerID: pi.ID(), |
| 193 | + Neighbors: []PeerInfo{}, |
| 194 | + ErrorBits: uint16(errorBits.Load()), |
| 195 | + Error: err, |
| 196 | + } |
| 197 | + |
| 198 | + for _, n := range allNeighbors { |
| 199 | + result.RoutingTable.Neighbors = append(result.RoutingTable.Neighbors, n) |
| 200 | + } |
| 201 | + |
| 202 | + // if there was a connection error, parse it to a known one |
| 203 | + if result.Error != nil { |
| 204 | + result.ErrorStr = db.NetError(result.Error) |
| 205 | + } |
| 206 | + |
| 207 | + return result |
| 208 | +} |
0 commit comments