Refactor window iteration into entry

This commit is contained in:
DataHoarder 2023-04-26 06:29:05 +02:00
parent 74ff0d870f
commit 17ff7124df
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
3 changed files with 20 additions and 18 deletions

8
bot.go
View file

@ -317,7 +317,7 @@ func main() {
var yourWeight types.Difficulty
var totalWeight types.Difficulty
for range index.IterateSideBlocksInPPLNSWindow(tip, e.Consensus, e.DifficultyFromHeight, e.SideBlockByTemplateId, e.SideBlockUnclesByTemplateId, func(b *index.SideBlock, weight types.Difficulty) {
e.IterateWindow(func(b *index.SideBlock, weight types.Difficulty) {
totalWeight = totalWeight.Add(weight)
if tip.Miner == b.Miner {
if b.IsUncle() {
@ -327,11 +327,7 @@ func main() {
}
yourWeight = yourWeight.Add(weight)
}
}, func(err error) {
}) {
}
})
shareRatio := float64(yourWeight.Lo) / float64(totalWeight.Lo)
if shareRatio > notificationPoolShare { //disable spammy notifications

View file

@ -71,7 +71,7 @@ var commands = []command{
tr = append(tr, &r)
}
for range index.IterateSideBlocksInPPLNSWindow(e.Tip, e.Consensus, e.DifficultyFromHeight, e.SideBlockByTemplateId, e.SideBlockUnclesByTemplateId, func(b *index.SideBlock, weight types.Difficulty) {
e.IterateWindow(func(b *index.SideBlock, weight types.Difficulty) {
for _, r := range tr {
r.TotalWeight = r.TotalWeight.Add(weight)
if b.MinerAddress.Compare(r.Address) == 0 {
@ -86,11 +86,7 @@ var commands = []command{
}
}
}
}, func(err error) {
}) {
}
})
for _, r := range tr {
if r.ShareCount > 0 || r.UncleCount > 0 {
@ -153,16 +149,12 @@ var commands = []command{
var yourWeight types.Difficulty
var totalWeight types.Difficulty
for range index.IterateSideBlocksInPPLNSWindow(e.Tip, e.Consensus, e.DifficultyFromHeight, e.SideBlockByTemplateId, e.SideBlockUnclesByTemplateId, func(b *index.SideBlock, weight types.Difficulty) {
e.IterateWindow(func(b *index.SideBlock, weight types.Difficulty) {
totalWeight = totalWeight.Add(weight)
if b.MinerAddress.Compare(addr) == 0 {
yourWeight = yourWeight.Add(weight)
}
}, func(err error) {
}) {
}
})
shareRatio := float64(yourWeight.Lo) / float64(totalWeight.Lo)
if shareRatio > notificationPoolShare { //warn about spammy notifications

View file

@ -58,6 +58,20 @@ func (c *channelEntry) SideBlockUnclesByTemplateId(id types.Hash) chan *index.Si
return result
}
// IterateWindow lock must be taken before this
func (c *channelEntry) IterateWindow(addWeightFunc index.SideBlockWindowAddWeightFunc) (bottomHeight uint64) {
if c.ApiEndpoint == "" {
return
}
for e := range index.IterateSideBlocksInPPLNSWindow(c.Tip, c.Consensus, c.DifficultyFromHeight, c.SideBlockByTemplateId, c.SideBlockUnclesByTemplateId, addWeightFunc, func(err error) {
}) {
bottomHeight = e.Block.SideHeight
}
return bottomHeight
}
func (c *channelEntry) SideBlockByTemplateId(id types.Hash) *index.SideBlock {
if b, ok := c.Window[id]; ok {
return b