From 156772d04a70654c2f72a53d9cc9cd5788cd5e9c Mon Sep 17 00:00:00 2001 From: Laura Cressman Date: Wed, 6 Sep 2017 20:29:07 -0400 Subject: [PATCH] channeldb: use binary.Read/Write with bools in channel.go Use binary.Read/Write in functions to serialize and deserialize channel close summary and HTLC boolean data, as well as in methods to put and fetch channel funding info. Remove lnd implementations of readBool and writeBool as they are no longer needed. Also fix a few minor typos. --- channeldb/channel.go | 65 ++++++++++++++------------------------------ lnwallet/channel.go | 8 +++--- routing/router.go | 10 +++---- 3 files changed, 30 insertions(+), 53 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index d7525a29..95308441 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -793,9 +793,9 @@ const ( ) // ChannelCloseSummary contains the final state of a channel at the point it -// was close. Once a channel is closed, all the information pertaining to that -// channel within the openChannelBucket is deleted, and a compact summary is -// but in place instead. +// was closed. Once a channel is closed, all the information pertaining to +// that channel within the openChannelBucket is deleted, and a compact +// summary is put in place instead. type ChannelCloseSummary struct { // ChanPoint is the outpoint for this channel's funding transaction, // and is used as a unique identifier for the channel. @@ -1001,7 +1001,7 @@ func putChannelCloseSummary(tx *bolt.Tx, chanID []byte, } func serializeChannelCloseSummary(w io.Writer, cs *ChannelCloseSummary) error { - if err := writeBool(w, cs.IsPending); err != nil { + if err := binary.Write(w, byteOrder, cs.IsPending); err != nil { return err } @@ -1055,8 +1055,8 @@ func deserializeCloseChannelSummary(r io.Reader) (*ChannelCloseSummary, error) { c := &ChannelCloseSummary{} var err error - c.IsPending, err = readBool(r) - if err != nil { + + if err := binary.Read(r, byteOrder, &c.IsPending); err != nil { return nil, err } @@ -1078,7 +1078,7 @@ func deserializeCloseChannelSummary(r io.Reader) (*ChannelCloseSummary, error) { } var closeType [1]byte - if _, err := io.ReadFull(r, closeType[:]); err != nil { + if err := binary.Read(r, byteOrder, closeType[:]); err != nil { return nil, err } c.CloseType = ClosureType(closeType[0]) @@ -1878,7 +1878,8 @@ func putChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error } else { boolByte[0] = 0 } - if _, err := b.Write(boolByte[:]); err != nil { + + if err := binary.Write(&b, byteOrder, boolByte[:]); err != nil { return err } @@ -1917,8 +1918,11 @@ func fetchChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) err infoBytes := bytes.NewReader(nodeChanBucket.Get(fundTxnKey)) + var err error var boolByte [1]byte - if _, err := io.ReadFull(infoBytes, boolByte[:]); err != nil { + err = binary.Read(infoBytes, byteOrder, boolByte[:]) + + if err != nil { return err } if boolByte[0] == 1 { @@ -1928,11 +1932,15 @@ func fetchChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) err } var chanType [1]byte - if _, err := io.ReadFull(infoBytes, chanType[:]); err != nil { + err = binary.Read(infoBytes, byteOrder, chanType[:]) + + if err != nil { return err } channel.ChanType = ChannelType(chanType[0]) - if _, err := io.ReadFull(infoBytes, channel.ChainHash[:]); err != nil { + err = binary.Read(infoBytes, byteOrder, channel.ChainHash[:]) + + if err != nil { return err } @@ -2072,7 +2080,7 @@ func serializeHTLC(w io.Writer, h *HTLC) error { boolByte[0] = 0 } - if _, err := w.Write(boolByte[:]); err != nil { + if err := binary.Write(w, byteOrder, boolByte[:]); err != nil { return err } @@ -2103,7 +2111,7 @@ func deserializeHTLC(r io.Reader) (*HTLC, error) { } var scratch [1]byte - if _, err := r.Read(scratch[:]); err != nil { + if err := binary.Read(r, byteOrder, scratch[:]); err != nil { return nil, err } @@ -2370,34 +2378,3 @@ func readOutpoint(r io.Reader, o *wire.OutPoint) error { return nil } - -func writeBool(w io.Writer, b bool) error { - boolByte := byte(0x01) - if !b { - boolByte = byte(0x00) - } - - if _, err := w.Write([]byte{boolByte}); err != nil { - return err - } - - return nil -} - -// TODO(roasbeef): make sweep to use this and above everywhere -// * using this because binary.Write can only handle bools post go1.8 -func readBool(r io.Reader) (bool, error) { - var boolByte [1]byte - if _, err := io.ReadFull(r, boolByte[:]); err != nil { - return false, err - } - - if boolByte[0] == 0x00 { - return false, nil - } - - return true, nil -} - -// TODO(roasbeef): add readElement/writeElement funcs -// * after go1.9 can use binary.WriteBool etc? diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 6dc8b4c0..5e13d25e 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -2304,10 +2304,10 @@ func genRemoteHtlcSigJobs(commitPoint *btcec.PublicKey, // decrements the available revocation window by 1. After a successful method // call, the remote party's commitment chain is extended by a new commitment // which includes all updates to the HTLC log prior to this method invocation. -// The first return parameter is the signature for the commitment transaction -// itself, while the second parameter is a slice of all HTLC signatures (if -// any). The HTLC signatures are sorted according to the BIP 69 order of the -// HTLC's on the commitment transaction. +// The first return parameter is the signature for the commitment +// transaction itself, while the second parameter is a slice of all +// HTLC signatures (if any). The HTLC signatures are sorted according to +// the BIP 69 order of the HTLC's on the commitment transaction. func (lc *LightningChannel) SignNextCommitment() (*btcec.Signature, []*btcec.Signature, error) { lc.Lock() defer lc.Unlock() diff --git a/routing/router.go b/routing/router.go index e6da3d0f..e588b04a 100644 --- a/routing/router.go +++ b/routing/router.go @@ -892,13 +892,13 @@ func (r *ChannelRouter) FindRoutes(target *btcec.PublicKey, } // Finally, we'll sort the set of validate routes to optimize for - // lowest total fees, using the required time-lock within the route as - // a tie-breaker. + // lowest total fees, using the required time-lock within the + // route as a tie-breaker. sort.Slice(validRoutes, func(i, j int) bool { // To make this decision we first check if the total fees - // required for both routes are equal. If so, then we'll let the total time - // lock be the tie breaker. Otherwise, we'll put the route with the lowest - // total fees first. + // required for both routes are equal. If so, then we'll let + // the total time lock be the tie breaker. Otherwise, we'll + // put the route with the lowest total fees first. if validRoutes[i].TotalFees == validRoutes[j].TotalFees { timeLockI := validRoutes[i].TotalTimeLock timeLockJ := validRoutes[j].TotalTimeLock