forked from planetdecred/dcrlibwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treasury.go
194 lines (175 loc) · 5.68 KB
/
treasury.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package dcrlibwallet
import (
"encoding/hex"
"fmt"
"decred.org/dcrwallet/v2/errors"
"github.com/decred/dcrd/blockchain/stake/v4"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
// SetTreasuryPolicy saves the voting policy for treasury spends by a particular
// PI key.
// If a ticket hash is provided, the voting policy is also updated with the VSP
// controlling the ticket. If a ticket hash isn't provided, the vote choice is
// saved to the local wallet database and the VSPs controlling all unspent,
// unexpired tickets are updated to use the specified vote policy.
func (wallet *Wallet) SetTreasuryPolicy(PiKey, newVotingPolicy, tixHash string, passphrase []byte) error {
var ticketHash *chainhash.Hash
if tixHash != "" {
tixHash, err := chainhash.NewHashFromStr(tixHash)
if err != nil {
return fmt.Errorf("invalid ticket hash: %w", err)
}
ticketHash = tixHash
}
pikey, err := hex.DecodeString(PiKey)
if err != nil {
return fmt.Errorf("invalid pikey: %w", err)
}
if len(pikey) != secp256k1.PubKeyBytesLenCompressed {
return fmt.Errorf("treasury pikey must be %d bytes", secp256k1.PubKeyBytesLenCompressed)
}
var policy stake.TreasuryVoteT
switch newVotingPolicy {
case "abstain", "invalid", "":
policy = stake.TreasuryVoteInvalid
case "yes":
policy = stake.TreasuryVoteYes
case "no":
policy = stake.TreasuryVoteNo
default:
return fmt.Errorf("invalid policy: unknown policy %q", newVotingPolicy)
}
// The wallet will need to be unlocked to sign the API
// request(s) for setting this voting policy with the VSP.
err = wallet.UnlockWallet(passphrase)
if err != nil {
return translateError(err)
}
defer wallet.LockWallet()
currentVotingPolicy := wallet.Internal().TreasuryKeyPolicy(pikey, ticketHash)
ctx := wallet.shutdownContext()
err = wallet.Internal().SetTreasuryKeyPolicy(ctx, pikey, policy, ticketHash)
if err != nil {
return err
}
var vspPreferenceUpdateSuccess bool
defer func() {
if !vspPreferenceUpdateSuccess {
// Updating the treasury spend voting preference with the vsp failed,
// revert the locally saved voting preference for the treasury spend.
revertError := wallet.Internal().SetTreasuryKeyPolicy(ctx, pikey, currentVotingPolicy, ticketHash)
if revertError != nil {
log.Errorf("unable to revert locally saved voting preference: %v", revertError)
}
}
}()
// If a ticket hash is provided, set the specified vote policy with
// the VSP associated with the provided ticket. Otherwise, set the
// vote policy with the VSPs associated with all "votable" tickets.
ticketHashes := make([]*chainhash.Hash, 0)
if ticketHash != nil {
ticketHashes = append(ticketHashes, ticketHash)
} else {
err = wallet.Internal().ForUnspentUnexpiredTickets(ctx, func(hash *chainhash.Hash) error {
ticketHashes = append(ticketHashes, hash)
return nil
})
if err != nil {
return fmt.Errorf("unable to fetch hashes for all unspent, unexpired tickets: %v", err)
}
}
// Never return errors from this for loop, so all tickets are tried.
// The first error will be returned to the caller.
var firstErr error
// Update voting preferences on VSPs if required.
policyMap := map[string]string{
PiKey: newVotingPolicy,
}
for _, tHash := range ticketHashes {
vspTicketInfo, err := wallet.Internal().VSPTicketInfo(ctx, tHash)
if err != nil {
// Ignore NotExist error, just means the ticket is not
// registered with a VSP, nothing more to do here.
if firstErr == nil && !errors.Is(err, errors.NotExist) {
firstErr = err
}
continue // try next tHash
}
// Update the vote policy for the ticket with the associated VSP.
vspClient, err := wallet.VSPClient(vspTicketInfo.Host, vspTicketInfo.PubKey)
if err != nil && firstErr == nil {
firstErr = err
continue // try next tHash
}
err = vspClient.SetVoteChoice(ctx, tHash, nil, nil, policyMap)
if err != nil && firstErr == nil {
firstErr = err
continue // try next tHash
}
}
vspPreferenceUpdateSuccess = firstErr == nil
return firstErr
}
// TreasuryPolicies returns saved voting policies for treasury spends
// per pi key. If a pi key is specified, the policy for that pi key
// is returned; otherwise the policies for all pi keys are returned.
// If a ticket hash is provided, the policy(ies) for that ticket
// is/are returned.
func (wallet *Wallet) TreasuryPolicies(PiKey, tixHash string) ([]*TreasuryKeyPolicy, error) {
var ticketHash *chainhash.Hash
if tixHash != "" {
tixHash, err := chainhash.NewHashFromStr(tixHash)
if err != nil {
return nil, fmt.Errorf("inavlid hash: %w", err)
}
ticketHash = tixHash
}
if PiKey != "" {
pikey, err := hex.DecodeString(PiKey)
if err != nil {
return nil, fmt.Errorf("invalid pikey: %w", err)
}
var policy string
switch wallet.Internal().TreasuryKeyPolicy(pikey, ticketHash) {
case stake.TreasuryVoteYes:
policy = "yes"
case stake.TreasuryVoteNo:
policy = "no"
default:
policy = "abstain"
}
res := []*TreasuryKeyPolicy{
{
TicketHash: tixHash,
PiKey: PiKey,
Policy: policy,
},
}
return res, nil
}
policies := wallet.Internal().TreasuryKeyPolicies()
res := make([]*TreasuryKeyPolicy, len(policies))
for i := range policies {
var policy string
switch policies[i].Policy {
case stake.TreasuryVoteYes:
policy = "yes"
case stake.TreasuryVoteNo:
policy = "no"
}
r := &TreasuryKeyPolicy{
PiKey: hex.EncodeToString(policies[i].PiKey),
Policy: policy,
}
if policies[i].Ticket != nil {
r.TicketHash = policies[i].Ticket.String()
}
res[i] = r
}
return res, nil
}
// PiKeys returns the sanctioned Politeia keys for the current network.
func (mw *MultiWallet) PiKeys() [][]byte {
return mw.chainParams.PiKeys
}