consensus/p2pool/sidechain/share.go
DataHoarder 4ef60296f1
Updated to Go 1.21
* Replaced exp/slices and exp/maps with slices/maps implementation
* Replaced utils.Min/Max with min/max
* Introduced GOEXPERIMENT=loopvar on build steps
* Updated tests/docker-compose to go1.21-rc-alpine
* Updated nginx to 1.25
* Preallocate mined blocks on Sidechain
* Update edwards25519 version
2023-07-20 07:40:18 +02:00

82 lines
1.6 KiB
Go

package sidechain
import (
"git.gammaspectra.live/P2Pool/p2pool-observer/monero/address"
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
"slices"
"sync"
)
type Shares []*Share
func (s Shares) Index(addr address.PackedAddress) int {
return slices.IndexFunc(s, func(share *Share) bool {
return share.Address.ComparePacked(addr) == 0
})
}
func (s Shares) Clone() (o Shares) {
o = make(Shares, len(s))
for i := range s {
o[i] = &Share{Address: s[i].Address, Weight: s[i].Weight}
}
return o
}
// Compact Removes dupe Share entries
func (s Shares) Compact() Shares {
// Sort shares based on address
slices.SortFunc(s, func(a *Share, b *Share) int {
return a.Address.ComparePacked(b.Address)
})
index := 0
for i, share := range s {
if i == 0 {
continue
}
if s[index].Address.ComparePacked(share.Address) == 0 {
s[index].Weight = s[index].Weight.Add(share.Weight)
} else {
index++
s[index].Address = share.Address
s[index].Weight = share.Weight
}
}
return s[:index+1]
}
type PreAllocatedSharesPool struct {
pool sync.Pool
}
func NewPreAllocatedSharesPool[T uint64 | int](n T) *PreAllocatedSharesPool {
p := &PreAllocatedSharesPool{}
p.pool.New = func() any {
return PreAllocateShares(n)
}
return p
}
func (p *PreAllocatedSharesPool) Get() Shares {
return p.pool.Get().(Shares)
}
func (p *PreAllocatedSharesPool) Put(s Shares) {
p.pool.Put(s)
}
func PreAllocateShares[T uint64 | int](n T) Shares {
preAllocatedShares := make(Shares, n)
for i := range preAllocatedShares {
preAllocatedShares[i] = &Share{}
}
return preAllocatedShares
}
type Share struct {
Weight types.Difficulty
Address address.PackedAddress
}