Merge pull request #4175 from cfromknecht/fix-chanstatus-string

channeldb: correct HasChanStatus for ChanStatusDefault, fix rpc channel status
This commit is contained in:
Olaoluwa Osuntokun authored and GitHub committed 2020-04-13 18:47:33 -07:00
commit a53a6f160e
3 files changed
+69 -9

No files matched your search

+7 -2
View File
@@ -475,7 +475,6 @@ var chanStatusStrings = map[ChannelStatus]string{
// orderedChanStatusFlags is an in-order list of all that channel status flags. // orderedChanStatusFlags is an in-order list of all that channel status flags.
var orderedChanStatusFlags = []ChannelStatus{ var orderedChanStatusFlags = []ChannelStatus{
ChanStatusDefault,
ChanStatusBorked, ChanStatusBorked,
ChanStatusCommitBroadcasted, ChanStatusCommitBroadcasted,
ChanStatusLocalDataLoss, ChanStatusLocalDataLoss,
@@ -488,7 +487,7 @@ var orderedChanStatusFlags = []ChannelStatus{
// String returns a human-readable representation of the ChannelStatus. // String returns a human-readable representation of the ChannelStatus.
func (c ChannelStatus) String() string { func (c ChannelStatus) String() string {
// If no flags are set, then this is the default case. // If no flags are set, then this is the default case.
if c == 0 { if c == ChanStatusDefault {
return chanStatusStrings[ChanStatusDefault] return chanStatusStrings[ChanStatusDefault]
} }
@@ -711,6 +710,12 @@ func (c *OpenChannel) HasChanStatus(status ChannelStatus) bool {
} }
func (c *OpenChannel) hasChanStatus(status ChannelStatus) bool { func (c *OpenChannel) hasChanStatus(status ChannelStatus) bool {
// Special case ChanStatusDefualt since it isn't actually flag, but a
// particular combination (or lack-there-of) of flags.
if status == ChanStatusDefault {
return c.chanStatus == ChanStatusDefault
}
return c.chanStatus&status == status return c.chanStatus&status == status
} }
+59
View File
@@ -1581,3 +1581,62 @@ func TestBalanceAtHeight(t *testing.T) {
}) })
} }
} }
// TestHasChanStatus asserts the behavior of HasChanStatus by checking the
// behavior of various status flags in addition to the special case of
// ChanStatusDefault which is treated like a flag in the code base even though
// it isn't.
func TestHasChanStatus(t *testing.T) {
tests := []struct {
name string
status ChannelStatus
expHas map[ChannelStatus]bool
}{
{
name: "default",
status: ChanStatusDefault,
expHas: map[ChannelStatus]bool{
ChanStatusDefault: true,
ChanStatusBorked: false,
},
},
{
name: "single flag",
status: ChanStatusBorked,
expHas: map[ChannelStatus]bool{
ChanStatusDefault: false,
ChanStatusBorked: true,
},
},
{
name: "multiple flags",
status: ChanStatusBorked | ChanStatusLocalDataLoss,
expHas: map[ChannelStatus]bool{
ChanStatusDefault: false,
ChanStatusBorked: true,
ChanStatusLocalDataLoss: true,
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
c := &OpenChannel{
chanStatus: test.status,
}
for status, expHas := range test.expHas {
has := c.HasChanStatus(status)
if has == expHas {
continue
}
t.Fatalf("expected chan status to "+
"have %s? %t, got: %t",
status, expHas, has)
}
})
}
}
+3 -7
View File
@@ -504,13 +504,9 @@ func (p *peer) loadActiveChannels(chans []*channeldb.OpenChannel) (
// Skip adding any permanently irreconcilable channels to the // Skip adding any permanently irreconcilable channels to the
// htlcswitch. // htlcswitch.
switch { switch {
case dbChan.HasChanStatus(channeldb.ChanStatusBorked): case !dbChan.HasChanStatus(channeldb.ChanStatusDefault) &&
fallthrough !dbChan.HasChanStatus(channeldb.ChanStatusRestored):
case dbChan.HasChanStatus(channeldb.ChanStatusCommitBroadcasted):
fallthrough
case dbChan.HasChanStatus(channeldb.ChanStatusCoopBroadcasted):
fallthrough
case dbChan.HasChanStatus(channeldb.ChanStatusLocalDataLoss):
peerLog.Warnf("ChannelPoint(%v) has status %v, won't "+ peerLog.Warnf("ChannelPoint(%v) has status %v, won't "+
"start.", chanPoint, dbChan.ChanStatus()) "start.", chanPoint, dbChan.ChanStatus())