/
agendas.go
56 lines (45 loc) · 1.85 KB
/
agendas.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
// Copyright (c) 2017 The Decred developers
// Copyright (c) 2018-2020 The Hc developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package udb
import (
"github.com/HcashOrg/hcwallet/apperrors"
"github.com/HcashOrg/hcwallet/walletdb"
)
type agendaPreferencesTy struct {
}
var agendaPreferences agendaPreferencesTy
var agendaPreferencesRootBucketKey = []byte("agendaprefs")
func (agendaPreferencesTy) rootBucketKey() []byte { return agendaPreferencesRootBucketKey }
func (agendaPreferencesTy) key(version uint32, agendaID string) []byte {
k := make([]byte, 4+len(agendaID))
byteOrder.PutUint32(k, version)
copy(k[4:], agendaID)
return k
}
func (t agendaPreferencesTy) setPreference(tx walletdb.ReadWriteTx, version uint32, agendaID, choiceID string) error {
b := tx.ReadWriteBucket(t.rootBucketKey())
return b.Put(t.key(version, agendaID), []byte(choiceID))
}
func (t agendaPreferencesTy) preference(tx walletdb.ReadTx, version uint32, agendaID string) (choiceID string) {
b := tx.ReadBucket(t.rootBucketKey())
v := b.Get(t.key(version, agendaID))
return string(v)
}
// SetAgendaPreference saves an agenda choice ID for an agenda ID and deployment
// version.
func SetAgendaPreference(tx walletdb.ReadWriteTx, version uint32, agendaID, choiceID string) error {
err := agendaPreferences.setPreference(tx, version, agendaID, choiceID)
if err != nil {
const str = "failed to put agenda preference"
return apperrors.E{ErrorCode: apperrors.ErrDatabase, Description: str, Err: err}
}
return nil
}
// AgendaPreference returns the saved choice ID, if any, for an agenda ID and
// deployment version. If no choice has been saved, this returns the empty
// string.
func AgendaPreference(tx walletdb.ReadTx, version uint32, agendaID string) (choiceID string) {
return agendaPreferences.preference(tx, version, agendaID)
}