Skip to content

Commit

Permalink
Move VSP client code to wallet package.
Browse files Browse the repository at this point in the history
This moves the VSP client code from its own package into the wallet package.
Moving this code should have minimal impact on consumers of the vsp package
because they will have been importing the wallet package already due to the fact
that a VSP client could not be created without an instance of wallet.

The benefits of this are:

- vsp client creation now exists only in the wallet package, rather than being
  scattered across 3 packages.
- vsp client loading funcs and variables are no longer package global.
- simplifies the creation of a vsp client because the wallet no longer needs to
  be passed into the func as a param.
- no longer necessary to abstract the vsp client away by referring to its
  methods, the client can just be used directly.
  • Loading branch information
jholdstock committed Sep 19, 2024
1 parent 17dd448 commit d7f30a9
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 185 deletions.
11 changes: 4 additions & 7 deletions dcrwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"decred.org/dcrwallet/v5/spv"
"decred.org/dcrwallet/v5/ticketbuyer"
"decred.org/dcrwallet/v5/version"
"decred.org/dcrwallet/v5/vsp"
"decred.org/dcrwallet/v5/wallet"
"github.com/decred/dcrd/addrmgr/v2"
"github.com/decred/dcrd/wire"
Expand Down Expand Up @@ -190,7 +189,7 @@ func run(ctx context.Context) error {
}()

