Skip to content

Commit

Permalink
Fix tests and always flush PoW headers in process header pow so calc …
Browse files Browse the repository at this point in the history
…difficulty logic works
  • Loading branch information
lazynina committed Dec 10, 2024
1 parent 8a0bf0f commit 7b311fd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
5 changes: 5 additions & 0 deletions lib/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2267,6 +2267,11 @@ func (bc *Blockchain) processHeaderPoW(
bc.blockIndex.setHeaderTip(newNode)
}

// Put the height hash to node info in the db.
if err = PutHeightHashToNodeInfo(bc.db, bc.snapshot, newNode, false /*bitcoinNodes*/, bc.eventManager); err != nil {
return nil, false, false, errors.Wrapf(err, "ProcessHeader: Problem calling PutHeightHashToNodeInfo")
}

return newNode, isMainChain, false, nil
}

Expand Down
28 changes: 16 additions & 12 deletions lib/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1247,15 +1247,15 @@ func TestCalcNextDifficultyTargetHalvingDoublingHitLimit(t *testing.T) {
NewBlockHash(RandomBytes(32)),
uint32(ii),
nextDiff,
nil,
big.NewInt(1),
&MsgDeSoHeader{
// Blocks generating every 1 second, which is 2x too fast.
TstampNanoSecs: SecondsToNanoSeconds(int64(ii)),
},
StatusHeaderValidated,
))
bc.blockIndex.setHeaderTip(nodes[len(nodes)-1])

require.NoError(PutHeightHashToNodeInfo(bc.db, nil, nodes[len(nodes)-1], false, nil))
diffsAsInts = append(diffsAsInts, HashToBigint(nextDiff).Int64())
}

Expand Down Expand Up @@ -1285,15 +1285,15 @@ func TestCalcNextDifficultyTargetHalvingDoublingHitLimit(t *testing.T) {
NewBlockHash(RandomBytes(32)),
uint32(ii),
nextDiff,
nil,
big.NewInt(1),
&MsgDeSoHeader{
// Blocks generating every 4 second, which is 2x too slow.
TstampNanoSecs: SecondsToNanoSeconds(int64(ii * 4)),
},
StatusHeaderValidated,
))
bc.blockIndex.setHeaderTip(nodes[len(nodes)-1])

require.NoError(PutHeightHashToNodeInfo(bc.db, nil, nodes[len(nodes)-1], false, nil))
diffsAsInts = append(diffsAsInts, HashToBigint(nextDiff).Int64())
}

Expand Down Expand Up @@ -1349,15 +1349,15 @@ func TestCalcNextDifficultyTargetHittingLimitsSlow(t *testing.T) {
NewBlockHash(RandomBytes(32)),
uint32(ii),
nextDiff,
nil,
big.NewInt(1),
&MsgDeSoHeader{
// Blocks generating every 1 second, which is 2x too fast.
TstampNanoSecs: SecondsToNanoSeconds(int64(ii)),
},
StatusHeaderValidated,
))
bc.blockIndex.setHeaderTip(nodes[len(nodes)-1])

require.NoError(PutHeightHashToNodeInfo(bc.db, nil, nodes[len(nodes)-1], false, nil))
diffsAsInts = append(diffsAsInts, HashToBigint(nextDiff).Int64())
}

Expand Down Expand Up @@ -1387,15 +1387,15 @@ func TestCalcNextDifficultyTargetHittingLimitsSlow(t *testing.T) {
NewBlockHash(RandomBytes(32)),
uint32(ii),
nextDiff,
nil,
big.NewInt(1),
&MsgDeSoHeader{
// Blocks generating every 8 second, which is >2x too slow.
TstampNanoSecs: SecondsToNanoSeconds(int64(ii * 4)),
},
StatusHeaderValidated,
))
bc.blockIndex.setHeaderTip(nodes[len(nodes)-1])

require.NoError(PutHeightHashToNodeInfo(bc.db, nil, nodes[len(nodes)-1], false, nil))
diffsAsInts = append(diffsAsInts, HashToBigint(nextDiff).Int64())
}

Expand Down Expand Up @@ -1451,14 +1451,15 @@ func TestCalcNextDifficultyTargetHittingLimitsFast(t *testing.T) {
NewBlockHash(RandomBytes(32)),
uint32(ii),
nextDiff,
nil,
big.NewInt(1),
&MsgDeSoHeader{
// Blocks generating all at once.
TstampNanoSecs: SecondsToNanoSeconds(0),
},
StatusHeaderValidated,
))
bc.blockIndex.setHeaderTip(nodes[len(nodes)-1])
require.NoError(PutHeightHashToNodeInfo(bc.db, nil, nodes[len(nodes)-1], false, nil))
diffsAsInts = append(diffsAsInts, HashToBigint(nextDiff).Int64())
}

Expand Down Expand Up @@ -1509,14 +1510,15 @@ func TestCalcNextDifficultyTargetJustRight(t *testing.T) {
NewBlockHash(RandomBytes(32)),
uint32(ii),
nextDiff,
nil,
big.NewInt(1),
&MsgDeSoHeader{
// Blocks generating every 2 second, which is under the limit.
TstampNanoSecs: SecondsToNanoSeconds(int64(ii * 2)),
},
StatusHeaderValidated,
))
bc.blockIndex.setHeaderTip(nodes[len(nodes)-1])
require.NoError(PutHeightHashToNodeInfo(bc.db, nil, nodes[len(nodes)-1], false, nil))
diffsAsInts = append(diffsAsInts, HashToBigint(nextDiff).Int64())
}

Expand Down Expand Up @@ -1568,14 +1570,15 @@ func TestCalcNextDifficultyTargetSlightlyOff(t *testing.T) {
NewBlockHash(RandomBytes(32)),
uint32(ii),
nextDiff,
nil,
big.NewInt(1),
&MsgDeSoHeader{
// Blocks generating every 1 second, which is 2x too fast.
TstampNanoSecs: SecondsToNanoSeconds(int64(ii)),
},
StatusHeaderValidated,
))
bc.blockIndex.setHeaderTip(nodes[len(nodes)-1])
require.NoError(PutHeightHashToNodeInfo(bc.db, nil, nodes[len(nodes)-1], false, nil))
diffsAsInts = append(diffsAsInts, HashToBigint(nextDiff).Int64())
}

Expand Down Expand Up @@ -1605,14 +1608,15 @@ func TestCalcNextDifficultyTargetSlightlyOff(t *testing.T) {
NewBlockHash(RandomBytes(32)),
uint32(ii),
nextDiff,
nil,
big.NewInt(1),
&MsgDeSoHeader{
// Blocks generating every 3 seconds, which is slow but under the limit.
TstampNanoSecs: SecondsToNanoSeconds(int64(ii) * 3),
},
StatusHeaderValidated,
))
bc.blockIndex.setHeaderTip(nodes[len(nodes)-1])
require.NoError(PutHeightHashToNodeInfo(bc.db, nil, nodes[len(nodes)-1], false, nil))
diffsAsInts = append(diffsAsInts, HashToBigint(nextDiff).Int64())
}

Expand Down

0 comments on commit 7b311fd

Please sign in to comment.