Skip to content

Commit 4e7ec31

Browse files
committed
firewalldb+refactor: move kvdb priv map code to dedicated file
In preparation for adding a file dedicated to the SQL implementation of the privacy mapper DB.
1 parent 42a6cf2 commit 4e7ec31

File tree

2 files changed

+198
-190
lines changed

2 files changed

+198
-190
lines changed

firewalldb/privacy_mapper.go

Lines changed: 0 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,12 @@ import (
1212
"sync"
1313

1414
"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
27-
)
28-
29-
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)
3615
)
3716

3817
// NewPrivacyMapDB is a function type that takes a group ID and uses it to
3918
// construct a new PrivacyMapDB.
4019
type NewPrivacyMapDB func(groupID session.ID) PrivacyMapDB
4120

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-
5821
// PrivacyMapDB provides an Update and View method that will allow the caller
5922
// to perform atomic read and write transactions defined by PrivacyMapTx on the
6023
// underlying DB.
@@ -79,159 +42,6 @@ type PrivacyMapTx interface {
7942
FetchAllPairs(ctx context.Context) (*PrivacyMapPairs, error)
8043
}
8144

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-
23545
func HideString(ctx context.Context, tx PrivacyMapTx, real string) (string,
23646
error) {
23747

0 commit comments

Comments
 (0)