2015-12-26 21:35:15 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2016-03-24 08:11:57 +03:00
|
|
|
"fmt"
|
2016-03-23 04:46:30 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-12-26 21:35:15 +03:00
|
|
|
"sync"
|
|
|
|
|
2016-03-23 04:46:30 +03:00
|
|
|
"github.com/boltdb/bolt"
|
2016-04-24 22:35:52 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2016-03-23 04:46:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dbName = "channel.db"
|
2015-12-26 21:35:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-03-24 08:11:57 +03:00
|
|
|
// Big endian is the preferred byte order, due to cursor scans over integer
|
|
|
|
// keys iterating in order.
|
|
|
|
byteOrder = binary.BigEndian
|
2015-12-26 21:35:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var bufPool = &sync.Pool{
|
|
|
|
New: func() interface{} { return new(bytes.Buffer) },
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:11:57 +03:00
|
|
|
// 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...
|
2015-12-26 21:35:15 +03:00
|
|
|
type DB struct {
|
2016-03-24 08:11:57 +03:00
|
|
|
store *bolt.DB
|
|
|
|
|
2016-04-24 22:35:52 +03:00
|
|
|
netParams *chaincfg.Params
|
|
|
|
|
2016-03-24 08:11:57 +03:00
|
|
|
cryptoSystem EncryptorDecryptor
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens an existing channeldb created under the passed namespace with
|
|
|
|
// sensitive data encrypted by the passed EncryptorDecryptor implementation.
|
|
|
|
// TODO(roasbeef): versioning?
|
2016-04-24 22:35:52 +03:00
|
|
|
func Open(dbPath string, netParams *chaincfg.Params) (*DB, error) {
|
2016-03-24 08:11:57 +03:00
|
|
|
path := filepath.Join(dbPath, dbName)
|
2015-12-26 21:35:15 +03:00
|
|
|
|
2016-03-25 00:31:46 +03:00
|
|
|
if !fileExists(path) {
|
|
|
|
if err := createChannelDB(dbPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 08:11:57 +03:00
|
|
|
|
2016-03-25 00:31:46 +03:00
|
|
|
bdb, err := bolt.Open(path, 0600, nil)
|
2016-03-24 08:11:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-24 22:35:52 +03:00
|
|
|
return &DB{store: bdb, netParams: netParams}, nil
|
2016-03-24 08:11:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterCryptoSystem...
|
|
|
|
func (d *DB) RegisterCryptoSystem(ed EncryptorDecryptor) {
|
|
|
|
d.cryptoSystem = ed
|
2015-12-26 21:35:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wipe...
|
|
|
|
func (d *DB) Wipe() error {
|
2016-03-24 08:11:57 +03:00
|
|
|
return d.store.Update(func(tx *bolt.Tx) error {
|
|
|
|
// TODO(roasbee): delete all other top-level buckets.
|
2016-03-23 04:46:30 +03:00
|
|
|
return tx.DeleteBucket(openChannelBucket)
|
2015-12-26 21:35:15 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:11:57 +03:00
|
|
|
// Close...
|
|
|
|
func (d *DB) Close() error {
|
|
|
|
return d.store.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// createChannelDB...
|
2016-03-25 00:31:46 +03:00
|
|
|
func createChannelDB(dbPath string) error {
|
2016-03-24 08:11:57 +03:00
|
|
|
if !fileExists(dbPath) {
|
|
|
|
if err := os.MkdirAll(dbPath, 0700); err != nil {
|
2016-03-25 00:31:46 +03:00
|
|
|
return err
|
2016-03-23 04:46:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(dbPath, dbName)
|
2016-03-24 08:11:57 +03:00
|
|
|
bdb, err := bolt.Open(path, 0600, nil)
|
2016-03-23 04:46:30 +03:00
|
|
|
if err != nil {
|
2016-03-25 00:31:46 +03:00
|
|
|
return err
|
2016-03-23 04:46:30 +03:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:11:57 +03:00
|
|
|
err = bdb.Update(func(tx *bolt.Tx) error {
|
|
|
|
if _, err := tx.CreateBucket(openChannelBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-26 21:35:15 +03:00
|
|
|
|
2016-03-24 08:11:57 +03:00
|
|
|
if _, err := tx.CreateBucket(closedChannelBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-26 21:35:15 +03:00
|
|
|
|
2016-03-24 08:11:57 +03:00
|
|
|
if _, err := tx.CreateBucket(channelLogBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-03-25 00:31:46 +03:00
|
|
|
return fmt.Errorf("unable to create new channeldb")
|
2016-03-24 08:11:57 +03:00
|
|
|
}
|
|
|
|
|
2016-03-25 00:31:46 +03:00
|
|
|
return bdb.Close()
|
2015-12-26 21:35:15 +03:00
|
|
|
}
|
2016-03-23 04:46:30 +03:00
|
|
|
|
2016-03-24 08:11:57 +03:00
|
|
|
// fileExists...
|
|
|
|
func fileExists(path string) bool {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2016-03-23 04:46:30 +03:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:39:52 +03:00
|
|
|
// FetchOpenChannel...
|
|
|
|
func (d *DB) FetchOpenChannel(nodeID [32]byte) (*OpenChannel, error) {
|
|
|
|
var channel *OpenChannel
|
|
|
|
err := d.store.View(func(tx *bolt.Tx) error {
|
|
|
|
// Get the bucket dedicated to storing the meta-data for open
|
|
|
|
// channels.
|
|
|
|
openChanBucket := tx.Bucket(openChannelBucket)
|
|
|
|
if openChannelBucket == nil {
|
|
|
|
return fmt.Errorf("open channel bucket does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
oChannel, err := fetchOpenChannel(openChanBucket, nodeID, d.cryptoSystem)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
channel = oChannel
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return channel, err
|
|
|
|
}
|