Optimize web /miners, optimize Hash / Bytes / Difficulty JSON encoding
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DataHoarder 2023-05-27 15:30:03 +02:00
parent d577856081
commit c103c46561
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
5 changed files with 33 additions and 18 deletions

View file

@ -81,11 +81,7 @@ type MinersPageMinerEntry struct {
{% for i, m := range p.Miners %}
<tr style="padding-bottom: 10px; border-bottom: solid #aaa 1px">
<td style="vertical-align: middle">{%d i+1 %}</td>
{% if m.Alias != "" %}
<th title="{%s m.Address.ToBase58() %} ({%s m.Alias %})" class="mono small"><a href="/miner/{%s m.Address.ToBase58() %}">{%s shorten(m.Alias, 10) %}</a></th>
{% else %}
<th title="{%s m.Address.ToBase58() %}" class="mono small" style="vertical-align: middle"><a href="/miner/{%s m.Address.ToBase58() %}">{%s shorten(m.Address.ToBase58(), 10) %}</a></th>
{% endif %}
{%= TemplateRowMinerWithTag(m.Address, m.Alias, "th") %}
<td style="vertical-align: middle">{%s software_info(m.SoftwareId, m.SoftwareVersion) %}</td>
{% code minerRatio := float64(m.Weight.Lo) / float64(p.WindowWeight.Lo) %}
<td style="vertical-align: middle">{%f.3 minerRatio*100 %}%</td>

View file

@ -1,10 +1,14 @@
{% import "git.gammaspectra.live/P2Pool/p2pool-observer/monero/address" %}
{% func TemplateRowMiner(addr *address.Address, alias string) %}
{%= TemplateRowMinerWithTag(addr, alias, "td") %}
{% endfunc %}
{% func TemplateRowMinerWithTag(addr *address.Address, alias string, tag string) %}
{% code encodedMinerAddress := addr.ToBase58() %}
{% if alias != "" %}
<td title="{%s encodedMinerAddress %} ({%s alias %})" class="mono small"><a href="/miner/{%s encodedMinerAddress %}">{%s shorten(alias, 10) %}</a></td>
<{%s tag %} title="{%s encodedMinerAddress %} ({%s alias %})" class="mono small"><a href="/miner/{%s encodedMinerAddress %}">{%s shorten(alias, 10) %}</a></{%s tag %}>
{% else %}
<td title="{%s encodedMinerAddress %}" class="mono small"><a href="/miner/{%s encodedMinerAddress %}">{%s shorten(encodedMinerAddress, 10) %}</a></td>
<{%s tag %} title="{%s encodedMinerAddress %}" class="mono small"><a href="/miner/{%s encodedMinerAddress %}">{%s shorten(encodedMinerAddress, 10) %}</a></{%s tag %}>
{% endif %}
{% endfunc %}

View file

@ -663,7 +663,8 @@ func main() {
}
var totalWeight types.Difficulty
for _, share := range shares {
var uncleShareIndex int
for i, share := range shares {
miner := share.Miner
if share.IsUncle() {
@ -676,10 +677,8 @@ func main() {
unclePenalty := types.DifficultyFrom64(share.Difficulty).Mul64(consensus.UnclePenalty).Div64(100)
uncleWeight := share.Difficulty - unclePenalty.Lo
if i := slices.IndexFunc(shares, func(block *index.SideBlock) bool {
return block.TemplateId == share.UncleOf
}); i != -1 {
parent := shares[i]
if shares[uncleShareIndex].TemplateId == share.UncleOf {
parent := shares[uncleShareIndex]
createMiner(parent.Miner, parent)
miners[parent.Miner].Weight = miners[parent.Miner].Weight.Add64(unclePenalty.Lo)
}
@ -687,6 +686,7 @@ func main() {
totalWeight = totalWeight.Add64(share.Difficulty)
} else {
uncleShareIndex = i
createMiner(share.Miner, share)
miners[miner].Shares.Add(int(int64(tip.SideHeight)-int64(share.SideHeight)), 1)
miners[miner].Weight = miners[miner].Weight.Add64(share.Difficulty)

View file

@ -192,7 +192,14 @@ func (d Difficulty) Big() *big.Int {
}
func (d Difficulty) MarshalJSON() ([]byte, error) {
return utils.MarshalJSON(d.String())
var encodeBuf [DifficultySize]byte
d.PutBytesBE(encodeBuf[:])
var buf [DifficultySize*2 + 2]byte
buf[0] = '"'
buf[DifficultySize*2+1] = '"'
hex.Encode(buf[1:], encodeBuf[:])
return buf[:], nil
}
func MustDifficultyFromString(s string) Difficulty {
@ -255,9 +262,9 @@ func (d *Difficulty) UnmarshalJSON(b []byte) error {
}
func (d Difficulty) Bytes() []byte {
buf := make([]byte, DifficultySize)
d.PutBytesBE(buf)
return buf
var buf [DifficultySize]byte
d.PutBytesBE(buf[:])
return buf[:]
}
func (d Difficulty) String() string {

View file

@ -17,7 +17,11 @@ type Hash [HashSize]byte
var ZeroHash Hash
func (h Hash) MarshalJSON() ([]byte, error) {
return utils.MarshalJSON(h.String())
var buf [HashSize*2 + 2]byte
buf[0] = '"'
buf[HashSize*2+1] = '"'
hex.Encode(buf[1:], h[:])
return buf[:], nil
}
func MustHashFromString(s string) Hash {
@ -140,7 +144,11 @@ func (h *Hash) UnmarshalJSON(b []byte) error {
type Bytes []byte
func (b Bytes) MarshalJSON() ([]byte, error) {
return utils.MarshalJSON(b.String())
buf := make([]byte, len(b)*2+2)
buf[0] = '"'
buf[len(buf)-1] = '"'
hex.Encode(buf[1:], b)
return buf, nil
}
func (b Bytes) String() string {
return hex.EncodeToString(b)