channeldb: switch to bolt.DB instead of walletdb.DB

This decouples channeldb from btcwallet, and also allows us access to
bolt’s Batch() call.
This commit is contained in:
Olaoluwa Osuntokun 2016-03-22 18:46:30 -07:00
parent 670d441dea
commit 3e3948a04f
2 changed files with 45 additions and 50 deletions

View File

@ -7,12 +7,12 @@ import (
"io"
"time"
"github.com/boltdb/bolt"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb"
"github.com/lightningnetwork/lnd/shachain"
)
@ -97,65 +97,35 @@ type OpenChannel struct {
CreationTime time.Time
}
// These don't really belong here but not sure which other file to put them yet.
// PutIdKey saves the private key used for
func (c *DB) PutIdKey(pkh []byte) error {
return c.namespace.Update(func(tx walletdb.Tx) error {
// Get the bucket dedicated to storing the meta-data for open
// channels.
rootBucket := tx.RootBucket()
return rootBucket.Put(identityKey, pkh)
})
}
// GetIdKey returns the IdKey
func (c *DB) GetIdAdr() (*btcutil.AddressPubKeyHash, error) {
var pkh []byte
err := c.namespace.View(func(tx walletdb.Tx) error {
// Get the bucket dedicated to storing the meta-data for open
// channels.
rootBucket := tx.RootBucket()
pkh = rootBucket.Get(identityKey)
return nil
})
if err != nil {
return nil, err
}
fmt.Printf("identity key has length %d\n", len(pkh))
return btcutil.NewAddressPubKeyHash(pkh, ActiveNetParams)
}
// PutOpenChannel...
func (c *DB) PutOpenChannel(channel *OpenChannel) error {
return c.namespace.Update(func(tx walletdb.Tx) error {
func (d *DB) PutOpenChannel(channel *OpenChannel) error {
return d.db.Update(func(tx *bolt.Tx) error {
// Get the bucket dedicated to storing the meta-data for open
// channels.
rootBucket := tx.RootBucket()
openChanBucket, err := rootBucket.CreateBucketIfNotExists(openChannelBucket)
openChanBucket, err := tx.CreateBucketIfNotExists(openChannelBucket)
if err != nil {
return err
}
return putOpenChannel(openChanBucket, channel, c.addrmgr)
return putOpenChannel(openChanBucket, channel, d.addrmgr)
})
}
// GetOpenChannel...
// TODO(roasbeef): assumes only 1 active channel per-node
func (c *DB) FetchOpenChannel(nodeID [32]byte) (*OpenChannel, error) {
func (d *DB) FetchOpenChannel(nodeID [32]byte) (*OpenChannel, error) {
var channel *OpenChannel
err := c.namespace.View(func(tx walletdb.Tx) error {
err := d.db.View(func(tx *bolt.Tx) error {
// Get the bucket dedicated to storing the meta-data for open
// channels.
rootBucket := tx.RootBucket()
openChanBucket := rootBucket.Bucket(openChannelBucket)
openChanBucket := tx.Bucket(openChannelBucket)
if openChannelBucket == nil {
return fmt.Errorf("open channel bucket does not exist")
}
oChannel, err := fetchOpenChannel(openChanBucket, nodeID,
c.addrmgr)
d.addrmgr)
if err != nil {
return err
}
@ -167,7 +137,7 @@ func (c *DB) FetchOpenChannel(nodeID [32]byte) (*OpenChannel, error) {
}
// putChannel...
func putOpenChannel(activeChanBucket walletdb.Bucket, channel *OpenChannel,
func putOpenChannel(activeChanBucket *bolt.Bucket, channel *OpenChannel,
addrmgr *waddrmgr.Manager) error {
// Generate a serialized version of the open channel. The addrmgr is
@ -188,7 +158,7 @@ func putOpenChannel(activeChanBucket walletdb.Bucket, channel *OpenChannel,
}
// fetchOpenChannel
func fetchOpenChannel(bucket walletdb.Bucket, nodeID [32]byte,
func fetchOpenChannel(bucket *bolt.Bucket, nodeID [32]byte,
addrmgr *waddrmgr.Manager) (*OpenChannel, error) {
// Grab the bucket dedicated to storing data related to this particular

View File

@ -3,10 +3,16 @@ package channeldb
import (
"bytes"
"encoding/binary"
"os"
"path/filepath"
"sync"
"github.com/boltdb/bolt"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb"
)
const (
dbName = "channel.db"
)
var (
@ -23,23 +29,34 @@ type DB struct {
// TODO(roasbeef): caching, etc?
addrmgr *waddrmgr.Manager
namespace walletdb.Namespace
db *bolt.DB
}
// Wipe...
func (d *DB) Wipe() error {
return d.namespace.Update(func(tx walletdb.Tx) error {
rootBucket := tx.RootBucket()
// TODO(roasbeef): other buckets
return rootBucket.DeleteBucket(openChannelBucket)
return d.db.Update(func(tx *bolt.Tx) error {
return tx.DeleteBucket(openChannelBucket)
})
}
// New...
// TODO(roasbeef): re-visit this dependancy...
func New(addrmgr *waddrmgr.Manager, namespace walletdb.Namespace) *DB {
// TODO(roasbeef): create buckets if not created?
return &DB{addrmgr, namespace}
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)
boltDB, err := bolt.Open(path, 0600, nil)
if err != nil {
return nil, err
}
return &DB{addrmgr, boltDB}, nil
}
// Open...
@ -52,3 +69,11 @@ func Open() *DB {
func Create() *DB {
return nil
}
// Close...
func (d *DB) Close() error {
return d.db.Close()
}
// TODO(roasbeef): SetCryptoSystem method...
// * don't have waddrmgr up before..