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" "io"
"time" "time"
"github.com/boltdb/bolt"
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/waddrmgr" "github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb"
"github.com/lightningnetwork/lnd/shachain" "github.com/lightningnetwork/lnd/shachain"
) )
@ -97,65 +97,35 @@ type OpenChannel struct {
CreationTime time.Time 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... // PutOpenChannel...
func (c *DB) PutOpenChannel(channel *OpenChannel) error { func (d *DB) PutOpenChannel(channel *OpenChannel) error {
return c.namespace.Update(func(tx walletdb.Tx) error { return d.db.Update(func(tx *bolt.Tx) error {
// Get the bucket dedicated to storing the meta-data for open // Get the bucket dedicated to storing the meta-data for open
// channels. // channels.
rootBucket := tx.RootBucket() openChanBucket, err := tx.CreateBucketIfNotExists(openChannelBucket)
openChanBucket, err := rootBucket.CreateBucketIfNotExists(openChannelBucket)
if err != nil { if err != nil {
return err return err
} }
return putOpenChannel(openChanBucket, channel, c.addrmgr) return putOpenChannel(openChanBucket, channel, d.addrmgr)
}) })
} }
// GetOpenChannel... // GetOpenChannel...
// TODO(roasbeef): assumes only 1 active channel per-node // 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 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 // Get the bucket dedicated to storing the meta-data for open
// channels. // channels.
rootBucket := tx.RootBucket() openChanBucket := tx.Bucket(openChannelBucket)
openChanBucket := rootBucket.Bucket(openChannelBucket)
if openChannelBucket == nil { if openChannelBucket == nil {
return fmt.Errorf("open channel bucket does not exist") return fmt.Errorf("open channel bucket does not exist")
} }
oChannel, err := fetchOpenChannel(openChanBucket, nodeID, oChannel, err := fetchOpenChannel(openChanBucket, nodeID,
c.addrmgr) d.addrmgr)
if err != nil { if err != nil {
return err return err
} }
@ -167,7 +137,7 @@ func (c *DB) FetchOpenChannel(nodeID [32]byte) (*OpenChannel, error) {
} }
// putChannel... // putChannel...
func putOpenChannel(activeChanBucket walletdb.Bucket, channel *OpenChannel, func putOpenChannel(activeChanBucket *bolt.Bucket, channel *OpenChannel,
addrmgr *waddrmgr.Manager) error { addrmgr *waddrmgr.Manager) error {
// Generate a serialized version of the open channel. The addrmgr is // Generate a serialized version of the open channel. The addrmgr is
@ -188,7 +158,7 @@ func putOpenChannel(activeChanBucket walletdb.Bucket, channel *OpenChannel,
} }
// fetchOpenChannel // fetchOpenChannel
func fetchOpenChannel(bucket walletdb.Bucket, nodeID [32]byte, func fetchOpenChannel(bucket *bolt.Bucket, nodeID [32]byte,
addrmgr *waddrmgr.Manager) (*OpenChannel, error) { addrmgr *waddrmgr.Manager) (*OpenChannel, error) {
// Grab the bucket dedicated to storing data related to this particular // Grab the bucket dedicated to storing data related to this particular

View File

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