channeldb: extract log update serialization

Extract functionality to methods as a preparation for serializing remote
log updates.
This commit is contained in:
Joost Jager 2020-01-02 17:35:03 +01:00
parent ea892fce17
commit 2d37d341a1
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -1564,6 +1564,42 @@ type CommitDiff struct {
SettleFailAcks []SettleFailRef SettleFailAcks []SettleFailRef
} }
// serializeLogUpdates serializes provided list of updates to a stream.
func serializeLogUpdates(w io.Writer, logUpdates []LogUpdate) error {
numUpdates := uint16(len(logUpdates))
if err := binary.Write(w, byteOrder, numUpdates); err != nil {
return err
}
for _, diff := range logUpdates {
err := WriteElements(w, diff.LogIndex, diff.UpdateMsg)
if err != nil {
return err
}
}
return nil
}
// deserializeLogUpdates deserializes a list of updates from a stream.
func deserializeLogUpdates(r io.Reader) ([]LogUpdate, error) {
var numUpdates uint16
if err := binary.Read(r, byteOrder, &numUpdates); err != nil {
return nil, err
}
logUpdates := make([]LogUpdate, numUpdates)
for i := 0; i < int(numUpdates); i++ {
err := ReadElements(r,
&logUpdates[i].LogIndex, &logUpdates[i].UpdateMsg,
)
if err != nil {
return nil, err
}
}
return logUpdates, nil
}
func serializeCommitDiff(w io.Writer, diff *CommitDiff) error { func serializeCommitDiff(w io.Writer, diff *CommitDiff) error {
if err := serializeChanCommit(w, &diff.Commitment); err != nil { if err := serializeChanCommit(w, &diff.Commitment); err != nil {
return err return err
@ -1573,18 +1609,10 @@ func serializeCommitDiff(w io.Writer, diff *CommitDiff) error {
return err return err
} }
numUpdates := uint16(len(diff.LogUpdates)) if err := serializeLogUpdates(w, diff.LogUpdates); err != nil {
if err := binary.Write(w, byteOrder, numUpdates); err != nil {
return err return err
} }
for _, diff := range diff.LogUpdates {
err := WriteElements(w, diff.LogIndex, diff.UpdateMsg)
if err != nil {
return err
}
}
numOpenRefs := uint16(len(diff.OpenedCircuitKeys)) numOpenRefs := uint16(len(diff.OpenedCircuitKeys))
if err := binary.Write(w, byteOrder, numOpenRefs); err != nil { if err := binary.Write(w, byteOrder, numOpenRefs); err != nil {
return err return err
@ -1628,21 +1656,11 @@ func deserializeCommitDiff(r io.Reader) (*CommitDiff, error) {
return nil, err return nil, err
} }
var numUpdates uint16 d.LogUpdates, err = deserializeLogUpdates(r)
if err := binary.Read(r, byteOrder, &numUpdates); err != nil { if err != nil {
return nil, err return nil, err
} }
d.LogUpdates = make([]LogUpdate, numUpdates)
for i := 0; i < int(numUpdates); i++ {
err := ReadElements(r,
&d.LogUpdates[i].LogIndex, &d.LogUpdates[i].UpdateMsg,
)
if err != nil {
return nil, err
}
}
var numOpenRefs uint16 var numOpenRefs uint16
if err := binary.Read(r, byteOrder, &numOpenRefs); err != nil { if err := binary.Read(r, byteOrder, &numOpenRefs); err != nil {
return nil, err return nil, err