Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: Use constant vars for hard-coded strings. #363

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,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
56 changes: 26 additions & 30 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 @@ -247,13 +246,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 @@ -263,13 +261,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 All @@ -283,14 +280,13 @@ func testChainState(t *testing.T) {
<-malformedMsg.Done

// Create confirmation block header.
headerE = "0700000083127dbbb05f24bd248798e1b912417f137cf2bb60c7f" +
"687db132e60797900007d79305d0f130f1371fe8e6a7d8aed721e969d45" +
"e94ecbffe229c85e3fea8d2b8aba150b44486aa58f4c01a574e6a6c8657" +
"66b3d3554fcc31d7061741e424158010000000000000000000a00000000" +
"004fa83b20204e0000000000002b0000003a0f00004548fa5d70230000e" +
"78793200000000000000000000000000000000000000000000000000000" +
"000000000000"
headerB, err = hex.DecodeString(headerE)
const headerE2 = "0700000083127dbbb05f24bd248798e1b912417f137cf2bb60c7f68" +
"7db132e60797900007d79305d0f130f1371fe8e6a7d8aed721e969d45e94ecbffe22" +
"9c85e3fea8d2b8aba150b44486aa58f4c01a574e6a6c865766b3d3554fcc31d70617" +
"41e424158010000000000000000000a00000000004fa83b20204e0000000000002b0" +
"000003a0f00004548fa5d70230000e78793200000000000000000000000000000000" +
"000000000000000000000000000000000"
headerB, err = hex.DecodeString(headerE2)
if err != nil {
t.Fatalf("unexpected encoding error %v", err)
}
Expand Down Expand Up @@ -383,7 +379,7 @@ func testChainState(t *testing.T) {

// Ensure the current work can be updated.
initialCurrentWork := cs.fetchCurrentWork()
updatedCurrentWork := headerE
updatedCurrentWork := headerE2
cs.setCurrentWork(updatedCurrentWork)
currentWork := cs.fetchCurrentWork()
if currentWork == initialCurrentWork {
Expand Down
8 changes: 4 additions & 4 deletions pool/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type ClientConfig struct {
// RemoveClient removes the client from the pool.
RemoveClient func(*Client)
// SubmitWork sends solved block data to the consensus daemon.
SubmitWork func(context.Context, *string) (bool, error)
SubmitWork func(context.Context, string) (bool, error)
// FetchCurrentWork returns the current work of the pool.
FetchCurrentWork func() string
// WithinLimit returns if the client is still within its request limits.
Expand Down Expand Up @@ -200,7 +200,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 Down Expand Up @@ -622,7 +622,7 @@ func (c *Client) handleSubmitWorkRequest(ctx context.Context, req *Request, allo
copy(submissionB[wire.MaxBlockHeaderPayload:],
c.cfg.Blake256Pad)
submission := hex.EncodeToString(submissionB)
accepted, err := c.cfg.SubmitWork(ctx, &submission)
accepted, err := c.cfg.SubmitWork(ctx, submission)
if err != nil {
sErr := NewStratumError(Unknown, err)
resp := SubmitWorkResponse(*req.ID, false, sErr)
Expand Down Expand Up @@ -1042,7 +1042,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
53 changes: 24 additions & 29 deletions pool/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var (
// Do Nothing.
},
RemoveClient: func(c *Client) {},
SubmitWork: func(_ context.Context, submission *string) (bool, error) {
SubmitWork: func(_ context.Context, submission string) (bool, error) {
return false, nil
},
FetchCurrentWork: func() string {
Expand Down Expand Up @@ -653,14 +653,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 @@ -963,14 +961,13 @@ func testClientMessageHandling(t *testing.T) {

// Ensure a CPU client receives an error response when
// submitting work referencing a non-existent job.
workE = "07000000e2bb3110848ec197118e8df2a3bc85dcaf5a787008a9c70721" +
"09dfb25e0a000047fe98e377430404709f8045ebf14b3a1903237c2adb49ed55" +
"72412eb2e0ca3c8ad3ffc23e946e1cce2dca67e2f711a78f41003358630b7923" +
"1f0af3311bd73c010000000000000000000a000000000064ad2620204e000000" +
"0000002e0000003b0f000005ec705e0000000000000000000000000000000000" +
"0000000000000000000000000000000000000000000000800000010000000000" +
"0005a0"
job = NewJob(workE, 46)
const workE2 = "07000000e2bb3110848ec197118e8df2a3bc85dcaf5a787008a9c7072" +
"109dfb25e0a000047fe98e377430404709f8045ebf14b3a1903237c2adb49ed55724" +
"12eb2e0ca3c8ad3ffc23e946e1cce2dca67e2f711a78f41003358630b79231f0af33" +
"11bd73c010000000000000000000a000000000064ad2620204e0000000000002e000" +
"0003b0f000005ec705e0000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000008000000100000000000005a0"
job = NewJob(workE2, 46)
err = client.cfg.db.persistJob(job)
if err != nil {
t.Fatalf("failed to persist job %v", err)
Expand Down Expand Up @@ -1017,7 +1014,7 @@ func testClientMessageHandling(t *testing.T) {
if err != nil {
t.Fatalf("unexpected set miner error: %v", err)
}
client.cfg.SubmitWork = func(_ context.Context, submission *string) (bool, error) {
client.cfg.SubmitWork = func(_ context.Context, submission string) (bool, error) {
return false, fmt.Errorf("unable to submit work")
}
id++
Expand Down Expand Up @@ -1048,11 +1045,11 @@ func testClientMessageHandling(t *testing.T) {
if resp.Error == nil {
t.Fatalf("expected a submit work error")
}
client.cfg.SubmitWork = func(_ context.Context, submission *string) (bool, error) {
client.cfg.SubmitWork = func(_ context.Context, submission string) (bool, error) {
return true, nil
}

setCurrentWork(workE)
setCurrentWork(workE2)

// Ensure a CPU client receives a non-error response when
// submitting valid work.
Expand Down Expand Up @@ -1115,7 +1112,7 @@ func testClientMessageHandling(t *testing.T) {
if resp.Error == nil {
t.Fatal("expected a work exists work submission error")
}
client.cfg.SubmitWork = func(_ context.Context, submission *string) (bool, error) {
client.cfg.SubmitWork = func(_ context.Context, submission string) (bool, error) {
return false, nil
}

Expand Down Expand Up @@ -1511,14 +1508,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
2 changes: 1 addition & 1 deletion pool/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type EndpointConfig struct {
// provided miner if it exists.
FetchMinerDifficulty func(string) (*DifficultyInfo, error)
// SubmitWork sends solved block data to the consensus daemon.
SubmitWork func(context.Context, *string) (bool, error)
SubmitWork func(context.Context, string) (bool, error)
// FetchCurrentWork returns the current work of the pool.
FetchCurrentWork func() string
// WithinLimit returns if a client is within its request limits.
Expand Down
2 changes: 1 addition & 1 deletion pool/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func testEndpoint(t *testing.T) {
FetchMinerDifficulty: func(miner string) (*DifficultyInfo, error) {
return poolDiffs.fetchMinerDifficulty(miner)
},
SubmitWork: func(_ context.Context, submission *string) (bool, error) {
SubmitWork: func(_ context.Context, submission string) (bool, error) {
return false, nil
},
FetchCurrentWork: func() string {
Expand Down
4 changes: 2 additions & 2 deletions pool/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,12 @@ func (h *Hub) Connect(ctx context.Context) error {
}

// submitWork sends solved block data to the consensus daemon for evaluation.
func (h *Hub) submitWork(ctx context.Context, data *string) (bool, error) {
func (h *Hub) submitWork(ctx context.Context, data string) (bool, error) {
if h.nodeConn == nil {
return false, errs.PoolError(errs.Disconnected, "node disconnected")
}

return h.nodeConn.GetWorkSubmit(ctx, *data)
return h.nodeConn.GetWorkSubmit(ctx, data)
}

// getWork fetches available work from the consensus daemon.
Expand Down
15 changes: 7 additions & 8 deletions pool/hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,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 All @@ -508,7 +507,7 @@ func testHub(t *testing.T) {
<-confNotif.Done

// Ensure the hub can process submitted work.
_, err = hub.submitWork(ctx, &workE)
_, err = hub.submitWork(ctx, workE)
if err != nil {
t.Fatalf("unexpected submit work error: %v", err)
}
Expand Down
14 changes: 7 additions & 7 deletions pool/paymentmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,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 @@ -441,7 +441,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 @@ -493,7 +493,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 funcName = "confirmCoinbases"
maxSpendableConfs := int32(pm.cfg.ActiveNet.CoinbaseMaturity) + 1

keys := make([]chainhash.Hash, 0, len(txHashes))
Expand Down Expand Up @@ -550,7 +550,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 funcName = "fetchRescanResponse"
respCh := make(chan *rescanMsg)
go func(ch chan *rescanMsg) {
resp, err := rescanSource()
Expand Down Expand Up @@ -580,7 +580,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 @@ -603,7 +603,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 @@ -713,7 +713,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 @@ -522,8 +522,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 @@ -752,7 +752,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
Loading
Loading