Skip to content

Commit

Permalink
pool: resolve review issues (2 of x).
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldd committed Sep 16, 2020
1 parent 5c4181b commit e5a3d3d
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 68 deletions.
14 changes: 4 additions & 10 deletions pool/acceptedwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
12 changes: 3 additions & 9 deletions pool/acceptedwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions pool/chainstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions pool/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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()
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pool/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions pool/hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pool/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
16 changes: 8 additions & 8 deletions pool/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pool/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
4 changes: 2 additions & 2 deletions pool/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
3 changes: 0 additions & 3 deletions pool/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions pool/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit e5a3d3d

Please sign in to comment.