Skip to content

Commit

Permalink
mining: Remove testnet min diff reduction support.
Browse files Browse the repository at this point in the history
This modifies the internal mining template generator and CPU miner to no
longer support the version 3 testnet minimum difficulty reduction logic
that no longer applies given all new blocks on testnet now enforce
difficulty throttling instead of the minimum difficulty reduction rule.

Note that this does not modify the consensus enforcement logic since it
is still required for all existing version 3 testnet blocks prior to the
difficulty throttling change.
  • Loading branch information
davecgh committed Apr 30, 2023
1 parent 768aefe commit 13c4b45
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 87 deletions.
8 changes: 2 additions & 6 deletions internal/mining/bgblktmplgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,8 @@ func NewBgBlkTmplGenerator(cfg *BgBlkTmplConfig) *BgBlkTmplGenerator {
// UpdateBlockTime updates the timestamp in the passed header to the current
// time while taking into account the median time of the last several blocks to
// ensure the new time is after that time per the chain consensus rules.
//
// Finally, it will update the target difficulty if needed based on the new time
// for the test networks since their target difficulty can change based upon
// time.
func (g *BgBlkTmplGenerator) UpdateBlockTime(header *wire.BlockHeader) error {
return g.tg.UpdateBlockTime(header)
func (g *BgBlkTmplGenerator) UpdateBlockTime(header *wire.BlockHeader) {
g.tg.UpdateBlockTime(header)
}

// sendQueueRegenEvent sends the provided regen event on the internal queue
Expand Down
25 changes: 3 additions & 22 deletions internal/mining/cpuminer/cpuminer.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,6 @@ func (m *CPUMiner) submitBlock(block *dcrutil.Block) bool {
return false
}

// When the reduce min difficulty option is set it is possible that the
// required difficulty changed while a block was being solved and will
// therefore result in an error due to having the incorrect required
// difficulty set in the header. In that case, only log the error as
// debug since it is expected to happen from time to time and not really
// an error.
if m.cfg.ChainParams.ReduceMinDifficulty &&
errors.Is(rErr, blockchain.ErrHighHash) {
log.Debugf("Block submitted via CPU miner rejected because of "+
"ReduceMinDifficulty time sync failure: %v", err)
return false
}

// Other rule errors should be reported.
log.Errorf("Block submitted via CPU miner rejected: %v", err)
return false
Expand Down Expand Up @@ -327,17 +314,11 @@ func (m *CPUMiner) solveBlock(ctx context.Context, header *wire.BlockHeader, sta
// Non-blocking select to fall through
}

err := m.g.UpdateBlockTime(header)
if err != nil {
log.Warnf("CPU miner unable to update block template "+
"time: %v", err)
}
m.g.UpdateBlockTime(header)

// Update the bits and time in the serialized header bytes
// directly too since they might have changed.
const bitsOffset = 116
// Update time in the serialized header bytes directly too since
// it might have changed.
const timestampOffset = 136
littleEndian.PutUint32(hdrBytes[bitsOffset:], header.Bits)
timestamp := uint32(header.Timestamp.Unix())
littleEndian.PutUint32(hdrBytes[timestampOffset:], timestamp)
}
Expand Down
32 changes: 1 addition & 31 deletions internal/mining/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,19 +871,6 @@ func (g *BlkTmplGenerator) handleTooFewVoters(nextHeight int64,
ts := g.medianAdjustedTime()
block.Header.Timestamp = ts

// If we're on testnet, the time since this last block listed as the
// parent must be taken into consideration.
if g.cfg.ChainParams.ReduceMinDifficulty {
parentHash := topBlock.MsgBlock().Header.PrevBlock

requiredDifficulty, err := g.cfg.CalcNextRequiredDifficulty(&parentHash, ts)
if err != nil {
return nil, makeError(ErrGettingDifficulty, err.Error())
}

block.Header.Bits = requiredDifficulty
}

// Recalculate the size.
block.Header.Size = uint32(block.SerializeSize())

Expand Down Expand Up @@ -2343,27 +2330,10 @@ nextPriorityQueueItem:
// UpdateBlockTime updates the timestamp in the passed header to the current
// time while taking into account the median time of the last several blocks to
// ensure the new time is after that time per the chain consensus rules.
//
// Finally, it will update the target difficulty if needed based on the new time
// for the test networks since their target difficulty can change based upon
// time.
func (g *BlkTmplGenerator) UpdateBlockTime(header *wire.BlockHeader) error {
func (g *BlkTmplGenerator) UpdateBlockTime(header *wire.BlockHeader) {
// The new timestamp is potentially adjusted to ensure it comes after
// the median time of the last several blocks per the chain consensus
// rules.
newTimestamp := g.medianAdjustedTime()
header.Timestamp = newTimestamp

// If running on a network that requires recalculating the difficulty,
// do so now.
if g.cfg.ChainParams.ReduceMinDifficulty {
difficulty, err := g.cfg.CalcNextRequiredDifficulty(&header.PrevBlock,
newTimestamp)
if err != nil {
return makeError(ErrGettingDifficulty, err.Error())
}
header.Bits = difficulty
}

return nil
}
2 changes: 1 addition & 1 deletion internal/rpcserver/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ type BlockTemplater interface {

// UpdateBlockTime updates the timestamp in the passed header to the current
// time while taking into account the consensus rules.
UpdateBlockTime(header *wire.BlockHeader) error
UpdateBlockTime(header *wire.BlockHeader)
}

// FiltererV2 provides an interface for retrieving a block's version 2 GCS
Expand Down
8 changes: 2 additions & 6 deletions internal/rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3771,11 +3771,7 @@ func handleGetWorkRequest(ctx context.Context, s *Server) (interface{}, error) {
// consensus rules. Note that the header is copied to avoid mutating the
// shared block template.
headerCopy := template.Block.Header
err := bt.UpdateBlockTime(&headerCopy)
if err != nil {
context := "Failed to update block time"
return nil, rpcInternalError(err.Error(), context)
}
bt.UpdateBlockTime(&headerCopy)

// Serialize the block header into a buffer large enough to hold the
// the block header and the internal blake256 padding that is added and
Expand All @@ -3788,7 +3784,7 @@ func handleGetWorkRequest(ctx context.Context, s *Server) (interface{}, error) {
// data[144:152] --> ExtraNonce
data := make([]byte, 0, getworkDataLen)
buf := bytes.NewBuffer(data)
err = headerCopy.Serialize(buf)
err := headerCopy.Serialize(buf)
if err != nil {
context := "Failed to serialize data"
return nil, rpcInternalError(err.Error(), context)
Expand Down
27 changes: 6 additions & 21 deletions internal/rpcserver/rpcserverhandlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,12 +1023,11 @@ func (s *testTemplateSubber) PublishTemplateNtfn(templateNtfn *mining.TemplateNt
// testBlockTemplater provides a mock block templater by implementing the
// mining.BlockTemplater interface.
type testBlockTemplater struct {
subscriptions map[*testTemplateSubber]struct{}
regenReason mining.TemplateUpdateReason
currTemplate *mining.BlockTemplate
currTemplateErr error
updateBlockTimeErr error
simulateNewNtfn bool
subscriptions map[*testTemplateSubber]struct{}
regenReason mining.TemplateUpdateReason
currTemplate *mining.BlockTemplate
currTemplateErr error
simulateNewNtfn bool
}

// ForceRegen asks the block templater to generate a new template immediately.
Expand Down Expand Up @@ -1066,9 +1065,7 @@ func (b *testBlockTemplater) CurrentTemplate() (*mining.BlockTemplate, error) {

// UpdateBlockTime updates the timestamp in the passed header to the current
// time while taking into account the consensus rules.
func (b *testBlockTemplater) UpdateBlockTime(header *wire.BlockHeader) error {
return b.updateBlockTimeErr
}
func (b *testBlockTemplater) UpdateBlockTime(header *wire.BlockHeader) {}

// testTxMempooler provides a mock mempool transaction data source by
// implementing the TxMempooler interface.
Expand Down Expand Up @@ -5765,18 +5762,6 @@ func TestHandleGetWork(t *testing.T) {
}(),
wantErr: true,
errCode: dcrjson.ErrRPCMisc,
}, {
name: "handleGetWork: unable to update block time",
handler: handleGetWork,
cmd: &types.GetWorkCmd{},
mockMiningState: defaultMockMiningState(),
mockBlockTemplater: func() *testBlockTemplater {
templater := defaultMockBlockTemplater()
templater.updateBlockTimeErr = errors.New("unable to update block time")
return templater
}(),
wantErr: true,
errCode: dcrjson.ErrRPCInternal.Code,
}, {
name: "handleGetWork: data is not equal to getworkDataLen (192 bytes)",
handler: handleGetWork,
Expand Down

0 comments on commit 13c4b45

Please sign in to comment.