From 217ceacc9517ceef164a187fe3ae56f941684427 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 4 Oct 2023 22:41:30 -0500 Subject: [PATCH] pool: Calculate share weights dynamically. This calculates the share weights in the global share weights map dynamically based on the entires in the map of the miner hash rates instead of hard-coding them to manually calculated values that can easily become outdated when the hash rates are updated. --- pool/share.go | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/pool/share.go b/pool/share.go index f4606abf..0acdebfe 100644 --- a/pool/share.go +++ b/pool/share.go @@ -13,13 +13,39 @@ import ( ) // ShareWeights represents 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: // -// (Hash of Miner X * Weight of LHM)/ Hash of LHM -var ShareWeights = map[string]*big.Rat{ - CPU: new(big.Rat).SetFloat64(1.0), // Reserved for testing. -} +// The weights are calculated as: +// +// Hash of Miner X / Hash of LHM +var ShareWeights = func() map[string]*big.Rat { + // In practice there will always be at least the CPU miner, however, be safe + // and return an empty map if the miner hashes somehow ends up empty in the + // future. + if len(minerHashes) == 0 { + return make(map[string]*big.Rat, len(minerHashes)) + } + + // Find the lowest hash rate miner. + lowestHashRate := big.NewInt(0) + for _, hashRate := range minerHashes { + if lowestHashRate.Sign() == 0 || hashRate.Cmp(lowestHashRate) < 0 { + lowestHashRate.Set(hashRate) + } + } + + shareWeights := make(map[string]*big.Rat, len(minerHashes)) + for miner, hashRate := range minerHashes { + // Ensure CPU is always a share weight of 1.0 as it is only valid in + // either testing scenarios or a CPU-only mining regime. + if miner == CPU { + shareWeights[CPU] = new(big.Rat).SetFloat64(1.0) + continue + } + + shareWeights[miner] = new(big.Rat).SetFrac(hashRate, lowestHashRate) + } + return shareWeights +}() // shareID generates a unique share id using the provided account, creation // time, and random uint64.