mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
[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:
parent
b4a9805157
commit
5f800890ec
56
blockdag/blockheap.go
Normal file
56
blockdag/blockheap.go
Normal 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()
|
||||
}
|
89
blockdag/blockheap_test.go
Normal file
89
blockdag/blockheap_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user