Skip to content

Commit

Permalink
pool: update hub errors.
Browse files Browse the repository at this point in the history
This updates errors and logs generated by the hub type
to use defined error types and provide more error context.
  • Loading branch information
dnldd committed Sep 11, 2020
1 parent ce40871 commit fd5aa4b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
52 changes: 29 additions & 23 deletions pool/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,7 @@ func NewHub(cancel context.CancelFunc, hcfg *HubConfig) (*Hub, error) {
log.Infof("Maximum work submission generation time at "+
"pool difficulty is %s.", maxGenTime)

var err error
h.poolDiffs, err = NewDifficultySet(h.cfg.ActiveNet, powLimit, maxGenTime)
if err != nil {
return nil, err
}
h.poolDiffs = NewDifficultySet(h.cfg.ActiveNet, powLimit, maxGenTime)

pCfg := &PaymentMgrConfig{
DB: h.db,
Expand All @@ -253,6 +249,8 @@ func NewHub(cancel context.CancelFunc, hcfg *HubConfig) (*Hub, error) {
FetchTxCreator: func() TxCreator { return h.nodeConn },
FetchTxBroadcaster: func() TxBroadcaster { return h.walletConn },
}

var err error
h.paymentMgr, err = NewPaymentMgr(pCfg)
if err != nil {
return nil, err
Expand Down Expand Up @@ -295,7 +293,7 @@ func NewHub(cancel context.CancelFunc, hcfg *HubConfig) (*Hub, error) {
// submitWork sends solved block data to the consensus daemon for evaluation.
func (h *Hub) submitWork(ctx context.Context, data *string) (bool, error) {
if h.nodeConn == nil {
return false, MakeError(ErrOther, "node connection unset", nil)
return false, poolError(ErrDisconnected, "node disconnected")
}

return h.nodeConn.GetWorkSubmit(ctx, *data)
Expand All @@ -304,12 +302,12 @@ func (h *Hub) submitWork(ctx context.Context, data *string) (bool, error) {
// getWork fetches available work from the consensus daemon.
func (h *Hub) getWork(ctx context.Context) (string, string, error) {
if h.nodeConn == nil {
return "", "", MakeError(ErrOther, "node connection unset", nil)
return "", "", poolError(ErrDisconnected, "node disonnected")
}

work, err := h.nodeConn.GetWork(ctx)
if err != nil {
return "", "", err
desc := fmt.Sprintf("unable to fetch current work: %v", err)
return "", "", poolError(ErrGetWork, desc)
}
return work.Data, work.Target, err
}
Expand All @@ -329,7 +327,8 @@ func (h *Hub) getTxConfNotifications(txHashes []*chainhash.Hash, stopAfter int32

err := h.notifClient.Send(req)
if err != nil {
return nil, err
desc := fmt.Sprintf("unable to fetch tx confirmations: %v", err)
return nil, poolError(ErrTxConf, desc)
}

return h.notifClient.Recv, nil
Expand All @@ -340,7 +339,8 @@ func (h *Hub) getTxConfNotifications(txHashes []*chainhash.Hash, stopAfter int32
func (h *Hub) getBlockConfirmations(ctx context.Context, hash *chainhash.Hash) (int64, error) {
info, err := h.nodeConn.GetBlockVerbose(ctx, hash, false)
if err != nil {
return 0, err
desc := fmt.Sprintf("unable to fetch block confirmations: %v", err)
return 0, poolError(ErrBlockConf, desc)
}
return info.Confirmations, nil
}
Expand All @@ -363,10 +363,15 @@ func (h *Hub) FetchLastPaymentHeight() uint32 {
// getBlock fetches the blocks associated with the provided block hash.
func (h *Hub) getBlock(ctx context.Context, blockHash *chainhash.Hash) (*wire.MsgBlock, error) {
if h.nodeConn == nil {
return nil, MakeError(ErrOther, "node connection unset", nil)
return nil, poolError(ErrDisconnected, "node disconnected")
}

return h.nodeConn.GetBlock(ctx, blockHash)
block, err := h.nodeConn.GetBlock(ctx, blockHash)
if err != nil {
desc := fmt.Sprintf("unable to fetch block %s: %v",
blockHash.String(), err)
return nil, poolError(ErrGetBlock, desc)
}
return block, nil
}

// fetchHostConnections returns the client connection count for the
Expand Down Expand Up @@ -398,7 +403,8 @@ func (h *Hub) removeConnection(host string) {
func (h *Hub) processWork(headerE string) {
heightD, err := hex.DecodeString(headerE[256:264])
if err != nil {
log.Errorf("failed to decode block height %s: %v", string(heightD), err)
log.Errorf("unable to decode block height %s: %v",
string(heightD), err)
return
}
height := binary.LittleEndian.Uint32(heightD)
Expand All @@ -416,12 +422,12 @@ func (h *Hub) processWork(headerE string) {
genTx2 := headerE[352:360]
job, err := NewJob(headerE, height)
if err != nil {
log.Errorf("failed to create job: %v", err)
log.Error(err)
return
}
err = job.Create(h.db)
if err != nil {
log.Errorf("failed to persist job: %v", err)
log.Error(err)
return
}
workNotif := WorkNotification(job.UUID, prevBlock, genTx1, genTx2,
Expand Down Expand Up @@ -464,8 +470,9 @@ func (h *Hub) Listen() error {
}
endpoint, err := NewEndpoint(eCfg, diffInfo, port, miner)
if err != nil {
desc := fmt.Sprintf("unable to create %s listener", miner)
return MakeError(ErrOther, desc, err)
desc := fmt.Sprintf("unable to create %s endpoint on port %d",
miner, port)
return poolError(ErrListener, desc)
}
h.endpoints = append(h.endpoints, endpoint)
}
Expand Down Expand Up @@ -514,8 +521,7 @@ func (h *Hub) CreateNotificationHandlers() *rpcclient.NotificationHandlers {
func (h *Hub) FetchWork(ctx context.Context) error {
work, _, err := h.getWork(ctx)
if err != nil {
desc := "unable to fetch current work"
return MakeError(ErrOther, desc, err)
return err
}
h.chainState.setCurrentWork(work)
return nil
Expand All @@ -533,7 +539,7 @@ func (h *Hub) backup(ctx context.Context) {
backupPath := filepath.Join(filepath.Dir(h.db.Path()), backupFile)
err := backup(h.db, backupPath)
if err != nil {
log.Errorf("unable to backup db: %v", err)
log.Error(err)
}
h.wg.Done()
}
Expand Down Expand Up @@ -653,7 +659,7 @@ func (h *Hub) CSRFSecret() ([]byte, error) {
pbkt := tx.Bucket(poolBkt)
if pbkt == nil {
desc := fmt.Sprintf("bucket %s not found", string(poolBkt))
return MakeError(ErrBucketNotFound, desc, nil)
return dbError(ErrBucketNotFound, desc)
}
v := pbkt.Get(csrfSecret)
if v != nil {
Expand Down
5 changes: 4 additions & 1 deletion pool/hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,15 @@ func testHub(t *testing.T, db *bolt.DB) {
go hub.Run(ctx)

// Create the mined work to be confirmed.
work := NewAcceptedWork(
work, err := 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

0 comments on commit fd5aa4b

Please sign in to comment.