Skip to content

Commit

Permalink
multi: resolve review issues (1 of x).
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldd committed Sep 16, 2020
1 parent 084eb71 commit 5c4181b
Show file tree
Hide file tree
Showing 26 changed files with 179 additions and 444 deletions.
2 changes: 1 addition & 1 deletion cmd/miner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func loadConfig() (*config, []string, error) {
}

// Create the home directory if it doesn't already exist.
funcName := "loadConfig"
const funcName = "loadConfig"
err = os.MkdirAll(cfg.HomeDir, 0700)
if err != nil {
// Show a nicer error message if it's because a symlink is
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func loadConfig() (*config, []string, error) {
}

// Create the home directory if it doesn't already exist.
funcName := "loadConfig"
const funcName = "loadConfig"
err = os.MkdirAll(cfg.HomeDir, 0700)
if err != nil {
// Show a nicer error message if it's because a symlink is
Expand Down
37 changes: 11 additions & 26 deletions pool/acceptedwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,18 @@ func bigEndianBytesToHeight(b []byte) uint32 {
}

// AcceptedWorkID generates a unique id for work accepted by the network.
func AcceptedWorkID(blockHash string, blockHeight uint32) ([]byte, error) {
funcName := "AcceptedWorkID"
buf := bytes.Buffer{}
_, err := buf.WriteString(hex.EncodeToString(heightToBigEndianBytes(blockHeight)))
if err != nil {
desc := fmt.Sprintf("%s: unable to write block height: %v",
funcName, err)
return nil, poolError(ErrID, desc)
}
func AcceptedWorkID(blockHash string, blockHeight uint32) []byte {
var buf bytes.Buffer
buf.WriteString(hex.EncodeToString(heightToBigEndianBytes(blockHeight)))
buf.WriteString(blockHash)
if err != nil {
desc := fmt.Sprintf("%s: unable to write block hash: %v",
funcName, err)
return nil, poolError(ErrID, desc)
}
return buf.Bytes(), nil
return buf.Bytes()
}

// NewAcceptedWork creates an accepted work.
func NewAcceptedWork(blockHash string, prevHash string, height uint32,
minedBy string, miner string) (*AcceptedWork, error) {
id, err := AcceptedWorkID(blockHash, height)
if err != nil {
return nil, err
}
return &AcceptedWork{
UUID: string(id),
UUID: string(AcceptedWorkID(blockHash, height)),
BlockHash: blockHash,
PrevHash: prevHash,
Height: height,
Expand All @@ -83,7 +68,7 @@ func NewAcceptedWork(blockHash string, prevHash string, height uint32,

// fetchWorkBucket is a helper function for getting the work bucket.
func fetchWorkBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
funcName := "fetchWorkBucket"
const funcName = "fetchWorkBucket"
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
Expand All @@ -101,7 +86,7 @@ func fetchWorkBucket(tx *bolt.Tx) (*bolt.Bucket, error) {

// FetchAcceptedWork fetches the accepted work referenced by the provided id.
func FetchAcceptedWork(db *bolt.DB, id []byte) (*AcceptedWork, error) {
funcName := "FetchAcceptedWork"
const funcName = "FetchAcceptedWork"
var work AcceptedWork
err := db.View(func(tx *bolt.Tx) error {
bkt, err := fetchWorkBucket(tx)
Expand Down Expand Up @@ -130,7 +115,7 @@ func FetchAcceptedWork(db *bolt.DB, id []byte) (*AcceptedWork, error) {

// Create persists the accepted work to the database.
func (work *AcceptedWork) Create(db *bolt.DB) error {
funcName := "AcceptedWork.Create"
const funcName = "AcceptedWork.Create"
return db.Update(func(tx *bolt.Tx) error {
bkt, err := fetchWorkBucket(tx)
if err != nil {
Expand All @@ -143,7 +128,7 @@ func (work *AcceptedWork) Create(db *bolt.DB) error {
if v != nil {
desc := fmt.Sprintf("%s: work %s already exists", funcName,
work.UUID)
return dbError(ErrWorkExists, desc)
return dbError(ErrValueFound, desc)
}
workBytes, err := json.Marshal(work)
if err != nil {
Expand All @@ -164,7 +149,7 @@ func (work *AcceptedWork) Create(db *bolt.DB) error {

// Update persists modifications to an existing work.
func (work *AcceptedWork) Update(db *bolt.DB) error {
funcName := "AcceptedWork.Update"
const funcName = "AcceptedWork.Update"
return db.Update(func(tx *bolt.Tx) error {
bkt, err := fetchWorkBucket(tx)
if err != nil {
Expand Down Expand Up @@ -204,7 +189,7 @@ func (work *AcceptedWork) Delete(db *bolt.DB) error {
//
// List is ordered, most recent comes first.
func ListMinedWork(db *bolt.DB) ([]*AcceptedWork, error) {
funcName := "ListMinedWork"
const funcName = "ListMinedWork"
minedWork := make([]*AcceptedWork, 0)
err := db.View(func(tx *bolt.Tx) error {
bkt, err := fetchWorkBucket(tx)
Expand Down
12 changes: 2 additions & 10 deletions pool/acceptedwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,14 @@ func testAcceptedWork(t *testing.T, db *bolt.DB) {
}

// Ensure fetching a non existent accepted work returns an error.
id, err := AcceptedWorkID(workC.BlockHash, workD.Height)
if err != nil {
t.Fatalf("unexpected work id error: %v", err)
}

id := AcceptedWorkID(workC.BlockHash, workD.Height)
_, err = FetchAcceptedWork(db, id)
if err == nil {
t.Fatalf("FetchAcceptedWork: expected a non-existent accepted work error")
}

// Fetch an accepted work with its id.
id, err = AcceptedWorkID(workC.BlockHash, workC.Height)
if err != nil {
t.Fatalf("unexpected work id error: %v", err)
}

id = AcceptedWorkID(workC.BlockHash, workC.Height)
fetchedWork, err := FetchAcceptedWork(db, id)
if err != nil {
t.Fatalf("FetchAcceptedWork error: %v", err)
Expand Down
28 changes: 8 additions & 20 deletions pool/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,16 @@ type Account struct {

// AccountID generates a unique id using provided address of the account.
func AccountID(address string, activeNet *chaincfg.Params) (string, error) {
funcName := "AcccountID"
const funcName = "AcccountID"
_, err := dcrutil.DecodeAddress(address, activeNet)
if err != nil {
desc := fmt.Sprintf("%s: unable to decode address %s: %v",
funcName, address, err)
return "", poolError(ErrID, desc)
return "", poolError(ErrDecode, desc)
}
hasher := blake256.New()
_, err = hasher.Write([]byte(address))
if err != nil {
desc := fmt.Sprintf("%s: unable to write address: %v", funcName, err)
return "", poolError(ErrID, desc)
}
id := hex.EncodeToString(hasher.Sum(nil))
return id, nil
_, _ = hasher.Write([]byte(address))
return hex.EncodeToString(hasher.Sum(nil)), nil
}

// NewAccount creates a new account.
Expand All @@ -60,7 +55,7 @@ func NewAccount(address string, activeNet *chaincfg.Params) (*Account, error) {

// fetchAccountBucket is a helper function for getting the account bucket.
func fetchAccountBucket(tx *bolt.Tx) (*bolt.Bucket, error) {
funcName := "fetchAccountBucket"
const funcName = "fetchAccountBucket"
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("%s: bucket %s not found", funcName,
Expand All @@ -78,7 +73,7 @@ func fetchAccountBucket(tx *bolt.Tx) (*bolt.Bucket, error) {

// FetchAccount fetches the account referenced by the provided id.
func FetchAccount(db *bolt.DB, id []byte) (*Account, error) {
funcName := "FetchAccount"
const funcName = "FetchAccount"
var account Account
err := db.View(func(tx *bolt.Tx) error {
bkt, err := fetchAccountBucket(tx)
Expand Down Expand Up @@ -108,7 +103,7 @@ func FetchAccount(db *bolt.DB, id []byte) (*Account, error) {

// Create persists the account to the database.
func (acc *Account) Create(db *bolt.DB) error {
funcName := "Account.Create"
const funcName = "Account.Create"
return db.Update(func(tx *bolt.Tx) error {
bkt, err := fetchAccountBucket(tx)
if err != nil {
Expand All @@ -121,7 +116,7 @@ func (acc *Account) Create(db *bolt.DB) error {
if v != nil {
desc := fmt.Sprintf("%s: account %s already exists", funcName,
acc.UUID)
return dbError(ErrAccountExists, desc)
return dbError(ErrValueFound, desc)
}
accBytes, err := json.Marshal(acc)
if err != nil {
Expand All @@ -139,13 +134,6 @@ func (acc *Account) Create(db *bolt.DB) error {
})
}

// Update is not supported for accounts.
func (acc *Account) Update(db *bolt.DB) error {
funcName := "Account.Update"
desc := fmt.Sprintf("%s: not supported", funcName)
return dbError(ErrNotSupported, desc)
}

// Delete purges the referenced account from the database.
func (acc *Account) Delete(db *bolt.DB) error {
return deleteEntry(db, accountBkt, []byte(acc.UUID))
Expand Down
6 changes: 0 additions & 6 deletions pool/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ func testAccount(t *testing.T, db *bolt.DB) {
accountA.UUID, fetchedAccount.UUID)
}

// Ensure accounts cannot be updated.
err = accountB.Update(db)
if err == nil {
t.Fatal("expected not supported error")
}

// Delete all accounts.
err = accountA.Delete(db)
if err != nil {
Expand Down
24 changes: 3 additions & 21 deletions pool/chainstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,7 @@ func (cs *ChainState) handleChainUpdates(ctx context.Context) {
// of the pool.
parentHeight := header.Height - 1
parentHash := header.PrevBlock.String()
parentID, err := AcceptedWorkID(parentHash, parentHeight)
if err != nil {
log.Error(err)
close(msg.Done)
cs.cfg.Cancel()
continue
}
parentID := AcceptedWorkID(parentHash, parentHeight)
work, err := FetchAcceptedWork(cs.cfg.DB, parentID)
if err != nil {
// If the parent of the connected block is not an accepted
Expand Down Expand Up @@ -476,13 +470,7 @@ func (cs *ChainState) handleChainUpdates(ctx context.Context) {
// does unconfirm it.
parentHeight := header.Height - 1
parentHash := header.PrevBlock.String()
parentID, err := AcceptedWorkID(parentHash, parentHeight)
if err != nil {
log.Error(err)
close(msg.Done)
cs.cfg.Cancel()
continue
}
parentID := AcceptedWorkID(parentHash, parentHeight)
confirmedWork, err := FetchAcceptedWork(cs.cfg.DB, parentID)
if err != nil {
// Errors generated, except for a value not found error,
Expand Down Expand Up @@ -522,13 +510,7 @@ func (cs *ChainState) handleChainUpdates(ctx context.Context) {
// If the disconnected block is an accepted work of the pool
// ensure it is not confirmed mined.
blockHash := header.BlockHash().String()
id, err := AcceptedWorkID(blockHash, header.Height)
if err != nil {
log.Error(err)
close(msg.Done)
cs.cfg.Cancel()
continue
}
id := AcceptedWorkID(blockHash, header.Height)
work, err := FetchAcceptedWork(cs.cfg.DB, id)
if err != nil {
// If the disconnected block is not an accepted
Expand Down
9 changes: 3 additions & 6 deletions pool/chainstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func testChainState(t *testing.T, db *bolt.DB) {
t.Fatalf("prunePayments error: %v", err)
}

aID, err := paymentID(paymentA.Height, paymentA.CreatedOn, paymentA.Account)
aID := paymentID(paymentA.Height, paymentA.CreatedOn, paymentA.Account)
if err != nil {
t.Fatalf("unexpected payment id err: %v", err)
}
Expand All @@ -222,7 +222,7 @@ func testChainState(t *testing.T, db *bolt.DB) {
t.Fatalf("unexpected error fetching payment A: %v", err)
}

bID, err := paymentID(paymentB.Height, paymentB.CreatedOn, paymentB.Account)
bID := paymentID(paymentB.Height, paymentB.CreatedOn, paymentB.Account)
if err != nil {
t.Fatalf("unexpected payment id err: %v", err)
}
Expand Down Expand Up @@ -284,10 +284,7 @@ func testChainState(t *testing.T, db *bolt.DB) {
"0004fa83b20204e0000000000002a000000a50300004348fa5d00000000" +
"00000000000000000000000000000000000000000000000000000000000" +
"00000000000008000000100000000000005a0"
job, err := NewJob(workE, 42)
if err != nil {
t.Fatalf("unable to create job %v", err)
}
job := NewJob(workE, 42)
err = job.Create(cs.cfg.DB)
if err != nil {
log.Errorf("failed to persist job %v", err)
Expand Down
41 changes: 10 additions & 31 deletions pool/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,7 @@ func (c *Client) claimWeightedShare() error {
return poolError(ErrClaimShare, desc)
}
weight := ShareWeights[c.cfg.FetchMiner()]
share, err := NewShare(c.account, weight)
if err != nil {
return err
}
share := NewShare(c.account, weight)
return share.Create(c.cfg.DB)
}

Expand Down Expand Up @@ -466,7 +463,7 @@ func (c *Client) handleSubmitWorkRequest(ctx context.Context, req *Request, allo
if err != nil {
// If the submitted accepted work already exists, ignore the
// submission.
if errors.Is(err, ErrWorkExists) {
if errors.Is(err, ErrValueFound) {
sErr := NewStratumError(DuplicateShare, err)
resp := SubmitWorkResponse(*req.ID, false, sErr)
c.ch <- resp
Expand Down Expand Up @@ -563,7 +560,7 @@ func (c *Client) read() {
// after client authentication and when the client is stalling on
// current work.
func (c *Client) updateWork() {
funcName := "updateWork"
const funcName = "updateWork"
// Only timestamp-roll current work for authorized and subscribed clients.
c.authorizedMtx.Lock()
authorized := c.authorized
Expand All @@ -584,24 +581,10 @@ func (c *Client) updateWork() {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, now)
timestampE := hex.EncodeToString(b)
buf := bytes.NewBufferString("")
_, err := buf.WriteString(currWorkE[:272])
if err != nil {
log.Errorf("%s: unable to write first current work slice: %v",
funcName, err)
return
}
_, err = buf.WriteString(timestampE)
if err != nil {
log.Errorf("%s: unable to write timetamp: %v", funcName, err)
return
}
_, err = buf.WriteString(currWorkE[280:])
if err != nil {
log.Errorf("%s: unable to write second current work slice: %v",
funcName, err)
return
}
var buf bytes.Buffer
buf.WriteString(currWorkE[:272])
buf.WriteString(timestampE)
buf.WriteString(currWorkE[280:])

updatedWorkE := buf.String()
blockVersion := updatedWorkE[:8]
Expand All @@ -620,11 +603,7 @@ func (c *Client) updateWork() {
height := binary.LittleEndian.Uint32(heightD)

// Create a job for the timestamp-rolled current work.
job, err := NewJob(updatedWorkE, height)
if err != nil {
log.Error(err)
return
}
job := NewJob(updatedWorkE, height)
err = job.Create(c.cfg.DB)
if err != nil {
log.Error(err)
Expand Down Expand Up @@ -734,11 +713,11 @@ func reversePrevBlockWords(hashE string) string {

// hexReversed reverses a hex string.
func hexReversed(in string) (string, error) {
funcName := "hexReversed"
const funcName = "hexReversed"
if len(in)%2 != 0 {
desc := fmt.Sprintf("%s: expected even hex input length, got %d",
funcName, len(in))
return "", dbError(ErrHexLength, desc)
return "", poolError(ErrHexLength, desc)
}
buf := bytes.NewBufferString("")
for i := len(in) - 1; i > -1; i -= 2 {
Expand Down
Loading

0 comments on commit 5c4181b

Please sign in to comment.