From aaf6456e12707295fa2ec86393cddd483eea6a9d Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 9 Dec 2018 19:24:21 -0800 Subject: [PATCH] channeldb: add HasChanStatus and ApplyChanState methods to OpenChannel These methods allow callers to properly query if a channel is in a particular flag, and also modify the channel status in a thread safe manner. --- channeldb/channel.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index 8bc248671..e33ae77ab 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -316,8 +316,8 @@ var ( // channel. ChanStatusLocalDataLoss ChannelStatus = 1 << 2 - // ChanStatusRestored is a status flag that signals that the chanel has - // been restored, and doesn't have all the fields a typical channel + // ChanStatusRestored is a status flag that signals that the channel + // has been restored, and doesn't have all the fields a typical channel // will have. ChanStatusRestored ChannelStatus = 1 << 3 ) @@ -531,6 +531,28 @@ func (c *OpenChannel) ChanStatus() ChannelStatus { return c.chanStatus } +// ApplyChanStatus allows the caller to modify the internal channel state in a +// thead-safe manner. +func (c *OpenChannel) ApplyChanStatus(status ChannelStatus) error { + c.Lock() + defer c.Unlock() + + return c.putChanStatus(status) +} + +// HasChanStatus returns true if the internal bitfield channel status of the +// target channel has the specified status bit set. +func (c *OpenChannel) HasChanStatus(status ChannelStatus) bool { + c.RLock() + defer c.RUnlock() + + return c.hasChanStatus(status) +} + +func (c *OpenChannel) hasChanStatus(status ChannelStatus) bool { + return c.chanStatus&status == status +} + // RefreshShortChanID updates the in-memory short channel ID using the latest // value observed on disk. func (c *OpenChannel) RefreshShortChanID() error {