mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-26 01:33:02 +01:00
lnwire: refactor Encode to use specific writers - I
This commit takes 10 types of messages and refactors their Encode method to use specific writers. The following commits will refactor the rest.
This commit is contained in:
parent
f04410c546
commit
563ff7266a
@ -126,23 +126,63 @@ func (a *AcceptChannel) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteElements(w,
|
||||
a.PendingChannelID[:],
|
||||
a.DustLimit,
|
||||
a.MaxValueInFlight,
|
||||
a.ChannelReserve,
|
||||
a.HtlcMinimum,
|
||||
a.MinAcceptDepth,
|
||||
a.CsvDelay,
|
||||
a.MaxAcceptedHTLCs,
|
||||
a.FundingKey,
|
||||
a.RevocationPoint,
|
||||
a.PaymentPoint,
|
||||
a.DelayedPaymentPoint,
|
||||
a.HtlcPoint,
|
||||
a.FirstCommitmentPoint,
|
||||
tlvRecords,
|
||||
)
|
||||
if err := WriteBytes(w, a.PendingChannelID[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteSatoshi(w, a.DustLimit); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteMilliSatoshi(w, a.MaxValueInFlight); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteSatoshi(w, a.ChannelReserve); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteMilliSatoshi(w, a.HtlcMinimum); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteUint32(w, a.MinAcceptDepth); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteUint16(w, a.CsvDelay); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteUint16(w, a.MaxAcceptedHTLCs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WritePublicKey(w, a.FundingKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WritePublicKey(w, a.RevocationPoint); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WritePublicKey(w, a.PaymentPoint); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WritePublicKey(w, a.DelayedPaymentPoint); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WritePublicKey(w, a.HtlcPoint); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WritePublicKey(w, a.FirstCommitmentPoint); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteBytes(w, tlvRecords)
|
||||
}
|
||||
|
||||
// Decode deserializes the serialized AcceptChannel stored in the passed
|
||||
|
@ -66,13 +66,23 @@ func (a *AnnounceSignatures) Decode(r io.Reader, pver uint32) error {
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (a *AnnounceSignatures) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return WriteElements(w,
|
||||
a.ChannelID,
|
||||
a.ShortChannelID,
|
||||
a.NodeSignature,
|
||||
a.BitcoinSignature,
|
||||
a.ExtraOpaqueData,
|
||||
)
|
||||
if err := WriteChannelID(w, a.ChannelID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteShortChannelID(w, a.ShortChannelID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteSig(w, a.NodeSignature); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteSig(w, a.BitcoinSignature); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteBytes(w, a.ExtraOpaqueData)
|
||||
}
|
||||
|
||||
// MsgType returns the integer uniquely identifying this message type on the
|
||||
|
@ -65,9 +65,19 @@ func (c *ClosingSigned) Decode(r io.Reader, pver uint32) error {
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (c *ClosingSigned) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return WriteElements(
|
||||
w, c.ChannelID, c.FeeSatoshis, c.Signature, c.ExtraData,
|
||||
)
|
||||
if err := WriteChannelID(w, c.ChannelID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteSatoshi(w, c.FeeSatoshis); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteSig(w, c.Signature); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteBytes(w, c.ExtraData)
|
||||
}
|
||||
|
||||
// MsgType returns the integer uniquely identifying this message type on the
|
||||
|
@ -71,12 +71,19 @@ func (c *CommitSig) Decode(r io.Reader, pver uint32) error {
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (c *CommitSig) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return WriteElements(w,
|
||||
c.ChanID,
|
||||
c.CommitSig,
|
||||
c.HtlcSigs,
|
||||
c.ExtraData,
|
||||
)
|
||||
if err := WriteChannelID(w, c.ChanID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteSig(w, c.CommitSig); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteSigs(w, c.HtlcSigs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteBytes(w, c.ExtraData)
|
||||
}
|
||||
|
||||
// MsgType returns the integer uniquely identifying this message type on the
|
||||
|
@ -105,10 +105,11 @@ func (c *Error) Decode(r io.Reader, pver uint32) error {
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (c *Error) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return WriteElements(w,
|
||||
c.ChanID,
|
||||
c.Data,
|
||||
)
|
||||
if err := WriteBytes(w, c.ChanID[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteErrorData(w, c.Data)
|
||||
}
|
||||
|
||||
// MsgType returns the integer uniquely identifying an Error message on the
|
||||
|
@ -42,10 +42,19 @@ var _ Message = (*FundingCreated)(nil)
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (f *FundingCreated) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return WriteElements(
|
||||
w, f.PendingChannelID[:], f.FundingPoint, f.CommitSig,
|
||||
f.ExtraData,
|
||||
)
|
||||
if err := WriteBytes(w, f.PendingChannelID[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteOutPoint(w, f.FundingPoint); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteSig(w, f.CommitSig); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteBytes(w, f.ExtraData)
|
||||
}
|
||||
|
||||
// Decode deserializes the serialized FundingCreated stored in the passed
|
||||
|
@ -60,11 +60,15 @@ func (c *FundingLocked) Decode(r io.Reader, pver uint32) error {
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (c *FundingLocked) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return WriteElements(w,
|
||||
c.ChanID,
|
||||
c.NextPerCommitmentPoint,
|
||||
c.ExtraData,
|
||||
)
|
||||
if err := WriteChannelID(w, c.ChanID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WritePublicKey(w, c.NextPerCommitmentPoint); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteBytes(w, c.ExtraData)
|
||||
}
|
||||
|
||||
// MsgType returns the uint32 code which uniquely identifies this message as a
|
||||
|
@ -33,7 +33,15 @@ var _ Message = (*FundingSigned)(nil)
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (f *FundingSigned) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return WriteElements(w, f.ChanID, f.CommitSig, f.ExtraData)
|
||||
if err := WriteChannelID(w, f.ChanID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteSig(w, f.CommitSig); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteBytes(w, f.ExtraData)
|
||||
}
|
||||
|
||||
// Decode deserializes the serialized FundingSigned stored in the passed
|
||||
|
@ -59,12 +59,19 @@ func (g *GossipTimestampRange) Decode(r io.Reader, pver uint32) error {
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (g *GossipTimestampRange) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return WriteElements(w,
|
||||
g.ChainHash[:],
|
||||
g.FirstTimestamp,
|
||||
g.TimestampRange,
|
||||
g.ExtraData,
|
||||
)
|
||||
if err := WriteBytes(w, g.ChainHash[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteUint32(w, g.FirstTimestamp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteUint32(w, g.TimestampRange); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteBytes(w, g.ExtraData)
|
||||
}
|
||||
|
||||
// MsgType returns the integer uniquely identifying this message type on the
|
||||
|
@ -60,11 +60,15 @@ func (msg *Init) Decode(r io.Reader, pver uint32) error {
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (msg *Init) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
return WriteElements(w,
|
||||
msg.GlobalFeatures,
|
||||
msg.Features,
|
||||
msg.ExtraData,
|
||||
)
|
||||
if err := WriteRawFeatureVector(w, msg.GlobalFeatures); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := WriteRawFeatureVector(w, msg.Features); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return WriteBytes(w, msg.ExtraData)
|
||||
}
|
||||
|
||||
// MsgType returns the integer uniquely identifying this message type on the
|
||||
|
Loading…
x
Reference in New Issue
Block a user