From f6524aabcbd7510a19411b2067cf81e3e325e24d Mon Sep 17 00:00:00 2001 From: eugene Date: Mon, 16 Nov 2020 17:52:31 -0500 Subject: [PATCH] lnd+funding: use funding.WriteOutpoint instead of lnd version This commit duplicates the utxonursery's writeOutpoint function in the funding package so that when the rest of the fundingmanager code is moved, it can use the WriteOutpoint function for its channel opening state data. --- funding/manager.go | 28 ++++++++++++++++++++++++++++ fundingmanager.go | 7 ++++--- 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 funding/manager.go diff --git a/funding/manager.go b/funding/manager.go new file mode 100644 index 000000000..7c5b8c057 --- /dev/null +++ b/funding/manager.go @@ -0,0 +1,28 @@ +package funding + +import ( + "encoding/binary" + "io" + + "github.com/btcsuite/btcd/wire" +) + +var ( + // byteOrder defines the endian-ness we use for encoding to and from + // buffers. + byteOrder = binary.BigEndian +) + +// WriteOutpoint writes an outpoint to an io.Writer. This is not the same as +// the channeldb variant as this uses WriteVarBytes for the Hash. +func WriteOutpoint(w io.Writer, o *wire.OutPoint) error { + scratch := make([]byte, 4) + + if err := wire.WriteVarBytes(w, 0, o.Hash[:]); err != nil { + return err + } + + byteOrder.PutUint32(scratch, o.Index) + _, err := w.Write(scratch) + return err +} diff --git a/fundingmanager.go b/fundingmanager.go index 219f4c58b..32a56a662 100644 --- a/fundingmanager.go +++ b/fundingmanager.go @@ -20,6 +20,7 @@ import ( "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/discovery" + "github.com/lightningnetwork/lnd/funding" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/keychain" @@ -3536,7 +3537,7 @@ func (f *fundingManager) saveChannelOpeningState(chanPoint *wire.OutPoint, } var outpointBytes bytes.Buffer - if err = writeOutpoint(&outpointBytes, chanPoint); err != nil { + if err = funding.WriteOutpoint(&outpointBytes, chanPoint); err != nil { return err } @@ -3568,7 +3569,7 @@ func (f *fundingManager) getChannelOpeningState(chanPoint *wire.OutPoint) ( } var outpointBytes bytes.Buffer - if err := writeOutpoint(&outpointBytes, chanPoint); err != nil { + if err := funding.WriteOutpoint(&outpointBytes, chanPoint); err != nil { return err } @@ -3597,7 +3598,7 @@ func (f *fundingManager) deleteChannelOpeningState(chanPoint *wire.OutPoint) err } var outpointBytes bytes.Buffer - if err := writeOutpoint(&outpointBytes, chanPoint); err != nil { + if err := funding.WriteOutpoint(&outpointBytes, chanPoint); err != nil { return err }