-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.go
161 lines (140 loc) · 3.84 KB
/
util.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
package client
import (
"context"
"crypto/rand"
"encoding/binary"
"sort"
"time"
"github.com/companyzero/bisonrelay/client/clientdb"
"github.com/companyzero/bisonrelay/client/internal/lowlevel"
)
// canceled returns true if the given context is done.
func canceled(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}
func zeroSlice(b []byte) {
for i := range b {
b[i] = 0
}
}
func (c *Client) mustRandomUint64() uint64 {
var b [8]byte
if n, err := rand.Read(b[:]); n < 8 || err != nil {
panic("out of entropy")
}
return binary.LittleEndian.Uint64(b[:])
}
// rvManagerDBAdapter adapts the client to the interface required by the
// RVManagerDB.
type rvManagerDBAdapter struct {
c *Client
}
func (rvdb *rvManagerDBAdapter) UnpaidRVs(rvs []lowlevel.RVID, expirationDays int) ([]lowlevel.RVID, error) {
var unpaid []lowlevel.RVID
err := rvdb.c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
for _, rv := range rvs {
if paid, err := rvdb.c.db.IsRVPaid(tx, rv, expirationDays); err != nil {
return err
} else if !paid {
unpaid = append(unpaid, rv)
}
}
return nil
})
return unpaid, err
}
func (rvdb *rvManagerDBAdapter) SavePaidRVs(rvs []lowlevel.RVID) error {
err := rvdb.c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
for _, rv := range rvs {
if err := rvdb.c.db.SaveRVPaid(tx, rv); err != nil {
return err
}
}
return nil
})
return err
}
func (rvdb *rvManagerDBAdapter) MarkRVUnpaid(rv lowlevel.RVID) error {
err := rvdb.c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
return rvdb.c.db.MarkRVUnpaid(tx, rv)
})
return err
}
// rmqDBAdapter is an adapter structure that satisfies the RMQDB interface using
// a client's db as backing storage.
type rmqDBAdapter struct {
c *Client
}
func (rmqdb *rmqDBAdapter) RVHasPaymentAttempt(rv lowlevel.RVID) (string, time.Time, error) {
var invoice string
var ts time.Time
err := rmqdb.c.dbView(func(tx clientdb.ReadTx) error {
var err error
invoice, ts, err = rmqdb.c.db.HasPushPaymentAttempt(tx, rv)
return err
})
return invoice, ts, err
}
func (rmqdb *rmqDBAdapter) StoreRVPaymentAttempt(rv lowlevel.RVID, invoice string, ts time.Time) error {
return rmqdb.c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
return rmqdb.c.db.StorePushPaymentAttempt(tx, rv, invoice, ts)
})
}
func (rmqdb *rmqDBAdapter) DeleteRVPaymentAttempt(rv lowlevel.RVID) error {
return rmqdb.c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
return rmqdb.c.db.DeletePushPaymentAttempt(tx, rv)
})
}
// SortedUserPayStatsIDs returns a sorted list of IDs from the passed stats
// map, ordered by largest total payments.
func SortedUserPayStatsIDs(stats map[UserID]clientdb.UserPayStats) []UserID {
res := make([]UserID, len(stats))
i := 0
for uid := range stats {
copy(res[i][:], uid[:])
i += 1
}
sort.Slice(res, func(i, j int) bool {
si := stats[res[i]]
sj := stats[res[j]]
ti := si.TotalSent + si.TotalReceived
tj := sj.TotalSent + sj.TotalReceived
return ti > tj
})
return res
}
type sliceChanges[T comparable] struct {
added []T
removed []T
}
// sliceDiff returns added and removed items in newSlice compared to oldSlice.
func sliceDiff[T comparable](oldSlice, newSlice []T) sliceChanges[T] {
var added, removed []T
// Aux map of items in newAux that already existed in oldSlice.
newAux := make(map[T]bool, len(newSlice))
for _, newV := range newSlice {
newAux[newV] = true
}
// Determine items that were removed or already existed in old.
for _, oldV := range oldSlice {
if _, ok := newAux[oldV]; !ok {
// Deleted from new.
removed = append(removed, oldV)
} else {
// Already existed in old.
newAux[oldV] = false
}
}
// Anything not set in the map is a new item.
for newV, isNew := range newAux {
if isNew {
added = append(added, newV)
}
}
return sliceChanges[T]{added: added, removed: removed}
}