channeldb: replace opening height will short chan ID in OpenChannel

This commit modifies the OpenChannel struct to include the full short
channel ID rather than simply the opening height. This new field will
be needed by an upcoming change to uniformly switch to using short
channel ID’s when forwarding HTLC’s due to the change in per-hop
payloads.
This commit is contained in:
Olaoluwa Osuntokun 2017-06-16 22:28:26 +02:00
parent 3efd3c4f8c
commit 4b8d052afc
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
3 changed files with 31 additions and 14 deletions

@ -10,6 +10,7 @@ import (
"time"
"github.com/boltdb/bolt"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg/chainhash"
@ -130,9 +131,10 @@ const (
// to an on-disk log, which can then subsequently be queried in order to
// "time-travel" to a prior state.
type OpenChannel struct {
// OpeningHeight is the height in which this channel was officially
// marked open.
OpeningHeight uint32
// ShortChannelID encodes the exact location in the chain in which the
// channel was initially confirmed. This includes: the block height,
// transaction index, and the output within the target transaction.
ShortChanID lnwire.ShortChannelID
// FundingBroadcastHeight is the height in which the funding
// transaction was broadcast. This value can be used by higher level
@ -1522,9 +1524,9 @@ func putChanConfInfo(openChanBucket *bolt.Bucket, channel *OpenChannel) error {
copy(keyPrefix[len(confInfoPrefix):], b.Bytes())
// We store the conf info in the following format: broadcast || open.
var scratch [8]byte
var scratch [12]byte
byteOrder.PutUint32(scratch[:], channel.FundingBroadcastHeight)
byteOrder.PutUint32(scratch[4:], channel.OpeningHeight)
byteOrder.PutUint64(scratch[4:], channel.ShortChanID.ToUint64())
return openChanBucket.Put(keyPrefix, scratch[:])
}
@ -1541,7 +1543,9 @@ func fetchChanConfInfo(openChanBucket *bolt.Bucket, channel *OpenChannel) error
confInfoBytes := openChanBucket.Get(keyPrefix)
channel.FundingBroadcastHeight = byteOrder.Uint32(confInfoBytes[:4])
channel.OpeningHeight = byteOrder.Uint32(confInfoBytes[4:])
channel.ShortChanID = lnwire.NewShortChanIDFromInt(
byteOrder.Uint64(confInfoBytes[4:]),
)
return nil
}

@ -10,6 +10,7 @@ import (
"time"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg"
@ -677,8 +678,12 @@ func TestFetchPendingChannels(t *testing.T) {
broadcastHeight)
}
const openHeight = 100
err = cdb.MarkChannelAsOpen(pendingChannels[0].ChanID, openHeight)
chanOpenLoc := lnwire.ShortChannelID{
BlockHeight: 5,
TxIndex: 10,
TxPosition: 15,
}
err = cdb.MarkChannelAsOpen(pendingChannels[0].ChanID, chanOpenLoc)
if err != nil {
t.Fatalf("unable to mark channel as open: %v", err)
}
@ -689,9 +694,10 @@ func TestFetchPendingChannels(t *testing.T) {
if err != nil {
t.Fatalf("unable to fetch channels: %v", err)
}
if openChans[0].OpeningHeight != openHeight {
if openChans[0].ShortChanID != chanOpenLoc {
t.Fatalf("channel opening heights don't match: expected %v, "+
"got %v", openChans[0].OpeningHeight, openHeight)
"got %v", spew.Sdump(openChans[0].ShortChanID),
chanOpenLoc)
}
if openChans[0].FundingBroadcastHeight != broadcastHeight {
t.Fatalf("broadcast height mismatch: expected %v, got %v",
@ -737,8 +743,12 @@ func TestFetchClosedChannels(t *testing.T) {
// Next, simulate the confirmation of the channel by marking it as
// pending within the database.
const openHeight = 100
if err := cdb.MarkChannelAsOpen(state.ChanID, openHeight); err != nil {
chanOpenLoc := lnwire.ShortChannelID{
BlockHeight: 5,
TxIndex: 10,
TxPosition: 15,
}
if err := cdb.MarkChannelAsOpen(state.ChanID, chanOpenLoc); err != nil {
t.Fatalf("unable to mark channel as open: %v", err)
}

@ -9,6 +9,7 @@ import (
"sync"
"github.com/boltdb/bolt"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/wire"
)
@ -364,7 +365,9 @@ func fetchChannels(d *DB, pendingOnly bool) ([]*OpenChannel, error) {
// MarkChannelAsOpen records the finalization of the funding process and marks
// a channel as available for use. Additionally the height in which this
// channel as opened will also be recorded within the database.
func (d *DB) MarkChannelAsOpen(outpoint *wire.OutPoint, openHeight uint32) error {
func (d *DB) MarkChannelAsOpen(outpoint *wire.OutPoint,
openLoc lnwire.ShortChannelID) error {
return d.Update(func(tx *bolt.Tx) error {
openChanBucket := tx.Bucket(openChannelBucket)
if openChanBucket == nil {
@ -399,7 +402,7 @@ func (d *DB) MarkChannelAsOpen(outpoint *wire.OutPoint, openHeight uint32) error
infoCopy := make([]byte, len(confInfoBytes))
copy(infoCopy[:], confInfoBytes)
byteOrder.PutUint32(infoCopy[4:], openHeight)
byteOrder.PutUint64(infoCopy[4:], openLoc.ToUint64())
return openChanBucket.Put(confInfoKey, infoCopy)
})