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 committed 2017-06-16 22:28:58 +02:00
1 parent 3efd3c4f8c
commit 4b8d052afc
3 files changed
+31 -14

No files matched your search

+10 -6
View File
@@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain" "github.com/lightningnetwork/lnd/shachain"
"github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg/chainhash" "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 // to an on-disk log, which can then subsequently be queried in order to
// "time-travel" to a prior state. // "time-travel" to a prior state.
type OpenChannel struct { type OpenChannel struct {
// OpeningHeight is the height in which this channel was officially // ShortChannelID encodes the exact location in the chain in which the
// marked open. // channel was initially confirmed. This includes: the block height,
OpeningHeight uint32 // transaction index, and the output within the target transaction.
ShortChanID lnwire.ShortChannelID
// FundingBroadcastHeight is the height in which the funding // FundingBroadcastHeight is the height in which the funding
// transaction was broadcast. This value can be used by higher level // 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()) copy(keyPrefix[len(confInfoPrefix):], b.Bytes())
// We store the conf info in the following format: broadcast || open. // 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[:], channel.FundingBroadcastHeight)
byteOrder.PutUint32(scratch[4:], channel.OpeningHeight) byteOrder.PutUint64(scratch[4:], channel.ShortChanID.ToUint64())
return openChanBucket.Put(keyPrefix, scratch[:]) return openChanBucket.Put(keyPrefix, scratch[:])
} }
@@ -1541,7 +1543,9 @@ func fetchChanConfInfo(openChanBucket *bolt.Bucket, channel *OpenChannel) error
confInfoBytes := openChanBucket.Get(keyPrefix) confInfoBytes := openChanBucket.Get(keyPrefix)
channel.FundingBroadcastHeight = byteOrder.Uint32(confInfoBytes[:4]) channel.FundingBroadcastHeight = byteOrder.Uint32(confInfoBytes[:4])
channel.OpeningHeight = byteOrder.Uint32(confInfoBytes[4:]) channel.ShortChanID = lnwire.NewShortChanIDFromInt(
byteOrder.Uint64(confInfoBytes[4:]),
)
return nil return nil
} }
+16 -6
View File
@@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain" "github.com/lightningnetwork/lnd/shachain"
"github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg" "github.com/roasbeef/btcd/chaincfg"
@@ -677,8 +678,12 @@ func TestFetchPendingChannels(t *testing.T) {
broadcastHeight) broadcastHeight)
} }
const openHeight = 100 chanOpenLoc := lnwire.ShortChannelID{
err = cdb.MarkChannelAsOpen(pendingChannels[0].ChanID, openHeight) BlockHeight: 5,
TxIndex: 10,
TxPosition: 15,
}
err = cdb.MarkChannelAsOpen(pendingChannels[0].ChanID, chanOpenLoc)
if err != nil { if err != nil {
t.Fatalf("unable to mark channel as open: %v", err) t.Fatalf("unable to mark channel as open: %v", err)
} }
@@ -689,9 +694,10 @@ func TestFetchPendingChannels(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to fetch channels: %v", err) 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, "+ 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 { if openChans[0].FundingBroadcastHeight != broadcastHeight {
t.Fatalf("broadcast height mismatch: expected %v, got %v", 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 // Next, simulate the confirmation of the channel by marking it as
// pending within the database. // pending within the database.
const openHeight = 100 chanOpenLoc := lnwire.ShortChannelID{
if err := cdb.MarkChannelAsOpen(state.ChanID, openHeight); err != nil { 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) t.Fatalf("unable to mark channel as open: %v", err)
} }
+5 -2
View File
@@ -9,6 +9,7 @@ import (
"sync" "sync"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/wire" "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 // MarkChannelAsOpen records the finalization of the funding process and marks
// a channel as available for use. Additionally the height in which this // a channel as available for use. Additionally the height in which this
// channel as opened will also be recorded within the database. // 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 { return d.Update(func(tx *bolt.Tx) error {
openChanBucket := tx.Bucket(openChannelBucket) openChanBucket := tx.Bucket(openChannelBucket)
if openChanBucket == nil { if openChanBucket == nil {
@@ -399,7 +402,7 @@ func (d *DB) MarkChannelAsOpen(outpoint *wire.OutPoint, openHeight uint32) error
infoCopy := make([]byte, len(confInfoBytes)) infoCopy := make([]byte, len(confInfoBytes))
copy(infoCopy[:], confInfoBytes) copy(infoCopy[:], confInfoBytes)
byteOrder.PutUint32(infoCopy[4:], openHeight) byteOrder.PutUint64(infoCopy[4:], openLoc.ToUint64())
return openChanBucket.Put(confInfoKey, infoCopy) return openChanBucket.Put(confInfoKey, infoCopy)
}) })