Skip to content

Commit

Permalink
fix(miner): ignore lastWork when selecting the best mining candidate
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Stebalien committed Oct 30, 2024
1 parent cfde1d5 commit 704855e
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,8 @@ type MiningBase struct {
}

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

if m.lastWork != nil {
if m.lastWork.TipSet.Equals(bts) {
return m.lastWork, nil
}

btsw, err := m.api.ChainTipSetWeight(ctx, bts.Key())
if err != nil {
return nil, err
}
ltsw, err := m.api.ChainTipSetWeight(ctx, m.lastWork.TipSet.Key())
if err != nil {
m.lastWork = nil
return nil, err
}

if types.BigCmp(btsw, ltsw) <= 0 {
return m.lastWork, nil
}
if m.lastWork == nil || !m.lastWork.TipSet.Equals(bts) {
m.lastWork = &MiningBase{TipSet: bts, ComputeTime: time.Now()}
}

m.lastWork = &MiningBase{TipSet: bts, ComputeTime: time.Now()}
return m.lastWork, nil
}

Expand Down

0 comments on commit 704855e

Please sign in to comment.