channeldb: rename AppendToRevocationLog to AdvanceCommitChainTail
In this commit, in addition to the renaming we’ve modified the behavior of AdvanceCommitChainTail as follows: this method now will simply atomically advance the commitment tail, set the new commitment to the prior dangling commitment, and update the on-disk revocation log. The macho expects the new revocation state to already be stored within the channel. This method is to be called once the remote party revokes their current commitment state.
This commit is contained in:
parent
b89c14616c
commit
374cab7467
@ -936,62 +936,85 @@ func (c *OpenChannel) InsertNextRevocation(revKey *btcec.PublicKey) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendToRevocationLog records the new state transition within an on-disk
|
// AdvanceCommitChainTail records the new state transition within an on-disk
|
||||||
// append-only log which records all state transitions by the remote peer. In
|
// append-only log which records all state transitions by the remote peer. In
|
||||||
// the case of an uncooperative broadcast of a prior state by the remote peer,
|
// the case of an uncooperative broadcast of a prior state by the remote peer,
|
||||||
// this log can be consulted in order to reconstruct the state needed to
|
// this log can be consulted in order to reconstruct the state needed to
|
||||||
// rectify the situation.
|
// rectify the situation. This method will add the current commitment for the
|
||||||
func (c *OpenChannel) AppendToRevocationLog(delta *ChannelDelta) error {
|
// remote party to the revocation log, and promote the current pending
|
||||||
return c.Db.Update(func(tx *bolt.Tx) error {
|
// commitment to the current remove commitment.
|
||||||
chanBucket, err := tx.CreateBucketIfNotExists(openChannelBucket)
|
func (c *OpenChannel) AdvanceCommitChainTail() error {
|
||||||
if err != nil {
|
c.Lock()
|
||||||
return err
|
defer c.Unlock()
|
||||||
}
|
|
||||||
|
|
||||||
id := c.IdentityPub.SerializeCompressed()
|
var newRemoteCommit *ChannelCommitment
|
||||||
nodeChanBucket, err := chanBucket.CreateBucketIfNotExists(id)
|
|
||||||
|
err := c.Db.Update(func(tx *bolt.Tx) error {
|
||||||
|
chanBucket, err := readChanBucket(tx, c.IdentityPub,
|
||||||
|
&c.FundingOutpoint, c.ChainHash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Persist the latest preimage state to disk as the remote peer
|
// Persist the latest preimage state to disk as the remote peer
|
||||||
// has just added to our local preimage store, and
|
// has just added to our local preimage store, and given us a
|
||||||
// given us a new pending revocation key.
|
// new pending revocation key.
|
||||||
if err := putChanRevocationState(nodeChanBucket, c); err != nil {
|
if err := putChanRevocationState(chanBucket, c); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// With the current preimage producer/store state updated,
|
// With the current preimage producer/store state updated,
|
||||||
// append a new log entry recording this the delta of this state
|
// append a new log entry recording this the delta of this
|
||||||
// transition.
|
// state transition.
|
||||||
|
//
|
||||||
// TODO(roasbeef): could make the deltas relative, would save
|
// TODO(roasbeef): could make the deltas relative, would save
|
||||||
// space, but then tradeoff for more disk-seeks to recover the
|
// space, but then tradeoff for more disk-seeks to recover the
|
||||||
// full state.
|
// full state.
|
||||||
logKey := channelLogBucket
|
logKey := revocationLogBucket
|
||||||
logBucket, err := nodeChanBucket.CreateBucketIfNotExists(logKey)
|
logBucket, err := chanBucket.CreateBucketIfNotExists(logKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...
|
// Before we append this revoked state to the revocation log,
|
||||||
diffBucket := tx.Bucket(commitDiffBucket)
|
// we'll swap out what's currently the tail of the commit tip,
|
||||||
if diffBucket != nil {
|
// with the current locked-in commitment for the remote party.
|
||||||
var outpoint bytes.Buffer
|
tipBytes := chanBucket.Get(commitDiffKey)
|
||||||
if err := writeOutpoint(&outpoint, &c.FundingOutpoint); err != nil {
|
tipReader := bytes.NewReader(tipBytes)
|
||||||
return err
|
newCommit, err := deserializeCommitDiff(tipReader)
|
||||||
}
|
if err != nil {
|
||||||
|
return err
|
||||||
key := []byte("cdf")
|
}
|
||||||
key = append(key, outpoint.Bytes()...)
|
err = putChanCommitment(chanBucket, &newCommit.Commitment, false)
|
||||||
if diffBucket.Get(key) != nil {
|
if err != nil {
|
||||||
if err := diffBucket.Delete(key); err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
}
|
if err := chanBucket.Delete(commitDiffKey); err != nil {
|
||||||
}
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return appendChannelLogEntry(logBucket, delta, &c.FundingOutpoint)
|
// With the commitment pointer swapped, we can now add the
|
||||||
|
// revoked (prior) state to the revocation log.
|
||||||
|
//
|
||||||
|
// TODO(roasbeef): store less
|
||||||
|
err = appendChannelLogEntry(logBucket, &c.RemoteCommitment)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
newRemoteCommit = &newCommit.Commitment
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// With the db transaction complete, we'll swap over the in-memory
|
||||||
|
// pointer of the new remote commitment, which was previously the tip
|
||||||
|
// of the commit chain.
|
||||||
|
c.RemoteCommitment = *newRemoteCommit
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevocationLogTail returns the "tail", or the end of the current revocation
|
// RevocationLogTail returns the "tail", or the end of the current revocation
|
||||||
|
Loading…
Reference in New Issue
Block a user