lnwallet: add new ActiveHtlcs to channel state machine

In this commit, we’ve added a new method to the channel state machine:
ActiveHtlcs. This method will allow callers to poll the state of the
channel to retrieve the set of HTLC’s active on *both* commitment
transactions.
This commit is contained in:
Olaoluwa Osuntokun 2018-01-16 18:10:58 -08:00
parent 5f6c15cfa4
commit 42cd2fa5be
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21
1 changed files with 29 additions and 0 deletions

View File

@ -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
}