2020-02-24 18:27:21 +03:00
|
|
|
package migtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2020-02-24 18:27:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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"),
|
|
|
|
// },
|
|
|
|
// }
|
2020-05-07 01:45:50 +03:00
|
|
|
func DumpDB(tx kvdb.RTx, rootKey []byte) error {
|
2019-12-13 05:22:19 +03:00
|
|
|
bucket := tx.ReadBucket(rootKey)
|
2020-02-24 18:27:21 +03:00
|
|
|
if bucket == nil {
|
|
|
|
return fmt.Errorf("bucket %v not found", string(rootKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
return dumpBucket(bucket)
|
|
|
|
}
|
|
|
|
|
2020-05-07 01:48:00 +03:00
|
|
|
func dumpBucket(bucket kvdb.RBucket) error {
|
2020-02-24 18:27:21 +03:00
|
|
|
fmt.Printf("map[string]interface{} {\n")
|
|
|
|
err := bucket.ForEach(func(k, v []byte) error {
|
|
|
|
key := toString(k)
|
|
|
|
fmt.Printf("%v: ", key)
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
subBucket := bucket.NestedReadBucket(k)
|
2020-02-24 18:27:21 +03:00
|
|
|
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.
|
2019-12-13 05:22:19 +03:00
|
|
|
func RestoreDB(tx kvdb.RwTx, rootKey []byte, data map[string]interface{}) error {
|
|
|
|
bucket, err := tx.CreateTopLevelBucket(rootKey)
|
2020-02-24 18:27:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return restoreDB(bucket, data)
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
func restoreDB(bucket kvdb.RwBucket, data map[string]interface{}) error {
|
2020-02-24 18:27:21 +03:00
|
|
|
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.
|
2020-05-07 01:45:50 +03:00
|
|
|
func VerifyDB(tx kvdb.RTx, rootKey []byte, data map[string]interface{}) error {
|
2019-12-13 05:22:19 +03:00
|
|
|
bucket := tx.ReadBucket(rootKey)
|
2020-02-24 18:27:21 +03:00
|
|
|
if bucket == nil {
|
|
|
|
return fmt.Errorf("bucket %v not found", string(rootKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
return verifyDB(bucket, data)
|
|
|
|
}
|
|
|
|
|
2020-05-07 01:48:00 +03:00
|
|
|
func verifyDB(bucket kvdb.RBucket, data map[string]interface{}) error {
|
2020-02-24 18:27:21 +03:00
|
|
|
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{}:
|
2019-12-13 05:22:19 +03:00
|
|
|
subBucket := bucket.NestedReadBucket(key)
|
2020-02-24 18:27:21 +03:00
|
|
|
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)
|
|
|
|
}
|