Skip to content

Commit

Permalink
Make VSP clients respect tor config.
Browse files Browse the repository at this point in the history
Ensure dialer is set correctly so VSPs are reached over tor if requested
in config.

This also has a nice side-effect of simplifying VSP client creation.
  • Loading branch information
jholdstock committed Sep 18, 2024
1 parent e0384fa commit 2ae3de2
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 19 deletions.
3 changes: 1 addition & 2 deletions dcrwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func run(ctx context.Context) error {
loader := ldr.NewLoader(activeNet.Params, dbDir, cfg.EnableVoting,
cfg.GapLimit, cfg.WatchLast, cfg.AllowHighFees, cfg.RelayFee.Amount,
cfg.AccountGapLimit, cfg.DisableCoinTypeUpgrades, !cfg.Mixing,
cfg.ManualTickets, cfg.MixSplitLimit)
cfg.ManualTickets, cfg.MixSplitLimit, cfg.dial)

// Stop any services started by the loader after the shutdown procedure is
// initialized and this function returns.
Expand Down Expand Up @@ -273,7 +273,6 @@ func run(ctx context.Context) error {
vspCfg := wallet.VSPClientConfig{
URL: cfg.VSPOpts.URL,
PubKey: cfg.VSPOpts.PubKey,
Dialer: cfg.dial,
Policy: &wallet.VSPPolicy{
MaxFee: cfg.VSPOpts.MaxFee.Amount,
FeeAcct: purchaseAcct,
Expand Down
7 changes: 6 additions & 1 deletion internal/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ type Loader struct {
manualTickets bool
relayFee dcrutil.Amount
mixSplitLimit int
dialer wallet.DialFunc

mu sync.Mutex
}

// NewLoader constructs a Loader.
func NewLoader(chainParams *chaincfg.Params, dbDirPath string, votingEnabled bool, gapLimit uint32,
watchLast uint32, allowHighFees bool, relayFee dcrutil.Amount, accountGapLimit int,
disableCoinTypeUpgrades bool, disableMixing bool, manualTickets bool, mixSplitLimit int) *Loader {
disableCoinTypeUpgrades bool, disableMixing bool, manualTickets bool, mixSplitLimit int, dialer wallet.DialFunc) *Loader {

return &Loader{
chainParams: chainParams,
Expand All @@ -69,6 +70,7 @@ func NewLoader(chainParams *chaincfg.Params, dbDirPath string, votingEnabled boo
manualTickets: manualTickets,
relayFee: relayFee,
mixSplitLimit: mixSplitLimit,
dialer: dialer,
}
}

Expand Down Expand Up @@ -176,6 +178,7 @@ func (l *Loader) CreateWatchingOnlyWallet(ctx context.Context, extendedPubKey st
RelayFee: l.relayFee,
MixSplitLimit: l.mixSplitLimit,
Params: l.chainParams,
Dialer: l.dialer,
}
w, err = wallet.Open(ctx, cfg)
if err != nil {
Expand Down Expand Up @@ -262,6 +265,7 @@ func (l *Loader) CreateNewWallet(ctx context.Context, pubPassphrase, privPassphr
AllowHighFees: l.allowHighFees,
RelayFee: l.relayFee,
Params: l.chainParams,
Dialer: l.dialer,
}
w, err = wallet.Open(ctx, cfg)
if err != nil {
Expand Down Expand Up @@ -319,6 +323,7 @@ func (l *Loader) OpenExistingWallet(ctx context.Context, pubPassphrase []byte) (
RelayFee: l.relayFee,
MixSplitLimit: l.mixSplitLimit,
Params: l.chainParams,
Dialer: l.dialer,
}
w, err = wallet.Open(ctx, cfg)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion internal/rpc/jsonrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -3347,7 +3347,6 @@ func (s *Server) purchaseTicket(ctx context.Context, icmd any) (any, error) {
cfg := wallet.VSPClientConfig{
URL: s.cfg.VSPHost,
PubKey: s.cfg.VSPPubKey,
Dialer: s.cfg.Dial,
Policy: &wallet.VSPPolicy{
MaxFee: s.cfg.VSPMaxFee,
FeeAcct: account,
Expand Down
8 changes: 0 additions & 8 deletions internal/rpc/rpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,6 @@ func (*versionServer) Version(ctx context.Context, req *pb.VersionRequest) (*pb.
}, nil
}

type dialFunc func(ctx context.Context, network, addr string) (net.Conn, error)

// StartWalletService starts the WalletService.
func StartWalletService(server *grpc.Server, wallet *wallet.Wallet) {
if walletService.ready.Swap(1) != 0 {
Expand Down Expand Up @@ -1817,7 +1815,6 @@ func (s *walletServer) PurchaseTickets(ctx context.Context,
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Policy: &wallet.VSPPolicy{
MaxFee: 0.1e8,
FeeAcct: req.Account,
Expand Down Expand Up @@ -2626,7 +2623,6 @@ func (t *ticketbuyerServer) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr pb
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Policy: &wallet.VSPPolicy{
MaxFee: 0.1e8,
FeeAcct: req.Account,
Expand Down Expand Up @@ -4112,7 +4108,6 @@ func (s *walletServer) SyncVSPFailedTickets(ctx context.Context, req *pb.SyncVSP
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Policy: policy,
}
vspClient, err := s.wallet.VSP(cfg)
Expand Down Expand Up @@ -4159,7 +4154,6 @@ func (s *walletServer) ProcessManagedTickets(ctx context.Context, req *pb.Proces
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Policy: policy,
}
vspClient, err := s.wallet.VSP(cfg)
Expand Down Expand Up @@ -4199,7 +4193,6 @@ func (s *walletServer) ProcessUnmanagedTickets(ctx context.Context, req *pb.Proc
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Policy: policy,
}
vspClient, err := s.wallet.VSP(cfg)
Expand Down Expand Up @@ -4236,7 +4229,6 @@ func (s *walletServer) SetVspdVoteChoices(ctx context.Context, req *pb.SetVspdVo
cfg := wallet.VSPClientConfig{
URL: vspHost,
PubKey: vspPubKey,
Dialer: nil,
Policy: policy,
}
vspClient, err := s.wallet.VSP(cfg)
Expand Down
7 changes: 2 additions & 5 deletions wallet/vsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,12 @@ type VSPClientConfig struct {
// PubKey specifies the VSP's base64 encoded public key
PubKey string

// Dialer specifies an optional dialer when connecting to the VSP.
Dialer DialFunc

// Default policy for fee payments unless another is provided by the
// caller.
Policy *VSPPolicy
}

func (w *Wallet) NewVSPClient(cfg VSPClientConfig, log slog.Logger) (*VSPClient, error) {
func (w *Wallet) NewVSPClient(cfg VSPClientConfig, log slog.Logger, dialer DialFunc) (*VSPClient, error) {
u, err := url.Parse(cfg.URL)
if err != nil {
return nil, err
Expand All @@ -74,7 +71,7 @@ func (w *Wallet) NewVSPClient(cfg VSPClientConfig, log slog.Logger) (*VSPClient,
Log: log,
}
client.Transport = &http.Transport{
DialContext: cfg.Dialer,
DialContext: dialer,
}

v := &VSPClient{
Expand Down
2 changes: 1 addition & 1 deletion wallet/vspclientloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (w *Wallet) VSP(cfg VSPClientConfig) (*VSPClient, error) {
if ok {
return client, nil
}
client, err := w.NewVSPClient(cfg, loggers.VspcLog)
client, err := w.NewVSPClient(cfg, loggers.VspcLog, w.dialer)
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ type Wallet struct {

vspClientsMu sync.Mutex
vspClients map[string]*VSPClient

dialer DialFunc
}

// Config represents the configuration options needed to initialize a wallet.
Expand All @@ -194,6 +196,8 @@ type Config struct {
AllowHighFees bool
RelayFee dcrutil.Amount
Params *chaincfg.Params

Dialer DialFunc
}

// DisapprovePercent returns the wallet's block disapproval percentage.
Expand Down Expand Up @@ -5406,6 +5410,8 @@ func Open(ctx context.Context, cfg *Config) (*Wallet, error) {
mixing: !cfg.DisableMixing,

vspClients: make(map[string]*VSPClient),

dialer: cfg.Dialer,
}

// Open database managers
Expand Down
2 changes: 1 addition & 1 deletion walletsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func createWallet(ctx context.Context, cfg *config) error {
loader := loader.NewLoader(activeNet.Params, dbDir, cfg.EnableVoting,
cfg.GapLimit, cfg.WatchLast, cfg.AllowHighFees, cfg.RelayFee.Amount,
cfg.AccountGapLimit, cfg.DisableCoinTypeUpgrades, !cfg.Mixing,
cfg.ManualTickets, cfg.MixSplitLimit)
cfg.ManualTickets, cfg.MixSplitLimit, cfg.dial)

var privPass, pubPass, seed []byte
var imported bool
Expand Down

0 comments on commit 2ae3de2

Please sign in to comment.