-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
98 lines (80 loc) · 2 KB
/
db.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
package main
import (
"errors"
"fmt"
"log"
"os"
"path"
"time"
"github.com/boltdb/bolt"
"github.com/securepollingsystem/tallyspider/screed"
)
var (
screedBucket = []byte("screed")
buckets = [][]byte{
screedBucket,
}
ErrScreedNotFound = errors.New("screed not found")
ErrScreedExists = errors.New("A screed already exists for that user")
)
func mustInitDB() *bolt.DB {
options := &bolt.Options{Timeout: 2 * time.Second}
dbPath := path.Join(os.Getenv("BOLT_PATH"), "screed.db")
// Open file
db, err := bolt.Open(dbPath, 0600, options)
if err != nil {
log.Fatalf("Error opening bolt DB: %v", err)
}
// Create buckets
err = db.Update(func(tx *bolt.Tx) error {
for _, bkt := range buckets {
_, err := tx.CreateBucketIfNotExists(bkt)
if err != nil {
return fmt.Errorf("Error creating bucket `%s`: %v", bkt, err)
}
}
return nil
})
if err != nil {
log.Fatalf("Error creating '%v' boltDB bucket: %v\n", screedBucket,
err)
}
return db
}
func GetScreedByPubkey(db *bolt.DB, pubkeyhex string) (s []byte, err error) {
err = db.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(screedBucket)
pubkeyB := bkt.Get([]byte(pubkeyhex))
if pubkeyB == nil {
return ErrScreedNotFound
}
s = pubkeyB
return nil
})
return s, err
}
func CreateScreedByPubkey(db *bolt.DB, s *screed.Screed) (pubkeyhex string, err error) {
if s == nil {
return "", errors.New("Cannot create nil *Screed!")
}
err = db.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket(screedBucket)
if s.VoterPubKey() == nil {
return errors.New("Cannot create *Screed with nil *VoterPubKey")
}
// Pubkey -> compress -> hex-encoded -> []byte
pubkeyhex = fmt.Sprintf("%x", s.VoterPubKey().SerializeCompressed())
pubkeyB := []byte(pubkeyhex)
// Only save if user has no existing screed
old := bkt.Get(pubkeyB)
if old != nil {
return ErrScreedExists
}
screedStr, err := s.Serialize()
if err != nil {
return err
}
return bkt.Put(pubkeyB, []byte(screedStr))
})
return pubkeyhex, err
}