From 7f572fc155ef5d1b567d6c0bacd4f9273a11a86d Mon Sep 17 00:00:00 2001 From: Andrey Samokhvalov Date: Wed, 3 May 2017 17:02:22 +0300 Subject: [PATCH] htlcswitch: add channel link config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Step №1 in making htlcManager (aka channelLink) testable: Start use config which will allow as mock/stub external subsystems. --- htlcswitch/interfaces.go | 19 +++++++++++++++++++ htlcswitch/link.go | 31 +++++++++++++++++++++++++++++++ htlcswitch/mock.go | 5 +++++ 3 files changed, 55 insertions(+) diff --git a/htlcswitch/interfaces.go b/htlcswitch/interfaces.go index d370b111..57328d03 100644 --- a/htlcswitch/interfaces.go +++ b/htlcswitch/interfaces.go @@ -3,10 +3,25 @@ package htlcswitch import ( "crypto/sha256" + "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" + "github.com/roasbeef/btcd/chaincfg/chainhash" "github.com/roasbeef/btcutil" ) +// InvoiceDatabase is an interface which represents the persistent subsystem +// which may search, lookup and settle invoices. +type InvoiceDatabase interface { + // LookupInvoice attempts to look up an invoice according to it's 32 + // byte payment hash. + LookupInvoice(chainhash.Hash) (*channeldb.Invoice, error) + + // SettleInvoice attempts to mark an invoice corresponding to the passed + // payment hash as fully settled. + SettleInvoice(chainhash.Hash) error +} + // ChannelLink is an interface which represents the subsystem for managing // the incoming htlc requests, applying the changes to the channel, and also // propagating/forwarding it to htlc switch. @@ -66,6 +81,10 @@ type Peer interface { // ID returns the lightning network peer id. ID() [sha256.Size]byte + // WipeChannel removes the passed channel from all indexes associated + // with the peer. + WipeChannel(*lnwallet.LightningChannel) error + // PubKey returns the peer public key. PubKey() []byte diff --git a/htlcswitch/link.go b/htlcswitch/link.go index b36e8166..26c4882b 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -17,6 +17,33 @@ import ( "github.com/roasbeef/btcutil" ) +// ChannelLinkConfig defines the configuration for the channel link. ALL +// elements within the configuration MUST be non-nil for channel link to carry +// out its duties. +type ChannelLinkConfig struct { + // Switch is a subsystem which is used to forward the incoming htlc + // packets to other peer which should handle it. + Switch *Switch + + // Peer is a lightning network node with which we have the channel + // link opened. + Peer Peer + + // Registry is a sub-system which responsible for managing the + // invoices in thread-safe manner. + Registry InvoiceDatabase + + // SettledContracts is used to notify that a channel has peacefully been + // closed. Once a channel has been closed the other subsystem no longer + // needs to watch for breach closes. + SettledContracts chan *wire.OutPoint + + // DebugHTLC should be turned on if you want all HTLCs sent to a node + // with the debug htlc R-Hash are immediately settled in the next + // available state transition. + DebugHTLC bool +} + // commitmentState is the volatile+persistent state of an active channel's // commitment update state-machine. This struct is used by htlcManager's to // save meta-state required for proper functioning. @@ -63,6 +90,10 @@ type commitmentState struct { channel *lnwallet.LightningChannel chanPoint *wire.OutPoint chanID lnwire.ChannelID + + // cfg is a structure which carries all dependable fields/handlers + // which may affect behaviour of the service. + cfg *ChannelLinkConfig } // htlcManager is the primary goroutine which drives a channel's commitment diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index 442a6300..cf0d6428 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -10,6 +10,7 @@ import ( "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/lnwire" "github.com/roasbeef/btcutil" + "github.com/lightningnetwork/lnd/lnwallet" ) type mockServer struct { @@ -152,6 +153,10 @@ func (s *mockServer) Disconnect() { s.t.Fatalf("server %v was disconnected", s.name) } +func (s *mockServer) WipeChannel(*lnwallet.LightningChannel) error { + return nil +} + func (s *mockServer) Stop() { if !atomic.CompareAndSwapInt32(&s.shutdown, 0, 1) { return