diff --git a/dcrwallet.go b/dcrwallet.go index 38eb48201..5982b6e11 100644 --- a/dcrwallet.go +++ b/dcrwallet.go @@ -293,71 +293,70 @@ func run(ctx context.Context) error { } } - var tb *ticketbuyer.TB if cfg.MixChange || cfg.EnableTicketBuyer { - tb = ticketbuyer.New(w) - } - - var lastFlag, lastLookup string - lookup := func(flag, name string) (account uint32) { - if tb != nil && err == nil { - lastFlag = flag - lastLookup = name - account, err = w.AccountNumber(ctx, name) + var err error + var lastFlag, lastLookup string + lookup := func(flag, name string) (account uint32) { + if err == nil { + lastFlag = flag + lastLookup = name + account, err = w.AccountNumber(ctx, name) + } + return } - return - } - var ( - purchaseAccount uint32 // enableticketbuyer - votingAccount uint32 // enableticketbuyer - mixedAccount uint32 // (enableticketbuyer && mixing) || mixchange - changeAccount uint32 // (enableticketbuyer && mixing) || mixchange - ticketSplitAccount uint32 // enableticketbuyer && mixing - - votingAddr = cfg.TBOpts.votingAddress - ) - if cfg.EnableTicketBuyer { - purchaseAccount = lookup("purchaseaccount", cfg.PurchaseAccount) - - if cfg.Mixing && cfg.TBOpts.VotingAccount == "" { - err := errors.New("cannot run mixed ticketbuyer without --votingaccount") - log.Error(err) - return err + var ( + purchaseAccount uint32 // enableticketbuyer + votingAccount uint32 // enableticketbuyer + mixedAccount uint32 // (enableticketbuyer && mixing) || mixchange + changeAccount uint32 // (enableticketbuyer && mixing) || mixchange + ticketSplitAccount uint32 // enableticketbuyer && mixing + + votingAddr = cfg.TBOpts.votingAddress + ) + if cfg.EnableTicketBuyer { + purchaseAccount = lookup("purchaseaccount", cfg.PurchaseAccount) + + if cfg.Mixing && cfg.TBOpts.VotingAccount == "" { + err := errors.New("cannot run mixed ticketbuyer without --votingaccount") + log.Error(err) + return err + } + if cfg.TBOpts.VotingAccount != "" { + votingAccount = lookup("ticketbuyer.votingaccount", cfg.TBOpts.VotingAccount) + votingAddr = nil + } } - if cfg.TBOpts.VotingAccount != "" { - votingAccount = lookup("ticketbuyer.votingaccount", cfg.TBOpts.VotingAccount) - votingAddr = nil + if (cfg.EnableTicketBuyer && cfg.Mixing) || cfg.MixChange { + mixedAccount = lookup("mixedaccount", cfg.mixedAccount) + changeAccount = lookup("changeaccount", cfg.ChangeAccount) + } + if cfg.EnableTicketBuyer && cfg.Mixing { + ticketSplitAccount = lookup("ticketsplitaccount", cfg.TicketSplitAccount) + } + + // Check if any of the above calls to lookup() have failed. + if err != nil { + log.Errorf("%s: account %q does not exist", lastFlag, lastLookup) + return err } - } - if (cfg.EnableTicketBuyer && cfg.Mixing) || cfg.MixChange { - mixedAccount = lookup("mixedaccount", cfg.mixedAccount) - changeAccount = lookup("changeaccount", cfg.ChangeAccount) - } - if cfg.EnableTicketBuyer && cfg.Mixing { - ticketSplitAccount = lookup("ticketsplitaccount", cfg.TicketSplitAccount) - } - if err != nil { - log.Errorf("%s: account %q does not exist", lastFlag, lastLookup) - return err - } - if tb != nil { // Start a ticket buyer. - tb.AccessConfig(func(c *ticketbuyer.Config) { - c.BuyTickets = cfg.EnableTicketBuyer - c.Account = purchaseAccount - c.Maintain = cfg.TBOpts.BalanceToMaintainAbsolute.Amount - c.VotingAddr = votingAddr - c.Limit = int(cfg.TBOpts.Limit) - c.VotingAccount = votingAccount - c.Mixing = cfg.Mixing - c.MixChange = cfg.MixChange - c.MixedAccount = mixedAccount - c.MixedAccountBranch = cfg.mixedBranch - c.TicketSplitAccount = ticketSplitAccount - c.ChangeAccount = changeAccount - c.VSP = vspClient + tb := ticketbuyer.New(w, ticketbuyer.Config{ + BuyTickets: cfg.EnableTicketBuyer, + Account: purchaseAccount, + Maintain: cfg.TBOpts.BalanceToMaintainAbsolute.Amount, + VotingAddr: votingAddr, + Limit: int(cfg.TBOpts.Limit), + VotingAccount: votingAccount, + Mixing: cfg.Mixing, + MixChange: cfg.MixChange, + MixedAccount: mixedAccount, + MixedAccountBranch: cfg.mixedBranch, + TicketSplitAccount: ticketSplitAccount, + ChangeAccount: changeAccount, + VSP: vspClient, }) + log.Infof("Starting auto transaction creator") tbdone := make(chan struct{}) go func() { diff --git a/internal/rpc/rpcserver/server.go b/internal/rpc/rpcserver/server.go index 031007394..e8c0cec86 100644 --- a/internal/rpc/rpcserver/server.go +++ b/internal/rpc/rpcserver/server.go @@ -2571,16 +2571,13 @@ func (t *accountMixerServer) RunAccountMixer(req *pb.RunAccountMixerRequest, svr return status.Errorf(codes.FailedPrecondition, "Wallet has not been loaded") } - tb := ticketbuyer.New(wallet) - - // Set ticketbuyer config - tb.AccessConfig(func(c *ticketbuyer.Config) { - c.Mixing = req.CsppServer != "" - c.MixedAccountBranch = req.MixedAccountBranch - c.MixedAccount = req.MixedAccount - c.ChangeAccount = req.ChangeAccount - c.BuyTickets = false - c.MixChange = true + tb := ticketbuyer.New(wallet, ticketbuyer.Config{ + Mixing: req.CsppServer != "", + MixedAccountBranch: req.MixedAccountBranch, + MixedAccount: req.MixedAccount, + ChangeAccount: req.ChangeAccount, + BuyTickets: false, + MixChange: true, }) if len(req.Passphrase) > 0 { @@ -2713,22 +2710,20 @@ func (t *ticketbuyerServer) RunTicketBuyer(req *pb.RunTicketBuyerRequest, svr pb // is defaulted to 20. limit := int(req.Limit) - tb := ticketbuyer.New(wallet) - // Set ticketbuyer config - tb.AccessConfig(func(c *ticketbuyer.Config) { - c.BuyTickets = true - c.Account = req.Account - c.VotingAccount = req.VotingAccount - c.Maintain = dcrutil.Amount(req.BalanceToMaintain) - c.VotingAddr = votingAddress - c.VSP = vspClient - c.Mixing = csppServer != "" - c.MixedAccount = mixedAccount - c.MixChange = mixedChange - c.ChangeAccount = changeAccount - c.MixedAccountBranch = mixedAccountBranch - c.TicketSplitAccount = mixedSplitAccount - c.Limit = limit + tb := ticketbuyer.New(wallet, ticketbuyer.Config{ + BuyTickets: true, + Account: req.Account, + VotingAccount: req.VotingAccount, + Maintain: dcrutil.Amount(req.BalanceToMaintain), + VotingAddr: votingAddress, + VSP: vspClient, + Mixing: csppServer != "", + MixedAccount: mixedAccount, + MixChange: mixedChange, + ChangeAccount: changeAccount, + MixedAccountBranch: mixedAccountBranch, + TicketSplitAccount: mixedSplitAccount, + Limit: limit, }) if len(req.Passphrase) > 0 { diff --git a/ticketbuyer/tb.go b/ticketbuyer/tb.go index 8fb06b8f4..4bf892f88 100644 --- a/ticketbuyer/tb.go +++ b/ticketbuyer/tb.go @@ -60,9 +60,9 @@ type TB struct { mu sync.Mutex } -// New returns a new TB to buy tickets from a wallet using the default config. -func New(w *wallet.Wallet) *TB { - return &TB{wallet: w} +// New returns a new TB to buy tickets from a wallet. +func New(w *wallet.Wallet, cfg Config) *TB { + return &TB{wallet: w, cfg: cfg} } // Run executes the ticket buyer. If the private passphrase is incorrect, or