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:
Olaoluwa Osuntokun 2016-03-23 22:11:57 -07:00
parent 3faa2c2ef5
commit 631e76519e
3 changed files with 135 additions and 36 deletions

View file

@ -3,12 +3,12 @@ package channeldb
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/btcsuite/btcwallet/waddrmgr"
) )
const ( const (
@ -16,64 +16,119 @@ const (
) )
var ( 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{ var bufPool = &sync.Pool{
New: func() interface{} { return new(bytes.Buffer) }, New: func() interface{} { return new(bytes.Buffer) },
} }
// Store... // EncryptorDecryptor...
// TODO(roasbeef): CHECKSUMS, REDUNDANCY, etc etc. // 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 { type DB struct {
// TODO(roasbeef): caching, etc? store *bolt.DB
addrmgr *waddrmgr.Manager
db *bolt.DB cryptoSystem EncryptorDecryptor
} }
// Wipe... // Open opens an existing channeldb created under the passed namespace with
func (d *DB) Wipe() error { // sensitive data encrypted by the passed EncryptorDecryptor implementation.
return d.db.Update(func(tx *bolt.Tx) error { // TODO(roasbeef): versioning?
return tx.DeleteBucket(openChannelBucket) func Open(dbPath string) (*DB, error) {
}) if !fileExists(dbPath) {
} return nil, ErrNoExists
// 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
}
}
} }
path := filepath.Join(dbPath, dbName) path := filepath.Join(dbPath, dbName)
boltDB, err := bolt.Open(path, 0600, nil) bdb, err := bolt.Open(path, 0600, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &DB{addrmgr, boltDB}, nil return &DB{store: bdb}, nil
}
// Open...
// TODO(roasbeef): create+open, ditch New, fixes above
func Open() *DB {
return nil
} }
// Create... // Create...
func Create() *DB { func Create(dbPath string) (*DB, error) {
return nil 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... // Close...
func (d *DB) Close() error { 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
View 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)
}
}

View file

@ -1 +1,7 @@
package channeldb package channeldb
import "fmt"
var (
ErrNoExists = fmt.Errorf("channel db has not yet been created")
)