channeldb+migration: export commonly used methods

This commit exports several commonly used methods that can be used by
later migrations. It also adds a channel commit deserializer.
This commit is contained in:
yyforyongyu
2022-04-16 11:45:05 +08:00
parent 22f8f6ed4a
commit 8b289e79f5
7 changed files with 109 additions and 20 deletions

View File

@@ -70,7 +70,7 @@ func MigrateFwdPkgCleanup(tx kvdb.RwTx) error {
// Iterate over all close channels and remove their forwarding packages. // Iterate over all close channels and remove their forwarding packages.
for _, summery := range chanSummaries { for _, summery := range chanSummaries {
sourceBytes := makeLogKey(summery.ShortChanID.ToUint64()) sourceBytes := MakeLogKey(summery.ShortChanID.ToUint64())
// First, we will try to find the corresponding bucket. If there // First, we will try to find the corresponding bucket. If there
// is not a nested bucket matching the ShortChanID, we will skip // is not a nested bucket matching the ShortChanID, we will skip
@@ -112,7 +112,7 @@ func deserializeCloseChannelSummary(
} }
// makeLogKey converts a uint64 into an 8 byte array. // makeLogKey converts a uint64 into an 8 byte array.
func makeLogKey(updateNum uint64) [8]byte { func MakeLogKey(updateNum uint64) [8]byte {
var key [8]byte var key [8]byte
binary.BigEndian.PutUint64(key[:], updateNum) binary.BigEndian.PutUint64(key[:], updateNum)
return key return key

View File

@@ -187,7 +187,7 @@ func genAfterMigration(deleted, untouched []int) func(kvdb.RwTx) error {
// Reading deleted buckets should return nil // Reading deleted buckets should return nil
for _, id := range deleted { for _, id := range deleted {
chanID := lnwire.NewShortChanIDFromInt(uint64(id)) chanID := lnwire.NewShortChanIDFromInt(uint64(id))
sourceKey := makeLogKey(chanID.ToUint64()) sourceKey := MakeLogKey(chanID.ToUint64())
sourceBkt := fwdPkgBkt.NestedReadBucket(sourceKey[:]) sourceBkt := fwdPkgBkt.NestedReadBucket(sourceKey[:])
if sourceBkt != nil { if sourceBkt != nil {
return fmt.Errorf( return fmt.Errorf(
@@ -200,7 +200,7 @@ func genAfterMigration(deleted, untouched []int) func(kvdb.RwTx) error {
// Reading untouched buckets should return not nil // Reading untouched buckets should return not nil
for _, id := range untouched { for _, id := range untouched {
chanID := lnwire.NewShortChanIDFromInt(uint64(id)) chanID := lnwire.NewShortChanIDFromInt(uint64(id))
sourceKey := makeLogKey(chanID.ToUint64()) sourceKey := MakeLogKey(chanID.ToUint64())
sourceBkt := fwdPkgBkt.NestedReadBucket(sourceKey[:]) sourceBkt := fwdPkgBkt.NestedReadBucket(sourceKey[:])
if sourceBkt == nil { if sourceBkt == nil {
return fmt.Errorf( return fmt.Errorf(
@@ -259,7 +259,7 @@ func createTestFwdPkgBucket(tx kvdb.RwTx, chanID lnwire.ShortChannelID) error {
return err return err
} }
source := makeLogKey(chanID.ToUint64()) source := MakeLogKey(chanID.ToUint64())
if _, err := fwdPkgBkt.CreateBucketIfNotExists(source[:]); err != nil { if _, err := fwdPkgBkt.CreateBucketIfNotExists(source[:]); err != nil {
return err return err
} }

View File

@@ -616,7 +616,7 @@ func serializeChannelCloseSummary(w io.Writer, cs *ChannelCloseSummary) error {
return err return err
} }
if err := writeChanConfig(w, &cs.LocalChanConfig); err != nil { if err := WriteChanConfig(w, &cs.LocalChanConfig); err != nil {
return err return err
} }
@@ -680,7 +680,7 @@ func deserializeCloseChannelSummary(r io.Reader) (*ChannelCloseSummary, error) {
return nil, err return nil, err
} }
if err := readChanConfig(r, &c.LocalChanConfig); err != nil { if err := ReadChanConfig(r, &c.LocalChanConfig); err != nil {
return nil, err return nil, err
} }
@@ -731,7 +731,7 @@ func deserializeCloseChannelSummary(r io.Reader) (*ChannelCloseSummary, error) {
return c, nil return c, nil
} }
func writeChanConfig(b io.Writer, c *ChannelConfig) error { func WriteChanConfig(b io.Writer, c *ChannelConfig) error {
return WriteElements(b, return WriteElements(b,
c.DustLimit, c.MaxPendingAmount, c.ChanReserve, c.MinHTLC, c.DustLimit, c.MaxPendingAmount, c.ChanReserve, c.MinHTLC,
c.MaxAcceptedHtlcs, c.CsvDelay, c.MultiSigKey, c.MaxAcceptedHtlcs, c.CsvDelay, c.MultiSigKey,
@@ -740,7 +740,7 @@ func writeChanConfig(b io.Writer, c *ChannelConfig) error {
) )
} }
func readChanConfig(b io.Reader, c *ChannelConfig) error { func ReadChanConfig(b io.Reader, c *ChannelConfig) error {
return ReadElements(b, return ReadElements(b,
&c.DustLimit, &c.MaxPendingAmount, &c.ChanReserve, &c.DustLimit, &c.MaxPendingAmount, &c.ChanReserve,
&c.MinHTLC, &c.MaxAcceptedHtlcs, &c.CsvDelay, &c.MinHTLC, &c.MaxAcceptedHtlcs, &c.CsvDelay,
@@ -749,3 +749,92 @@ func readChanConfig(b io.Reader, c *ChannelConfig) error {
&c.HtlcBasePoint, &c.HtlcBasePoint,
) )
} }
func DeserializeChanCommit(r io.Reader) (ChannelCommitment, error) {
var c ChannelCommitment
err := ReadElements(r,
&c.CommitHeight, &c.LocalLogIndex, &c.LocalHtlcIndex, &c.RemoteLogIndex,
&c.RemoteHtlcIndex, &c.LocalBalance, &c.RemoteBalance,
&c.CommitFee, &c.FeePerKw, &c.CommitTx, &c.CommitSig,
)
if err != nil {
return c, err
}
c.Htlcs, err = DeserializeHtlcs(r)
if err != nil {
return c, err
}
return c, nil
}
// DeserializeHtlcs attempts to read out a slice of HTLC's from the passed
// io.Reader. The bytes within the passed reader MUST have been previously
// written to using the SerializeHtlcs function.
//
// NOTE: This API is NOT stable, the on-disk format will likely change in the
// future.
func DeserializeHtlcs(r io.Reader) ([]HTLC, error) {
var numHtlcs uint16
if err := ReadElement(r, &numHtlcs); err != nil {
return nil, err
}
var htlcs []HTLC
if numHtlcs == 0 {
return htlcs, nil
}
htlcs = make([]HTLC, numHtlcs)
for i := uint16(0); i < numHtlcs; i++ {
if err := ReadElements(r,
&htlcs[i].Signature, &htlcs[i].RHash, &htlcs[i].Amt,
&htlcs[i].RefundTimeout, &htlcs[i].OutputIndex,
&htlcs[i].Incoming, &htlcs[i].OnionBlob,
&htlcs[i].HtlcIndex, &htlcs[i].LogIndex,
); err != nil {
return htlcs, err
}
}
return htlcs, nil
}
func SerializeChanCommit(w io.Writer, c *ChannelCommitment) error {
if err := WriteElements(w,
c.CommitHeight, c.LocalLogIndex, c.LocalHtlcIndex,
c.RemoteLogIndex, c.RemoteHtlcIndex, c.LocalBalance,
c.RemoteBalance, c.CommitFee, c.FeePerKw, c.CommitTx,
c.CommitSig,
); err != nil {
return err
}
return SerializeHtlcs(w, c.Htlcs...)
}
// SerializeHtlcs writes out the passed set of HTLC's into the passed writer
// using the current default on-disk serialization format.
//
// NOTE: This API is NOT stable, the on-disk format will likely change in the
// future.
func SerializeHtlcs(b io.Writer, htlcs ...HTLC) error {
numHtlcs := uint16(len(htlcs))
if err := WriteElement(b, numHtlcs); err != nil {
return err
}
for _, htlc := range htlcs {
if err := WriteElements(b,
htlc.Signature, htlc.RHash, htlc.Amt, htlc.RefundTimeout,
htlc.OutputIndex, htlc.Incoming, htlc.OnionBlob[:],
htlc.HtlcIndex, htlc.LogIndex,
); err != nil {
return err
}
}
return nil
}

View File

@@ -15,9 +15,9 @@ import (
"github.com/lightningnetwork/lnd/shachain" "github.com/lightningnetwork/lnd/shachain"
) )
// writeOutpoint writes an outpoint to the passed writer using the minimal // WriteOutpoint writes an outpoint to the passed writer using the minimal
// amount of bytes possible. // amount of bytes possible.
func writeOutpoint(w io.Writer, o *wire.OutPoint) error { func WriteOutpoint(w io.Writer, o *wire.OutPoint) error {
if _, err := w.Write(o.Hash[:]); err != nil { if _, err := w.Write(o.Hash[:]); err != nil {
return err return err
} }
@@ -28,9 +28,9 @@ func writeOutpoint(w io.Writer, o *wire.OutPoint) error {
return nil return nil
} }
// readOutpoint reads an outpoint from the passed reader that was previously // ReadOutpoint reads an outpoint from the passed reader that was previously
// written using the writeOutpoint struct. // written using the writeOutpoint struct.
func readOutpoint(r io.Reader, o *wire.OutPoint) error { func ReadOutpoint(r io.Reader, o *wire.OutPoint) error {
if _, err := io.ReadFull(r, o.Hash[:]); err != nil { if _, err := io.ReadFull(r, o.Hash[:]); err != nil {
return err return err
} }
@@ -88,7 +88,7 @@ func WriteElement(w io.Writer, element interface{}) error {
} }
case wire.OutPoint: case wire.OutPoint:
return writeOutpoint(w, &e) return WriteOutpoint(w, &e)
case lnwire.ShortChannelID: case lnwire.ShortChannelID:
if err := binary.Write(w, byteOrder, e.ToUint64()); err != nil { if err := binary.Write(w, byteOrder, e.ToUint64()); err != nil {
@@ -258,7 +258,7 @@ func ReadElement(r io.Reader, element interface{}) error {
} }
case *wire.OutPoint: case *wire.OutPoint:
return readOutpoint(r, e) return ReadOutpoint(r, e)
case *lnwire.ShortChannelID: case *lnwire.ShortChannelID:
var a uint64 var a uint64

View File

@@ -836,7 +836,7 @@ func deserializeChanEdgeInfo(r io.Reader) (ChannelEdgeInfo, error) {
} }
edgeInfo.ChannelPoint = wire.OutPoint{} edgeInfo.ChannelPoint = wire.OutPoint{}
if err := readOutpoint(r, &edgeInfo.ChannelPoint); err != nil { if err := ReadOutpoint(r, &edgeInfo.ChannelPoint); err != nil {
return ChannelEdgeInfo{}, err return ChannelEdgeInfo{}, err
} }
if err := binary.Read(r, byteOrder, &edgeInfo.Capacity); err != nil { if err := binary.Read(r, byteOrder, &edgeInfo.Capacity); err != nil {

View File

@@ -34,7 +34,7 @@ func deserializeCloseChannelSummaryV6(r io.Reader) (*ChannelCloseSummary, error)
return nil, err return nil, err
} }
if err := readChanConfig(r, &c.LocalChanConfig); err != nil { if err := ReadChanConfig(r, &c.LocalChanConfig); err != nil {
return nil, err return nil, err
} }

View File

@@ -203,7 +203,7 @@ func TestMigrateOptionalChannelCloseSummaryFields(t *testing.T) {
} }
var chanPointBuf bytes.Buffer var chanPointBuf bytes.Buffer
err = writeOutpoint(&chanPointBuf, &chanState.FundingOutpoint) err = WriteOutpoint(&chanPointBuf, &chanState.FundingOutpoint)
if err != nil { if err != nil {
t.Fatalf("unable to write outpoint: %v", err) t.Fatalf("unable to write outpoint: %v", err)
} }
@@ -303,7 +303,7 @@ func TestMigrateOptionalChannelCloseSummaryFields(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = writeChanConfig(&buf, &cs.LocalChanConfig) err = WriteChanConfig(&buf, &cs.LocalChanConfig)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -354,7 +354,7 @@ func TestMigrateOptionalChannelCloseSummaryFields(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = writeChanConfig(&buf, &cs.LocalChanConfig) err = WriteChanConfig(&buf, &cs.LocalChanConfig)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }