Skip to content

Commit

Permalink
pool: resolve review issues (1 of x).
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldd committed May 12, 2021
1 parent d86a5fa commit e673794
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 66 deletions.
2 changes: 1 addition & 1 deletion pool/chainstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ChainStateConfig struct {
db Database
// SoloPool represents the solo pool mining mode.
SoloPool bool
// ProcessPayments relays payment signals for Processing.
// ProcessPayments relays payment signals for processing.
ProcessPayments func(msg *paymentMsg)
// GeneratePayments creates payments for participating accounts in pool
// mining mode based on the configured payment scheme.
Expand Down
12 changes: 6 additions & 6 deletions pool/paymentmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const (
// failures before a wallet rescan is requested.
maxTxConfThreshold = uint32(3)

// paymentBufferSize repreents the buffering on the payment channel.
// paymentBufferSize is the size of the buffer on the payment channel.
paymentBufferSize = uint32(30)
)

Expand Down Expand Up @@ -783,17 +783,17 @@ func (pm *PaymentMgr) payDividends(ctx context.Context, height uint32, treasuryA
return errs.PoolError(errs.Disconnected, desc)
}

// Request a wallet rescan if tx confirmation failures are
// at threshold.
pCtx, pCancel := context.WithTimeout(ctx, pm.cfg.CoinbaseConfTimeout)
defer pCancel()

// Request a wallet rescan if tx confirmation failures are
// at threshold.
txConfCount := atomic.LoadUint32(&pm.failedTxConfs)
if txConfCount == maxTxConfThreshold {
if txConfCount >= maxTxConfThreshold {
beginHeight := uint32(0)

// Having no tx conf hashes at threshold indicates an
// underlining error.
// underlying error.
pm.mtx.Lock()
if len(pm.txConfHashes) == 0 {
pm.mtx.Unlock()
Expand Down Expand Up @@ -822,7 +822,7 @@ func (pm *PaymentMgr) payDividends(ctx context.Context, height uint32, treasuryA
}
rescanSource, err := txB.Rescan(pCtx, rescanReq)
if err != nil {
desc := fmt.Sprintf("%s: tx creator cannot be nil", funcName)
desc := fmt.Sprintf("%s: rescan source cannot be nil", funcName)
return errs.PoolError(errs.Rescan, desc)
}

Expand Down
72 changes: 13 additions & 59 deletions pool/paymentmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestSharePercentages(t *testing.T) {
}
}

func createPaymentMgr(paymentMethod string) (*PaymentMgr, context.Context, context.CancelFunc, error) {
func createPaymentMgr(t *testing.T, paymentMethod string) (*PaymentMgr, context.Context, context.CancelFunc) {
activeNet := chaincfg.SimNetParams()

getBlockConfirmations := func(context.Context, *chainhash.Hash) (int64, error) {
Expand Down Expand Up @@ -189,17 +189,14 @@ func createPaymentMgr(paymentMethod string) (*PaymentMgr, context.Context, conte

mgr, err := NewPaymentMgr(pCfg)
if err != nil {
return nil, nil, nil, err
t.Fatalf("[createPaymentMgr] unexpected error: %v", err)
}

return mgr, ctx, cancel, err
return mgr, ctx, cancel
}

func testPaymentMgrPPS(t *testing.T) {
mgr, _, _, err := createPaymentMgr(PPS)
if err != nil {
t.Fatalf("[createPaymentMgr] unexpected error: %v", err)
}
mgr, _, _ := createPaymentMgr(t, PPS)

// Ensure Pay-Per-Share (PPS) works as expected.
now := time.Now()
Expand Down Expand Up @@ -293,10 +290,7 @@ func testPaymentMgrPPS(t *testing.T) {
}

func testPaymentMgrPPLNS(t *testing.T) {
mgr, _, _, err := createPaymentMgr(PPLNS)
if err != nil {
t.Fatalf("[createPaymentMgr] unexpected error: %v", err)
}
mgr, _, _ := createPaymentMgr(t, PPLNS)

// Ensure Pay-Per-Last-N-Shares (PPLNS) works as expected.
now := time.Now()
Expand Down Expand Up @@ -391,10 +385,7 @@ func testPaymentMgrPPLNS(t *testing.T) {
}

func testPaymentMgrMaturity(t *testing.T) {
mgr, _, _, err := createPaymentMgr(PPLNS)
if err != nil {
t.Fatalf("[createPaymentMgr] unexpected error: %v", err)
}
mgr, _, _ := createPaymentMgr(t, PPLNS)

now := time.Now()
shareCount := 3
Expand All @@ -407,15 +398,15 @@ func testPaymentMgrMaturity(t *testing.T) {
// Ensure payment maturity works as expected.
for i := 0; i < shareCount; i++ {
// Create readily available shares for account X.
err = persistShare(db, xID, weight, thirtyBefore+int64(i))
err := persistShare(db, xID, weight, thirtyBefore+int64(i))
if err != nil {
t.Fatal(err)
}
}
sixtyAfter := time.Now().Add((time.Second * 60)).UnixNano()
for i := 0; i < shareCount; i++ {
// Create future shares for account Y.
err = persistShare(db, yID, weight, sixtyAfter+int64(i))
err := persistShare(db, yID, weight, sixtyAfter+int64(i))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -487,11 +478,7 @@ func testPaymentMgrPayment(t *testing.T) {
t.Fatalf("failed to insert account: %v", err)
}

mgr, _, _, err := createPaymentMgr(PPS)
if err != nil {
t.Fatalf("[createPaymentMgr] unexpected error: %v", err)
}

mgr, _, _ := createPaymentMgr(t, PPS)
height := uint32(20)

// pruneOrphanedPayments tests.
Expand Down Expand Up @@ -1208,31 +1195,6 @@ func testPaymentMgrPayment(t *testing.T) {
t.Fatalf("expected a rescan error, got %v", err)
}

// Ensure dividend payment returns an error if fetching rescan responses fail.
mgr.cfg.FetchTxBroadcaster = func() TxBroadcaster {
return &txBroadcasterImpl{
signTransaction: func(ctx context.Context, req *walletrpc.SignTransactionRequest, options ...grpc.CallOption) (*walletrpc.SignTransactionResponse, error) {
return &walletrpc.SignTransactionResponse{
Transaction: txBytes,
}, nil
},
publishTransaction: func(ctx context.Context, req *walletrpc.PublishTransactionRequest, options ...grpc.CallOption) (*walletrpc.PublishTransactionResponse, error) {
return nil, fmt.Errorf("unable to publish transaction")
},
rescan: func(ctx context.Context, req *walletrpc.RescanRequest, options ...grpc.CallOption) (walletrpc.WalletService_RescanClient, error) {
return &tRescanClient{
err: fmt.Errorf("internal error"),
}, nil
},
}
}

err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
if !errors.Is(err, errs.Rescan) {
cancel()
t.Fatalf("expected a rescan error, got %v", err)
}

// Clear out the tx confirmation hashes to be rescanned for.
var confHashes map[chainhash.Hash]uint32
mgr.mtx.Lock()
Expand Down Expand Up @@ -1331,11 +1293,7 @@ func testPaymentMgrPayment(t *testing.T) {
}

func testPaymentMgrDust(t *testing.T) {
mgr, _, _, err := createPaymentMgr(PPLNS)
if err != nil {
t.Fatalf("[createPaymentMgr] unexpected error: %v", err)
}

mgr, _, _ := createPaymentMgr(t, PPLNS)
height := uint32(20)

// Ensure dust payments are forfeited by their originating accounts and
Expand All @@ -1347,7 +1305,7 @@ func testPaymentMgrDust(t *testing.T) {
yWeight := new(big.Rat).Mul(weight, new(big.Rat).SetInt64(int64(mul)))

// Create shares for account x and y.
err = persistShare(db, xID, weight, now.UnixNano())
err := persistShare(db, xID, weight, now.UnixNano())
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1425,11 +1383,7 @@ func testPaymentMgrSignals(t *testing.T) {
t.Fatalf("failed to insert account: %v", err)
}

mgr, ctx, cancel, err := createPaymentMgr(PPLNS)
if err != nil {
t.Fatalf("[createPaymentMgr] unexpected error: %v", err)
}

mgr, ctx, cancel := createPaymentMgr(t, PPLNS)
var randBytes [chainhash.HashSize + 1]byte
_, err = rand.Read(randBytes[:])
if err != nil {
Expand Down Expand Up @@ -1547,7 +1501,7 @@ func testPaymentMgrSignals(t *testing.T) {
return int64(estMaturity) + 1, nil
}

// Ensure the payment lifecycle process recieves the payment signal and
// Ensure the payment lifecycle process receives the payment signal and
// processes mature payments.
msgA := paymentMsg{
CurrentHeight: estMaturity + 1,
Expand Down

0 comments on commit e673794

Please sign in to comment.