2016-01-06 22:03:17 +01:00
|
|
|
package btcdnotify
|
|
|
|
|
2016-12-25 01:42:13 +01:00
|
|
|
import "github.com/lightningnetwork/lnd/chainntnfs"
|
|
|
|
|
2016-08-04 07:13:10 +02:00
|
|
|
// confEntry represents an entry in the min-confirmation heap. .
|
2016-01-06 22:03:17 +01:00
|
|
|
type confEntry struct {
|
|
|
|
*confirmationsNotification
|
|
|
|
|
2016-12-25 01:42:13 +01:00
|
|
|
initialConfDetails *chainntnfs.TxConfirmation
|
|
|
|
|
2016-01-06 22:03:17 +01:00
|
|
|
triggerHeight uint32
|
|
|
|
}
|
|
|
|
|
2016-08-04 07:13:10 +02:00
|
|
|
// confirmationHeap is a list of confEntries sorted according to nearest
|
|
|
|
// "confirmation" height.Each entry within the min-confirmation heap is sorted
|
|
|
|
// according to the smallest dleta from the current blockheight to the
|
|
|
|
// triggerHeight of the next entry confirmationHeap
|
2016-01-06 22:03:17 +01:00
|
|
|
type confirmationHeap struct {
|
|
|
|
items []*confEntry
|
|
|
|
}
|
|
|
|
|
2016-08-04 07:13:10 +02:00
|
|
|
// newConfirmationHeap returns a new confirmationHeap with zero items.
|
2016-01-06 22:03:17 +01:00
|
|
|
func newConfirmationHeap() *confirmationHeap {
|
|
|
|
var confItems []*confEntry
|
|
|
|
return &confirmationHeap{confItems}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of items in the priority queue. It is part of the
|
|
|
|
// heap.Interface implementation.
|
|
|
|
func (c *confirmationHeap) Len() int { return len(c.items) }
|
|
|
|
|
|
|
|
// Less returns whether the item in the priority queue with index i should sort
|
|
|
|
// before the item with index j. It is part of the heap.Interface implementation.
|
|
|
|
func (c *confirmationHeap) Less(i, j int) bool {
|
|
|
|
return c.items[i].triggerHeight < c.items[j].triggerHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap swaps the items at the passed indices in the priority queue. It is
|
|
|
|
// part of the heap.Interface implementation.
|
|
|
|
func (c *confirmationHeap) Swap(i, j int) {
|
|
|
|
c.items[i], c.items[j] = c.items[j], c.items[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push pushes the passed item onto the priority queue. It is part of the
|
|
|
|
// heap.Interface implementation.
|
|
|
|
func (c *confirmationHeap) Push(x interface{}) {
|
|
|
|
c.items = append(c.items, x.(*confEntry))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop removes the highest priority item (according to Less) from the priority
|
|
|
|
// queue and returns it. It is part of the heap.Interface implementation.
|
|
|
|
func (c *confirmationHeap) Pop() interface{} {
|
|
|
|
n := len(c.items)
|
|
|
|
x := c.items[n-1]
|
2016-02-27 01:27:32 +01:00
|
|
|
c.items[n-1] = nil
|
2016-01-06 22:03:17 +01:00
|
|
|
c.items = c.items[0 : n-1]
|
|
|
|
return x
|
|
|
|
}
|