Skip to content

Commit c0fba9a

Browse files
authored
Merge pull request #1039 from ellemouton/sql30
[sql-30] firewalldb: privacy mapper clean up in preparation for SQL impl
2 parents 1541695 + 98c83ab commit c0fba9a

File tree

3 files changed

+295
-199
lines changed

3 files changed

+295
-199
lines changed

firewalldb/privacy_mapper.go

+12-187
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,34 @@ import (
55
"crypto/rand"
66
"encoding/binary"
77
"encoding/hex"
8+
"errors"
89
"fmt"
910
"math/big"
1011
"strconv"
1112
"strings"
1213
"sync"
1314

1415
"github.com/lightninglabs/lightning-terminal/session"
15-
"go.etcd.io/bbolt"
16-
)
17-
18-
/*
19-
The PrivacyMapper data is stored in the following structure in the db:
20-
21-
privacy -> group id -> real-to-pseudo -> {k:v}
22-
-> pseudo-to-real -> {k:v}
23-
*/
24-
25-
const (
26-
txidStringLen = 64
2716
)
2817

2918
var (
30-
privacyBucketKey = []byte("privacy")
31-
realToPseudoKey = []byte("real-to-pseudo")
32-
pseudoToRealKey = []byte("pseudo-to-real")
33-
34-
pseudoStrAlphabet = []rune("abcdef0123456789")
35-
pseudoStrAlphabetLen = len(pseudoStrAlphabet)
19+
// ErrDuplicateRealValue is returned when an attempt is made to insert
20+
// a new real-pseudo pair into the db, but the real value already exists
21+
// in the db.
22+
ErrDuplicateRealValue = errors.New("an entry with the given real " +
23+
"value already exists")
24+
25+
// ErrDuplicatePseudoValue is returned when an attempt is made to
26+
// insert a new real-pseudo pair into the db, but the pseudo value
27+
// already exists in the db.
28+
ErrDuplicatePseudoValue = errors.New("an entry with the given pseudo " +
29+
"value already exists")
3630
)
3731

3832
// NewPrivacyMapDB is a function type that takes a group ID and uses it to
3933
// construct a new PrivacyMapDB.
4034
type NewPrivacyMapDB func(groupID session.ID) PrivacyMapDB
4135

42-
// PrivacyDB constructs a PrivacyMapDB that will be indexed under the given
43-
// group ID key.
44-
func (db *BoltDB) PrivacyDB(groupID session.ID) PrivacyMapDB {
45-
return &kvdbExecutor[PrivacyMapTx]{
46-
db: db.DB,
47-
wrapTx: func(tx *bbolt.Tx) PrivacyMapTx {
48-
return &privacyMapTx{
49-
boltTx: tx,
50-
privacyMapDB: &privacyMapDB{
51-
groupID: groupID,
52-
},
53-
}
54-
},
55-
}
56-
}
57-
5836
// PrivacyMapDB provides an Update and View method that will allow the caller
5937
// to perform atomic read and write transactions defined by PrivacyMapTx on the
6038
// underlying DB.
@@ -79,159 +57,6 @@ type PrivacyMapTx interface {
7957
FetchAllPairs(ctx context.Context) (*PrivacyMapPairs, error)
8058
}
8159

