Skip to content

Commit

Permalink
pool: Improve payment UUID with random value.
Browse files Browse the repository at this point in the history
The UUID used for the payment ID was not created with any randomness to
it, leading to occasional test failures due to duplicated values.
Appending a random value to the UUID helps to ensure uniqueness.

The commit also updates the database upgrade code to ensure the old
payment ID format is used when performing the old version upgrade.
  • Loading branch information
jholdstock committed Sep 26, 2023
1 parent 4b3828b commit 78e6177
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
9 changes: 7 additions & 2 deletions pool/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ package pool

import (
"context"
"crypto/rand"
crand "crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math/big"
mrand "math/rand"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -95,6 +96,10 @@ var (
// high value to reduce the number of round trips to the pool by connected
// pool clients since pool shares are a non factor in solo pool mode.
soloMaxGenTime = time.Second * 28

// uuidPRNG is a pseudo-random number generator used as a part of generating
// the UUIDs for payments and submitted shares.
uuidPRNG = mrand.New(mrand.NewSource(time.Now().UnixNano()))
)

// WalletConnection defines the functionality needed by a wallet
Expand Down Expand Up @@ -766,7 +771,7 @@ func (h *Hub) CSRFSecret() ([]byte, error) {
// If the database doesnt contain a CSRF secret, generate one and
// persist it.
secret = make([]byte, 32)
_, err = rand.Read(secret)
_, err = crand.Read(secret)
if err != nil {
return nil, err
}
Expand Down
11 changes: 8 additions & 3 deletions pool/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package pool

import (
"bytes"
"encoding/binary"
"encoding/hex"
"time"

Expand All @@ -32,12 +33,16 @@ type Payment struct {
Source *PaymentSource `json:"source"`
}

// paymentID generates a unique id using the provided payment details.
func paymentID(height uint32, createdOnNano int64, account string) string {
// paymentID generates a unique id using the provided payment details and a
// random uint64.
func paymentID(height uint32, createdOnNano int64, account string, randVal uint64) string {
var randValEncoded [8]byte
binary.BigEndian.PutUint64(randValEncoded[:], randVal)
var buf bytes.Buffer
_, _ = buf.WriteString(hex.EncodeToString(heightToBigEndianBytes(height)))
_, _ = buf.WriteString(hex.EncodeToString(nanoToBigEndianBytes(createdOnNano)))
_, _ = buf.WriteString(account)
_, _ = buf.WriteString(hex.EncodeToString(randValEncoded[:]))
return buf.String()
}

Expand All @@ -46,7 +51,7 @@ func NewPayment(account string, source *PaymentSource, amount dcrutil.Amount,
height uint32, estMaturity uint32) *Payment {
now := time.Now().UnixNano()
return &Payment{
UUID: paymentID(height, now, account),
UUID: paymentID(height, now, account, uuidPRNG.Uint64()),
Account: account,
Amount: amount,
Height: height,
Expand Down
5 changes: 0 additions & 5 deletions pool/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ import (
"encoding/binary"
"encoding/hex"
"math/big"
"math/rand"
"time"
)

// uuidPRNG is a pseudo-random number generator used as a part of generating the
// UUID for submitted shares.
var uuidPRNG = rand.New(rand.NewSource(time.Now().UnixNano()))

// ShareWeights reprsents the associated weights for each known DCR miner.
// With the share weight of the lowest hash DCR miner (LHM) being 1, the
// rest were calculated as :
Expand Down

0 comments on commit 78e6177

Please sign in to comment.