Skip to content

Commit

Permalink
Merge dd26cb1 into fe0a7b6
Browse files Browse the repository at this point in the history
  • Loading branch information
flaurida committed Sep 7, 2017
2 parents fe0a7b6 + dd26cb1 commit 3ae30be
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 102 deletions.
41 changes: 5 additions & 36 deletions channeldb/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// was closed. 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.
// 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.
Expand Down Expand Up @@ -978,7 +978,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
}

Expand Down Expand Up @@ -1032,8 +1032,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
}

Expand Down Expand Up @@ -2347,34 +2347,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?
11 changes: 6 additions & 5 deletions lnwallet/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2272,7 +2272,7 @@ 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 it he signature for 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.
Expand All @@ -2297,7 +2297,7 @@ func (lc *LightningChannel) SignNextCommitment() (*btcec.Signature, []*btcec.Sig
return nil, nil, err
}

// Grab the next commitment point for the remote party. This well be
// Grab the next commitment point for the remote party. This will be
// used within fetchCommitmentView to derive all the keys necessary to
// construct the commitment state.
commitPoint := lc.channelState.RemoteNextRevocation
Expand Down Expand Up @@ -2358,13 +2358,14 @@ func (lc *LightningChannel) SignNextCommitment() (*btcec.Signature, []*btcec.Sig
// We'll need to send over the signatures to the remote party in the
// order as they appear on the commitment transaction after BIP 69
// sorting.
sortedSigs := sortableSignBatch(sigBatch)
sort.Sort(sortedSigs)
sort.Slice(sigBatch, func(i, j int) bool {
return sigBatch[i].outputIndex < sigBatch[j].outputIndex
})

// With the jobs sorted, we'll now iterate through all the responses to
// gather each of the signatures in order.
htlcSigs := make([]*btcec.Signature, 0, len(sigBatch))
for _, htlcSigJob := range sortedSigs {
for _, htlcSigJob := range sigBatch {
jobResp := <-htlcSigJob.resp

// If an error occurred, then we'll cancel any other active
Expand Down
23 changes: 0 additions & 23 deletions lnwallet/sigpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,6 @@ type signJob struct {
resp chan signJobResp
}

// sortableSignBatch is a type wrapper around a slice of signJobs which is able
// to sort each job according to tis outputs index. Such sorting is necessary
// as when creating a new commitment state, we need to send over all the HTLC
// signatures (if any) in the exact order the appears on the commitment
// transaction after BIP 69 sorting.
type sortableSignBatch []signJob

// Len returns the number of items sortable batch of sign jobs. It is part of
// the sort.Interface implementation.
func (s sortableSignBatch) Len() int { return len(s) }

// Less returns whether the item in the batch with index i should sort before
// the item with index j. It is part of the sort.Interface implementation.
func (s sortableSignBatch) Less(i, j int) bool {
return s[i].outputIndex < s[j].outputIndex
}

// Swap swaps the items at the passed indices in the priority queue. It is part
// of the sort.Interface implementation.
func (s sortableSignBatch) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// signJobResp is the response to a sign job. Both channels are to be read in
// order to ensure no unnecessary goroutine blocking occurs. Additionally, both
// channels should be buffered.
Expand Down
4 changes: 3 additions & 1 deletion routing/heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func TestHeapOrdering(t *testing.T) {

// Sort the regular slice, we'll compare this against all the entries
// popped from the heap.
sort.Sort(&distanceHeap{sortedEntries})
sort.Slice(sortedEntries, func(i, j int) bool {
return sortedEntries[i].dist < sortedEntries[j].dist
})

// One by one, pop of all the entries from the heap, they should come
// out in sorted order.
Expand Down
35 changes: 0 additions & 35 deletions routing/pathfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,41 +159,6 @@ func (r *Route) ToHopPayloads() []sphinx.HopData {
return hopPayloads
}

// sortableRoutes is a slice of routes that can be sorted. Routes are typically
// sorted according to their total cumulative fee within the route. In the case
// that two routes require and identical amount of fees, then the total
// time-lock will be used as the tie breaker.
type sortableRoutes []*Route

// Len returns the number of routes in the collection.
//
// NOTE: This is part of the sort.Interface implementation.
func (s sortableRoutes) Len() int {
return len(s)
}

// Less reports whether the route with index i should sort before the route
// with index j. 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.
//
// NOTE: This is part of the sort.Interface implementation.
func (s sortableRoutes) Less(i, j int) bool {
if s[i].TotalFees == s[j].TotalFees {
return s[i].TotalTimeLock < s[j].TotalTimeLock
}

return s[i].TotalFees < s[j].TotalFees
}

// Swap swaps the elements with indexes i and j.
//
// NOTE: This is part of the sort.Interface implementation.
func (s sortableRoutes) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// newRoute returns a fully valid route between the source and target that's
// capable of supporting a payment of `amtToSend` after fees are fully
// computed. If the route is too long, or the selected path cannot support the
Expand Down
14 changes: 12 additions & 2 deletions routing/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ func (r *ChannelRouter) FindRoutes(target *btcec.PublicKey,
// each path. During this process, some paths may be discarded if they
// aren't able to support the total satoshis flow once fees have been
// factored in.
validRoutes := make(sortableRoutes, 0, len(shortestPaths))
validRoutes := make([]*Route, 0, len(shortestPaths))
for _, path := range shortestPaths {
// Attempt to make the path into a route. We snip off the first
// hop in the path as it contains a "self-hop" that is inserted
Expand All @@ -874,7 +874,17 @@ 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.
sort.Sort(validRoutes)
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.
if validRoutes[i].TotalFees == validRoutes[j].TotalFees {
return validRoutes[i].TotalTimeLock < validRoutes[j].TotalTimeLock
}

return validRoutes[i].TotalFees < validRoutes[j].TotalFees
})

log.Debugf("Obtained %v paths sending %v to %x: %v", len(validRoutes),
amt, dest, newLogClosure(func() string {
Expand Down

0 comments on commit 3ae30be

Please sign in to comment.