From 81e65e00e5e242c860eb3a0d20f4547fcf1360f7 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 17 Nov 2016 18:28:07 -0800 Subject: [PATCH] channeldb: include the output index within stored HTLC's --- channeldb/channel.go | 23 ++++++++++++++++------- channeldb/channel_test.go | 1 + 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index 3639b9965..48c659f7b 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -409,13 +409,14 @@ type HTLC struct { // must wait before reclaiming the funds in limbo. RefundTimeout uint32 - // RevocationDelay is the relative timeout the party who broadcasts - // the commitment transaction must wait before being able to fully - // sweep the funds on-chain in the case of a unilateral channel - // closure. + // RevocationDelay is the relative timeout the party who broadcasts the + // commitment transaction must wait before being able to fully sweep + // the funds on-chain in the case of a unilateral channel closure. RevocationDelay uint32 - // TODO(roasbeef): add output index? + // OutputIndex is the vout output index for this particular HTLC output + // on the commitment transaction. + OutputIndex uint16 } // Copy returns a full copy of the target HTLC. @@ -425,6 +426,7 @@ func (h *HTLC) Copy() HTLC { Amt: h.Amt, RefundTimeout: h.RefundTimeout, RevocationDelay: h.RevocationDelay, + OutputIndex: h.OutputIndex, } copy(clone.RHash[:], h.RHash[:]) @@ -1446,8 +1448,8 @@ func fetchChanDeliveryScripts(nodeChanBucket *bolt.Bucket, channel *OpenChannel) // htlcDiskSize represents the number of btyes a serialized HTLC takes up on // disk. The size of an HTLC on disk is 49 bytes total: incoming (1) + amt (8) -// + rhash (32) + timeouts (8) -const htlcDiskSize = 1 + 8 + 32 + 4 + 4 +// + rhash (32) + timeouts (8) + output index (2) +const htlcDiskSize = 1 + 8 + 32 + 4 + 4 + 2 func serializeHTLC(w io.Writer, h *HTLC) error { var buf [htlcDiskSize]byte @@ -1468,6 +1470,8 @@ func serializeHTLC(w io.Writer, h *HTLC) error { n += 4 byteOrder.PutUint32(buf[n:], h.RevocationDelay) n += 4 + byteOrder.PutUint16(buf[n:], h.OutputIndex) + n += 2 if _, err := w.Write(buf[:]); err != nil { return err @@ -1509,6 +1513,11 @@ func deserializeHTLC(r io.Reader) (*HTLC, error) { } h.RevocationDelay = byteOrder.Uint32(scratch[:]) + if _, err := io.ReadFull(r, scratch[:2]); err != nil { + return nil, err + } + h.OutputIndex = byteOrder.Uint16(scratch[:]) + return h, nil } diff --git a/channeldb/channel_test.go b/channeldb/channel_test.go index 01551e878..5216d9a4c 100644 --- a/channeldb/channel_test.go +++ b/channeldb/channel_test.go @@ -384,6 +384,7 @@ func TestChannelStateTransition(t *testing.T) { RHash: key, RefundTimeout: i, RevocationDelay: i + 2, + OutputIndex: uint16(i * 3), } htlcs = append(htlcs, htlc) }