Skip to content

Commit

Permalink
multi: Use constant vars for hard-coded strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
davecgh committed Sep 14, 2023
1 parent 2392120 commit 605c006
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 66 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func fileExists(name string) bool {

// genCertPair generates a key/cert pair to the paths provided.
func genCertPair(certFile, keyFile string) error {
org := "dcrpool autogenerated cert"
const org = "dcrpool autogenerated cert"
validUntil := time.Now().Add(10 * 365 * 24 * time.Hour)
cert, key, err := certgen.NewTLSCertPair(elliptic.P256(), org,
validUntil, nil)
Expand Down
39 changes: 18 additions & 21 deletions pool/chainstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ func testChainState(t *testing.T) {
var minedHeader wire.BlockHeader

// Create mined work header.
headerE := "07000000ff7d6ee2e7380b94e6215f933f55649a12f1f21da4cf" +
"9601e90946eeb46f000066f27e7f98656bc19195a0a6d3a93d0d774b2e5" +
"83f49f20f6fef11b38443e21a05bad23ac3f14278f0ad74a86ce08ca44d" +
"05e0e2b0cd3bc91066904c311f482e01000000000000000000000000000" +
"0004fa83b20204e0000000000002a000000a50300004348fa5db0350100" +
"e7879320000000000000000000000000000000000000000000000000000" +
"0000000000000"
const headerE = "07000000ff7d6ee2e7380b94e6215f933f55649a12f1f21da4cf9601" +
"e90946eeb46f000066f27e7f98656bc19195a0a6d3a93d0d774b2e583f49f20f6fef" +
"11b38443e21a05bad23ac3f14278f0ad74a86ce08ca44d05e0e2b0cd3bc91066904c" +
"311f482e010000000000000000000000000000004fa83b20204e0000000000002a00" +
"0000a50300004348fa5db0350100e787932000000000000000000000000000000000" +
"00000000000000000000000000000000"
headerB, err := hex.DecodeString(headerE)
if err != nil {
t.Fatalf("unexpected encoding error %v", err)
Expand Down Expand Up @@ -244,13 +243,12 @@ func testChainState(t *testing.T) {
}

// Create associated job of the work to be confirmed.
workE := "07000000ff7d6ee2e7380b94e6215f933f55649a12f1f21da4cf" +
"9601e90946eeb46f000066f27e7f98656bc19195a0a6d3a93d0d774b2e5" +
"83f49f20f6fef11b38443e21a05bad23ac3f14278f0ad74a86ce08ca44d" +
"05e0e2b0cd3bc91066904c311f482e01000000000000000000000000000" +
"0004fa83b20204e0000000000002a000000a50300004348fa5d00000000" +
"00000000000000000000000000000000000000000000000000000000000" +
"00000000000008000000100000000000005a0"
const workE = "07000000ff7d6ee2e7380b94e6215f933f55649a12f1f21da4cf9601e9" +
"0946eeb46f000066f27e7f98656bc19195a0a6d3a93d0d774b2e583f49f20f6fef11" +
"b38443e21a05bad23ac3f14278f0ad74a86ce08ca44d05e0e2b0cd3bc91066904c31" +
"1f482e010000000000000000000000000000004fa83b20204e0000000000002a0000" +
"00a50300004348fa5d00000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000008000000100000000000005a0"
job := NewJob(workE, 42)
err = cs.cfg.db.persistJob(job)
if err != nil {
Expand All @@ -260,13 +258,12 @@ func testChainState(t *testing.T) {

// Ensure a malformed connected block does not terminate the chain
// state process.
headerME := "0700000083127dbbb05f24bd248798e1b912417f137cf2bb60c7f" +
"687db132e60797900007d79305d0f130f1371fe8e6a7d8aed721e969d45" +
"e94ecbffe229c85e3fea8d2b8aba150b44486aa58f4c01a574e6a6c8657" +
"66b3d3554fcc31d7061741e424158010000000000000000000a00000000" +
"004fa83b20204e0000000000002b0000003a0f00004548fa5d70230000e" +
"78793200000000000000000000000000000000000000000000000000000" +
"0000000000"
const headerME = "0700000083127dbbb05f24bd248798e1b912417f137cf2bb60c7f68" +
"7db132e60797900007d79305d0f130f1371fe8e6a7d8aed721e969d45e94ecbffe22" +
"9c85e3fea8d2b8aba150b44486aa58f4c01a574e6a6c865766b3d3554fcc31d70617" +
"41e424158010000000000000000000a00000000004fa83b20204e0000000000002b0" +
"000003a0f00004548fa5d70230000e78793200000000000000000000000000000000" +
"0000000000000000000000000000000"
headerMB, err := hex.DecodeString(headerME)
if err != nil {
t.Fatalf("unexpected encoding error %v", err)
Expand Down
6 changes: 3 additions & 3 deletions pool/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (c *Client) shutdown() {
// serves as proof of verifiable work contributed to the mining pool.
func (c *Client) claimWeightedShare() error {
if c.cfg.SoloPool {
desc := "cannot claim shares in solo pool mode"
const desc = "cannot claim shares in solo pool mode"
return errs.PoolError(errs.ClaimShare, desc)
}

Expand All @@ -205,7 +205,7 @@ func (c *Client) claimWeightedShare() error {
c.mtx.RUnlock()

if c.cfg.ActiveNet.Name == chaincfg.MainNetParams().Name && miner == CPU {
desc := "cannot claim shares for cpu miners on mainnet, " +
const desc = "cannot claim shares for cpu miners on mainnet, " +
"reserved for testing purposes only (simnet, testnet)"
return errs.PoolError(errs.ClaimShare, desc)
}
Expand Down Expand Up @@ -1040,7 +1040,7 @@ func (c *Client) handleWhatsminerD1Work(req *Request) {

// handleCPUWork prepares work for the cpu miner.
func (c *Client) handleCPUWork(req *Request) {
miner := "CPU"
const miner = "CPU"
err := c.encoder.Encode(req)
if err != nil {
log.Errorf("%s: work encoding error: %v", miner, err)
Expand Down
28 changes: 12 additions & 16 deletions pool/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,12 @@ func testClientMessageHandling(t *testing.T) {
t.Fatalf("expected a subscribed mining client")
}

workE := "07000000022b580ca96146e9c85fa1ee2ec02e0e2579a" +
"f4e3881fc619ec52d64d83e0000bd646e312ff574bc90e08ed91f1" +
"d99a85b318cb4464f2a24f9ad2bf3b9881c2bc9c344adde75e89b1" +
"4b627acce606e6d652915bdb71dcf5351e8ad6128faab9e0100000" +
"00000000000000000000000003e133920204e00000000000029000" +
"000a6030000954cee5d00000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000800000010" +
"0000000000005a0"
const workE = "07000000022b580ca96146e9c85fa1ee2ec02e0e2579af4e3881fc619e" +
"c52d64d83e0000bd646e312ff574bc90e08ed91f1d99a85b318cb4464f2a24f9ad2b" +
"f3b9881c2bc9c344adde75e89b14b627acce606e6d652915bdb71dcf5351e8ad6128" +
"faab9e010000000000000000000000000000003e133920204e000000000000290000" +
"00a6030000954cee5d00000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000008000000100000000000005a0"
job := NewJob(workE, 41)
err = client.cfg.db.persistJob(job)
if err != nil {
Expand Down Expand Up @@ -1512,14 +1510,12 @@ func testClientTimeRolledWork(t *testing.T) {
t.Fatalf("expected a subscribed mining client")
}

workE := "07000000022b580ca96146e9c85fa1ee2ec02e0e2579a" +
"f4e3881fc619ec52d64d83e0000bd646e312ff574bc90e08ed91f1" +
"d99a85b318cb4464f2a24f9ad2bf3b9881c2bc9c344adde75e89b1" +
"4b627acce606e6d652915bdb71dcf5351e8ad6128faab9e0100000" +
"00000000000000000000000003e133920204e00000000000029000" +
"000a6030000954cee5d00000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000800000010" +
"0000000000005a0"
const workE = "07000000022b580ca96146e9c85fa1ee2ec02e0e2579af4e3881fc619e" +
"c52d64d83e0000bd646e312ff574bc90e08ed91f1d99a85b318cb4464f2a24f9ad2b" +
"f3b9881c2bc9c344adde75e89b14b627acce606e6d652915bdb71dcf5351e8ad6128" +
"faab9e010000000000000000000000000000003e133920204e000000000000290000" +
"00a6030000954cee5d00000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000008000000100000000000005a0"

// Trigger time-rolled work updates to the CPU client.
setCurrentWork(workE)
Expand Down
13 changes: 6 additions & 7 deletions pool/hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,12 @@ func testHub(t *testing.T) {
hub.endpoint.clientsMtx.Unlock()

// Ensure the hub can process and distribute work to connected clients.
workE := "07000000ddb9fb70cb6ed184f57bfb94abebe7e7b9819e27d6e3ca8" +
"19f1f73c7218100007de69dd9365ba5a39178870780d78d86aa6d53a649a5" +
"4bd65faac4be8123253e7f98f31055b0f3e94dd48e67f43742b028623192d" +
"d684d053d6681759c8ebfa70100000000000000000000003c00000045bc4d" +
"20204e00000000000039000000b3060000a912825e0000000000000000000" +
"0000000000000000000000000000000000000000000000000000000000000" +
"8000000100000000000005a0"
const workE = "07000000ddb9fb70cb6ed184f57bfb94abebe7e7b9819e27d6e3ca819f" +
"1f73c7218100007de69dd9365ba5a39178870780d78d86aa6d53a649a54bd65faac4" +
"be8123253e7f98f31055b0f3e94dd48e67f43742b028623192dd684d053d6681759c" +
"8ebfa70100000000000000000000003c00000045bc4d20204e000000000000390000" +
"00b3060000a912825e00000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000008000000100000000000005a0"
hub.processWork(workE)

// Get a block and publish a bogus transaction by confirming the
Expand Down
14 changes: 7 additions & 7 deletions pool/paymentmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (pm *PaymentMgr) PPLNSSharePercentages() (map[string]*big.Rat, error) {
// calculatePayments creates the payments due participating accounts.
func (pm *PaymentMgr) calculatePayments(ratios map[string]*big.Rat, source *PaymentSource,
total dcrutil.Amount, poolFee float64, height uint32, estMaturity uint32) ([]*Payment, int64, error) {
funcName := "calculatePayments"
const funcName = "calculatePayments"
if len(ratios) == 0 {
desc := fmt.Sprintf("%s: valid share ratios required to "+
"generate payments", funcName)
Expand Down Expand Up @@ -443,7 +443,7 @@ func (pm *PaymentMgr) pruneOrphanedPayments(ctx context.Context, pmts map[string
// pool fees.
func (pm *PaymentMgr) applyTxFees(inputs []chainjson.TransactionInput, outputs map[string]dcrutil.Amount,
tOut dcrutil.Amount, feeAddr stdaddr.Address) (dcrutil.Amount, dcrutil.Amount, error) {
funcName := "applyTxFees"
const funcName = "applyTxFees"
if len(inputs) == 0 {
desc := fmt.Sprintf("%s: cannot create a payout transaction "+
"without a tx input", funcName)
Expand Down Expand Up @@ -495,7 +495,7 @@ func (pm *PaymentMgr) applyTxFees(inputs []chainjson.TransactionInput, outputs m
// The context passed to this function must have a corresponding
// cancellation to allow for a clean shutdown process.
func (pm *PaymentMgr) confirmCoinbases(ctx context.Context, txB TxBroadcaster, txHashes map[chainhash.Hash]uint32) error {
funcName := "confirmCoinbases"
const = "confirmCoinbases"

Check failure on line 498 in pool/paymentmgr.go

View workflow job for this annotation

GitHub Actions / Go CI (1.19)

syntax error: unexpected =, expecting name

Check failure on line 498 in pool/paymentmgr.go

View workflow job for this annotation

GitHub Actions / Go CI (1.20)

syntax error: unexpected =, expected name
maxSpendableConfs := int32(pm.cfg.ActiveNet.CoinbaseMaturity) + 1

keys := make([]chainhash.Hash, 0, len(txHashes))
Expand Down Expand Up @@ -552,7 +552,7 @@ func (pm *PaymentMgr) confirmCoinbases(ctx context.Context, txB TxBroadcaster, t
// It will return when either a response or error is received from the
// provided rescan source, or when the provided context is cancelled.
func fetchRescanResponse(ctx context.Context, rescanSource func() (*walletrpc.RescanResponse, error)) (*walletrpc.RescanResponse, error) {
funcName := "fetchRescanResponse"
const = "fetchRescanResponse"

Check failure on line 555 in pool/paymentmgr.go

View workflow job for this annotation

GitHub Actions / Go CI (1.19)

syntax error: unexpected =, expecting name

Check failure on line 555 in pool/paymentmgr.go

View workflow job for this annotation

GitHub Actions / Go CI (1.20)

syntax error: unexpected =, expected name
respCh := make(chan *rescanMsg)
go func(ch chan *rescanMsg) {
resp, err := rescanSource()
Expand Down Expand Up @@ -582,7 +582,7 @@ func fetchRescanResponse(ctx context.Context, rescanSource func() (*walletrpc.Re
// The context passed to this function must have a corresponding
// cancellation to allow for a clean shutdown process.
func (pm *PaymentMgr) monitorRescan(ctx context.Context, rescanSource walletrpc.WalletService_RescanClient, height int32) error {
funcName := "monitorRescan"
const funcName = "monitorRescan"
for {
resp, err := fetchRescanResponse(ctx, rescanSource.Recv)
if err != nil {
Expand All @@ -605,7 +605,7 @@ func (pm *PaymentMgr) monitorRescan(ctx context.Context, rescanSource walletrpc.
// from the provided payments
func (pm *PaymentMgr) generatePayoutTxDetails(ctx context.Context, txC TxCreator, feeAddr stdaddr.Address, payments map[string][]*Payment, treasuryActive bool) ([]chainjson.TransactionInput,
map[chainhash.Hash]uint32, map[string]dcrutil.Amount, dcrutil.Amount, error) {
funcName := "generatePayoutTxDetails"
const funcName = "generatePayoutTxDetails"

// The coinbase output prior to
// [DCP0006](https://github.com/decred/dcps/pull/17)
Expand Down Expand Up @@ -715,7 +715,7 @@ func (pm *PaymentMgr) generatePayoutTxDetails(ctx context.Context, txC TxCreator

// PayDividends pays mature mining rewards to participating accounts.
func (pm *PaymentMgr) payDividends(ctx context.Context, height uint32, treasuryActive bool) error {
funcName := "payDividends"
const funcName = "payDividends"
mPmts, err := pm.cfg.db.maturePendingPayments(height)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions pool/paymentmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ func testPaymentMgrPayment(t *testing.T) {
}

// Create an invalid block hash / payment set entry.
invalidBlockHash := "0123456789012345678901234567890123456789" +
"0123456789012345678912345"
const invalidBlockHash = "01234567890123456789012345678901234567890123456" +
"789012345678912345"
mPmts[invalidBlockHash] = []*Payment{pmtB}

mgr.cfg.GetBlockConfirmations = func(ctx context.Context, bh *chainhash.Hash) (int64, error) {
Expand Down Expand Up @@ -753,7 +753,7 @@ func testPaymentMgrPayment(t *testing.T) {

// Ensure generating payout tx details returns an error if an account
// referenced by a payment cannot be found.
unknownID := "abcd"
const unknownID = "abcd"
unknownIDCoinbase := chainhash.Hash{'u'}
pmtD := NewPayment(unknownID, randSource, amt, height, estMaturity)
mPmts[unknownIDCoinbase.String()] = []*Payment{pmtD}
Expand Down
14 changes: 8 additions & 6 deletions pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ var (
)

func setupPostgresDB() (*PostgresDB, error) {
pgHost := "127.0.0.1"
pgPort := uint32(5432)
pgUser := "dcrpooluser"
pgPass := "12345"
pgDBName := "dcrpooltestdb"
purgeDB := false
const (
pgHost = "127.0.0.1"
pgPort = uint32(5432)
pgUser = "dcrpooluser"
pgPass = "12345"
pgDBName = "dcrpooltestdb"
purgeDB = false
)
return InitPostgresDB(pgHost, pgPort, pgUser, pgPass, pgDBName, purgeDB)
}

Expand Down
4 changes: 2 additions & 2 deletions pool/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func InitPostgresDB(host string, port uint32, user, pass, dbName string, purgeDB

// Close closes the postgres database connection.
func (db *PostgresDB) Close() error {
funcName := "Close"
const funcName = "Close"
err := db.DB.Close()
if err != nil {
desc := fmt.Sprintf("%s: unable to close db: %v", funcName, err)
Expand All @@ -109,7 +109,7 @@ func (db *PostgresDB) Close() error {
// purge wipes all persisted data. This is intended for use with testnet and
// simnet testing purposes only.
func (db *PostgresDB) purge() error {
funcName := "purge"
const funcName = "purge"
_, err := db.DB.Exec(purgeDB)
if err != nil {
desc := fmt.Sprintf("%s: unable to purge db: %v", funcName, err)
Expand Down

0 comments on commit 605c006

Please sign in to comment.