2015-12-26 21:35:15 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
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"
|
2015-12-26 21:35:15 +03:00
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
2016-03-23 04:46:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dbName = "channel.db"
|
2015-12-26 21:35:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
endian = binary.BigEndian
|
|
|
|
)
|
|
|
|
|
|
|
|
var bufPool = &sync.Pool{
|
|
|
|
New: func() interface{} { return new(bytes.Buffer) },
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store...
|
|
|
|
// TODO(roasbeef): CHECKSUMS, REDUNDANCY, etc etc.
|
|
|
|
type DB struct {
|
|
|
|
// TODO(roasbeef): caching, etc?
|
|
|
|
addrmgr *waddrmgr.Manager
|
|
|
|
|
2016-03-23 04:46:30 +03:00
|
|
|
db *bolt.DB
|
2015-12-26 21:35:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wipe...
|
|
|
|
func (d *DB) Wipe() error {
|
2016-03-23 04:46:30 +03:00
|
|
|
return d.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
return tx.DeleteBucket(openChannelBucket)
|
2015-12-26 21:35:15 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// New...
|
|
|
|
// TODO(roasbeef): re-visit this dependancy...
|
2016-03-23 04:46:30 +03:00
|
|
|
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
|
2015-12-26 21:35:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open...
|
|
|
|
// TODO(roasbeef): create+open, ditch New, fixes above
|
|
|
|
func Open() *DB {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create...
|
|
|
|
func Create() *DB {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-23 04:46:30 +03:00
|
|
|
|
|
|
|
// Close...
|
|
|
|
func (d *DB) Close() error {
|
|
|
|
return d.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): SetCryptoSystem method...
|
|
|
|
// * don't have waddrmgr up before..
|