From 4b8d052afc881cacb9704e96eec9c3c274622684 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 16 Jun 2017 22:28:26 +0200 Subject: [PATCH] channeldb: replace opening height will short chan ID in OpenChannel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- channeldb/channel.go | 16 ++++++++++------ channeldb/channel_test.go | 22 ++++++++++++++++------ channeldb/db.go | 7 +++++-- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index e06c402d..d72e222c 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -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 } diff --git a/channeldb/channel_test.go b/channeldb/channel_test.go index 3eef2717..ec72d5c4 100644 --- a/channeldb/channel_test.go +++ b/channeldb/channel_test.go @@ -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) } diff --git a/channeldb/db.go b/channeldb/db.go index df236c27..078d0c0f 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -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) })