fundingmanager+server: adding ChannelAcceptor to the fundingmanager

This commit is contained in:
nsa 2019-08-07 22:18:23 -04:00
parent c2a6c86e6b
commit 4d02100e12
No known key found for this signature in database
GPG Key ID: 118759E83439A9B1
4 changed files with 36 additions and 3 deletions

@ -15,6 +15,7 @@ import (
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chanacceptor"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/discovery" "github.com/lightningnetwork/lnd/discovery"
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
@ -338,6 +339,11 @@ type fundingConfig struct {
// NotifyOpenChannelEvent informs the ChannelNotifier when channels // NotifyOpenChannelEvent informs the ChannelNotifier when channels
// transition from pending open to open. // transition from pending open to open.
NotifyOpenChannelEvent func(wire.OutPoint) NotifyOpenChannelEvent func(wire.OutPoint)
// OpenChannelPredicate is a predicate on the lnwire.OpenChannel message
// and on the requesting node's public key that returns a bool which tells
// the funding manager whether or not to accept the channel.
OpenChannelPredicate chanacceptor.ChannelAcceptor
} }
// fundingManager acts as an orchestrator/bridge between the wallet's // fundingManager acts as an orchestrator/bridge between the wallet's
@ -1057,7 +1063,23 @@ func (f *fundingManager) handleFundingOpen(fmsg *fundingOpenMsg) {
if f.cfg.RejectPush && msg.PushAmount > 0 { if f.cfg.RejectPush && msg.PushAmount > 0 {
f.failFundingFlow( f.failFundingFlow(
fmsg.peer, fmsg.msg.PendingChannelID, fmsg.peer, fmsg.msg.PendingChannelID,
lnwallet.ErrNonZeroPushAmount()) lnwallet.ErrNonZeroPushAmount(),
)
return
}
// Send the OpenChannel request to the ChannelAcceptor to determine whether
// this node will accept the channel.
chanReq := &chanacceptor.ChannelAcceptRequest{
Node: fmsg.peer.IdentityKey(),
OpenChanMsg: fmsg.msg,
}
if !f.cfg.OpenChannelPredicate.Accept(chanReq) {
f.failFundingFlow(
fmsg.peer, fmsg.msg.PendingChannelID,
fmt.Errorf("open channel request rejected"),
)
return return
} }

@ -23,6 +23,7 @@ import (
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chanacceptor"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/discovery" "github.com/lightningnetwork/lnd/discovery"
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
@ -281,6 +282,8 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
var chanIDSeed [32]byte var chanIDSeed [32]byte
chainedAcceptor := chanacceptor.NewChainedAcceptor()
fundingCfg := fundingConfig{ fundingCfg := fundingConfig{
IDKey: privKey.PubKey(), IDKey: privKey.PubKey(),
Wallet: lnw, Wallet: lnw,
@ -364,6 +367,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
ReservationTimeout: 1 * time.Nanosecond, ReservationTimeout: 1 * time.Nanosecond,
MaxPendingChannels: DefaultMaxPendingChannels, MaxPendingChannels: DefaultMaxPendingChannels,
NotifyOpenChannelEvent: func(wire.OutPoint) {}, NotifyOpenChannelEvent: func(wire.OutPoint) {},
OpenChannelPredicate: chainedAcceptor,
} }
for _, op := range options { for _, op := range options {
@ -414,6 +418,8 @@ func recreateAliceFundingManager(t *testing.T, alice *testNode) {
oldCfg := alice.fundingMgr.cfg oldCfg := alice.fundingMgr.cfg
chainedAcceptor := chanacceptor.NewChainedAcceptor()
f, err := newFundingManager(fundingConfig{ f, err := newFundingManager(fundingConfig{
IDKey: oldCfg.IDKey, IDKey: oldCfg.IDKey,
Wallet: oldCfg.Wallet, Wallet: oldCfg.Wallet,
@ -458,6 +464,7 @@ func recreateAliceFundingManager(t *testing.T, alice *testNode) {
}, },
ZombieSweeperInterval: oldCfg.ZombieSweeperInterval, ZombieSweeperInterval: oldCfg.ZombieSweeperInterval,
ReservationTimeout: oldCfg.ReservationTimeout, ReservationTimeout: oldCfg.ReservationTimeout,
OpenChannelPredicate: chainedAcceptor,
}) })
if err != nil { if err != nil {
t.Fatalf("failed recreating aliceFundingManager: %v", err) t.Fatalf("failed recreating aliceFundingManager: %v", err)

2
lnd.go

@ -496,7 +496,7 @@ func Main(lisCfg ListenerCfg) error {
// connections. // connections.
server, err := newServer( server, err := newServer(
cfg.Listeners, chanDB, towerClientDB, activeChainControl, cfg.Listeners, chanDB, towerClientDB, activeChainControl,
idPrivKey, walletInitParams.ChansToRestore, idPrivKey, walletInitParams.ChansToRestore, chainedAcceptor,
) )
if err != nil { if err != nil {
err := fmt.Errorf("Unable to create server: %v", err) err := fmt.Errorf("Unable to create server: %v", err)

@ -28,6 +28,7 @@ import (
sphinx "github.com/lightningnetwork/lightning-onion" sphinx "github.com/lightningnetwork/lightning-onion"
"github.com/lightningnetwork/lnd/autopilot" "github.com/lightningnetwork/lnd/autopilot"
"github.com/lightningnetwork/lnd/brontide" "github.com/lightningnetwork/lnd/brontide"
"github.com/lightningnetwork/lnd/chanacceptor"
"github.com/lightningnetwork/lnd/chanbackup" "github.com/lightningnetwork/lnd/chanbackup"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channelnotifier" "github.com/lightningnetwork/lnd/channelnotifier"
@ -297,7 +298,8 @@ func noiseDial(idPriv *btcec.PrivateKey) func(net.Addr) (net.Conn, error) {
func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
towerClientDB *wtdb.ClientDB, cc *chainControl, towerClientDB *wtdb.ClientDB, cc *chainControl,
privKey *btcec.PrivateKey, privKey *btcec.PrivateKey,
chansToRestore walletunlocker.ChannelsToRecover) (*server, error) { chansToRestore walletunlocker.ChannelsToRecover,
chanPredicate chanacceptor.ChannelAcceptor) (*server, error) {
var err error var err error
@ -908,6 +910,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
if _, err := rand.Read(chanIDSeed[:]); err != nil { if _, err := rand.Read(chanIDSeed[:]); err != nil {
return nil, err return nil, err
} }
s.fundingMgr, err = newFundingManager(fundingConfig{ s.fundingMgr, err = newFundingManager(fundingConfig{
IDKey: privKey.PubKey(), IDKey: privKey.PubKey(),
Wallet: cc.wallet, Wallet: cc.wallet,
@ -1069,6 +1072,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
MaxPendingChannels: cfg.MaxPendingChannels, MaxPendingChannels: cfg.MaxPendingChannels,
RejectPush: cfg.RejectPush, RejectPush: cfg.RejectPush,
NotifyOpenChannelEvent: s.channelNotifier.NotifyOpenChannelEvent, NotifyOpenChannelEvent: s.channelNotifier.NotifyOpenChannelEvent,
OpenChannelPredicate: chanPredicate,
}) })
if err != nil { if err != nil {
return nil, err return nil, err