Skip to content

Commit 3d0112e

Browse files
KubuxuStebalien
andauthored
fix(miner): ignore lastWork when selecting the best mining candidate (#12690)
* fix(miner): ignore lastWork when selecting the best mining candidate Previously, we only took the new head if it's heavier than the last head. Unfortunately, this meant that F3 finalization wasn't properly propagated to the miner. In terms of impact: 1. It seems likely that this check was simply defensive as, prior to F3, the new head should never have a lower weight (unless you're talking to multiple lotus nodes, I guess...). 2. The `lastWork` field is mostly used to track null blocks. Worst-case scenario, if we switch heads, we'll attempt to re-mine previous heights. However, that should be relatively fast and, due to the slash filter, we won't attempt to re-broadcast any of those blocks. Signed-off-by: Jakub Sztandera <[email protected]> * fix(miner): continue mining if we fail to submit a block Signed-off-by: Jakub Sztandera <[email protected]> * fix(miner): check the slash filter with the correct parent height We also perform this check inside `SyncSubmitBlock` so we did have an effective filter, but this was still wrong. Signed-off-by: Jakub Sztandera <[email protected]> --------- Signed-off-by: Jakub Sztandera <[email protected]> Co-authored-by: Steven Allen <[email protected]>
1 parent ffe5b28 commit 3d0112e

File tree

2 files changed

+33
-46
lines changed

2 files changed

+33
-46
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
- The mining loop will now correctly "stick" to the same upstream lotus node for all operations pertaining to mining a single block ([filecoin-project/lotus#12665](https://github.com/filecoin-project/lotus/pull/12665)).
3030
- Make the ordering of event output for `eth_` APIs and `GetActorEventsRaw` consistent, sorting ascending on: epoch, message index, event index and original event entry order. ([filecoin-project/lotus#12623](https://github.com/filecoin-project/lotus/pull/12623))
3131

32+
## Changes
33+
34+
- The Lotus Miner will now always mine on the latest chain head returned by lotus, even if that head has less "weight" than the previously seen head. This is necessary because F3 may end up finalizing a tipset with a lower weight, although this situation should be rare on the Filecoin mainnet. ([filecoin-project/lotus#12659](https://github.com/filecoin-project/lotus/pull/12659))
35+
3236
## Deps
3337

3438
# Node and Miner v1.30.0 / 2024-11-06

miner/miner.go

+29-46
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ minerLoop:
311311
onDone(b != nil, h, nil)
312312

313313
// Process the mined block.
314-
if b != nil {
314+
switch {
315+
case b != nil:
315316
// Record the event of mining a block.
316317
m.journal.RecordEvent(m.evtTypes[evtTypeBlockMined], func() interface{} {
317318
return map[string]interface{}{
@@ -344,23 +345,23 @@ minerLoop:
344345

345346
// Check for slash filter conditions.
346347
if os.Getenv("LOTUS_MINER_NO_SLASHFILTER") != "_yes_i_know_i_can_and_probably_will_lose_all_my_fil_and_power_" && !buildconstants.IsNearUpgrade(base.TipSet.Height(), buildconstants.UpgradeWatermelonFixHeight) {
347-
witness, fault, err := m.sf.MinedBlock(ctx, b.Header, base.TipSet.Height()+base.NullRounds)
348+
witness, fault, err := m.sf.MinedBlock(ctx, b.Header, base.TipSet.Height())
348349
if err != nil {
349350
log.Errorf("<!!> SLASH FILTER ERRORED: %s", err)
350351
// Continue here, because it's _probably_ wiser to not submit this block
351-
continue
352+
break
352353
}
353354

354355
if fault {
355356
log.Errorf("<!!> SLASH FILTER DETECTED FAULT due to blocks %s and %s", b.Header.Cid(), witness)
356-
continue
357+
break
357358
}
358359
}
359360

360361
// Check for blocks created at the same height.
361362
if _, ok := m.minedBlockHeights.Get(b.Header.Height); ok {
362363
log.Warnw("Created a block at the same height as another block we've created", "height", b.Header.Height, "miner", b.Header.Miner, "parents", b.Header.Parents)
363-
continue
364+
break
364365
}
365366

366367
// Add the block height to the mined block heights.
@@ -369,24 +370,26 @@ minerLoop:
369370
// Submit the newly mined block.
370371
if err := m.api.SyncSubmitBlock(ctx, b); err != nil {
371372
log.Errorf("failed to submit newly mined block: %+v", err)
373+
break
372374
}
373-
} else {
374-
// If no block was mined, increase the null rounds and wait for the next epoch.
375-
base.NullRounds++
376-
377-
// Calculate the time for the next round.
378-
nextRound := time.Unix(int64(base.TipSet.MinTimestamp()+buildconstants.BlockDelaySecs*uint64(base.NullRounds))+int64(buildconstants.PropagationDelaySecs), 0)
379-
380-
// Wait for the next round or stop signal.
381-
select {
382-
case <-build.Clock.After(build.Clock.Until(nextRound)):
383-
case <-m.stop:
384-
stopping := m.stopping
385-
m.stop = nil
386-
m.stopping = nil
387-
close(stopping)
388-
return
389-
}
375+
continue // TODO: we should probably remove this continue and wait in this case as well... but that's a bigger change.
376+
}
377+
378+
// If no block was mined or if we fail to submit the block, increase the null rounds and wait for the next epoch.
379+
base.NullRounds++
380+
381+
// Calculate the time for the next round.
382+
nextRound := time.Unix(int64(base.TipSet.MinTimestamp()+buildconstants.BlockDelaySecs*uint64(base.NullRounds))+int64(buildconstants.PropagationDelaySecs), 0)
383+
384+
// Wait for the next round or stop signal.
385+
select {
386+
case <-build.Clock.After(build.Clock.Until(nextRound)):
387+
case <-m.stop:
388+
stopping := m.stopping
389+
m.stop = nil
390+
m.stopping = nil
391+
close(stopping)
392+
return
390393
}
391394
}
392395
}
@@ -400,11 +403,8 @@ type MiningBase struct {
400403
}
401404

402405
// GetBestMiningCandidate implements the fork choice rule from a miner's
403-
// perspective.
404-
//
405-
// It obtains the current chain head (HEAD), and compares it to the last tipset
406-
// we selected as our mining base (LAST). If HEAD's weight is larger than
407-
// LAST's weight, it selects HEAD to build on. Else, it selects LAST.
406+
// perspective, returning the best head to mine on. This includes the number of null rounds we think
407+
// we should insert and the time at which we received said head.
408408
func (m *Miner) GetBestMiningCandidate(ctx context.Context) (*MiningBase, error) {
409409
m.lk.Lock()
410410
defer m.lk.Unlock()
@@ -414,27 +414,10 @@ func (m *Miner) GetBestMiningCandidate(ctx context.Context) (*MiningBase, error)
414414
return nil, err
415415
}
416416

417-
if m.lastWork != nil {
418-
if m.lastWork.TipSet.Equals(bts) {
419-
return m.lastWork, nil
420-
}
421-
422-
btsw, err := m.api.ChainTipSetWeight(ctx, bts.Key())
423-
if err != nil {
424-
return nil, err
425-
}
426-
ltsw, err := m.api.ChainTipSetWeight(ctx, m.lastWork.TipSet.Key())
427-
if err != nil {
428-
m.lastWork = nil
429-
return nil, err
430-
}
431-
432-
if types.BigCmp(btsw, ltsw) <= 0 {
433-
return m.lastWork, nil
434-
}
417+
if m.lastWork == nil || !m.lastWork.TipSet.Equals(bts) {
418+
m.lastWork = &MiningBase{TipSet: bts, ComputeTime: time.Now()}
435419
}
436420

437-
m.lastWork = &MiningBase{TipSet: bts, ComputeTime: time.Now()}
438421
return m.lastWork, nil
439422
}
440423

0 commit comments

Comments
 (0)