Speedup Difficulty Cmp / Add / Mul64, consensus Uncle Penalty method
This commit is contained in:
parent
d7532fedd3
commit
894ebbba1b
|
@ -686,15 +686,14 @@ func main() {
|
|||
createMiner(share.Miner, share)
|
||||
miners[miner].Uncles.Add(int(int64(tip.SideHeight)-int64(share.SideHeight)), 1)
|
||||
|
||||
unclePenalty := types.DifficultyFrom64(share.Difficulty).Mul64(consensus.UnclePenalty).Div64(100)
|
||||
uncleWeight := share.Difficulty - unclePenalty.Lo
|
||||
uncleWeight, unclePenalty := consensus.ApplyUnclePenalty(types.DifficultyFrom64(share.Difficulty))
|
||||
|
||||
if shares[uncleShareIndex].TemplateId == share.UncleOf {
|
||||
parent := shares[uncleShareIndex]
|
||||
createMiner(parent.Miner, parent)
|
||||
miners[parent.Miner].Weight = miners[parent.Miner].Weight.Add64(unclePenalty.Lo)
|
||||
}
|
||||
miners[miner].Weight = miners[miner].Weight.Add64(uncleWeight)
|
||||
miners[miner].Weight = miners[miner].Weight.Add64(uncleWeight.Lo)
|
||||
|
||||
totalWeight = totalWeight.Add64(share.Difficulty)
|
||||
} else {
|
||||
|
@ -926,8 +925,7 @@ func main() {
|
|||
|
||||
unclesFound.Add(int(int64(tipHeight)-int64(share.SideHeight)), 1)
|
||||
|
||||
unclePenalty := types.DifficultyFrom64(share.Difficulty).Mul64(consensus.UnclePenalty).Div64(100)
|
||||
uncleWeight := share.Difficulty - unclePenalty.Lo
|
||||
uncleWeight, unclePenalty := consensus.ApplyUnclePenalty(types.DifficultyFrom64(share.Difficulty))
|
||||
|
||||
if i := slices.IndexFunc(shares, func(block *index.SideBlock) bool {
|
||||
return block.TemplateId == share.UncleOf
|
||||
|
@ -938,10 +936,10 @@ func main() {
|
|||
longDiff = longDiff.Add64(unclePenalty.Lo)
|
||||
}
|
||||
if share.SideHeight > wend {
|
||||
windowDiff = windowDiff.Add64(uncleWeight)
|
||||
windowDiff = windowDiff.Add64(uncleWeight.Lo)
|
||||
unclesInWindow.Add(int(int64(tipHeight)-int64(share.SideHeight)), 1)
|
||||
}
|
||||
longDiff = longDiff.Add64(uncleWeight)
|
||||
longDiff = longDiff.Add64(uncleWeight.Lo)
|
||||
} else {
|
||||
sharesFound.Add(int(int64(tipHeight)-toInt64(share.SideHeight)), 1)
|
||||
if share.SideHeight > wend {
|
||||
|
|
|
@ -1278,7 +1278,9 @@ func (i *Index) GetSideBlockFromPoolBlock(b *sidechain.PoolBlock, inclusion Bloc
|
|||
tip.EffectiveHeight = tip.SideHeight
|
||||
tip.Inclusion = inclusion
|
||||
|
||||
if bottomHeight, err := sidechain.BlocksInPPLNSWindow(b, i.consensus, i.GetDifficultyByHeight, i.GetByTemplateId, nil); err != nil {
|
||||
if bottomHeight, err := sidechain.BlocksInPPLNSWindow(b, i.consensus, i.GetDifficultyByHeight, i.GetByTemplateId, func(b *sidechain.PoolBlock, weight types.Difficulty) {
|
||||
|
||||
}); err != nil {
|
||||
// unknown
|
||||
tip.WindowDepth = 0
|
||||
} else {
|
||||
|
@ -1311,7 +1313,9 @@ func (i *Index) GetSideBlockFromPoolBlock(b *sidechain.PoolBlock, inclusion Bloc
|
|||
uncleBlock.Inclusion = InclusionInVerifiedChain
|
||||
}
|
||||
|
||||
if uncleBottomHeight, err := sidechain.BlocksInPPLNSWindow(uncle, i.consensus, i.GetDifficultyByHeight, i.GetByTemplateId, nil); err != nil {
|
||||
if uncleBottomHeight, err := sidechain.BlocksInPPLNSWindow(uncle, i.consensus, i.GetDifficultyByHeight, i.GetByTemplateId, func(b *sidechain.PoolBlock, weight types.Difficulty) {
|
||||
|
||||
}); err != nil {
|
||||
// unknown
|
||||
uncleBlock.WindowDepth = 0
|
||||
} else {
|
||||
|
|
|
@ -74,8 +74,7 @@ func IterateSideBlocksInPPLNSWindow(tip *SideBlock, consensus *sidechain.Consens
|
|||
}
|
||||
|
||||
// Take some % of uncle's weight into this share
|
||||
unclePenalty := types.DifficultyFrom64(uncle.Difficulty).Mul64(consensus.UnclePenalty).Div64(100)
|
||||
uncleWeight := types.DifficultyFrom64(uncle.Difficulty).Sub(unclePenalty)
|
||||
uncleWeight, unclePenalty := consensus.ApplyUnclePenalty(types.DifficultyFrom64(uncle.Difficulty))
|
||||
newPplnsWeight := pplnsWeight.Add(uncleWeight)
|
||||
|
||||
// Skip uncles that push PPLNS weight above the limit
|
||||
|
|
|
@ -280,5 +280,12 @@ func (c *Consensus) CalculateId() types.Hash {
|
|||
return randomx.ConsensusHash(buf)
|
||||
}
|
||||
|
||||
// ApplyUnclePenalty Applies UnclePenalty efficiently
|
||||
func (c *Consensus) ApplyUnclePenalty(weight types.Difficulty) (uncleWeight, unclePenalty types.Difficulty) {
|
||||
unclePenalty = weight.Mul64(c.UnclePenalty).Div64(100)
|
||||
uncleWeight = weight.Sub(unclePenalty)
|
||||
return
|
||||
}
|
||||
|
||||
var ConsensusDefault = &Consensus{NetworkType: NetworkMainnet, PoolName: "mainnet test 2", TargetBlockTime: 10, MinimumDifficulty: 100000, ChainWindowSize: 2160, UnclePenalty: 20, HardForks: p2poolMainNetHardForks, Id: types.Hash{34, 175, 126, 231, 181, 11, 104, 146, 227, 153, 218, 107, 44, 108, 68, 39, 178, 81, 4, 212, 169, 4, 142, 0, 177, 110, 157, 240, 68, 7, 249, 24}}
|
||||
var ConsensusMini = &Consensus{NetworkType: NetworkMainnet, PoolName: "mini", TargetBlockTime: 10, MinimumDifficulty: 100000, ChainWindowSize: 2160, UnclePenalty: 20, HardForks: p2poolMainNetHardForks, Id: types.Hash{57, 130, 201, 26, 149, 174, 199, 250, 66, 80, 189, 18, 108, 216, 194, 220, 136, 23, 63, 24, 64, 113, 221, 44, 219, 86, 39, 163, 53, 24, 126, 196}}
|
||||
|
|
|
@ -121,8 +121,7 @@ func IterateBlocksInPPLNSWindow(tip *PoolBlock, consensus *Consensus, difficulty
|
|||
}
|
||||
|
||||
// Take some % of uncle's weight into this share
|
||||
unclePenalty := uncle.Side.Difficulty.Mul64(consensus.UnclePenalty).Div64(100)
|
||||
uncleWeight := uncle.Side.Difficulty.Sub(unclePenalty)
|
||||
uncleWeight, unclePenalty := consensus.ApplyUnclePenalty(uncle.Side.Difficulty)
|
||||
newPplnsWeight := pplnsWeight.Add(uncleWeight)
|
||||
|
||||
// Skip uncles that push PPLNS weight above the limit
|
||||
|
@ -213,8 +212,8 @@ func BlocksInPPLNSWindow(tip *PoolBlock, consensus *Consensus, difficultyByHeigh
|
|||
}
|
||||
|
||||
// Take some % of uncle's weight into this share
|
||||
unclePenalty := uncle.Side.Difficulty.Mul64(consensus.UnclePenalty).Div64(100)
|
||||
uncleWeight := uncle.Side.Difficulty.Sub(unclePenalty)
|
||||
uncleWeight, unclePenalty := consensus.ApplyUnclePenalty(uncle.Side.Difficulty)
|
||||
|
||||
newPplnsWeight := pplnsWeight.Add(uncleWeight)
|
||||
|
||||
// Skip uncles that push PPLNS weight above the limit
|
||||
|
@ -223,9 +222,7 @@ func BlocksInPPLNSWindow(tip *PoolBlock, consensus *Consensus, difficultyByHeigh
|
|||
}
|
||||
curWeight = curWeight.Add(unclePenalty)
|
||||
|
||||
if addWeightFunc != nil {
|
||||
addWeightFunc(uncle, uncleWeight)
|
||||
}
|
||||
addWeightFunc(uncle, uncleWeight)
|
||||
|
||||
pplnsWeight = newPplnsWeight
|
||||
|
||||
|
@ -236,9 +233,7 @@ func BlocksInPPLNSWindow(tip *PoolBlock, consensus *Consensus, difficultyByHeigh
|
|||
// Always add non-uncle shares even if PPLNS weight goes above the limit
|
||||
bottomHeight = cur.Side.Height
|
||||
|
||||
if addWeightFunc != nil {
|
||||
addWeightFunc(cur, curWeight)
|
||||
}
|
||||
addWeightFunc(cur, curWeight)
|
||||
|
||||
pplnsWeight = pplnsWeight.Add(curWeight)
|
||||
|
||||
|
|
|
@ -34,7 +34,14 @@ func (d Difficulty) Equals64(v uint64) bool {
|
|||
}
|
||||
|
||||
func (d Difficulty) Cmp(v Difficulty) int {
|
||||
return uint128.Uint128(d).Cmp(uint128.Uint128(v))
|
||||
//return uint128.Uint128(d).Cmp(uint128.Uint128(v))
|
||||
if d == v {
|
||||
return 0
|
||||
} else if d.Hi < v.Hi || (d.Hi == v.Hi && d.Lo < v.Lo) {
|
||||
return -1
|
||||
} else {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func (d Difficulty) Cmp64(v uint64) int {
|
||||
|
@ -67,7 +74,10 @@ func (d Difficulty) Xor64(v uint64) Difficulty {
|
|||
|
||||
// Add All calls can wrap
|
||||
func (d Difficulty) Add(v Difficulty) Difficulty {
|
||||
return Difficulty(uint128.Uint128(d).AddWrap(uint128.Uint128(v)))
|
||||
//return Difficulty(uint128.Uint128(d).AddWrap(uint128.Uint128(v)))
|
||||
lo, carry := bits.Add64(d.Lo, v.Lo, 0)
|
||||
hi, _ := bits.Add64(d.Hi, v.Hi, carry)
|
||||
return Difficulty{Lo: lo, Hi: hi}
|
||||
}
|
||||
|
||||
// Add64 All calls can wrap
|
||||
|
@ -92,7 +102,10 @@ func (d Difficulty) Mul(v Difficulty) Difficulty {
|
|||
|
||||
// Mul64 All calls can wrap
|
||||
func (d Difficulty) Mul64(v uint64) Difficulty {
|
||||
return Difficulty(uint128.Uint128(d).MulWrap64(v))
|
||||
//return Difficulty(uint128.Uint128(d).MulWrap64(v))
|
||||
hi, lo := bits.Mul64(d.Lo, v)
|
||||
hi += d.Hi * v
|
||||
return Difficulty{Lo: lo, Hi: hi}
|
||||
}
|
||||
|
||||
func (d Difficulty) Div(v Difficulty) Difficulty {
|
||||
|
|
Loading…
Reference in a new issue