New utilities and additions for types / sidechain
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
DataHoarder 2023-05-30 08:49:52 +02:00
parent f0d81343cc
commit cd99095931
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
5 changed files with 55 additions and 2 deletions

View file

@ -234,7 +234,13 @@ func getServerMux(instance *p2pool.P2Pool) *mux.Router {
}
})
serveMux.HandleFunc("/mainchain/tip", func(writer http.ResponseWriter, request *http.Request) {
result := instance.GetMinimalBlockHeaderByHeight(instance.GetMinerDataTip().Height - 1)
minerData := instance.GetMinerDataTip()
if minerData == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)
return
}
result := instance.GetMinimalBlockHeaderByHeight(minerData.Height - 1)
if result == nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusNotFound)

View file

@ -539,7 +539,7 @@ func (s *Server) Listen() (err error) {
return errors.New("peer is IPv6 but we do not allow it")
}
if ok, b := s.IsBanned(netip.MustParseAddrPort(conn.RemoteAddr().String()).Addr()); ok {
if ok, b := s.IsBanned(addr); ok {
return fmt.Errorf("peer is banned: %w", b.Error)
}
}

View file

@ -3,6 +3,7 @@ package sidechain
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero"
@ -338,6 +339,19 @@ var zeroFullId FullId
type FullId [FullIdSize]byte
func FullIdFromString(s string) (FullId, error) {
var h FullId
if buf, err := hex.DecodeString(s); err != nil {
return h, err
} else {
if len(buf) != FullIdSize {
return h, errors.New("wrong hash size")
}
copy(h[:], buf)
return h, nil
}
}
func (id FullId) TemplateId() (h types.Hash) {
return types.Hash(id[:types.HashSize])
}
@ -350,6 +364,10 @@ func (id FullId) ExtraNonce() uint32 {
return binary.LittleEndian.Uint32(id[types.HashSize+unsafe.Sizeof(uint32(0)):])
}
func (id FullId) String() string {
return hex.EncodeToString(id[:])
}
func (b *PoolBlock) CalculateFullId(consensus *Consensus) FullId {
var buf FullId
sidechainId := b.SideTemplateId(consensus)

View file

@ -2,6 +2,7 @@ package sidechain
import (
"bytes"
"context"
"errors"
"fmt"
"git.gammaspectra.live/P2Pool/p2pool-observer/monero"
@ -30,6 +31,7 @@ type Cache interface {
type P2PoolInterface interface {
ConsensusProvider
Cache
Context() context.Context
UpdateTip(tip *PoolBlock)
Broadcast(block *PoolBlock)
ClientRPC() *client.Client
@ -895,6 +897,10 @@ func (c *SideChain) calculateOutputs(block *PoolBlock) (outputs transaction.Outp
return CalculateOutputs(block, c.Consensus(), c.server.GetDifficultyByHeight, c.getPoolBlockByTemplateId, c.derivationCache, preAllocatedShares)
}
func (c *SideChain) Server() P2PoolInterface {
return c.server
}
func (c *SideChain) getShares(tip *PoolBlock, preAllocatedShares Shares) (shares Shares, bottomHeight uint64) {
return GetShares(tip, c.Consensus(), c.server.GetDifficultyByHeight, c.getPoolBlockByTemplateId, preAllocatedShares)
}

View file

@ -8,6 +8,7 @@ import (
"git.gammaspectra.live/P2Pool/p2pool-observer/utils"
"github.com/holiman/uint256"
"lukechampine.com/uint128"
"math"
"math/big"
"math/bits"
"strings"
@ -316,3 +317,25 @@ func DifficultyFromPoW(powHash Hash) Difficulty {
func (d Difficulty) CheckPoW(pow Hash) bool {
return DifficultyFromPoW(pow).Cmp(d) >= 0
}
// Target
// Finds a 64-bit target for mining (target = 2^64 / difficulty) and rounds up the result of division
// Because of that, there's a very small chance that miners will find a hash that meets the target but is still wrong (hash * difficulty >= 2^256)
// A proper difficulty check is in check_pow()
func (d Difficulty) Target() uint64 {
if d.Hi > 0 {
return 1
}
// Safeguard against division by zero (CPU will trigger it even if lo = 1 because result doesn't fit in 64 bits)
if d.Lo <= 1 {
return math.MaxUint64
}
q, rem := Difficulty{Hi: 1, Lo: 0}.QuoRem64(d.Lo)
if rem > 0 {
return q.Lo + 1
} else {
return q.Lo
}
}