Skip to content

Commit

Permalink
Add cfg to ticketbuyer.New.
Browse files Browse the repository at this point in the history
Ticket buyer config is only set once at creation and then not accessed
again from externally, so there is no need for an AccessConfig func, it
can just be passed into New.
  • Loading branch information
jholdstock committed Sep 9, 2024
1 parent dfab814 commit 83dd3e5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 100 deletions.
117 changes: 58 additions & 59 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(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,
}, w)

log.Infof("Starting auto transaction creator")
tbdone := make(chan struct{})
go func() {
Expand Down
51 changes: 23 additions & 28 deletions internal/rpc/rpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2571,17 +2571,14 @@ 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(ticketbuyer.Config{
Mixing: req.CsppServer != "",
MixedAccountBranch: req.MixedAccountBranch,
MixedAccount: req.MixedAccount,
ChangeAccount: req.ChangeAccount,
BuyTickets: false,
MixChange: true,
}, wallet)

if len(req.Passphrase) > 0 {
lock := make(chan time.Time, 1)
Expand Down Expand Up @@ -2713,23 +2710,21 @@ 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(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,
}, wallet)

if len(req.Passphrase) > 0 {
lock := make(chan time.Time, 1)
Expand Down
16 changes: 3 additions & 13 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(cfg Config, w *wallet.Wallet) *TB {
return &TB{cfg: cfg, wallet: w}
}

// Run executes the ticket buyer. If the private passphrase is incorrect, or
Expand Down Expand Up @@ -313,16 +313,6 @@ func (tb *TB) buy(ctx context.Context, passphrase []byte, tip *wire.BlockHeader,
return err
}

// AccessConfig runs f with the current config passed as a parameter. The
// config is protected by a mutex and this function is safe for concurrent
// access to read or modify the config. It is unsafe to leak a pointer to the
// config, but a copy of *cfg is legal.
func (tb *TB) AccessConfig(f func(cfg *Config)) {
tb.mu.Lock()
f(&tb.cfg)
tb.mu.Unlock()
}

func (tb *TB) mixChange(ctx context.Context, cfg *Config) error {
// Read config
mixing := cfg.Mixing
Expand Down

0 comments on commit 83dd3e5

Please sign in to comment.