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
…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]>
  • Loading branch information
Kubuxu and Stebalien authored Nov 12, 2024
1 parent ffe5b28 commit 3d0112e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
- 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)).
- 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))

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

# Node and Miner v1.30.0 / 2024-11-06
Expand Down
75 changes: 29 additions & 46 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ minerLoop:
onDone(b != nil, h, nil)

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

// Check for slash filter conditions.
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) {
witness, fault, err := m.sf.MinedBlock(ctx, b.Header, base.TipSet.Height()+base.NullRounds)
witness, fault, err := m.sf.MinedBlock(ctx, b.Header, base.TipSet.Height())
if err != nil {
log.Errorf("<!!> SLASH FILTER ERRORED: %s", err)
// Continue here, because it's _probably_ wiser to not submit this block
continue
break
}

if fault {
log.Errorf("<!!> SLASH FILTER DETECTED FAULT due to blocks %s and %s", b.Header.Cid(), witness)
continue
break
}
}

// Check for blocks created at the same height.
if _, ok := m.minedBlockHeights.Get(b.Header.Height); ok {
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)
continue
break
}

// Add the block height to the mined block heights.
Expand All @@ -369,24 +370,26 @@ minerLoop:
// Submit the newly mined block.
if err := m.api.SyncSubmitBlock(ctx, b); err != nil {
log.Errorf("failed to submit newly mined block: %+v", err)
break
}
} else {
// If no block was mined, increase the null rounds and wait for the next epoch.
base.NullRounds++

// Calculate the time for the next round.
nextRound := time.Unix(int64(base.TipSet.MinTimestamp()+buildconstants.BlockDelaySecs*uint64(base.NullRounds))+int64(buildconstants.PropagationDelaySecs), 0)

// Wait for the next round or stop signal.
select {
case <-build.Clock.After(build.Clock.Until(nextRound)):
case <-m.stop:
stopping := m.stopping
m.stop = nil
m.stopping = nil
close(stopping)
return
}
continue // TODO: we should probably remove this continue and wait in this case as well... but that's a bigger change.
}

// If no block was mined or if we fail to submit the block, increase the null rounds and wait for the next epoch.
base.NullRounds++

// Calculate the time for the next round.
nextRound := time.Unix(int64(base.TipSet.MinTimestamp()+buildconstants.BlockDelaySecs*uint64(base.NullRounds))+int64(buildconstants.PropagationDelaySecs), 0)

// Wait for the next round or stop signal.
select {
case <-build.Clock.After(build.Clock.Until(nextRound)):
case <-m.stop:
stopping := m.stopping
m.stop = nil
m.stopping = nil
close(stopping)
return
}
}
}
Expand All @@ -400,11 +403,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 +414,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 3d0112e

Please sign in to comment.