Skip to content

Commit b646752

Browse files
jholdstockjrick
authored andcommitted
multi: Introduce AgendaChoices type.
Replaces []AgendaChoice. This makes it easy to pass choices into vspd client as a map[string]string instead of a wallet type.
1 parent 816f16d commit b646752

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

internal/rpc/jsonrpc/methods.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4691,7 +4691,7 @@ func (s *Server) setVoteChoice(ctx context.Context, icmd interface{}) (interface
46914691
ticketHash = hash
46924692
}
46934693

4694-
choice := []wallet.AgendaChoice{
4694+
choice := wallet.AgendaChoices{
46954695
{
46964696
AgendaID: cmd.AgendaID,
46974697
ChoiceID: cmd.ChoiceID,
@@ -4708,7 +4708,8 @@ func (s *Server) setVoteChoice(ctx context.Context, icmd interface{}) (interface
47084708
}
47094709

47104710
func (s *Server) updateVSPVoteChoices(ctx context.Context, w *wallet.Wallet, ticketHash *chainhash.Hash,
4711-
choices []wallet.AgendaChoice, tspendPolicy map[string]string, treasuryPolicy map[string]string) error {
4711+
choices wallet.AgendaChoices, tspendPolicy map[string]string, treasuryPolicy map[string]string) error {
4712+
47124713
if ticketHash != nil {
47134714
vspHost, err := w.VSPHostForTicket(ctx, ticketHash)
47144715
if err != nil {
@@ -4722,7 +4723,7 @@ func (s *Server) updateVSPVoteChoices(ctx context.Context, w *wallet.Wallet, tic
47224723
if err != nil {
47234724
return err
47244725
}
4725-
err = vspClient.SetVoteChoice(ctx, ticketHash, choices, tspendPolicy, treasuryPolicy)
4726+
err = vspClient.SetVoteChoice(ctx, ticketHash, choices.Map(), tspendPolicy, treasuryPolicy)
47264727
return err
47274728
}
47284729
var firstErr error
@@ -4743,7 +4744,7 @@ func (s *Server) updateVSPVoteChoices(ctx context.Context, w *wallet.Wallet, tic
47434744
}
47444745
// Never return errors here, so all tickets are tried.
47454746
// The first error will be returned to the user.
4746-
err = vspClient.SetVoteChoice(ctx, hash, choices, tspendPolicy, treasuryPolicy)
4747+
err = vspClient.SetVoteChoice(ctx, hash, choices.Map(), tspendPolicy, treasuryPolicy)
47474748
if err != nil && firstErr == nil {
47484749
firstErr = err
47494750
}

internal/rpc/rpcserver/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3354,7 +3354,7 @@ func (s *votingServer) SetVoteChoices(ctx context.Context, req *pb.SetVoteChoice
33543354
return nil, status.Errorf(codes.InvalidArgument, "%v", err)
33553355
}
33563356
}
3357-
choices := make([]wallet.AgendaChoice, len(req.Choices))
3357+
choices := make(wallet.AgendaChoices, len(req.Choices))
33583358
for i, c := range req.Choices {
33593359
choices[i] = wallet.AgendaChoice{
33603360
AgendaID: c.AgendaId,
@@ -4312,7 +4312,7 @@ func (s *walletServer) SetVspdVoteChoices(ctx context.Context, req *pb.SetVspdVo
43124312
return err
43134313
}
43144314
if ticketHost == vspHost {
4315-
_ = vspClient.SetVoteChoice(ctx, hash, choices, tSpendChoices, treasuryChoices)
4315+
_ = vspClient.SetVoteChoice(ctx, hash, choices.Map(), tSpendChoices, treasuryChoices)
43164316
}
43174317
return nil
43184318
})

internal/vsp/feepayment.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ func (c *Client) status(ctx context.Context, ticketHash *chainhash.Hash) (*types
629629
}
630630

631631
func (c *Client) setVoteChoices(ctx context.Context, ticketHash *chainhash.Hash,
632-
choices []wallet.AgendaChoice, tspendPolicy map[string]string, treasuryPolicy map[string]string) error {
632+
choices map[string]string, tspendPolicy map[string]string, treasuryPolicy map[string]string) error {
633633
w := c.wallet
634634
params := w.ChainParams()
635635

@@ -651,17 +651,10 @@ func (c *Client) setVoteChoices(ctx context.Context, ticketHash *chainhash.Hash,
651651
ticketHash, err)
652652
}
653653

654-
agendaChoices := make(map[string]string, len(choices))
655-
656-
// Prepare agenda choice
657-
for _, c := range choices {
658-
agendaChoices[c.AgendaID] = c.ChoiceID
659-
}
660-
661654
req := types.SetVoteChoicesRequest{
662655
Timestamp: time.Now().Unix(),
663656
TicketHash: ticketHash.String(),
664-
VoteChoices: agendaChoices,
657+
VoteChoices: choices,
665658
TSpendPolicy: tspendPolicy,
666659
TreasuryPolicy: treasuryPolicy,
667660
}

internal/vsp/vsp.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (c *Client) Process(ctx context.Context, ticketHash *chainhash.Hash, feeTx
292292
// the connected VSP. The status provides the current voting preferences so we
293293
// can just update from there if need be.
294294
func (c *Client) SetVoteChoice(ctx context.Context, hash *chainhash.Hash,
295-
choices []wallet.AgendaChoice, tspendPolicy map[string]string, treasuryPolicy map[string]string) error {
295+
choices map[string]string, tspendPolicy map[string]string, treasuryPolicy map[string]string) error {
296296

297297
// Retrieve current voting preferences from VSP.
298298
status, err := c.status(ctx, hash)
@@ -309,20 +309,18 @@ func (c *Client) SetVoteChoice(ctx context.Context, hash *chainhash.Hash,
309309
update := false
310310

311311
// Check consensus vote choices.
312-
for _, newChoice := range choices {
313-
vspChoice, ok := status.VoteChoices[newChoice.AgendaID]
312+
for newAgenda, newChoice := range choices {
313+
vspChoice, ok := status.VoteChoices[newAgenda]
314314
if !ok {
315315
update = true
316316
break
317317
}
318-
if vspChoice != newChoice.ChoiceID {
318+
if vspChoice != newChoice {
319319
update = true
320320
break
321321
}
322322
}
323323

324-
// Apply the above changes to the two checks below.
325-
326324
// Check tspend policies.
327325
for newTSpend, newChoice := range tspendPolicy {
328326
vspChoice, ok := status.TSpendPolicy[newTSpend]

wallet/wallet.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,18 +398,30 @@ type AgendaChoice struct {
398398
ChoiceID string
399399
}
400400

401+
type AgendaChoices []AgendaChoice
402+
403+
// Map returns the agenda choices formatted as map["AgendaID"] = "ChoiceID".
404+
func (a AgendaChoices) Map() map[string]string {
405+
choices := make(map[string]string, len(a))
406+
407+
for _, c := range a {
408+
choices[c.AgendaID] = c.ChoiceID
409+
}
410+
return choices
411+
}
412+
401413
// AgendaChoices returns the choice IDs for every agenda of the supported stake
402414
// version. Abstains are included. Returns choice IDs set for the specified
403415
// non-nil ticket hash, or the default choice IDs if the ticket hash is nil or
404416
// there are no choices set for the ticket.
405-
func (w *Wallet) AgendaChoices(ctx context.Context, ticketHash *chainhash.Hash) (choices []AgendaChoice, voteBits uint16, err error) {
417+
func (w *Wallet) AgendaChoices(ctx context.Context, ticketHash *chainhash.Hash) (choices AgendaChoices, voteBits uint16, err error) {
406418
const op errors.Op = "wallet.AgendaChoices"
407419
version, deployments := CurrentAgendas(w.chainParams)
408420
if len(deployments) == 0 {
409421
return nil, 0, nil
410422
}
411423

412-
choices = make([]AgendaChoice, len(deployments))
424+
choices = make(AgendaChoices, len(deployments))
413425
for i := range choices {
414426
choices[i].AgendaID = deployments[i].Vote.Id
415427
choices[i].ChoiceID = "abstain"

0 commit comments

Comments
 (0)