Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cfg to ticketbuyer.New. #2415

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 57 additions & 58 deletions dcrwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
47 changes: 21 additions & 26 deletions internal/rpc/rpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions ticketbuyer/tb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading