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 e1a0572
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
- Change the F3 HeadLookback parameter to 4 ([filecoin-project/lotus#12648](https://github.com/filecoin-project/lotus/pull/12648)).
- Upgrade go-f3 to 0.7.1 to resolve Tipset not found errors when trying to establish instance start time ([filecoin-project/lotus#12651](https://github.com/filecoin-project/lotus/pull/12651)).

## Changes

- 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))

## Deps

# UNRELEASED Node v1.30.0
Expand Down
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 e1a0572

Please sign in to comment.