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.
This commit is contained in:
parent
29687f49eb
commit
156772d04a
@ -793,9 +793,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ChannelCloseSummary contains the final state of a channel at the point it
|
// 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
|
// was closed. Once a channel is closed, all the information pertaining to
|
||||||
// channel within the openChannelBucket is deleted, and a compact summary is
|
// that channel within the openChannelBucket is deleted, and a compact
|
||||||
// but in place instead.
|
// summary is put in place instead.
|
||||||
type ChannelCloseSummary struct {
|
type ChannelCloseSummary struct {
|
||||||
// ChanPoint is the outpoint for this channel's funding transaction,
|
// ChanPoint is the outpoint for this channel's funding transaction,
|
||||||
// and is used as a unique identifier for the channel.
|
// 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 {
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1055,8 +1055,8 @@ func deserializeCloseChannelSummary(r io.Reader) (*ChannelCloseSummary, error) {
|
|||||||
c := &ChannelCloseSummary{}
|
c := &ChannelCloseSummary{}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
c.IsPending, err = readBool(r)
|
|
||||||
if err != nil {
|
if err := binary.Read(r, byteOrder, &c.IsPending); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1078,7 +1078,7 @@ func deserializeCloseChannelSummary(r io.Reader) (*ChannelCloseSummary, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var closeType [1]byte
|
var closeType [1]byte
|
||||||
if _, err := io.ReadFull(r, closeType[:]); err != nil {
|
if err := binary.Read(r, byteOrder, closeType[:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c.CloseType = ClosureType(closeType[0])
|
c.CloseType = ClosureType(closeType[0])
|
||||||
@ -1878,7 +1878,8 @@ func putChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error
|
|||||||
} else {
|
} else {
|
||||||
boolByte[0] = 0
|
boolByte[0] = 0
|
||||||
}
|
}
|
||||||
if _, err := b.Write(boolByte[:]); err != nil {
|
|
||||||
|
if err := binary.Write(&b, byteOrder, boolByte[:]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1917,8 +1918,11 @@ func fetchChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) err
|
|||||||
|
|
||||||
infoBytes := bytes.NewReader(nodeChanBucket.Get(fundTxnKey))
|
infoBytes := bytes.NewReader(nodeChanBucket.Get(fundTxnKey))
|
||||||
|
|
||||||
|
var err error
|
||||||
var boolByte [1]byte
|
var boolByte [1]byte
|
||||||
if _, err := io.ReadFull(infoBytes, boolByte[:]); err != nil {
|
err = binary.Read(infoBytes, byteOrder, boolByte[:])
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if boolByte[0] == 1 {
|
if boolByte[0] == 1 {
|
||||||
@ -1928,11 +1932,15 @@ func fetchChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
var chanType [1]byte
|
var chanType [1]byte
|
||||||
if _, err := io.ReadFull(infoBytes, chanType[:]); err != nil {
|
err = binary.Read(infoBytes, byteOrder, chanType[:])
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
channel.ChanType = ChannelType(chanType[0])
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2072,7 +2080,7 @@ func serializeHTLC(w io.Writer, h *HTLC) error {
|
|||||||
boolByte[0] = 0
|
boolByte[0] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := w.Write(boolByte[:]); err != nil {
|
if err := binary.Write(w, byteOrder, boolByte[:]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2103,7 +2111,7 @@ func deserializeHTLC(r io.Reader) (*HTLC, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var scratch [1]byte
|
var scratch [1]byte
|
||||||
if _, err := r.Read(scratch[:]); err != nil {
|
if err := binary.Read(r, byteOrder, scratch[:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2370,34 +2378,3 @@ func readOutpoint(r io.Reader, o *wire.OutPoint) error {
|
|||||||
|
|
||||||
return nil
|
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?
|
|
||||||
|
@ -2304,10 +2304,10 @@ func genRemoteHtlcSigJobs(commitPoint *btcec.PublicKey,
|
|||||||
// decrements the available revocation window by 1. After a successful method
|
// decrements the available revocation window by 1. After a successful method
|
||||||
// call, the remote party's commitment chain is extended by a new commitment
|
// 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.
|
// which includes all updates to the HTLC log prior to this method invocation.
|
||||||
// The first return parameter is the signature for the commitment transaction
|
// The first return parameter is the signature for the commitment
|
||||||
// itself, while the second parameter is a slice of all HTLC signatures (if
|
// transaction itself, while the second parameter is a slice of all
|
||||||
// any). The HTLC signatures are sorted according to the BIP 69 order of the
|
// HTLC signatures (if any). The HTLC signatures are sorted according to
|
||||||
// HTLC's on the commitment transaction.
|
// the BIP 69 order of the HTLC's on the commitment transaction.
|
||||||
func (lc *LightningChannel) SignNextCommitment() (*btcec.Signature, []*btcec.Signature, error) {
|
func (lc *LightningChannel) SignNextCommitment() (*btcec.Signature, []*btcec.Signature, error) {
|
||||||
lc.Lock()
|
lc.Lock()
|
||||||
defer lc.Unlock()
|
defer lc.Unlock()
|
||||||
|
@ -892,13 +892,13 @@ func (r *ChannelRouter) FindRoutes(target *btcec.PublicKey,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Finally, we'll sort the set of validate routes to optimize for
|
// Finally, we'll sort the set of validate routes to optimize for
|
||||||
// lowest total fees, using the required time-lock within the route as
|
// lowest total fees, using the required time-lock within the
|
||||||
// a tie-breaker.
|
// route as a tie-breaker.
|
||||||
sort.Slice(validRoutes, func(i, j int) bool {
|
sort.Slice(validRoutes, func(i, j int) bool {
|
||||||
// To make this decision we first check if the total fees
|
// 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
|
// required for both routes are equal. If so, then we'll let
|
||||||
// lock be the tie breaker. Otherwise, we'll put the route with the lowest
|
// the total time lock be the tie breaker. Otherwise, we'll
|
||||||
// total fees first.
|
// put the route with the lowest total fees first.
|
||||||
if validRoutes[i].TotalFees == validRoutes[j].TotalFees {
|
if validRoutes[i].TotalFees == validRoutes[j].TotalFees {
|
||||||
timeLockI := validRoutes[i].TotalTimeLock
|
timeLockI := validRoutes[i].TotalTimeLock
|
||||||
timeLockJ := validRoutes[j].TotalTimeLock
|
timeLockJ := validRoutes[j].TotalTimeLock
|
||||||
|
Loading…
Reference in New Issue
Block a user