-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
raw_db.go
185 lines (153 loc) · 3.58 KB
/
raw_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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package migtest
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"strings"
"github.com/lightningnetwork/lnd/kvdb"
)
// DumpDB dumps go code describing the contents of the database to stdout. This
// function is only intended for use during development.
//
// Example output:
//
// map[string]interface{}{
// hex("1234"): map[string]interface{}{
// "human-readable": hex("102030"),
// hex("1111"): hex("5783492373"),
// },
// }
func DumpDB(tx kvdb.RTx, rootKey []byte) error {
bucket := tx.ReadBucket(rootKey)
if bucket == nil {
return fmt.Errorf("bucket %v not found", string(rootKey))
}
return dumpBucket(bucket)
}
func dumpBucket(bucket kvdb.RBucket) error {
fmt.Printf("map[string]interface{} {\n")
err := bucket.ForEach(func(k, v []byte) error {
key := toString(k)
fmt.Printf("%v: ", key)
subBucket := bucket.NestedReadBucket(k)
if subBucket != nil {
err := dumpBucket(subBucket)
if err != nil {
return err
}
} else {
fmt.Print(toHex(v))
}
fmt.Printf(",\n")
return nil
})
if err != nil {
return err
}
fmt.Printf("}")
return nil
}
// RestoreDB primes the database with the given data set.
func RestoreDB(tx kvdb.RwTx, rootKey []byte, data map[string]interface{}) error {
bucket, err := tx.CreateTopLevelBucket(rootKey)
if err != nil {
return err
}
return restoreDB(bucket, data)
}
func restoreDB(bucket kvdb.RwBucket, data map[string]interface{}) error {
for k, v := range data {
key := []byte(k)
switch value := v.(type) {
// Key contains value.
case string:
err := bucket.Put(key, []byte(value))
if err != nil {
return err
}
// Key contains a sub-bucket.
case map[string]interface{}:
subBucket, err := bucket.CreateBucket(key)
if err != nil {
return err
}
if err := restoreDB(subBucket, value); err != nil {
return err
}
default:
return errors.New("invalid type")
}
}
return nil
}
// VerifyDB verifies the database against the given data set.
func VerifyDB(tx kvdb.RTx, rootKey []byte, data map[string]interface{}) error {
bucket := tx.ReadBucket(rootKey)
if bucket == nil {
return fmt.Errorf("bucket %v not found", string(rootKey))
}
return verifyDB(bucket, data)
}
func verifyDB(bucket kvdb.RBucket, data map[string]interface{}) error {
for k, v := range data {
key := []byte(k)
switch value := v.(type) {
// Key contains value.
case string:
expectedValue := []byte(value)
dbValue := bucket.Get(key)
if !bytes.Equal(dbValue, expectedValue) {
return errors.New("value mismatch")
}
// Key contains a sub-bucket.
case map[string]interface{}:
subBucket := bucket.NestedReadBucket(key)
if subBucket == nil {
return fmt.Errorf("bucket %v not found", k)
}
err := verifyDB(subBucket, value)
if err != nil {
return err
}
default:
return errors.New("invalid type")
}
}
keyCount := 0
err := bucket.ForEach(func(k, v []byte) error {
keyCount++
return nil
})
if err != nil {
return err
}
if keyCount != len(data) {
return errors.New("unexpected keys in database")
}
return nil
}
func toHex(v []byte) string {
if len(v) == 0 {
return "nil"
}
return "hex(\"" + hex.EncodeToString(v) + "\")"
}
func toString(v []byte) string {
readableChars := "abcdefghijklmnopqrstuvwxyz0123456789-"
for _, c := range v {
if !strings.Contains(readableChars, string(c)) {
return toHex(v)
}
}
return "\"" + string(v) + "\""
}
// Hex is a test helper function to convert readable hex arrays to raw byte
// strings.
func Hex(value string) string {
b, err := hex.DecodeString(value)
if err != nil {
panic(err)
}
return string(b)
}