Skip to content

Commit 8a73bea

Browse files
vsp: don't set/use a default policy
This avoids the possibility of using a previously created vsp client that has a policy different from the one intended by the caller. In most cases already, the caller has a policy defined that should be used rather than some previously set default policy. The only other case where a policy to be used isn't readily handy is in the json-rpc handler for the processUnmanagedTicket method. That method uses the vsp client set in config and should thus use the policy set in config.
1 parent 9a8688e commit 8a73bea

File tree

9 files changed

+719
-707
lines changed

9 files changed

+719
-707
lines changed

dcrwallet.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func run(ctx context.Context) error {
187187

188188
// Open the wallet when --noinitialload was not set.
189189
var vspClient *vsp.Client
190+
var vspFeePolicy vsp.Policy
190191
passphrase := []byte{}
191192
if !cfg.NoInitialLoad {
192193
walletPass := []byte(cfg.WalletPass)
@@ -267,16 +268,16 @@ func run(ctx context.Context) error {
267268
cfg.PurchaseAccount, err)
268269
return err
269270
}
271+
vspFeePolicy = vsp.Policy{
272+
MaxFee: cfg.VSPOpts.MaxFee.Amount,
273+
FeeAcct: purchaseAcct,
274+
ChangeAcct: changeAcct,
275+
}
270276
vspCfg := vsp.Config{
271277
URL: cfg.VSPOpts.URL,
272278
PubKey: cfg.VSPOpts.PubKey,
273279
Dialer: cfg.dial,
274280
Wallet: w,
275-
Policy: vsp.Policy{
276-
MaxFee: cfg.VSPOpts.MaxFee.Amount,
277-
FeeAcct: purchaseAcct,
278-
ChangeAcct: changeAcct,
279-
},
280281
}
281282
vspClient, err = ldr.VSP(vspCfg)
282283
if err != nil {
@@ -354,6 +355,7 @@ func run(ctx context.Context) error {
354355
c.TicketSplitAccount = ticketSplitAccount
355356
c.ChangeAccount = changeAccount
356357
c.VSP = vspClient
358+
c.VSPFeePolicy = vspFeePolicy
357359
})
358360
log.Infof("Starting auto transaction creator")
359361
tbdone := make(chan struct{})
@@ -421,7 +423,7 @@ func run(ctx context.Context) error {
421423

422424
loader.RunAfterLoad(func(w *wallet.Wallet) {
423425
if vspClient != nil && cfg.VSPOpts.Sync {
424-
vspClient.ProcessManagedTickets(ctx, vspClient.Policy)
426+
vspClient.ProcessManagedTickets(ctx, vspFeePolicy)
425427
}
426428

427429
if cfg.SPV {

internal/rpc/jsonrpc/config.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package jsonrpc
77
import (
88
"context"
99
"net"
10+
11+
"github.com/decred/dcrd/dcrutil/v4"
1012
)
1113

1214
// Options contains the required options for running the legacy RPC server.
@@ -24,7 +26,16 @@ type Options struct {
2426
MixChangeAccount string
2527
TicketSplitAccount string
2628

27-
VSPHost string
28-
VSPPubKey string
29-
Dial func(ctx context.Context, network, addr string) (net.Conn, error)
29+
VSPOpts VSPOptions
30+
31+
Dial func(ctx context.Context, network, addr string) (net.Conn, error)
32+
}
33+
34+
// VSPOptions defines options for processing tickets with a VSP server.
35+
type VSPOptions struct {
36+
Host string
37+
PubKey string
38+
MaxFee dcrutil.Amount
39+
PurchaseAccount string
40+
ChangeAccount string
3041
}

internal/rpc/jsonrpc/methods.go

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3366,9 +3366,9 @@ func (s *Server) purchaseTicket(ctx context.Context, icmd interface{}) (interfac
33663366
var vspHost string
33673367
var vspPubKey string
33683368
var vspClient *vsp.Client
3369-
if s.cfg.VSPHost != "" || s.cfg.VSPPubKey != "" {
3370-
vspHost = s.cfg.VSPHost
3371-
vspPubKey = s.cfg.VSPPubKey
3369+
if s.cfg.VSPOpts.Host != "" || s.cfg.VSPOpts.PubKey != "" {
3370+
vspHost = s.cfg.VSPOpts.Host
3371+
vspPubKey = s.cfg.VSPOpts.PubKey
33723372
if vspPubKey == "" {
33733373
return nil, rpcErrorf(dcrjson.ErrRPCInvalidParameter,
33743374
"vsp pubkey can not be null")
@@ -3382,11 +3382,6 @@ func (s *Server) purchaseTicket(ctx context.Context, icmd interface{}) (interfac
33823382
PubKey: vspPubKey,
33833383
Dialer: s.cfg.Dial,
33843384
Wallet: w,
3385-
Policy: vsp.Policy{
3386-
MaxFee: 0.2e8,
3387-
FeeAcct: account,
3388-
ChangeAcct: changeAccount,
3389-
},
33903385
}
33913386
vspClient, err = loader.VSP(cfg)
33923387
if err != nil {
@@ -3415,7 +3410,14 @@ func (s *Server) purchaseTicket(ctx context.Context, icmd interface{}) (interfac
34153410
}
34163411

34173412
if vspClient != nil {
3418-
request.VSPFeePaymentProcess = vspClient.Process
3413+
policy := vsp.Policy{
3414+
MaxFee: 0.2e8,
3415+
FeeAcct: account,
3416+
ChangeAcct: changeAccount,
3417+
}
3418+
request.VSPFeePaymentProcess = func(ctx context.Context, ticketHash *chainhash.Hash, feeTx *wire.MsgTx) error {
3419+
return vspClient.Process(ctx, ticketHash, feeTx, policy)
3420+
}
34193421
request.VSPFeeProcess = vspClient.FeePercentage
34203422
}
34213423

@@ -3467,6 +3469,10 @@ func (s *Server) purchaseTicket(ctx context.Context, icmd interface{}) (interfac
34673469
// start managing it for the set vsp client from the config.
34683470
func (s *Server) processUnmanagedTicket(ctx context.Context, icmd interface{}) (interface{}, error) {
34693471
cmd := icmd.(*types.ProcessUnmanagedTicketCmd)
3472+
w, ok := s.walletLoader.LoadedWallet()
3473+
if !ok {
3474+
return nil, errUnloadedWallet
3475+
}
34703476

34713477
var ticketHash *chainhash.Hash
34723478
if cmd.TicketHash != nil {
@@ -3478,7 +3484,7 @@ func (s *Server) processUnmanagedTicket(ctx context.Context, icmd interface{}) (
34783484
} else {
34793485
return nil, rpcErrorf(dcrjson.ErrRPCInvalidParameter, "ticket hash must be provided")
34803486
}
3481-
vspHost := s.cfg.VSPHost
3487+
vspHost := s.cfg.VSPOpts.Host
34823488
if vspHost == "" {
34833489
return nil, rpcErrorf(dcrjson.ErrRPCInvalidParameter, "vsphost must be set in options")
34843490
}
@@ -3487,7 +3493,30 @@ func (s *Server) processUnmanagedTicket(ctx context.Context, icmd interface{}) (
34873493
return nil, err
34883494
}
34893495

3490-
err = vspClient.ProcessTicket(ctx, ticketHash)
3496+
purchaseAcct, err := w.AccountNumber(ctx, s.cfg.VSPOpts.PurchaseAccount)
3497+
if err != nil {
3498+
return nil, rpcErrorf(dcrjson.ErrRPCInvalidParameter, "invalid purchaseaccount set in options")
3499+
}
3500+
3501+
var changeAcct uint32
3502+
changeAccountName := s.cfg.VSPOpts.ChangeAccount
3503+
if changeAccountName == "" && s.cfg.CSPPServer == "" {
3504+
log.Warnf("Change account not set, using "+
3505+
"purchase account %q", s.cfg.VSPOpts.PurchaseAccount)
3506+
changeAcct = purchaseAcct
3507+
} else {
3508+
changeAcct, err = w.AccountNumber(ctx, changeAccountName)
3509+
if err != nil {
3510+
return nil, rpcErrorf(dcrjson.ErrRPCInvalidParameter, "invalid changeaccount set in options")
3511+
}
3512+
}
3513+
3514+
policy := vsp.Policy{
3515+
MaxFee: s.cfg.VSPOpts.MaxFee,
3516+
FeeAcct: purchaseAcct,
3517+
ChangeAcct: changeAcct,
3518+
}
3519+
err = vspClient.ProcessTicket(ctx, ticketHash, policy)
34913520
if err != nil {
34923521
return nil, err
34933522
}
@@ -4628,7 +4657,7 @@ func (s *Server) setVoteChoice(ctx context.Context, icmd interface{}) (interface
46284657
return nil, err
46294658
}
46304659

4631-
vspHost := s.cfg.VSPHost
4660+
vspHost := s.cfg.VSPOpts.Host
46324661
if vspHost == "" {
46334662
return nil, nil
46344663
}

internal/rpc/rpcserver/server.go

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,11 +1747,6 @@ func (s *walletServer) PurchaseTickets(ctx context.Context,
17471747
PubKey: vspPubKey,
17481748
Dialer: nil,
17491749
Wallet: s.wallet,
1750-
Policy: vsp.Policy{
1751-
MaxFee: 0.1e8,
1752-
FeeAcct: req.Account,
1753-
ChangeAcct: req.ChangeAccount,
1754-
},
17551750
}
17561751
vspClient, err = loader.VSP(cfg)
17571752
if err != nil {
@@ -1844,7 +1839,14 @@ func (s *walletServer) PurchaseTickets(ctx context.Context,
18441839
}
18451840

18461841
if vspClient != nil {
1847-
request.VSPFeePaymentProcess = vspClient.Process
1842+
policy := vsp.Policy{
1843+
MaxFee: 0.1e8,
1844+
FeeAcct: req.Account,
1845+
ChangeAcct: req.ChangeAccount,
1846+
}
1847+
request.VSPFeePaymentProcess = func(ctx context.Context, ticketHash *chainhash.Hash, feeTx *wire.MsgTx) error {
1848+
return vspClient.Process(ctx, ticketHash, feeTx, policy)
1849+
}
18481850
request.VSPFeeProcess = vspClient.FeePercentage
18491851
}
18501852

@@ -2650,6 +2652,7 @@ func (t *ticketbuyerV2Server) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr
26502652
var vspHost string
26512653
var vspPubKey string
26522654
var vspClient *vsp.Client
2655+
var vspFeePolicy vsp.Policy
26532656
if req.VspHost != "" || req.VspPubkey != "" {
26542657
vspHost = req.VspHost
26552658
vspPubKey = req.VspPubkey
@@ -2664,16 +2667,16 @@ func (t *ticketbuyerV2Server) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr
26642667
PubKey: vspPubKey,
26652668
Dialer: nil,
26662669
Wallet: wallet,
2667-
Policy: vsp.Policy{
2668-
MaxFee: 0.1e8,
2669-
FeeAcct: req.Account,
2670-
ChangeAcct: req.Account,
2671-
},
26722670
}
26732671
vspClient, err = loader.VSP(cfg)
26742672
if err != nil {
26752673
return status.Errorf(codes.Unknown, "TicketBuyerV3 instance failed to start. Error: %v", err)
26762674
}
2675+
vspFeePolicy = vsp.Policy{
2676+
MaxFee: 0.1e8,
2677+
FeeAcct: req.Account,
2678+
ChangeAcct: req.Account,
2679+
}
26772680
}
26782681
if req.BalanceToMaintain < 0 {
26792682
return status.Errorf(codes.InvalidArgument, "Negative balance to maintain given")
@@ -2728,6 +2731,7 @@ func (t *ticketbuyerV2Server) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr
27282731
c.PoolFeeAddr = poolAddress
27292732
c.PoolFees = req.PoolFees
27302733
c.VSP = vspClient
2734+
c.VSPFeePolicy = vspFeePolicy
27312735
c.MixedAccount = mixedAccount
27322736
c.MixChange = mixedChange
27332737
c.CSPPServer = csppServer
@@ -3922,27 +3926,26 @@ func (s *walletServer) SyncVSPFailedTickets(ctx context.Context, req *pb.SyncVSP
39223926
if vspHost == "" {
39233927
return nil, status.Errorf(codes.InvalidArgument, "vsp host can not be null")
39243928
}
3925-
policy := vsp.Policy{
3926-
MaxFee: 0.1e8,
3927-
FeeAcct: req.Account,
3928-
ChangeAcct: req.ChangeAccount,
3929-
}
39303929
cfg := vsp.Config{
39313930
URL: vspHost,
39323931
PubKey: vspPubKey,
39333932
Dialer: nil,
39343933
Wallet: s.wallet,
3935-
Policy: policy,
39363934
}
39373935
vspClient, err := loader.VSP(cfg)
39383936
if err != nil {
39393937
return nil, status.Errorf(codes.Unknown, "TicketBuyerV3 instance failed to start. Error: %v", err)
39403938
}
39413939

39423940
// process tickets fee if needed.
3941+
policy := vsp.Policy{
3942+
MaxFee: 0.1e8,
3943+
FeeAcct: req.Account,
3944+
ChangeAcct: req.ChangeAccount,
3945+
}
39433946
for _, ticketHash := range failedTicketsFee {
39443947
feeTx := new(wire.MsgTx)
3945-
err := vspClient.ProcessWithPolicy(ctx, &ticketHash, feeTx, policy)
3948+
err := vspClient.Process(ctx, &ticketHash, feeTx, policy)
39463949
if err != nil {
39473950
// if it fails to process again, we log it and continue with
39483951
// the wallet start.
@@ -3963,23 +3966,22 @@ func (s *walletServer) ProcessManagedTickets(ctx context.Context, req *pb.Proces
39633966
if vspHost == "" {
39643967
return nil, status.Errorf(codes.InvalidArgument, "vsp host can not be null")
39653968
}
3966-
policy := vsp.Policy{
3967-
MaxFee: 0.1e8,
3968-
FeeAcct: req.FeeAccount,
3969-
ChangeAcct: req.ChangeAccount,
3970-
}
39713969
cfg := vsp.Config{
39723970
URL: vspHost,
39733971
PubKey: vspPubKey,
39743972
Dialer: nil,
39753973
Wallet: s.wallet,
3976-
Policy: policy,
39773974
}
39783975
vspClient, err := loader.VSP(cfg)
39793976
if err != nil {
39803977
return nil, status.Errorf(codes.Unknown, "VSPClient instance failed to start. Error: %v", err)
39813978
}
39823979

3980+
policy := vsp.Policy{
3981+
MaxFee: 0.1e8,
3982+
FeeAcct: req.FeeAccount,
3983+
ChangeAcct: req.ChangeAccount,
3984+
}
39833985
err = vspClient.ProcessManagedTickets(ctx, policy)
39843986
if err != nil {
39853987
return nil, status.Errorf(codes.Unknown, "ProcessManagedTickets failed. Error: %v", err)
@@ -3999,17 +4001,11 @@ func (s *walletServer) ProcessUnmanagedTickets(ctx context.Context, req *pb.Proc
39994001
if vspHost == "" {
40004002
return nil, status.Errorf(codes.InvalidArgument, "vsp host can not be null")
40014003
}
4002-
policy := vsp.Policy{
4003-
MaxFee: 0.1e8,
4004-
FeeAcct: req.FeeAccount,
4005-
ChangeAcct: req.ChangeAccount,
4006-
}
40074004
cfg := vsp.Config{
40084005
URL: vspHost,
40094006
PubKey: vspPubKey,
40104007
Dialer: nil,
40114008
Wallet: s.wallet,
4012-
Policy: policy,
40134009
}
40144010
vspClient, err := loader.VSP(cfg)
40154011
if err != nil {
@@ -4025,14 +4021,19 @@ func (s *walletServer) ProcessUnmanagedTickets(ctx context.Context, req *pb.Proc
40254021
return nil
40264022
})
40274023
if errors.Is(err, errUnmanagedTickets) {
4024+
policy := vsp.Policy{
4025+
MaxFee: 0.1e8,
4026+
FeeAcct: req.FeeAccount,
4027+
ChangeAcct: req.ChangeAccount,
4028+
}
40284029
vspClient.ProcessUnprocessedTickets(ctx, policy)
40294030
}
40304031

40314032
return &pb.ProcessUnmanagedTicketsResponse{}, nil
40324033
}
40334034

40344035
func (s *walletServer) SetVspdVoteChoices(ctx context.Context, req *pb.SetVspdVoteChoicesRequest) (
4035-
*pb.SetVspdVoteChoicesResponse, error) {
4036+
*pb.SetVspdVoteChoicesResponse, error) { // TODO: Remove req.FeeAccount and req.ChangeAccount.
40364037

40374038
vspHost := req.VspHost
40384039
vspPubKey := req.VspPubkey
@@ -4042,17 +4043,11 @@ func (s *walletServer) SetVspdVoteChoices(ctx context.Context, req *pb.SetVspdVo
40424043
if vspHost == "" {
40434044
return nil, status.Errorf(codes.InvalidArgument, "vsp host can not be null")
40444045
}
4045-
policy := vsp.Policy{
4046-
MaxFee: 0.1e8,
4047-
FeeAcct: req.FeeAccount,
4048-
ChangeAcct: req.ChangeAccount,
4049-
}
40504046
cfg := vsp.Config{
40514047
URL: vspHost,
40524048
PubKey: vspPubKey,
40534049
Dialer: nil,
40544050
Wallet: s.wallet,
4055-
Policy: policy,
40564051
}
40574052
vspClient, err := loader.VSP(cfg)
40584053
if err != nil {

0 commit comments

Comments
 (0)