From 4fd1f832d72914742ed1a56cb5b79ff39c0d7a1f Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 4 Jan 2019 14:59:04 -0800 Subject: [PATCH] contractcourt+rpc: use new FetchChannel method instead of scanning In this commit, we modify areas where we need to force close a channel to use the new FetchChannel method instead of manually scanning. This dramatically reduces the CPU usage when doing things like closing a large number of channels within lnd. --- contractcourt/chain_arbitrator.go | 15 +------------- rpcserver.go | 33 ++----------------------------- 2 files changed, 3 insertions(+), 45 deletions(-) diff --git a/contractcourt/chain_arbitrator.go b/contractcourt/chain_arbitrator.go index 696b4636..6652df23 100644 --- a/contractcourt/chain_arbitrator.go +++ b/contractcourt/chain_arbitrator.go @@ -224,23 +224,10 @@ func newActiveChannelArbitrator(channel *channeldb.OpenChannel, // With the channels fetched, attempt to locate // the target channel according to its channel // point. - dbChannels, err := c.chanSource.FetchAllChannels() + channel, err := c.chanSource.FetchChannel(chanPoint) if err != nil { return nil, err } - var channel *channeldb.OpenChannel - for _, dbChannel := range dbChannels { - if dbChannel.FundingOutpoint == chanPoint { - channel = dbChannel - break - } - } - - // If the channel cannot be located, then we - // exit with an error to the channel. - if channel == nil { - return nil, fmt.Errorf("unable to find channel") - } chanMachine, err := lnwallet.NewLightningChannel( c.cfg.Signer, c.cfg.PreimageDB, channel, nil, diff --git a/rpcserver.go b/rpcserver.go index f992023d..46be68e8 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1709,7 +1709,7 @@ func (r *rpcServer) AbandonChannel(ctx context.Context, // With the chanPoint constructed, we'll attempt to find the target // channel in the database. If we can't find the channel, then we'll // return the error back to the caller. - dbChan, err := r.fetchOpenDbChannel(*chanPoint) + dbChan, err := r.server.chanDB.FetchChannel(*chanPoint) if err != nil { return nil, err } @@ -1746,42 +1746,13 @@ func (r *rpcServer) AbandonChannel(ctx context.Context, return &lnrpc.AbandonChannelResponse{}, nil } -// fetchOpenDbChannel attempts to locate a channel identified by its channel -// point from the database's set of all currently opened channels. -func (r *rpcServer) fetchOpenDbChannel(chanPoint wire.OutPoint) ( - *channeldb.OpenChannel, error) { - - dbChannels, err := r.server.chanDB.FetchAllChannels() - if err != nil { - return nil, err - } - - // With the channels fetched, attempt to locate the target channel - // according to its channel point. - var dbChan *channeldb.OpenChannel - for _, dbChannel := range dbChannels { - if dbChannel.FundingOutpoint == chanPoint { - dbChan = dbChannel - break - } - } - - // If the channel cannot be located, then we exit with an error to the - // caller. - if dbChan == nil { - return nil, fmt.Errorf("unable to find channel") - } - - return dbChan, nil -} - // fetchActiveChannel attempts to locate a channel identified by its channel // point from the database's set of all currently opened channels and // return it as a fully populated state machine func (r *rpcServer) fetchActiveChannel(chanPoint wire.OutPoint) ( *lnwallet.LightningChannel, error) { - dbChan, err := r.fetchOpenDbChannel(chanPoint) + dbChan, err := r.server.chanDB.FetchChannel(chanPoint) if err != nil { return nil, err }