channeldb: add method to OpenChannel for querying num updates

This commit adds a new method to the `OpenChannel` struct:
CommitmentHeight(). This method allows multiple callers holding the
same instance of an OpenChannel struct tied to the same on-disk channel
to consistently query the current commitment height for a channel. Such
a modification will prove useful later as sections of the code-base are
separated in order to allow more vigilant watching of channel breaches.
This commit is contained in:
Olaoluwa Osuntokun 2016-11-27 19:10:05 -08:00
parent 5a668c9321
commit 60946e4ddc
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 36 additions and 0 deletions

@ -487,6 +487,34 @@ func (c *OpenChannel) AppendToRevocationLog(delta *ChannelDelta) error {
})
}
// CommitmentHeight returns the current commitment height. The commitment
// height represents the number of updates to the commitment state to data.
// This value is always monotonically increasing. This method is provided in
// order to allow multiple instances of a particular open channel to obtain a
// consistent view of the number of channel updates to data.
func (c *OpenChannel) CommitmentHeight() (uint64, error) {
// TODO(roasbeef): this is super hacky, remedy during refactor!!!
o := &OpenChannel{
ChanID: c.ChanID,
}
err := c.Db.View(func(tx *bolt.Tx) error {
// Get the bucket dedicated to storing the meta-data for open
// channels.
openChanBucket := tx.Bucket(openChannelBucket)
if openChanBucket == nil {
return ErrNoActiveChannels
}
return fetchChanNumUpdates(openChanBucket, o)
})
if err != nil {
return 0, nil
}
return o.NumUpdates, nil
}
// FindPreviousState scans through the append-only log in an attempt to recover
// the previous channel state indicated by the update number. This method is
// intended to be used for obtaining the relevant data needed to claim all

@ -434,6 +434,14 @@ func TestChannelStateTransition(t *testing.T) {
t.Fatalf("update # doesn't match: %v vs %v",
updatedChannel[0].NumUpdates, delta.UpdateNum)
}
numDiskUpdates, err := updatedChannel[0].CommitmentHeight()
if err != nil {
t.Fatalf("unable to read commitment height from disk: %v", err)
}
if numDiskUpdates != uint64(delta.UpdateNum) {
t.Fatalf("num disk updates doesn't match: %v vs %v",
numDiskUpdates, delta.UpdateNum)
}
for i := 0; i < len(updatedChannel[0].Htlcs); i++ {
originalHTLC := updatedChannel[0].Htlcs[i]
diskHTLC := channel.Htlcs[i]