kaspad/blockdag/blockheap.go
Ori Newman 904f2cf2e3 [DEV-72] Write Blues()
* [DEV-62] add phantom constructs to blocknode

* [DEV-62] add phantom constructs to blocknode

* [DEV-72] write blues()

* [DEV-72] write blues()

* [DEV-72] write blues()

* [DEV-62] add comments to new phantom constructs in blocknode

* Fixed dbIndexConnectBlock. (#33)

* Fixed dbIndexConnectBlock.

* Removed redundant check in storeFilter.

* Created a new method to BlockHeader: IsGenesis.

* [DEV-71] Implement BlockHeap (#35)

* [DEV-71] Implemented BlockHeap.

* [DEV-71] Removed irrelevant comment.

* [DEV-71] Renamed variables in Pop() and split Less() to multiple lines.

* [DEV-72] write blues()

* [DEV-72] write blues()

* [DEV-72] write blues()

* [DEV-72] write blues tests

* [DEV-72] write blues tests

* [DEV-72] remove relevant past

* [DEV-72] write blues tests

* [DEV-72] write blues tests

* [DEV-72] write blues tests

* [DEV-72] write functions to order blockSet by hash and write blue tests

* [DEV-72] add secret mining and censorship attack tests

* [DEV-72] remove prints

* [DEV-72] remove K from dagconfig.Params

* [DEV-72] remove K from dagconfig.Params

* [DEV-72] change blueScore to uint64

* [DEV-72] block V was missing, so renamed w -> v, x -> w etc

* [DEV-72] use node.String instead of %v

* [DEV-72] block V was missing, so renamed w -> v, x -> w etc

* [DEV-72] add K to dagconfig.Params, and add expected reds to all phantom tests

* [DEV-72] set K=10 and add comments to phantom and phantom tests

* [DEV-72] fix formatting and add comments to TestPhantom

* [DEV-72] fix grammar
2018-08-02 16:34:40 +03:00

61 lines
1.3 KiB
Go

package blockdag
import (
"container/heap"
"github.com/daglabs/btcd/dagconfig/daghash"
)
// baseHeap is an implementation for heap.Interface that sorts blocks by their height
type baseHeap []*blockNode
func (h baseHeap) Len() int { return len(h) }
func (h baseHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *baseHeap) Push(x interface{}) {
*h = append(*h, x.(*blockNode))
}
func (h *baseHeap) Pop() interface{} {
oldHeap := *h
oldLength := len(oldHeap)
popped := oldHeap[oldLength-1]
*h = oldHeap[0 : oldLength-1]
return popped
}
func (h baseHeap) Less(i, j int) bool {
if h[i].height == h[j].height {
return daghash.HashToBig(&h[i].hash).Cmp(daghash.HashToBig(&h[j].hash)) > 0
}
return h[i].height > h[j].height
}
// BlockHeap represents a mutable heap of Blocks, sorted by their height
type BlockHeap struct {
impl heap.Interface
}
// NewHeap initializes and returns a new BlockHeap
func NewHeap() BlockHeap {
h := BlockHeap{impl: &baseHeap{}}
heap.Init(h.impl)
return h
}
// Pop removes the block with lowest height from this heap and returns it
func (bh BlockHeap) Pop() *blockNode {
return heap.Pop(bh.impl).(*blockNode)
}
// Push pushes the block onto the heap
func (bh BlockHeap) Push(block *blockNode) {
heap.Push(bh.impl, block)
}
// Len returns the length of this heap
func (bh BlockHeap) Len() int {
return bh.impl.Len()
}