From 893cda3ac27fe000031d64834b135e470759de92 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 9 Nov 2017 20:44:45 -0800 Subject: [PATCH] channeldb: add new AppendRemoteCommitChain method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this commit, we add a new method AppendRemoteCommitChain. This method is meant to be used once we extend a new state to the remote party, but before we actually transmit the CommitSig message. With this method, we store a fully valid CommitDiff on disk which can be used in the case that we need to retransmit the state to the party as they didn’t fully receive it. --- channeldb/channel.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index 9584094c..ac775345 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -843,28 +843,31 @@ func deserializeCommitDiff(r io.Reader) (*CommitDiff, error) { return &d, nil } -// AddCommitDiff... -func AddCommitDiff(db *DB, fundingOutpoint *wire.OutPoint, - diff *CommitDiff) error { - return db.Update(func(tx *bolt.Tx) error { - bucket, err := tx.CreateBucketIfNotExists(commitDiffBucket) +// AppendRemoteCommitChain appends a new CommitDiff to the end of the +// commitment chain for the remote party. This method is to be used once we +// have prepared a new commitment state for the remote party, but before we +// transmit it to the remote party. The contents of the argument should be +// sufficient to retransmit the updates and signature needed to reconstruct the +// state in full, in the case that we need to retransmit. +func (c *OpenChannel) AppendRemoteCommitChain(diff *CommitDiff) error { + return c.Db.Update(func(tx *bolt.Tx) error { + // First, we'll grab the writeable bucket where this channel's + // data resides. + chanBucket, err := updateChanBucket(tx, c.IdentityPub, + &c.FundingOutpoint, c.ChainHash) if err != nil { return err } + // TODO(roasbeef): use seqno to derive key for later LCP + + // With the bucket retrieved, we'll now serialize the commit + // diff itself, and write it to disk. var b bytes.Buffer - if err := diff.decode(&b); err != nil { + if err := serializeCommitDiff(&b, diff); err != nil { return err } - - var outpoint bytes.Buffer - if err := writeOutpoint(&outpoint, fundingOutpoint); err != nil { - return err - } - - key := []byte("cdf") - key = append(key, outpoint.Bytes()...) - return bucket.Put(key, b.Bytes()) + return chanBucket.Put(commitDiffKey, b.Bytes()) }) }