(blockNum uint64)
| 252 | } |
| 253 | |
| 254 | func (l *Light) getCache(blockNum uint64) *cache { |
| 255 | var c *cache |
| 256 | epoch := blockNum / epochLength |
| 257 | |
| 258 | // If we have a PoW for that epoch, use that |
| 259 | l.mu.Lock() |
| 260 | if l.caches == nil { |
| 261 | l.caches = make(map[uint64]*cache) |
| 262 | } |
| 263 | if l.NumCaches == 0 { |
| 264 | l.NumCaches = 3 |
| 265 | } |
| 266 | c = l.caches[epoch] |
| 267 | if c == nil { |
| 268 | // No cached DAG, evict the oldest if the cache limit was reached |
| 269 | if len(l.caches) >= l.NumCaches { |
| 270 | var evict *cache |
| 271 | for _, cache := range l.caches { |
| 272 | if evict == nil || evict.used.After(cache.used) { |
| 273 | evict = cache |
| 274 | } |
| 275 | } |
| 276 | log.Debug(fmt.Sprintf("Evicting DAG for epoch %d in favour of epoch %d", evict.epoch, epoch)) |
| 277 | delete(l.caches, evict.epoch) |
| 278 | } |
| 279 | // If we have the new DAG pre-generated, use that, otherwise create a new one |
| 280 | if l.future != nil && l.future.epoch == epoch { |
| 281 | log.Debug(fmt.Sprintf("Using pre-generated DAG for epoch %d", epoch)) |
| 282 | c, l.future = l.future, nil |
| 283 | } else { |
| 284 | log.Debug(fmt.Sprintf("No pre-generated DAG available, creating new for epoch %d", epoch)) |
| 285 | c = &cache{epoch: epoch, test: l.test} |
| 286 | } |
| 287 | l.caches[epoch] = c |
| 288 | |
| 289 | // If we just used up the future cache, or need a refresh, regenerate |
| 290 | if l.future == nil || l.future.epoch <= epoch { |
| 291 | log.Debug(fmt.Sprintf("Pre-generating DAG for epoch %d", epoch+1)) |
| 292 | l.future = &cache{epoch: epoch + 1, test: l.test} |
| 293 | go l.future.generate() |
| 294 | } |
| 295 | } |
| 296 | c.used = time.Now() |
| 297 | l.mu.Unlock() |
| 298 | |
| 299 | // Wait for generation finish and return the cache |
| 300 | c.generate() |
| 301 | return c |
| 302 | } |
| 303 | |
| 304 | // dag wraps an ethash_full_t with some metadata |
| 305 | // and automatic memory management. |
no test coverage detected