Skip to content

Commit d8ba4a4

Browse files
committed
pool: Remove treasuryActive bool from func sigs.
The only reason dcrpool needs to be aware of DCP-0006 activation is to determine the index of the coinbase output, so rather than passing around a treasuryActive bool, the index itself can be passed around.
1 parent 2705938 commit d8ba4a4

3 files changed

Lines changed: 57 additions & 56 deletions

File tree

pool/chainstate.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,23 @@ func isTreasuryActive(tx *wire.MsgTx) bool {
190190
return true
191191
}
192192

193+
// coinbaseIndex returns the index of the coinbase output in the provided
194+
// transaction.
195+
func coinbaseIndex(tx *wire.MsgTx) uint32 {
196+
treasuryActive := isTreasuryActive(tx)
197+
198+
// The coinbase output prior to
199+
// [DCP0006](https://github.com/decred/dcps/pull/17)
200+
// activation is at the third index position and at
201+
// the second index position once DCP0006 is activated.
202+
coinbaseIndex := uint32(1)
203+
if !treasuryActive {
204+
coinbaseIndex = 2
205+
}
206+
207+
return coinbaseIndex
208+
}
209+
193210
// handleChainUpdates processes connected and disconnected block
194211
// notifications from the consensus daemon.
195212
//
@@ -223,14 +240,14 @@ func (cs *ChainState) handleChainUpdates(ctx context.Context) error {
223240
}
224241

225242
coinbaseTx := block.Transactions[0]
226-
treasuryActive := isTreasuryActive(coinbaseTx)
243+
coinbaseIndex := coinbaseIndex(coinbaseTx)
227244

228245
soloPool := cs.cfg.SoloPool
229246
if !soloPool {
230247
go cs.cfg.ProcessPayments(ctx, &paymentMsg{
231-
CurrentHeight: header.Height,
232-
TreasuryActive: treasuryActive,
233-
Done: make(chan struct{}),
248+
CurrentHeight: header.Height,
249+
CoinbaseIndex: coinbaseIndex,
250+
Done: make(chan struct{}),
234251
})
235252
}
236253

@@ -359,14 +376,7 @@ func (cs *ChainState) handleChainUpdates(ctx context.Context) error {
359376
Coinbase: coinbaseTx.TxHash().String(),
360377
}
361378

362-
// The coinbase output prior to
363-
// [DCP0006](https://github.com/decred/dcps/pull/17)
364-
// activation is at the third index position and at
365-
// the second index position once DCP0006 is activated.
366-
amt := dcrutil.Amount(coinbaseTx.TxOut[1].Value)
367-
if !treasuryActive {
368-
amt = dcrutil.Amount(coinbaseTx.TxOut[2].Value)
369-
}
379+
amt := dcrutil.Amount(coinbaseTx.TxOut[coinbaseIndex].Value)
370380

371381
err = cs.cfg.GeneratePayments(block.Header.Height, source,
372382
amt, work.CreatedOn)

