From 826dbbd1f70899be3fe4b22e08f1c53cee3fa1e6 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 4 May 2017 15:21:35 -0700 Subject: [PATCH] channeldb: add new method to db, MarkChanFullyClosed This commit adds a new method to the database which allows callers to mark a channel as fully closed once certain conditions have been reached. If a channel was cooperatively closed, then it can be marked as fully closed as soon as the channel has been confirmed. If the channel was marked as force closed, then it should be marked as closed as soon as _all_ the funds in limbo have been swept. --- channeldb/db.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/channeldb/db.go b/channeldb/db.go index be317e33a..24b32e6da 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -430,6 +430,38 @@ func (d *DB) FetchClosedChannels(pendingOnly bool) ([]*ChannelCloseSummary, erro return chanSummaries, nil } + +// MarkChanFullyClosed marks a channel as fully closed within the database. A +// channel should be marked as fully closed if the channel was initially +// cooperatively closed and it's reach a single confirmation, or after all the +// pending funds in a channel that has been forcibly closed have been swept. +func (d *DB) MarkChanFullyClosed(chanPoint *wire.OutPoint) error { + return d.Update(func(tx *bolt.Tx) error { + var b bytes.Buffer + if err := writeOutpoint(&b, chanPoint); err != nil { + return err + } + + chanID := b.Bytes() + + closedChanBucket, err := tx.CreateBucketIfNotExists(closedChannelBucket) + if err != nil { + return err + } + + chanSummary := closedChanBucket.Get(chanID) + if chanSummary == nil { + return fmt.Errorf("no closed channel by that chanID found") + } + + newSummary := make([]byte, len(chanSummary)) + copy(newSummary[:], chanSummary[:]) + newSummary[0] = 0x00 + + return closedChanBucket.Put(chanID, newSummary) + }) +} + // syncVersions function is used for safe db version synchronization. It applies // migration functions to the current database and recovers the previous // state of db if at least one error/panic appeared during migration.