Introduce index.SortSideBlock and Difficulty Float64()
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
bea6b16479
commit
71c27726be
|
@ -135,29 +135,7 @@ func setupEventHandler(p2api *api.P2PoolApi, indexDb *index.Index) {
|
|||
}()
|
||||
|
||||
//remember last few template for events
|
||||
sideBlockBuffer := NewSortedBuf[*index.SideBlock](int(p2api.Consensus().ChainWindowSize*2), func(a, b *index.SideBlock) int {
|
||||
if a == b {
|
||||
return 0
|
||||
}
|
||||
if a == nil {
|
||||
return -1
|
||||
} else if b == nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
if a.EffectiveHeight < b.EffectiveHeight {
|
||||
return -1
|
||||
} else if a.EffectiveHeight > b.EffectiveHeight {
|
||||
return 1
|
||||
}
|
||||
if a.SideHeight < b.SideHeight {
|
||||
return -1
|
||||
} else if a.SideHeight > b.SideHeight {
|
||||
return 1
|
||||
}
|
||||
//same height, sort by main id
|
||||
return a.MainId.Compare(b.MainId)
|
||||
})
|
||||
sideBlockBuffer := NewSortedBuf[*index.SideBlock](int(p2api.Consensus().ChainWindowSize*2), index.SortSideBlock)
|
||||
|
||||
//remember last few found main for events
|
||||
foundBlockBuffer := NewSortedBuf[*index.FoundBlock](10, func(a, b *index.FoundBlock) int {
|
||||
|
@ -420,18 +398,8 @@ func setupEventHandler(p2api *api.P2PoolApi, indexDb *index.Index) {
|
|||
} else if a.MainHeight > b.MainHeight {
|
||||
return 1
|
||||
}
|
||||
if a.EffectiveHeight < b.EffectiveHeight {
|
||||
return -1
|
||||
} else if a.EffectiveHeight > b.EffectiveHeight {
|
||||
return 1
|
||||
}
|
||||
if a.SideHeight < b.SideHeight {
|
||||
return -1
|
||||
} else if a.SideHeight > b.SideHeight {
|
||||
return 1
|
||||
}
|
||||
//same height, sort by main id
|
||||
return a.MainId.Compare(b.MainId)
|
||||
|
||||
return index.SortSideBlock(a, b)
|
||||
})
|
||||
|
||||
func() {
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/sidechain"
|
||||
p2pooltypes "git.gammaspectra.live/P2Pool/p2pool-observer/p2pool/types"
|
||||
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
|
||||
"slices"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
|
@ -191,3 +192,50 @@ func (b *SideBlock) Weight(tipHeight, windowSize, consensusUnclePenalty uint64)
|
|||
return weight, 0
|
||||
}
|
||||
}
|
||||
|
||||
func HashRatioSideBlocks(shares []*SideBlock, latest *SideBlock, consensusUnclePenalty uint64) (hashrate uint64, ratio float64, weight, total types.Difficulty) {
|
||||
if len(shares) == 0 || latest == nil {
|
||||
return 0, 0, types.ZeroDifficulty, types.ZeroDifficulty
|
||||
}
|
||||
|
||||
shares = slices.Clone(shares)
|
||||
slices.SortFunc(shares, SortSideBlock)
|
||||
|
||||
var totalWeight types.Difficulty
|
||||
for _, s := range shares {
|
||||
w, _ := s.Weight(s.SideHeight, uint64(s.WindowDepth), consensusUnclePenalty)
|
||||
totalWeight = totalWeight.Add64(w)
|
||||
}
|
||||
|
||||
cumWeight := latest.CumulativeDifficulty.Sub(shares[0].CumulativeDifficulty)
|
||||
|
||||
return totalWeight.Div64(latest.Timestamp - shares[0].Timestamp).Lo,
|
||||
totalWeight.Float64() / cumWeight.Float64(),
|
||||
totalWeight,
|
||||
cumWeight
|
||||
}
|
||||
|
||||
func SortSideBlock(a, b *SideBlock) int {
|
||||
if a == b {
|
||||
return 0
|
||||
}
|
||||
|
||||
if a == nil {
|
||||
return -1
|
||||
} else if b == nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
if a.EffectiveHeight < b.EffectiveHeight {
|
||||
return -1
|
||||
} else if a.EffectiveHeight > b.EffectiveHeight {
|
||||
return 1
|
||||
}
|
||||
if a.SideHeight < b.SideHeight {
|
||||
return -1
|
||||
} else if a.SideHeight > b.SideHeight {
|
||||
return 1
|
||||
}
|
||||
//same height, sort by main id
|
||||
return a.MainId.Compare(b.MainId)
|
||||
}
|
||||
|
|
|
@ -190,6 +190,10 @@ func (d Difficulty) Big() *big.Int {
|
|||
return uint128.Uint128(d).Big()
|
||||
}
|
||||
|
||||
func (d Difficulty) Float64() float64 {
|
||||
return float64(d.Lo) + float64(d.Hi)*math.MaxUint64
|
||||
}
|
||||
|
||||
func (d Difficulty) MarshalJSON() ([]byte, error) {
|
||||
if d.Hi == 0 {
|
||||
return []byte(strconv.FormatUint(d.Lo, 10)), nil
|
||||
|
|
11
utils/count.go
Normal file
11
utils/count.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package utils
|
||||
|
||||
func SliceCount[S ~[]E, E any](s S, f func(E) bool) (count int) {
|
||||
for i := range s {
|
||||
if f(s[i]) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
Loading…
Reference in a new issue