82-
// privacyMapDB is an implementation of PrivacyMapDB.
83-
type privacyMapDB struct {
84-
groupID session.ID
85-
}
86-
87-
// privacyMapTx is an implementation of PrivacyMapTx.
88-
type privacyMapTx struct {
89-
*privacyMapDB
90-
boltTx *bbolt.Tx
91-
}
92-
93-
// NewPair inserts a new real-pseudo pair into the db.
94-
//
95-
// NOTE: this is part of the PrivacyMapTx interface.
96-
func (p *privacyMapTx) NewPair(_ context.Context, real, pseudo string) error {
97-
privacyBucket, err := getBucket(p.boltTx, privacyBucketKey)
98-
if err != nil {
99-
return err
100-
}
101-
102-
sessBucket, err := privacyBucket.CreateBucketIfNotExists(p.groupID[:])
103-
if err != nil {
104-
return err
105-
}
106-
107-
realToPseudoBucket, err := sessBucket.CreateBucketIfNotExists(
108-
realToPseudoKey,
109-
)
110-
if err != nil {
111-
return err
112-
}
113-
114-
pseudoToRealBucket, err := sessBucket.CreateBucketIfNotExists(
115-
pseudoToRealKey,
116-
)
117-
if err != nil {
118-
return err
119-
}
120-
121-
if len(realToPseudoBucket.Get([]byte(real))) != 0 {
122-
return fmt.Errorf("an entry already exists for real "+
123-
"value: %x", real)
124-
}
125-
126-
if len(pseudoToRealBucket.Get([]byte(pseudo))) != 0 {
127-
return fmt.Errorf("an entry already exists for pseudo "+
128-
"value: %x", pseudo)
129-
}
130-
131-
err = realToPseudoBucket.Put([]byte(real), []byte(pseudo))
132-
if err != nil {
133-
return err
134-
}
135-
136-
return pseudoToRealBucket.Put([]byte(pseudo), []byte(real))
137-
}
138-
139-
// PseudoToReal will check the db to see if the given pseudo key exists. If
140-
// it does then the real value is returned, else an error is returned.
141-
//
142-
// NOTE: this is part of the PrivacyMapTx interface.
143-
func (p *privacyMapTx) PseudoToReal(_ context.Context, pseudo string) (string,
144-
error) {
145-
146-
privacyBucket, err := getBucket(p.boltTx, privacyBucketKey)
147-
if err != nil {
148-
return "", err
149-
}
150-
151-
sessBucket := privacyBucket.Bucket(p.groupID[:])
152-
if sessBucket == nil {
153-
return "", ErrNoSuchKeyFound
154-
}
155-
156-
pseudoToRealBucket := sessBucket.Bucket(pseudoToRealKey)
157-
if pseudoToRealBucket == nil {
158-
return "", ErrNoSuchKeyFound
159-
}
160-
161-
real := pseudoToRealBucket.Get([]byte(pseudo))
162-
if len(real) == 0 {
163-
return "", ErrNoSuchKeyFound
164-
}
165-
166-
return string(real), nil
167-
}
168-
169-
// RealToPseudo will check the db to see if the given real key exists. If
170-
// it does then the pseudo value is returned, else an error is returned.
171-
//
172-
// NOTE: this is part of the PrivacyMapTx interface.
173-
func (p *privacyMapTx) RealToPseudo(_ context.Context, real string) (string,
174-
error) {
175-
176-
privacyBucket, err := getBucket(p.boltTx, privacyBucketKey)
177-
if err != nil {
178-
return "", err
179-
}
180-
181-
sessBucket := privacyBucket.Bucket(p.groupID[:])
182-
if sessBucket == nil {
183-
return "", ErrNoSuchKeyFound
184-
}
185-
186-
realToPseudoBucket := sessBucket.Bucket(realToPseudoKey)
187-
if realToPseudoBucket == nil {
188-
return "", ErrNoSuchKeyFound
189-
}
190-
191-
pseudo := realToPseudoBucket.Get([]byte(real))
192-
if len(pseudo) == 0 {
193-
return "", ErrNoSuchKeyFound
194-
}
195-
196-
return string(pseudo), nil
197-
}
198-
199-
// FetchAllPairs loads and returns the real-to-pseudo pairs.
200-
//
201-
// NOTE: this is part of the PrivacyMapTx interface.
202-
func (p *privacyMapTx) FetchAllPairs(_ context.Context) (*PrivacyMapPairs,
203-
error) {
204-
205-
privacyBucket, err := getBucket(p.boltTx, privacyBucketKey)
206-
if err != nil {
207-
return nil, err
208-
}
209-
210-
sessBucket := privacyBucket.Bucket(p.groupID[:])
211-
if sessBucket == nil {
212-
// If the bucket has not been created yet, then there are no
213-
// privacy pairs yet.
214-
return NewPrivacyMapPairs(nil), nil
215-
}
216-
217-
realToPseudoBucket := sessBucket.Bucket(realToPseudoKey)
218-
if realToPseudoBucket == nil {
219-
return nil, ErrNoSuchKeyFound
220-
}
221-
222-
pairs := make(map[string]string)
223-
err = realToPseudoBucket.ForEach(func(r, p []byte) error {
224-
pairs[string(r)] = string(p)
225-
226-
return nil
227-
})
228-
if err != nil {
229-
return nil, err
230-
}
231-
232-
return NewPrivacyMapPairs(pairs), nil
233-
}
234-
23560
func HideString(ctx context.Context, tx PrivacyMapTx, real string) (string,
23661
error) {
23762

0 commit comments

Comments
 (0)