// Open the wallet when --noinitialload was not set.
var vspClient *vsp.Client
var vspClient *wallet.VSPClient
passphrase := []byte{}
if !cfg.NoInitialLoad {
walletPass := []byte(cfg.WalletPass)
Expand Down Expand Up @@ -271,19 +270,17 @@ func run(ctx context.Context) error {
cfg.PurchaseAccount, err)
return err
}
vspCfg := vsp.Config{
vspCfg := wallet.VSPClientConfig{
URL: cfg.VSPOpts.URL,
PubKey: cfg.VSPOpts.PubKey,
Dialer: cfg.dial,
Wallet: w,
Policy: &vsp.Policy{
Policy: &wallet.VSPPolicy{
MaxFee: cfg.VSPOpts.MaxFee.Amount,
FeeAcct: purchaseAcct,
ChangeAcct: changeAcct,
},
Params: w.ChainParams(),
}
vspClient, err = ldr.VSP(vspCfg)
vspClient, err = w.VSP(vspCfg)
if err != nil {
log.Errorf("vsp: %v", err)
return err
Expand Down
30 changes: 12 additions & 18 deletions internal/rpc/jsonrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (

"decred.org/dcrwallet/v5/chain"
"decred.org/dcrwallet/v5/errors"
"decred.org/dcrwallet/v5/internal/loader"
"decred.org/dcrwallet/v5/p2p"
"decred.org/dcrwallet/v5/rpc/client/dcrd"
"decred.org/dcrwallet/v5/rpc/jsonrpc/types"
"decred.org/dcrwallet/v5/spv"
"decred.org/dcrwallet/v5/version"
"decred.org/dcrwallet/v5/vsp"
"decred.org/dcrwallet/v5/wallet"
"decred.org/dcrwallet/v5/wallet/txauthor"
"decred.org/dcrwallet/v5/wallet/txrules"
Expand Down Expand Up @@ -3344,21 +3342,19 @@ func (s *Server) purchaseTicket(ctx context.Context, icmd any) (any, error) {
}
}

var vspClient *vsp.Client
var vspClient *wallet.VSPClient
if s.cfg.VSPHost != "" {
cfg := vsp.Config{
cfg := wallet.VSPClientConfig{
URL: s.cfg.VSPHost,
PubKey: s.cfg.VSPPubKey,
Dialer: s.cfg.Dial,
Wallet: w,
Policy: &vsp.Policy{
Policy: &wallet.VSPPolicy{
MaxFee: s.cfg.VSPMaxFee,
FeeAcct: account,
ChangeAcct: changeAccount,
},
Params: w.ChainParams(),
}
vspClient, err = loader.VSP(cfg)
vspClient, err = w.VSP(cfg)
if err != nil {
return nil, rpcErrorf(dcrjson.ErrRPCMisc,
"VSP Server instance failed to start: %v", err)
Expand All @@ -3378,11 +3374,8 @@ func (s *Server) purchaseTicket(ctx context.Context, icmd any) (any, error) {
MixedAccountBranch: mixedAccountBranch,
MixedSplitAccount: mixedSplitAccount,
ChangeAccount: changeAccount,
}

if vspClient != nil {
request.VSPFeePaymentProcess = vspClient.Process
request.VSPFeePercent = vspClient.FeePercentage
VSPClient: vspClient,
}

ticketsResponse, err := w.PurchaseTickets(ctx, n, request)
Expand Down Expand Up @@ -3447,16 +3440,17 @@ func (s *Server) processUnmanagedTicket(ctx context.Context, icmd any) (any, err
if vspHost == "" {
return nil, rpcErrorf(dcrjson.ErrRPCInvalidParameter, "vsphost must be set in options")
}
vspClient, err := loader.LookupVSP(vspHost)
if err != nil {
return nil, err
}

w, ok := s.walletLoader.LoadedWallet()
if !ok {
return nil, errUnloadedWallet
}

vspClient, err := w.LookupVSP(vspHost)
if err != nil {
return nil, err
}

ticket, err := w.NewVSPTicket(ctx, hash)
if err != nil {
return nil, err
Expand Down Expand Up @@ -4703,7 +4697,7 @@ func (s *Server) updateVSPVoteChoices(ctx context.Context, w *wallet.Wallet, tic
}
return err
}
vspClient, err := loader.LookupVSP(vspHost)
vspClient, err := w.LookupVSP(vspHost)
if err != nil {
return err
}
Expand All @@ -4726,7 +4720,7 @@ func (s *Server) updateVSPVoteChoices(ctx context.Context, w *wallet.Wallet, tic
}
return err
}
vspClient, err := loader.LookupVSP(vspHost)
vspClient, err := w.LookupVSP(vspHost)
if err != nil {
return err
}
Expand Down
74 changes: 29 additions & 45 deletions internal/rpc/rpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
pb "decred.org/dcrwallet/v5/rpc/walletrpc"
"decred.org/dcrwallet/v5/spv"
"decred.org/dcrwallet/v5/ticketbuyer"
"decred.org/dcrwallet/v5/vsp"
"decred.org/dcrwallet/v5/wallet"
"decred.org/dcrwallet/v5/wallet/txauthor"
"decred.org/dcrwallet/v5/wallet/txrules"
Expand Down Expand Up @@ -1805,7 +1804,7 @@ func (s *walletServer) PurchaseTickets(ctx context.Context,
// new vspd request
var vspHost string
var vspPubKey string
var vspClient *vsp.Client
var vspClient *wallet.VSPClient
if req.VspHost != "" || req.VspPubkey != "" {
vspHost = req.VspHost
vspPubKey = req.VspPubkey
Expand All @@ -1815,19 +1814,17 @@ func (s *walletServer) PurchaseTickets(ctx context.Context,
if vspHost == "" {
return nil, status.Errorf(codes.InvalidArgument, "vsp host can not be null")
}
cfg := vsp.Config{
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Wallet: s.wallet,
Policy: &vsp.Policy{
Policy: &wallet.VSPPolicy{
MaxFee: 0.1e8,
FeeAcct: req.Account,
ChangeAcct: req.ChangeAccount,
},
Params: s.wallet.ChainParams(),
}
vspClient, err = loader.VSP(cfg)
vspClient, err = s.wallet.VSP(cfg)
if err != nil {
return nil, status.Errorf(codes.Unknown, "VSP Server instance failed to start: %v", err)
}
Expand Down Expand Up @@ -1894,11 +1891,8 @@ func (s *walletServer) PurchaseTickets(ctx context.Context,
MixedAccountBranch: mixedAccountBranch,
MixedSplitAccount: mixedSplitAccount,
ChangeAccount: changeAccount,
}

if vspClient != nil {
request.VSPFeePaymentProcess = vspClient.Process
request.VSPFeePercent = vspClient.FeePercentage
VSPClient: vspClient,
}

// If dontSignTx is false we unlock the wallet so we can sign the tx.
Expand Down Expand Up @@ -2608,7 +2602,7 @@ func StartTicketBuyerService(server *grpc.Server, loader *loader.Loader) {

// RunTicketBuyer starts the automatic ticket buyer.
func (t *ticketbuyerServer) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr pb.TicketBuyerService_RunTicketBuyerServer) error {
wallet, ok := t.loader.LoadedWallet()
w, ok := t.loader.LoadedWallet()
if !ok {
return status.Errorf(codes.FailedPrecondition, "Wallet has not been loaded")
}
Expand All @@ -2619,7 +2613,7 @@ func (t *ticketbuyerServer) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr pb
var err error
var vspHost string
var vspPubKey string
var vspClient *vsp.Client
var vspClient *wallet.VSPClient
if req.VspHost != "" || req.VspPubkey != "" {
vspHost = req.VspHost
vspPubKey = req.VspPubkey
Expand All @@ -2629,19 +2623,17 @@ func (t *ticketbuyerServer) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr pb
if vspHost == "" {
return status.Errorf(codes.InvalidArgument, "vsp host can not be null")
}
cfg := vsp.Config{
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Wallet: wallet,
Policy: &vsp.Policy{
Policy: &wallet.VSPPolicy{
MaxFee: 0.1e8,
FeeAcct: req.Account,
ChangeAcct: req.Account,
},
Params: wallet.ChainParams(),
}
vspClient, err = loader.VSP(cfg)
vspClient, err = w.VSP(cfg)
if err != nil {
return status.Errorf(codes.Unknown, "TicketBuyer instance failed to start. Error: %v", err)
}
Expand All @@ -2659,7 +2651,7 @@ func (t *ticketbuyerServer) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr pb
if req.EnableMixing {
mixedChange = true
mixedAccount = req.MixedAccount
_, err = wallet.AccountName(ctx, mixedAccount)
_, err = w.AccountName(ctx, mixedAccount)
if err != nil {
return status.Errorf(codes.InvalidArgument,
"Mixing requested, but error on mixed account: %v", err)
Expand All @@ -2670,12 +2662,12 @@ func (t *ticketbuyerServer) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr pb
"MixedAccountBranch should be 0 or 1.")
}
mixedSplitAccount = req.MixedSplitAccount
_, err = wallet.AccountName(ctx, mixedSplitAccount)
_, err = w.AccountName(ctx, mixedSplitAccount)
if err != nil {
return status.Errorf(codes.InvalidArgument,
"Mixing requested, but error on mixedSplitAccount: %v", err)
}
_, err = wallet.AccountName(ctx, changeAccount)
_, err = w.AccountName(ctx, changeAccount)
if err != nil {
return status.Errorf(codes.InvalidArgument,
"Mixing requested, but error on changeAccount: %v", err)
Expand All @@ -2686,7 +2678,7 @@ func (t *ticketbuyerServer) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr pb
// is defaulted to 20.
limit := int(req.Limit)

tb := ticketbuyer.New(wallet, ticketbuyer.Config{
tb := ticketbuyer.New(w, ticketbuyer.Config{
BuyTickets: true,
Account: req.Account,
VotingAccount: req.VotingAccount,
Expand All @@ -2709,7 +2701,7 @@ func (t *ticketbuyerServer) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr pb
zero(req.Passphrase)
}

err = wallet.Unlock(svr.Context(), req.Passphrase, lock)
err = w.Unlock(svr.Context(), req.Passphrase, lock)
if err != nil {
return translateError(err)
}
Expand Down Expand Up @@ -4112,20 +4104,18 @@ func (s *walletServer) SyncVSPFailedTickets(ctx context.Context, req *pb.SyncVSP
if vspHost == "" {
return nil, status.Errorf(codes.InvalidArgument, "vsp host can not be null")
}
policy := &vsp.Policy{
policy := &wallet.VSPPolicy{
MaxFee: 0.1e8,
FeeAcct: req.Account,
ChangeAcct: req.ChangeAccount,
}
cfg := vsp.Config{
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Wallet: s.wallet,
Policy: policy,
Params: s.wallet.ChainParams(),
}
vspClient, err := loader.VSP(cfg)
vspClient, err := s.wallet.VSP(cfg)
if err != nil {
return nil, status.Errorf(codes.Unknown, "TicketBuyer instance failed to start. Error: %v", err)
}
Expand Down Expand Up @@ -4161,20 +4151,18 @@ func (s *walletServer) ProcessManagedTickets(ctx context.Context, req *pb.Proces
if vspHost == "" {
return nil, status.Errorf(codes.InvalidArgument, "vsp host can not be null")
}
policy := &vsp.Policy{
policy := &wallet.VSPPolicy{
MaxFee: 0.1e8,
FeeAcct: req.FeeAccount,
ChangeAcct: req.ChangeAccount,
}
cfg := vsp.Config{
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Wallet: s.wallet,
Policy: policy,
Params: s.wallet.ChainParams(),
}
vspClient, err := loader.VSP(cfg)
vspClient, err := s.wallet.VSP(cfg)
if err != nil {
return nil, status.Errorf(codes.Unknown, "VSPClient instance failed to start. Error: %v", err)
}
Expand Down Expand Up @@ -4203,20 +4191,18 @@ func (s *walletServer) ProcessUnmanagedTickets(ctx context.Context, req *pb.Proc
if vspHost == "" {
return nil, status.Errorf(codes.InvalidArgument, "vsp host can not be null")
}
policy := &vsp.Policy{
policy := &wallet.VSPPolicy{
MaxFee: 0.1e8,
FeeAcct: req.FeeAccount,
ChangeAcct: req.ChangeAccount,
}
cfg := vsp.Config{
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Wallet: s.wallet,
Policy: policy,
Params: s.wallet.ChainParams(),
}
vspClient, err := loader.VSP(cfg)
vspClient, err := s.wallet.VSP(cfg)
if err != nil {
return nil, status.Errorf(codes.Unknown, "VSPClient instance failed to start. Error: %v", err)
}
Expand All @@ -4242,20 +4228,18 @@ func (s *walletServer) SetVspdVoteChoices(ctx context.Context, req *pb.SetVspdVo
if vspHost == "" {
return nil, status.Errorf(codes.InvalidArgument, "vsp host can not be null")
}
policy := &vsp.Policy{
policy := &wallet.VSPPolicy{
MaxFee: 0.1e8,
FeeAcct: req.FeeAccount,
ChangeAcct: req.ChangeAccount,
}
cfg := vsp.Config{
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Wallet: s.wallet,
Policy: policy,
Params: s.wallet.ChainParams(),
}
vspClient, err := loader.VSP(cfg)
vspClient, err := s.wallet.VSP(cfg)
if err != nil {
return nil, status.Errorf(codes.Unknown, "VSPClient instance failed to start. Error: %v", err)
}
Expand Down Expand Up @@ -4320,7 +4304,7 @@ func (s *walletServer) SetVspdVoteChoices(ctx context.Context, req *pb.SetVspdVo
return &pb.SetVspdVoteChoicesResponse{}, nil
}

func marshalVSPTrackedTickets(tickets []*vsp.TicketInfo) []*pb.GetTrackedVSPTicketsResponse_Ticket {
func marshalVSPTrackedTickets(tickets []*wallet.VSPTicketInfo) []*pb.GetTrackedVSPTicketsResponse_Ticket {
res := make([]*pb.GetTrackedVSPTicketsResponse_Ticket, len(tickets))
for i, ticket := range tickets {
res[i] = &pb.GetTrackedVSPTicketsResponse_Ticket{
Expand All @@ -4336,7 +4320,7 @@ func marshalVSPTrackedTickets(tickets []*vsp.TicketInfo) []*pb.GetTrackedVSPTick
}

func (s *walletServer) GetTrackedVSPTickets(ctx context.Context, req *pb.GetTrackedVSPTicketsRequest) (*pb.GetTrackedVSPTicketsResponse, error) {
vspClients := loader.AllVSPs()
vspClients := s.wallet.AllVSPs()
res := &pb.GetTrackedVSPTicketsResponse{
Vsps: make([]*pb.GetTrackedVSPTicketsResponse_VSP, 0, len(vspClients)),
}
Expand Down
Loading

0 comments on commit d7f30a9

Please sign in to comment.