From de01721aed48f8afa1728431fd0a08a79177a7fc Mon Sep 17 00:00:00 2001 From: Andrey Samokhvalov Date: Tue, 2 May 2017 22:01:46 +0300 Subject: [PATCH] htlcswitch: add hop iterator Short: such abstraction give as ability to test the channel link in the future. Long: hop iterator represents the entity which is be able to give payment route hop by hop. This interface will be used to have an abstraction over the algorithm which we use to determine the next hope in htlc route and also helps the unit test to create mock representation of such algorithm which uses simple array of hops. --- htlcswitch/iterator.go | 12 ++++++++++++ htlcswitch/link.go | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/htlcswitch/iterator.go b/htlcswitch/iterator.go index 833aaff6..9387df6f 100644 --- a/htlcswitch/iterator.go +++ b/htlcswitch/iterator.go @@ -3,6 +3,7 @@ package htlcswitch import ( "bytes" "encoding/hex" + "io" "github.com/btcsuite/golangcrypto/ripemd160" "github.com/roasbeef/btcutil" @@ -29,3 +30,14 @@ func (h HopID) String() string { func (h HopID) IsEqual(h2 HopID) bool { return bytes.Equal(h[:], h2[:]) } + +// HopIterator interface represent the entity which is able to give route +// hops one by one. This interface is used to have an abstraction over the +// algorithm which we use to determine the next hope in htlc route. +type HopIterator interface { + // Next returns next hop if exist and nil if route is ended. + Next() *HopID + + // Encode encodes iterator and writes it to the writer. + Encode(w io.Writer) error +} diff --git a/htlcswitch/link.go b/htlcswitch/link.go index aee12639..e37edc05 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -7,6 +7,8 @@ import ( "sync/atomic" "time" + "io" + "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/lnwallet" @@ -26,6 +28,10 @@ type ChannelLinkConfig struct { // packets to other peer which should handle it. Switch *Switch + // DecodeOnion function responsible for decoding htlc Sphinx onion blob, + // and creating hop iterator which will give us next destination of htlc. + DecodeOnion func(r io.Reader, meta []byte) (HopIterator, error) + // Peer is a lightning network node with which we have the channel // link opened. Peer Peer