From e1a0572a1576bdd0f9e2153df291ba7ac9c9052a Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 30 Oct 2024 08:28:33 -0700 Subject: [PATCH] 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. --- CHANGELOG.md | 4 ++++ miner/miner.go | 28 ++++------------------------ 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9dcd7bf9fc..fc8361cbf77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/miner/miner.go b/miner/miner.go index b7e39a953c4..785cc3a2c9a 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -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() @@ -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 }