peer: properly initialize ChannelLink with new block+height info

This commit is contained in:
Olaoluwa Osuntokun 2017-08-02 21:15:49 -07:00
parent c14eaa7b6c
commit 79e68a2fdf
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
1 changed files with 39 additions and 19 deletions

58
peer.go
View File

@ -307,21 +307,29 @@ func (p *peer) loadActiveChannels(chans []*channeldb.OpenChannel) error {
// Register this new channel link with the HTLC Switch. This is // Register this new channel link with the HTLC Switch. This is
// necessary to properly route multi-hop payments, and forward // necessary to properly route multi-hop payments, and forward
// new payments triggered by RPC clients. // new payments triggered by RPC clients.
link := htlcswitch.NewChannelLink( blockEpoch, err := p.server.cc.chainNotifier.RegisterBlockEpochNtfn()
htlcswitch.ChannelLinkConfig{ if err != nil {
Peer: p, return err
DecodeHopIterator: p.server.sphinx.DecodeHopIterator, }
DecodeOnionObfuscator: p.server.sphinx.DecodeOnionObfuscator, _, currentHeight, err := p.server.cc.chainIO.GetBestBlock()
GetLastChannelUpdate: createGetLastUpdate(p.server.chanRouter, if err != nil {
p.PubKey(), lnChan.ShortChanID()), return err
SettledContracts: p.server.breachArbiter.settledContracts, }
DebugHTLC: cfg.DebugHTLC, linkCfg := htlcswitch.ChannelLinkConfig{
Registry: p.server.invoices, Peer: p,
Switch: p.server.htlcSwitch, DecodeHopIterator: p.server.sphinx.DecodeHopIterator,
FwrdingPolicy: p.server.cc.routingPolicy, DecodeOnionObfuscator: p.server.sphinx.DecodeOnionObfuscator,
}, GetLastChannelUpdate: createGetLastUpdate(p.server.chanRouter,
lnChan, p.PubKey(), lnChan.ShortChanID()),
) SettledContracts: p.server.breachArbiter.settledContracts,
DebugHTLC: cfg.DebugHTLC,
Registry: p.server.invoices,
Switch: p.server.htlcSwitch,
FwrdingPolicy: p.server.cc.routingPolicy,
BlockEpochs: blockEpoch,
}
link := htlcswitch.NewChannelLink(linkCfg, lnChan,
uint32(currentHeight))
if err := p.server.htlcSwitch.AddLink(link); err != nil { if err := p.server.htlcSwitch.AddLink(link); err != nil {
return err return err
@ -927,6 +935,18 @@ out:
// Next, we'll assemble a ChannelLink along with the // Next, we'll assemble a ChannelLink along with the
// necessary items it needs to function. // necessary items it needs to function.
//
// TODO(roasbeef): panic on below?
blockEpoch, err := p.server.cc.chainNotifier.RegisterBlockEpochNtfn()
if err != nil {
peerLog.Errorf("unable to register for block epoch: %v", err)
continue
}
_, currentHeight, err := p.server.cc.chainIO.GetBestBlock()
if err != nil {
peerLog.Errorf("unable to get best block: %v", err)
continue
}
linkConfig := htlcswitch.ChannelLinkConfig{ linkConfig := htlcswitch.ChannelLinkConfig{
Peer: p, Peer: p,
DecodeHopIterator: p.server.sphinx.DecodeHopIterator, DecodeHopIterator: p.server.sphinx.DecodeHopIterator,
@ -938,14 +958,15 @@ out:
Registry: p.server.invoices, Registry: p.server.invoices,
Switch: p.server.htlcSwitch, Switch: p.server.htlcSwitch,
FwrdingPolicy: p.server.cc.routingPolicy, FwrdingPolicy: p.server.cc.routingPolicy,
BlockEpochs: blockEpoch,
} }
link := htlcswitch.NewChannelLink(linkConfig, newChan) link := htlcswitch.NewChannelLink(linkConfig, newChan,
uint32(currentHeight))
// With the channel link created, we'll now notify the // With the channel link created, we'll now notify the
// htlc switch so this channel can be used to dispatch // htlc switch so this channel can be used to dispatch
// local payments and also passively forward payments. // local payments and also passively forward payments.
err := p.server.htlcSwitch.AddLink(link) if err := p.server.htlcSwitch.AddLink(link); err != nil {
if err != nil {
peerLog.Errorf("can't register new channel "+ peerLog.Errorf("can't register new channel "+
"link(%v) with peerId(%v)", chanPoint, p.id) "link(%v) with peerId(%v)", chanPoint, p.id)
} }
@ -1525,7 +1546,6 @@ func (p *peer) WipeChannel(channel *lnwallet.LightningChannel) error {
// Instruct the Htlc Switch to close this link as the channel is no // Instruct the Htlc Switch to close this link as the channel is no
// longer active. // longer active.
if err := p.server.htlcSwitch.RemoveLink(chanID); err != nil { if err := p.server.htlcSwitch.RemoveLink(chanID); err != nil {
if err == htlcswitch.ErrChannelLinkNotFound { if err == htlcswitch.ErrChannelLinkNotFound {
peerLog.Warnf("unable remove channel link with "+ peerLog.Warnf("unable remove channel link with "+