pool/paymentmgr.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ type PaymentMgrConfig struct {
121121

122122
// paymentMsg represents a payment processing signal.
123123
type paymentMsg struct {
124-
CurrentHeight uint32
125-
TreasuryActive bool
126-
Done chan struct{}
124+
CurrentHeight uint32
125+
CoinbaseIndex uint32
126+
Done chan struct{}
127127
}
128128

129129
// PaymentMgr handles generating shares and paying out dividends to
@@ -591,19 +591,10 @@ func (pm *PaymentMgr) monitorRescan(ctx context.Context, rescanSource walletrpc.
591591

592592
// generatePayoutTxDetails creates the payout transaction inputs and outputs
593593
// from the provided payments
594-
func (pm *PaymentMgr) generatePayoutTxDetails(ctx context.Context, txC txCreator, feeAddr stdaddr.Address, payments map[string][]*Payment, treasuryActive bool) ([]chainjson.TransactionInput,
594+
func (pm *PaymentMgr) generatePayoutTxDetails(ctx context.Context, txC txCreator, feeAddr stdaddr.Address, payments map[string][]*Payment, coinbaseIndex uint32) ([]chainjson.TransactionInput,
595595
map[chainhash.Hash]uint32, map[string]dcrutil.Amount, dcrutil.Amount, error) {
596596
const funcName = "generatePayoutTxDetails"
597597

598-
// The coinbase output prior to
599-
// [DCP0006](https://github.com/decred/dcps/pull/17)
600-
// activation is at the third index position and at
601-
// the second index position once DCP0006 is activated.
602-
coinbaseIndex := uint32(1)
603-
if !treasuryActive {
604-
coinbaseIndex = 2
605-
}
606-
607598
var tIn, tOut dcrutil.Amount
608599
inputs := make([]chainjson.TransactionInput, 0)
609600
inputTxHashes := make(map[chainhash.Hash]uint32)
@@ -701,7 +692,7 @@ func (pm *PaymentMgr) generatePayoutTxDetails(ctx context.Context, txC txCreator
701692
}
702693

703694
// PayDividends pays mature mining rewards to participating accounts.
704-
func (pm *PaymentMgr) payDividends(ctx context.Context, height uint32, treasuryActive bool) error {
695+
func (pm *PaymentMgr) payDividends(ctx context.Context, height uint32, coinbaseIndex uint32) error {
705696
const funcName = "payDividends"
706697
mPmts, err := pm.cfg.db.maturePendingPayments(height)
707698
if err != nil {
@@ -805,7 +796,7 @@ func (pm *PaymentMgr) payDividends(ctx context.Context, height uint32, treasuryA
805796
feeAddr := pm.cfg.PoolFeeAddrs[pm.prng.Intn(len(pm.cfg.PoolFeeAddrs))]
806797

807798
inputs, inputTxHashes, outputs, tOut, err :=
808-
pm.generatePayoutTxDetails(ctx, txC, feeAddr, pmts, treasuryActive)
799+
pm.generatePayoutTxDetails(ctx, txC, feeAddr, pmts, coinbaseIndex)
809800
if err != nil {
810801
return err
811802
}
@@ -947,7 +938,7 @@ func (pm *PaymentMgr) handlePayments(ctx context.Context) {
947938

948939
case msg := <-pm.paymentCh:
949940
if !pm.cfg.SoloPool {
950-
err := pm.payDividends(ctx, msg.CurrentHeight, msg.TreasuryActive)
941+
err := pm.payDividends(ctx, msg.CurrentHeight, msg.CoinbaseIndex)
951942
if err != nil {
952943
log.Errorf("unable to process payments: %v", err)
953944
close(msg.Done)

pool/paymentmgr_test.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ func testPaymentMgrPayment(t *testing.T) {
710710
mPmts[zeroSource.Coinbase] = []*Payment{pmtA}
711711
pmtB = NewPayment(yID, randSource, amt, height, estMaturity)
712712
mPmts[randSource.Coinbase] = []*Payment{pmtB}
713-
treasuryActive := true
713+
coinbaseIndex := uint32(1)
714714

715715
// Ensure generating payout tx details returns an error if fetching txOut
716716
// information fails.
@@ -720,7 +720,7 @@ func testPaymentMgrPayment(t *testing.T) {
720720
},
721721
}
722722
_, _, _, _, err = mgr.generatePayoutTxDetails(ctx, txC, poolFeeAddrs,
723-
mPmts, treasuryActive)
723+
mPmts, coinbaseIndex)
724724
if !errors.Is(err, errs.TxOut) {
725725
cancel()
726726
t.Fatalf("expected a fetch txOut error, got %v", err)
@@ -740,7 +740,7 @@ func testPaymentMgrPayment(t *testing.T) {
740740
}
741741

742742
_, _, _, _, err = mgr.generatePayoutTxDetails(ctx, txC, poolFeeAddrs,
743-
mPmts, treasuryActive)
743+
mPmts, coinbaseIndex)
744744
if !errors.Is(err, errs.Coinbase) {
745745
cancel()
746746
t.Fatalf("expected a spendable error")
@@ -764,7 +764,7 @@ func testPaymentMgrPayment(t *testing.T) {
764764
}
765765

766766
_, _, _, _, err = mgr.generatePayoutTxDetails(ctx, txC, poolFeeAddrs,
767-
mPmts, treasuryActive)
767+
mPmts, coinbaseIndex)
768768
if !errors.Is(err, errs.ValueNotFound) {
769769
cancel()
770770
t.Fatalf("expected an account not found error")
@@ -785,7 +785,7 @@ func testPaymentMgrPayment(t *testing.T) {
785785
}
786786

787787
_, _, _, _, err = mgr.generatePayoutTxDetails(ctx, txC, poolFeeAddrs,
788-
mPmts, treasuryActive)
788+
mPmts, coinbaseIndex)
789789
if !errors.Is(err, errs.CreateTx) {
790790
cancel()
791791
t.Fatalf("expected an input output mismatch error")
@@ -806,7 +806,7 @@ func testPaymentMgrPayment(t *testing.T) {
806806
}
807807

808808
_, _, _, _, err = mgr.generatePayoutTxDetails(ctx, txC, poolFeeAddrs,
809-
mPmts, treasuryActive)
809+
mPmts, coinbaseIndex)
810810
if !errors.Is(err, errs.CreateTx) {
811811
cancel()
812812
t.Fatalf("expected an unclaimed input value error, got %v", err)
@@ -826,7 +826,7 @@ func testPaymentMgrPayment(t *testing.T) {
826826

827827
inputs, inputTxHashes, outputs, _, err := mgr.generatePayoutTxDetails(ctx,
828828
txC, poolFeeAddrs,
829-
mPmts, treasuryActive)
829+
mPmts, coinbaseIndex)
830830
if err != nil {
831831
cancel()
832832
t.Fatalf("unexpected payout tx details error, got %v", err)
@@ -897,7 +897,7 @@ func testPaymentMgrPayment(t *testing.T) {
897897

898898
// Ensure dividend payments returns no error if there are no mature
899899
// payments to work with.
900-
err = mgr.payDividends(ctx, estMaturity-1, treasuryActive)
900+
err = mgr.payDividends(ctx, estMaturity-1, coinbaseIndex)
901901
if err != nil {
902902
cancel()
903903
t.Fatal("expected no error since there are no mature payments")
@@ -907,7 +907,7 @@ func testPaymentMgrPayment(t *testing.T) {
907907
// fetched.
908908
mgr.cfg.TxCreator = nil
909909

910-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
910+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
911911
if !errors.Is(err, errs.Disconnected) {
912912
cancel()
913913
t.Fatalf("expected a nil tx creator error, got %v", err)
@@ -920,7 +920,7 @@ func testPaymentMgrPayment(t *testing.T) {
920920
return -1, fmt.Errorf("unable to confirm blocks")
921921
}
922922

923-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
923+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
924924
if err == nil {
925925
cancel()
926926
t.Fatalf("expected a prune orphan payments error")
@@ -945,7 +945,7 @@ func testPaymentMgrPayment(t *testing.T) {
945945
},
946946
}
947947

948-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
948+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
949949
if !errors.Is(err, errs.TxOut) {
950950
cancel()
951951
t.Fatalf("expected a generate payout tx details error, got %v", err)
@@ -957,7 +957,7 @@ func testPaymentMgrPayment(t *testing.T) {
957957
return -1, nil
958958
}
959959

960-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
960+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
961961
if !errors.Is(err, errs.TxIn) {
962962
cancel()
963963
t.Fatalf("expected an apply tx fee error, got %v", err)
@@ -978,7 +978,7 @@ func testPaymentMgrPayment(t *testing.T) {
978978
return int64(estMaturity) + 1, nil
979979
}
980980

981-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
981+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
982982
if err == nil {
983983
cancel()
984984
t.Fatalf("expected a coinbase confirmation error, got %v", err)
@@ -1005,7 +1005,7 @@ func testPaymentMgrPayment(t *testing.T) {
10051005

10061006
mgr.cfg.CoinbaseConfTimeout = time.Millisecond * 500
10071007

1008-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
1008+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
10091009
if err == nil {
10101010
cancel()
10111011
t.Fatal("expected a create transaction error")
@@ -1032,7 +1032,7 @@ func testPaymentMgrPayment(t *testing.T) {
10321032
mgr.cfg.TxBroadcaster = nil
10331033
mgr.cfg.WalletPass = "123"
10341034

1035-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
1035+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
10361036
if !errors.Is(err, errs.Disconnected) {
10371037
cancel()
10381038
t.Fatalf("expected a fetch tx broadcaster error, got %v", err)
@@ -1065,7 +1065,7 @@ func testPaymentMgrPayment(t *testing.T) {
10651065
},
10661066
}
10671067

1068-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
1068+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
10691069
if !errors.Is(err, errs.SignTx) {
10701070
cancel()
10711071
t.Fatalf("expected a signing error, got %v", err)
@@ -1098,7 +1098,7 @@ func testPaymentMgrPayment(t *testing.T) {
10981098
},
10991099
}
11001100

1101-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
1101+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
11021102
if !errors.Is(err, errs.PublishTx) {
11031103
cancel()
11041104
t.Fatalf("expected a publish error, got %v", err)
@@ -1128,7 +1128,7 @@ func testPaymentMgrPayment(t *testing.T) {
11281128

11291129
atomic.StoreUint32(&mgr.failedTxConfs, maxTxConfThreshold)
11301130

1131-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
1131+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
11321132
if !errors.Is(err, errs.Rescan) {
11331133
cancel()
11341134
t.Fatalf("expected a rescan error, got %v", err)
@@ -1157,7 +1157,7 @@ func testPaymentMgrPayment(t *testing.T) {
11571157
},
11581158
}
11591159

1160-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
1160+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
11611161
if !errors.Is(err, errs.Rescan) {
11621162
cancel()
11631163
t.Fatalf("expected a rescan error, got %v", err)
@@ -1172,7 +1172,7 @@ func testPaymentMgrPayment(t *testing.T) {
11721172

11731173
// Ensure dividend payment returns an error when there are no tx
11741174
// confirmation hashes to rescan.
1175-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
1175+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
11761176
if !errors.Is(err, errs.TxConf) {
11771177
cancel()
11781178
t.Fatalf("expected a no tx conf error, got %v", err)
@@ -1208,7 +1208,7 @@ func testPaymentMgrPayment(t *testing.T) {
12081208
},
12091209
}
12101210

1211-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
1211+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
12121212
if !errors.Is(err, errs.PublishTx) {
12131213
cancel()
12141214
t.Fatalf("expected a publish error, got %v", err)
@@ -1242,7 +1242,7 @@ func testPaymentMgrPayment(t *testing.T) {
12421242
},
12431243
}
12441244

1245-
err = mgr.payDividends(ctx, estMaturity+1, treasuryActive)
1245+
err = mgr.payDividends(ctx, estMaturity+1, coinbaseIndex)
12461246
if err != nil {
12471247
cancel()
12481248
t.Fatalf("unexpected dividend payment error, got %v", err)
@@ -1459,9 +1459,9 @@ func testPaymentMgrSignals(t *testing.T) {
14591459
// Ensure the payment lifecycle process receives the payment signal and
14601460
// processes mature payments.
14611461
msgA := paymentMsg{
1462-
CurrentHeight: estMaturity + 1,
1463-
TreasuryActive: false,
1464-
Done: make(chan struct{}),
1462+
CurrentHeight: estMaturity + 1,
1463+
CoinbaseIndex: 2,
1464+
Done: make(chan struct{}),
14651465
}
14661466

14671467
var wg sync.WaitGroup
@@ -1491,9 +1491,9 @@ func testPaymentMgrSignals(t *testing.T) {
14911491
}
14921492

14931493
msgB := paymentMsg{
1494-
CurrentHeight: estMaturity + 1,
1495-
TreasuryActive: false,
1496-
Done: make(chan struct{}),
1494+
CurrentHeight: estMaturity + 1,
1495+
CoinbaseIndex: 2,
1496+
Done: make(chan struct{}),
14971497
}
14981498
mgr.processPayments(ctx, &msgB)
14991499
<-msgB.Done

0 commit comments

Comments
 (0)