funding: refactor negotiateCommitmentType
- use a switch instead of nested ifs to improve readability - improve some variable names - reword comments
This commit is contained in:
1 file changed
+35
-21
@@ -28,37 +28,51 @@ func negotiateCommitmentType(desiredChanType *lnwire.ChannelType, local,
|
|||||||
|
|
||||||
// BOLT#2 specifies we MUST use explicit negotiation if both peers
|
// BOLT#2 specifies we MUST use explicit negotiation if both peers
|
||||||
// signal for it.
|
// signal for it.
|
||||||
if hasFeatures(local, remote, lnwire.ExplicitChannelTypeOptional) {
|
explicitNegotiation := hasFeatures(
|
||||||
if desiredChanType != nil {
|
local, remote, lnwire.ExplicitChannelTypeOptional,
|
||||||
commitType, err := explicitNegotiateCommitmentType(
|
)
|
||||||
*desiredChanType, local, remote,
|
|
||||||
)
|
|
||||||
|
|
||||||
return desiredChanType, commitType, err
|
chanTypeRequested := desiredChanType != nil
|
||||||
}
|
|
||||||
|
|
||||||
// Explicitly signal the "implicit" negotiation commitment type
|
switch {
|
||||||
// as default when a desired channel type isn't specified.
|
case explicitNegotiation && chanTypeRequested:
|
||||||
chanType, commitType := implicitNegotiateCommitmentType(local,
|
commitType, err := explicitNegotiateCommitmentType(
|
||||||
remote)
|
*desiredChanType, local, remote,
|
||||||
|
)
|
||||||
|
|
||||||
return chanType, commitType, nil
|
return desiredChanType, commitType, err
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, we'll use implicit negotiation. In this case, we are
|
// We don't have a specific channel type requested, so we select a
|
||||||
// restricted to the newest channel type advertised. If the passed-in
|
// default type as if implicit negotiation were used, and then we
|
||||||
// channelType doesn't match what was advertised, we fail.
|
// explicitly signal that default type.
|
||||||
chanType, commitType := implicitNegotiateCommitmentType(local, remote)
|
case explicitNegotiation && !chanTypeRequested:
|
||||||
|
defaultChanType, commitType := implicitNegotiateCommitmentType(
|
||||||
|
local, remote,
|
||||||
|
)
|
||||||
|
|
||||||
|
return defaultChanType, commitType, nil
|
||||||
|
|
||||||
|
// A specific channel type was requested, but we can't explicitly signal
|
||||||
|
// it. So if implicit negotiation wouldn't select the desired channel
|
||||||
|
// type, we must return an error.
|
||||||
|
case !explicitNegotiation && chanTypeRequested:
|
||||||
|
implicitChanType, commitType := implicitNegotiateCommitmentType(
|
||||||
|
local, remote,
|
||||||
|
)
|
||||||
|
|
||||||
if desiredChanType != nil {
|
|
||||||
expected := lnwire.RawFeatureVector(*desiredChanType)
|
expected := lnwire.RawFeatureVector(*desiredChanType)
|
||||||
actual := lnwire.RawFeatureVector(*chanType)
|
actual := lnwire.RawFeatureVector(*implicitChanType)
|
||||||
if !expected.Equals(&actual) {
|
if !expected.Equals(&actual) {
|
||||||
return nil, 0, errUnsupportedChannelType
|
return nil, 0, errUnsupportedChannelType
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return nil, commitType, nil
|
return nil, commitType, nil
|
||||||
|
|
||||||
|
default: // !explicitNegotiation && !chanTypeRequested
|
||||||
|
_, commitType := implicitNegotiateCommitmentType(local, remote)
|
||||||
|
|
||||||
|
return nil, commitType, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// explicitNegotiateCommitmentType attempts to explicitly negotiate for a
|
// explicitNegotiateCommitmentType attempts to explicitly negotiate for a
|
||||||
|
|||||||
Reference in new issue
Block a user