kaspad/blockdag/blocknode_test.go
Ori Newman ef01f6410d [DEV-301] implement blockNode.chainHeight as the height of the selectedParent chain
* [DEV-301] implement blockNode.chainHeight as the height of the selected parent chain

* [DEV-301] add description to TestChainHeight

* [DEV-301] Delete comment on TestChainHeight and expalain about the dag
2018-12-23 16:54:45 +02:00

89 lines
2.1 KiB
Go

package blockdag
import (
"testing"
)
func TestChainHeight(t *testing.T) {
phantomK := uint32(2)
buildNode := buildNodeGenerator(phantomK)
buildWithChildren := func(parents blockSet) *blockNode {
node := buildNode(parents)
addNodeAsChildToParents(node)
return node
}
node0 := buildWithChildren(setFromSlice())
node1 := buildWithChildren(setFromSlice(node0))
node2 := buildWithChildren(setFromSlice(node0))
node3 := buildWithChildren(setFromSlice(node0))
node4 := buildWithChildren(setFromSlice(node1, node2, node3))
node5 := buildWithChildren(setFromSlice(node1, node2, node3))
node6 := buildWithChildren(setFromSlice(node1, node2, node3))
node7 := buildWithChildren(setFromSlice(node0))
node8 := buildWithChildren(setFromSlice(node7))
node9 := buildWithChildren(setFromSlice(node8))
node10 := buildWithChildren(setFromSlice(node9, node6))
// Because nodes 7 & 8 were mined secretly, node10's selected
// parent will be node6, although node9 is higher. So in this
// case, node10.height and node10.chainHeight will be different
tests := []struct {
node *blockNode
expectedChainHeight uint32
}{
{
node: node0,
expectedChainHeight: 0,
},
{
node: node1,
expectedChainHeight: 1,
},
{
node: node2,
expectedChainHeight: 1,
},
{
node: node3,
expectedChainHeight: 1,
},
{
node: node4,
expectedChainHeight: 2,
},
{
node: node5,
expectedChainHeight: 2,
},
{
node: node6,
expectedChainHeight: 2,
},
{
node: node7,
expectedChainHeight: 1,
},
{
node: node8,
expectedChainHeight: 2,
},
{
node: node9,
expectedChainHeight: 3,
},
{
node: node10,
expectedChainHeight: 3,
},
}
for _, test := range tests {
if test.node.chainHeight != test.expectedChainHeight {
t.Errorf("block %v expected chain height %v but got %v", test.node, test.expectedChainHeight, test.node.chainHeight)
}
}
}