7 "github.com/bytom/bytom/protocol/bc"
8 "github.com/bytom/bytom/protocol/state"
11 type treeNode struct {
12 checkpoint *state.Checkpoint
16 func makeTree(root *state.Checkpoint, successors []*state.Checkpoint) *treeNode {
17 parentToSuccessors := make(map[bc.Hash][]*state.Checkpoint)
18 for _, successor := range successors {
19 parentToSuccessors[successor.ParentHash] = append(parentToSuccessors[successor.ParentHash], successor)
22 rootNode := &treeNode{checkpoint: root}
23 nodes := []*treeNode{rootNode}
26 for _, successor := range parentToSuccessors[node.checkpoint.Hash] {
27 child := &treeNode{checkpoint: successor}
28 successor.Parent = node.checkpoint
29 node.children = append(node.children, child)
30 nodes = append(nodes, child)
37 func (t *treeNode) addChild(child *treeNode) {
38 for i, n := range t.children {
39 if n.checkpoint.Hash == child.checkpoint.Hash {
44 t.children = append(t.children, child)
47 func (t *treeNode) nodeByHash(blockHash bc.Hash) (*treeNode, error) {
48 if c := t.findOnlyOne(func(c *state.Checkpoint) bool {
49 return c.Hash == blockHash
54 return nil, errors.New(fmt.Sprintf("fail to find checkpoint of hash:%s", blockHash.String()))
57 func (t *treeNode) findOnlyOne(predicate func(*state.Checkpoint) bool) *treeNode {
58 if predicate(t.checkpoint) {
62 for _, child := range t.children {
63 if node := child.findOnlyOne(predicate); node != nil {