lnd.xprv/channeldb/nodes.go
Olaoluwa Osuntokun 87603e780f channeldb: re-design OpenChannel schema, update tests
The state of OpenChannel on disk has now been partitioned into several
buckets+keys within the db. At the top level, a set of prefixed keys
stored common data updated frequently (with every channel update).
These fields are stored at the top level in order to facilities prefix
scans, and to avoid read/write amplification due to
serialization/deserialization with each read/write.

Within the active channel bucket, a nested bucket keyed on the node’s
ID stores the remainder of the channel.

Additionally OpenChannel now uses elkrem rather than shachain, delivery
scripts instead of addresses, stores the total net fees, and splits the
csv delay into the remote vs local node’s.

Several TODO’s have been left lingering, to be visited in the near
future.
2016-03-23 22:39:52 -07:00

57 lines
1.3 KiB
Go

package channeldb
import (
"fmt"
"golang.org/x/crypto/ripemd160"
"github.com/boltdb/bolt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
)
var (
idBucket = []byte("i")
ActiveNetParams = &chaincfg.TestNet3Params
)
// PutIdKey saves the hash160 of the public key used for our identity within
// the Lightning Network.
func (d *DB) PutIdKey(pkh []byte) error {
return d.store.Update(func(tx *bolt.Tx) error {
// Get the bucket dedicated to storing the meta-data for open
// channels.
bucket, err := tx.CreateBucketIfNotExists(idBucket)
if err != nil {
return err
}
return bucket.Put(identityKey, pkh)
})
}
// GetIdKey returns the hash160 of the public key used for out identity within
// the Lightning Network as a p2pkh bitcoin address.
func (d *DB) GetIdAdr() (*btcutil.AddressPubKeyHash, error) {
pkh := make([]byte, ripemd160.Size)
err := d.store.View(func(tx *bolt.Tx) error {
// Get the bucket dedicated to storing the meta-data for open
// channels.
bucket := tx.Bucket(idBucket)
if bucket == nil {
return fmt.Errorf("id bucket not created")
}
pkBytes := bucket.Get(identityKey)
copy(pkh, pkBytes)
return nil
})
if err != nil {
return nil, err
}
log.Infof("identity key has length %d", len(pkh))
return btcutil.NewAddressPubKeyHash(pkh, ActiveNetParams)
}