diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 13fe9d7f..36002a34 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -5284,3 +5284,32 @@ func (lc *LightningChannel) State() *channeldb.OpenChannel { func (lc *LightningChannel) ObserverQuit() chan struct{} { return lc.observerQuit } + +// ActiveHtlcs returns a slice of HTLC's which are currently active on *both* +// commitment transactions. +func (lc *LightningChannel) ActiveHtlcs() []channeldb.HTLC { + lc.RLock() + defer lc.RUnlock() + + // We'll only return HTLC's that are locked into *both* commitment + // transactions. So we'll iterate through their set of HTLC's to note + // which ones are present on thir commitment. + remoteHtlcs := make(map[[32]byte]struct{}) + for _, htlc := range lc.channelState.RemoteCommitment.Htlcs { + onionHash := sha256.Sum256(htlc.OnionBlob[:]) + remoteHtlcs[onionHash] = struct{}{} + } + + // Now tht we know which HTLC's they have, we'll only mark the HTLC's + // as active if *we* know them as well. + activeHtlcs := make([]channeldb.HTLC, 0, len(remoteHtlcs)) + for _, htlc := range lc.channelState.LocalCommitment.Htlcs { + if _, ok := remoteHtlcs[sha256.Sum256(htlc.OnionBlob[:])]; !ok { + continue + } + + activeHtlcs = append(activeHtlcs, htlc) + } + + return activeHtlcs +}