[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.
This commit is contained in:
stasatdaglabs 2018-07-22 17:26:18 +03:00 committed by Ori Newman
parent b4a9805157
commit 5f800890ec
2 changed files with 145 additions and 0 deletions

56
blockdag/blockheap.go Normal file
View File

@ -0,0 +1,56 @@
package blockdag
import "container/heap"
// 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 HashToBig(&h[i].hash).Cmp(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()
}

View File

@ -0,0 +1,89 @@
package blockdag
import (
"testing"
"github.com/daglabs/btcd/dagconfig/daghash"
"github.com/daglabs/btcd/dagconfig"
)
// TestBlockHeap tests pushing, popping, and determining the length of the heap.
func TestBlockHeap(t *testing.T) {
block0Header := dagconfig.MainNetParams.GenesisBlock.Header
block0 := newBlockNode(&block0Header, newSet())
block100000Header := Block100000.Header
block100000 := newBlockNode(&block100000Header, setFromSlice(block0))
block0smallHash := newBlockNode(&block0Header, newSet())
block0smallHash.hash = daghash.Hash{}
tests := []struct {
name string
toPush []*blockNode
expectedLength int
expectedPop *blockNode
}{
{
name: "empty heap must have length 0",
toPush: []*blockNode{},
expectedLength: 0,
expectedPop: nil,
},
{
name: "heap with one push must have length 1",
toPush: []*blockNode{block0},
expectedLength: 1,
expectedPop: nil,
},
{
name: "heap with one push and one pop",
toPush: []*blockNode{block0},
expectedLength: 0,
expectedPop: block0,
},
{
name: "push two blocks with different heights, heap shouldn't have to rebalance",
toPush: []*blockNode{block100000, block0},
expectedLength: 1,
expectedPop: block100000,
},
{
name: "push two blocks with different heights, heap must rebalance",
toPush: []*blockNode{block0, block100000},
expectedLength: 1,
expectedPop: block100000,
},
{
name: "push two blocks with equal heights but different hashes, heap shouldn't have to rebalance",
toPush: []*blockNode{block0, block0smallHash},
expectedLength: 1,
expectedPop: block0,
},
{
name: "push two blocks with equal heights but different hashes, heap must rebalance",
toPush: []*blockNode{block0smallHash, block0},
expectedLength: 1,
expectedPop: block0,
},
}
for _, test := range tests {
heap := NewHeap()
for _, block := range test.toPush {
heap.Push(block)
}
var poppedBlock *blockNode
if test.expectedPop != nil {
poppedBlock = heap.Pop()
}
if heap.Len() != test.expectedLength {
t.Errorf("unexpected heap length in test \"%s\". "+
"Expected: %v, got: %v", test.name, test.expectedLength, heap.Len())
}
if poppedBlock != test.expectedPop {
t.Errorf("unexpected popped block in test \"%s\". "+
"Expected: %v, got: %v", test.name, test.expectedPop, poppedBlock)
}
}
}