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.
This commit is contained in:
parent
821de3e107
commit
4fd1f832d7
@ -224,23 +224,10 @@ func newActiveChannelArbitrator(channel *channeldb.OpenChannel,
|
|||||||
// With the channels fetched, attempt to locate
|
// With the channels fetched, attempt to locate
|
||||||
// the target channel according to its channel
|
// the target channel according to its channel
|
||||||
// point.
|
// point.
|
||||||
dbChannels, err := c.chanSource.FetchAllChannels()
|
channel, err := c.chanSource.FetchChannel(chanPoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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(
|
chanMachine, err := lnwallet.NewLightningChannel(
|
||||||
c.cfg.Signer, c.cfg.PreimageDB, channel, nil,
|
c.cfg.Signer, c.cfg.PreimageDB, channel, nil,
|
||||||
|
33
rpcserver.go
33
rpcserver.go
@ -1709,7 +1709,7 @@ func (r *rpcServer) AbandonChannel(ctx context.Context,
|
|||||||
// With the chanPoint constructed, we'll attempt to find the target
|
// 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
|
// channel in the database. If we can't find the channel, then we'll
|
||||||
// return the error back to the caller.
|
// return the error back to the caller.
|
||||||
dbChan, err := r.fetchOpenDbChannel(*chanPoint)
|
dbChan, err := r.server.chanDB.FetchChannel(*chanPoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -1746,42 +1746,13 @@ func (r *rpcServer) AbandonChannel(ctx context.Context,
|
|||||||
return &lnrpc.AbandonChannelResponse{}, nil
|
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
|
// fetchActiveChannel attempts to locate a channel identified by its channel
|
||||||
// point from the database's set of all currently opened channels and
|
// point from the database's set of all currently opened channels and
|
||||||
// return it as a fully populated state machine
|
// return it as a fully populated state machine
|
||||||
func (r *rpcServer) fetchActiveChannel(chanPoint wire.OutPoint) (
|
func (r *rpcServer) fetchActiveChannel(chanPoint wire.OutPoint) (
|
||||||
*lnwallet.LightningChannel, error) {
|
*lnwallet.LightningChannel, error) {
|
||||||
|
|
||||||
dbChan, err := r.fetchOpenDbChannel(chanPoint)
|
dbChan, err := r.server.chanDB.FetchChannel(chanPoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user