channeldb: switch to Open/Create methods rather than New
Commit includes basic tests for Open/Create. Additionally, rather than relying on btcwallet’s addmgr for encryption/decryption, this package now exposes a simple crypto system interface.
This commit is contained in:
parent
3faa2c2ef5
commit
631e76519e
127
channeldb/db.go
127
channeldb/db.go
@ -3,12 +3,12 @@ package channeldb
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/btcsuite/btcwallet/waddrmgr"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -16,64 +16,119 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
endian = binary.BigEndian
|
||||
// Big endian is the preferred byte order, due to cursor scans over integer
|
||||
// keys iterating in order.
|
||||
byteOrder = binary.BigEndian
|
||||
)
|
||||
|
||||
var bufPool = &sync.Pool{
|
||||
New: func() interface{} { return new(bytes.Buffer) },
|
||||
}
|
||||
|
||||
// Store...
|
||||
// TODO(roasbeef): CHECKSUMS, REDUNDANCY, etc etc.
|
||||
// EncryptorDecryptor...
|
||||
// TODO(roasbeef): ability to rotate EncryptorDecryptor's across DB
|
||||
type EncryptorDecryptor interface {
|
||||
Encrypt(in []byte) ([]byte, error)
|
||||
Decrypt(in []byte) ([]byte, error)
|
||||
OverheadSize() uint32
|
||||
}
|
||||
|
||||
// DB...
|
||||
type DB struct {
|
||||
// TODO(roasbeef): caching, etc?
|
||||
addrmgr *waddrmgr.Manager
|
||||
store *bolt.DB
|
||||
|
||||
db *bolt.DB
|
||||
cryptoSystem EncryptorDecryptor
|
||||
}
|
||||
|
||||
// Wipe...
|
||||
func (d *DB) Wipe() error {
|
||||
return d.db.Update(func(tx *bolt.Tx) error {
|
||||
return tx.DeleteBucket(openChannelBucket)
|
||||
})
|
||||
}
|
||||
|
||||
// New...
|
||||
// TODO(roasbeef): re-visit this dependancy...
|
||||
func New(dbPath string, addrmgr *waddrmgr.Manager) (*DB, error) {
|
||||
if _, err := os.Stat(dbPath); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(dbPath, 0700); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
// Open opens an existing channeldb created under the passed namespace with
|
||||
// sensitive data encrypted by the passed EncryptorDecryptor implementation.
|
||||
// TODO(roasbeef): versioning?
|
||||
func Open(dbPath string) (*DB, error) {
|
||||
if !fileExists(dbPath) {
|
||||
return nil, ErrNoExists
|
||||
}
|
||||
|
||||
path := filepath.Join(dbPath, dbName)
|
||||
boltDB, err := bolt.Open(path, 0600, nil)
|
||||
bdb, err := bolt.Open(path, 0600, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DB{addrmgr, boltDB}, nil
|
||||
}
|
||||
|
||||
// Open...
|
||||
// TODO(roasbeef): create+open, ditch New, fixes above
|
||||
func Open() *DB {
|
||||
return nil
|
||||
return &DB{store: bdb}, nil
|
||||
}
|
||||
|
||||
// Create...
|
||||
func Create() *DB {
|
||||
return nil
|
||||
func Create(dbPath string) (*DB, error) {
|
||||
bdb, err := createChannelDB(dbPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DB{store: bdb}, nil
|
||||
}
|
||||
|
||||
// RegisterCryptoSystem...
|
||||
func (d *DB) RegisterCryptoSystem(ed EncryptorDecryptor) {
|
||||
d.cryptoSystem = ed
|
||||
}
|
||||
|
||||
// Wipe...
|
||||
func (d *DB) Wipe() error {
|
||||
return d.store.Update(func(tx *bolt.Tx) error {
|
||||
// TODO(roasbee): delete all other top-level buckets.
|
||||
return tx.DeleteBucket(openChannelBucket)
|
||||
})
|
||||
}
|
||||
|
||||
// Close...
|
||||
func (d *DB) Close() error {
|
||||
return d.db.Close()
|
||||
return d.store.Close()
|
||||
}
|
||||
|
||||
// createChannelDB...
|
||||
func createChannelDB(dbPath string) (*bolt.DB, error) {
|
||||
if !fileExists(dbPath) {
|
||||
if err := os.MkdirAll(dbPath, 0700); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
path := filepath.Join(dbPath, dbName)
|
||||
bdb, err := bolt.Open(path, 0600, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = bdb.Update(func(tx *bolt.Tx) error {
|
||||
if _, err := tx.CreateBucket(openChannelBucket); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := tx.CreateBucket(closedChannelBucket); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := tx.CreateBucket(channelLogBucket); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to create new channeldb")
|
||||
}
|
||||
|
||||
return bdb, nil
|
||||
}
|
||||
|
||||
// fileExists...
|
||||
func fileExists(path string) bool {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// TODO(roasbeef): SetCryptoSystem method...
|
||||
// * don't have waddrmgr up before..
|
||||
|
38
channeldb/db_test.go
Normal file
38
channeldb/db_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
package channeldb
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOpenNotCreated(t *testing.T) {
|
||||
if _, err := Open("path doesn't exist"); err != ErrNoExists {
|
||||
t.Fatalf("channeldb Open should fail due to non-existant dir")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateThenOpen(t *testing.T) {
|
||||
// First, create a temporary directory to be used for the duration of
|
||||
// this test.
|
||||
tempDirName, err := ioutil.TempDir("", "channeldb")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create temp dir: %v")
|
||||
}
|
||||
defer os.RemoveAll(tempDirName)
|
||||
|
||||
// Next, create channeldb for the first time.
|
||||
cdb, err := Create(tempDirName)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create channeldb: %v", err)
|
||||
}
|
||||
if err := cdb.Close(); err != nil {
|
||||
t.Fatalf("unable to close channeldb: %v", err)
|
||||
}
|
||||
|
||||
// Open should now succeed as the cdb was created above.
|
||||
cdb, err = Open(tempDirName)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to open channeldb: %v", err)
|
||||
}
|
||||
}
|
@ -1 +1,7 @@
|
||||
package channeldb
|
||||
|
||||
import "fmt"
|
||||
|
||||
var (
|
||||
ErrNoExists = fmt.Errorf("channel db has not yet been created")
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user