2016-01-05 19:19:22 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-06-21 07:55:07 +03:00
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
2016-01-05 19:19:22 +03:00
|
|
|
)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// CommitRevocation is sent by either side once a CommitSignature message has
|
|
|
|
// been received, and validated. This message serves to revoke the prior
|
|
|
|
// commitment transaction, which was the most up to date version until a
|
2016-06-21 07:55:07 +03:00
|
|
|
// CommitSignature message referencing the specified ChannelPoint was received.
|
2016-05-23 23:54:34 +03:00
|
|
|
// Additionally, this message also piggyback's the next revocation hash that
|
|
|
|
// Alice should use when constructing the Bob's version of the next commitment
|
|
|
|
// transaction (which would be done before sending a CommitSignature message).
|
|
|
|
// This piggybacking allows Alice to send the next CommitSignature message
|
|
|
|
// modifying Bob's commitment transaction without first asking for a revocation
|
|
|
|
// hash initially.
|
2016-01-05 19:19:22 +03:00
|
|
|
type CommitRevocation struct {
|
2016-06-21 07:55:07 +03:00
|
|
|
// ChannelPoint uniquely identifies to which currently active channel this
|
2016-05-23 23:54:34 +03:00
|
|
|
// CommitRevocation applies to.
|
2016-06-21 07:55:07 +03:00
|
|
|
ChannelPoint *wire.OutPoint
|
2016-05-23 23:54:34 +03:00
|
|
|
|
|
|
|
// Revocation is the pre-image to the revocation hash of the now prior
|
|
|
|
// commitment transaction.
|
|
|
|
Revocation [20]byte
|
|
|
|
|
|
|
|
// NextRevocationHash is the next revocation hash to use to create the
|
|
|
|
// next commitment transaction which is to be created upon receipt of a
|
|
|
|
// CommitSignature message.
|
2016-03-18 02:51:30 +03:00
|
|
|
NextRevocationHash [20]byte
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// NewCommitRevocation creates a new CommitRevocation message.
|
|
|
|
func NewCommitRevocation() *CommitRevocation {
|
|
|
|
return &CommitRevocation{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure CommitRevocation implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*CommitRevocation)(nil)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Decode deserializes a serialized CommitRevocation message stored in the
|
|
|
|
// passed io.Reader observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitRevocation) Decode(r io.Reader, pver uint32) error {
|
2016-06-21 07:55:07 +03:00
|
|
|
// ChannelPoint (8)
|
2016-05-23 23:54:34 +03:00
|
|
|
// NextRevocationHash (20)
|
|
|
|
// Revocation (20)
|
2016-01-05 19:19:22 +03:00
|
|
|
err := readElements(r,
|
2016-06-21 07:55:07 +03:00
|
|
|
&c.ChannelPoint,
|
2016-03-18 02:51:30 +03:00
|
|
|
&c.NextRevocationHash,
|
2016-04-11 10:55:48 +03:00
|
|
|
&c.Revocation,
|
2016-01-05 19:19:22 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Encode serializes the target CommitRevocation into the passed io.Writer
|
|
|
|
// observing the protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitRevocation) Encode(w io.Writer, pver uint32) error {
|
|
|
|
err := writeElements(w,
|
2016-06-21 07:55:07 +03:00
|
|
|
c.ChannelPoint,
|
2016-03-18 02:51:30 +03:00
|
|
|
c.NextRevocationHash,
|
2016-04-11 10:55:48 +03:00
|
|
|
c.Revocation,
|
2016-01-05 19:19:22 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Command returns the integer uniquely identifying this message type on the
|
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitRevocation) Command() uint32 {
|
|
|
|
return CmdCommitRevocation
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for a
|
|
|
|
// CommitRevocation complete message observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitRevocation) MaxPayloadLength(uint32) uint32 {
|
2016-06-21 07:55:07 +03:00
|
|
|
// 36 + 20 + 20
|
|
|
|
return 76
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Validate performs any necessary sanity checks to ensure all fields present
|
|
|
|
// on the CommitRevocation are valid.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitRevocation) Validate() error {
|
2016-01-17 04:14:35 +03:00
|
|
|
// We're good!
|
2016-01-05 19:19:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// String returns the string representation of the target CommitRevocation.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *CommitRevocation) String() string {
|
|
|
|
return fmt.Sprintf("\n--- Begin CommitRevocation ---\n") +
|
2016-06-21 07:55:07 +03:00
|
|
|
fmt.Sprintf("ChannelPoint:\t%d\n", c.ChannelPoint) +
|
2016-03-18 02:51:30 +03:00
|
|
|
fmt.Sprintf("NextRevocationHash:\t%x\n", c.NextRevocationHash) +
|
2016-04-11 10:55:48 +03:00
|
|
|
fmt.Sprintf("Revocation:\t%x\n", c.Revocation) +
|
2016-01-05 19:19:22 +03:00
|
|
|
fmt.Sprintf("--- End CommitRevocation ---\n")
|
|
|
|
}
|