From e5a3d3dc660f61396ec12ea2b762696001296c39 Mon Sep 17 00:00:00 2001 From: Donald Adu-Poku Date: Tue, 15 Sep 2020 16:47:58 +0000 Subject: [PATCH] pool: resolve review issues (2 of x). --- pool/acceptedwork.go | 14 ++++---------- pool/acceptedwork_test.go | 12 +++--------- pool/chainstate_test.go | 11 +---------- pool/client.go | 24 ++++++++++++------------ pool/error.go | 4 ++-- pool/hub_test.go | 5 +---- pool/job.go | 4 ++-- pool/message.go | 16 ++++++++-------- pool/payment.go | 6 +++--- pool/share.go | 4 ++-- pool/share_test.go | 3 --- pool/upgrades_test.go | 7 ++++--- 12 files changed, 42 insertions(+), 68 deletions(-) diff --git a/pool/acceptedwork.go b/pool/acceptedwork.go index 05e208d8..4dfafb20 100644 --- a/pool/acceptedwork.go +++ b/pool/acceptedwork.go @@ -38,23 +38,17 @@ func heightToBigEndianBytes(height uint32) []byte { return b } -// bigEndianBytesToHeight returns the block height of the provided 4-byte big -// endian representation. -func bigEndianBytesToHeight(b []byte) uint32 { - return binary.BigEndian.Uint32(b[0:4]) -} - // AcceptedWorkID generates a unique id for work accepted by the network. func AcceptedWorkID(blockHash string, blockHeight uint32) []byte { var buf bytes.Buffer - buf.WriteString(hex.EncodeToString(heightToBigEndianBytes(blockHeight))) - buf.WriteString(blockHash) + _, _ = buf.WriteString(hex.EncodeToString(heightToBigEndianBytes(blockHeight))) + _, _ = buf.WriteString(blockHash) return buf.Bytes() } // NewAcceptedWork creates an accepted work. func NewAcceptedWork(blockHash string, prevHash string, height uint32, - minedBy string, miner string) (*AcceptedWork, error) { + minedBy string, miner string) *AcceptedWork { return &AcceptedWork{ UUID: string(AcceptedWorkID(blockHash, height)), BlockHash: blockHash, @@ -63,7 +57,7 @@ func NewAcceptedWork(blockHash string, prevHash string, height uint32, MinedBy: minedBy, Miner: miner, CreatedOn: time.Now().UnixNano(), - }, nil + } } // fetchWorkBucket is a helper function for getting the work bucket. diff --git a/pool/acceptedwork_test.go b/pool/acceptedwork_test.go index 09cbbe23..ffa1a6c7 100644 --- a/pool/acceptedwork_test.go +++ b/pool/acceptedwork_test.go @@ -10,11 +10,8 @@ import ( func persistAcceptedWork(db *bolt.DB, blockHash string, prevHash string, height uint32, minedBy string, miner string) (*AcceptedWork, error) { - acceptedWork, err := NewAcceptedWork(blockHash, prevHash, height, minedBy, miner) - if err != nil { - return nil, err - } - err = acceptedWork.Create(db) + acceptedWork := NewAcceptedWork(blockHash, prevHash, height, minedBy, miner) + err := acceptedWork.Create(db) if err != nil { return nil, err } @@ -85,13 +82,10 @@ func testAcceptedWork(t *testing.T, db *bolt.DB) { t.Fatal(err) } - workE, err := NewAcceptedWork( + workE := NewAcceptedWork( "0000000000000000032e25218be722327ae3dccf9015756facb2f98931fda7b8", "00000000000000000476712b2f5df31bc62b9976066262af2d639a551853c056", 431611, xID, "dcr1") - if err != nil { - t.Fatal(err) - } // Ensure updating a non persisted accepted work returns an error. err = workE.Update(db) diff --git a/pool/chainstate_test.go b/pool/chainstate_test.go index 55a41a75..db00b519 100644 --- a/pool/chainstate_test.go +++ b/pool/chainstate_test.go @@ -214,18 +214,12 @@ func testChainState(t *testing.T, db *bolt.DB) { } aID := paymentID(paymentA.Height, paymentA.CreatedOn, paymentA.Account) - if err != nil { - t.Fatalf("unexpected payment id err: %v", err) - } _, err = FetchPayment(db, aID) if err != nil { t.Fatalf("unexpected error fetching payment A: %v", err) } bID := paymentID(paymentB.Height, paymentB.CreatedOn, paymentB.Account) - if err != nil { - t.Fatalf("unexpected payment id err: %v", err) - } _, err = FetchPayment(db, bID) if err != nil { t.Fatalf("unexpected error fetching payment B: %v", err) @@ -264,13 +258,10 @@ func testChainState(t *testing.T, db *bolt.DB) { go cs.handleChainUpdates(ctx) // Create the accepted work to be confirmed. - work, err := NewAcceptedWork( + work := NewAcceptedWork( "00007979602e13db87f6c760bbf27c137f4112b9e1988724bd245fb0bb7d1283", "00006fb4ee4609e90196cfa41df2f1129a64553f935f21e6940b38e7e26e7dff", 42, xID, CPU) - if err != nil { - t.Fatalf("unexpected accepted work error: %v", err) - } err = work.Create(cs.cfg.DB) if err != nil { t.Fatalf("unable to persist accepted work %v", err) diff --git a/pool/client.go b/pool/client.go index 061d8be5..5da61b62 100644 --- a/pool/client.go +++ b/pool/client.go @@ -451,7 +451,7 @@ func (c *Client) handleSubmitWorkRequest(ctx context.Context, req *Request, allo // Create accepted work if the work submission is accepted // by the mining node. - work, err := NewAcceptedWork(hash.String(), header.PrevBlock.String(), + work := NewAcceptedWork(hash.String(), header.PrevBlock.String(), header.Height, c.account, c.cfg.FetchMiner()) if err != nil { sErr := NewStratumError(Unknown, err) @@ -582,9 +582,9 @@ func (c *Client) updateWork() { binary.LittleEndian.PutUint32(b, now) timestampE := hex.EncodeToString(b) var buf bytes.Buffer - buf.WriteString(currWorkE[:272]) - buf.WriteString(timestampE) - buf.WriteString(currWorkE[280:]) + _, _ = buf.WriteString(currWorkE[:272]) + _, _ = buf.WriteString(timestampE) + _, _ = buf.WriteString(currWorkE[280:]) updatedWorkE := buf.String() blockVersion := updatedWorkE[:8] @@ -701,12 +701,12 @@ func (c *Client) process() { // reversePrevBlockWords reverses each 4-byte word in the provided hex encoded // previous block hash. func reversePrevBlockWords(hashE string) string { - buf := bytes.NewBufferString("") + var buf bytes.Buffer for i := 0; i < len(hashE); i += 8 { - buf.WriteString(hashE[i+6 : i+8]) - buf.WriteString(hashE[i+4 : i+6]) - buf.WriteString(hashE[i+2 : i+4]) - buf.WriteString(hashE[i : i+2]) + _, _ = buf.WriteString(hashE[i+6 : i+8]) + _, _ = buf.WriteString(hashE[i+4 : i+6]) + _, _ = buf.WriteString(hashE[i+2 : i+4]) + _, _ = buf.WriteString(hashE[i : i+2]) } return buf.String() } @@ -719,10 +719,10 @@ func hexReversed(in string) (string, error) { funcName, len(in)) return "", poolError(ErrHexLength, desc) } - buf := bytes.NewBufferString("") + var buf bytes.Buffer for i := len(in) - 1; i > -1; i -= 2 { - buf.WriteByte(in[i-1]) - buf.WriteByte(in[i]) + _ = buf.WriteByte(in[i-1]) + _ = buf.WriteByte(in[i]) } return buf.String(), nil } diff --git a/pool/error.go b/pool/error.go index 683befb1..f8da7235 100644 --- a/pool/error.go +++ b/pool/error.go @@ -121,7 +121,7 @@ func (e Error) Unwrap() error { return e.Err } -// poolError creates an Error given a set of arguments. This hould only be +// poolError creates an Error given a set of arguments. This should only be // used when creating error related to the mining pool and its processes. func poolError(kind ErrorKind, desc string) Error { return Error{Err: kind, Description: desc} @@ -133,7 +133,7 @@ func dbError(kind ErrorKind, desc string) Error { return Error{Err: kind, Description: desc} } -// msgError creates am Error given a set of arguments. This should only be +// msgError creates an Error given a set of arguments. This should only be // used when creating errors related to sending, receiving and processing // messages. func msgError(kind ErrorKind, desc string) Error { diff --git a/pool/hub_test.go b/pool/hub_test.go index f4d8afd7..b6540a4c 100644 --- a/pool/hub_test.go +++ b/pool/hub_test.go @@ -333,15 +333,12 @@ func testHub(t *testing.T, db *bolt.DB) { go hub.Run(ctx) // Create the mined work to be confirmed. - work, err := NewAcceptedWork( + work := NewAcceptedWork( "00008121c7731f9f81cae3d6279e81b9e7e7ebab94fb7bf584d16ecb70fbb9dd", "000033925cfb136f209b2722c4149dd53fceb0323f74b39be753887c19edcd2c", 56, "193c4b8fd02aaed33ab9c5418ace9bec4047f61f923767bceb5a51c6e368bfa6", CPU) - if err != nil { - t.Fatalf("[NewAcceptedWork] unexpected error: %v", err) - } err = work.Create(db) if err != nil { diff --git a/pool/job.go b/pool/job.go index 5a4a5e03..050c5428 100644 --- a/pool/job.go +++ b/pool/job.go @@ -33,8 +33,8 @@ func nanoToBigEndianBytes(nano int64) []byte { // jobID generates a unique job id of the provided block height. func jobID(height uint32) string { var buf bytes.Buffer - buf.Write(heightToBigEndianBytes(height)) - buf.Write(nanoToBigEndianBytes(time.Now().UnixNano())) + _, _ = buf.Write(heightToBigEndianBytes(height)) + _, _ = buf.Write(nanoToBigEndianBytes(time.Now().UnixNano())) return hex.EncodeToString(buf.Bytes()) } diff --git a/pool/message.go b/pool/message.go index b446e057..3579ae5b 100644 --- a/pool/message.go +++ b/pool/message.go @@ -448,7 +448,7 @@ func ParseWorkNotification(req *Request) (string, string, string, string, string } // Note that param[4] which is the list of merkle branches is not - // applicable for decred, the final merkle root is already + // applicable for decred, the final merkle root is already // included in the block. blockVersion, ok := params[5].(string) @@ -488,13 +488,13 @@ func ParseWorkNotification(req *Request) (string, string, string, string, string func GenerateBlockHeader(blockVersionE string, prevBlockE string, genTx1E string, extraNonce1E string, genTx2E string) (*wire.BlockHeader, error) { const funcName = "GenerateBlockHeader" - buf := bytes.NewBufferString("") - buf.WriteString(blockVersionE) - buf.WriteString(prevBlockE) - buf.WriteString(genTx1E) - buf.WriteString(extraNonce1E) - buf.WriteString(strings.Repeat("0", 56)) - buf.WriteString(genTx2E) + var buf bytes.Buffer + _, _ = buf.WriteString(blockVersionE) + _, _ = buf.WriteString(prevBlockE) + _, _ = buf.WriteString(genTx1E) + _, _ = buf.WriteString(extraNonce1E) + _, _ = buf.WriteString(strings.Repeat("0", 56)) + _, _ = buf.WriteString(genTx2E) headerE := buf.String() headerD, err := hex.DecodeString(headerE) diff --git a/pool/payment.go b/pool/payment.go index 7453c319..cd64735b 100644 --- a/pool/payment.go +++ b/pool/payment.go @@ -52,9 +52,9 @@ func NewPayment(account string, source *PaymentSource, amount dcrutil.Amount, // paymentID generates a unique id using the provided payment details. func paymentID(height uint32, createdOnNano int64, account string) []byte { var buf bytes.Buffer - buf.WriteString(hex.EncodeToString(heightToBigEndianBytes(height))) - buf.WriteString(hex.EncodeToString(nanoToBigEndianBytes(createdOnNano))) - buf.WriteString(account) + _, _ = buf.WriteString(hex.EncodeToString(heightToBigEndianBytes(height))) + _, _ = buf.WriteString(hex.EncodeToString(nanoToBigEndianBytes(createdOnNano))) + _, _ = buf.WriteString(account) return buf.Bytes() } diff --git a/pool/share.go b/pool/share.go index bf2a71f8..758a2131 100644 --- a/pool/share.go +++ b/pool/share.go @@ -32,8 +32,8 @@ var ShareWeights = map[string]*big.Rat{ // created. func shareID(account string, createdOn int64) []byte { var buf bytes.Buffer - buf.WriteString(hex.EncodeToString(nanoToBigEndianBytes(createdOn))) - buf.WriteString(account) + _, _ = buf.WriteString(hex.EncodeToString(nanoToBigEndianBytes(createdOn))) + _, _ = buf.WriteString(account) return buf.Bytes() } diff --git a/pool/share_test.go b/pool/share_test.go index 440a3ca3..a736c478 100644 --- a/pool/share_test.go +++ b/pool/share_test.go @@ -49,9 +49,6 @@ func testShares(t *testing.T, db *bolt.DB) { t.Fatalf("unexpected error fetching share A: %v", err) } bID := shareID(yID, shareBCreatedOn) - if err != nil { - t.Fatalf("unexpected share id creation error: %v", err) - } _, err = fetchShare(db, bID) if err != nil { t.Fatalf("unexpected error fetching share B: %v", err) diff --git a/pool/upgrades_test.go b/pool/upgrades_test.go index 52a1c7f6..809817f5 100644 --- a/pool/upgrades_test.go +++ b/pool/upgrades_test.go @@ -142,8 +142,8 @@ func verifyV3Upgrade(t *testing.T, db *bolt.DB) { } if payment.Source == nil { - return fmt.Errorf("%s: expected a non-nil "+ - "payment source: %v", funcName, err) + return fmt.Errorf("%s: expected a non-nil payment source", + funcName) } } @@ -185,7 +185,8 @@ func verifyV4Upgrade(t *testing.T, db *bolt.DB) { err := db.View(func(tx *bolt.Tx) error { pbkt := tx.Bucket(poolBkt) if pbkt == nil { - return fmt.Errorf("bucket %s not found", string(poolBkt)) + return fmt.Errorf("%s: bucket %s not found", string(poolBkt), + funcName) } v := pbkt.Get([]byte("txfeereserve"))