-
Notifications
You must be signed in to change notification settings - Fork 0
/
reachabilitydata.go
118 lines (100 loc) · 4.12 KB
/
reachabilitydata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package model
import (
"fmt"
"github.com/Nirvana-Chain/nirvanad/domain/consensus/model/externalapi"
)
// MutableReachabilityData represents a node in the reachability tree
// of some DAG block. It mainly provides the ability to query *tree*
// reachability with O(1) query time. It does so by managing an
// index interval for each node and making sure all nodes in its
// subtree are indexed within the interval, so the query
// B ∈ subtree(A) simply becomes B.interval ⊂ A.interval.
//
// The main challenge of maintaining such intervals is that our tree
// is an ever-growing tree and as such pre-allocated intervals may
// not suffice as per future events. This is where the reindexing
// algorithm below comes into place.
// We use the reasonable assumption that the initial root interval
// (e.g., [0, 2^64-1]) should always suffice for any practical use-
// case, and so reindexing should always succeed unless more than
// 2^64 blocks are added to the DAG/tree.
//
// In addition, we keep a future covering set for every node.
// This set allows to query reachability over the entirety of the DAG.
// See documentation of FutureCoveringTreeNodeSet for additional details.
// ReachabilityData is a read-only version of a block's MutableReachabilityData
// Use CloneWritable to edit the MutableReachabilityData.
type ReachabilityData interface {
Children() []*externalapi.DomainHash
Parent() *externalapi.DomainHash
Interval() *ReachabilityInterval
FutureCoveringSet() FutureCoveringTreeNodeSet
CloneMutable() MutableReachabilityData
Equal(other ReachabilityData) bool
}
// MutableReachabilityData represents a block's MutableReachabilityData, with ability to edit it
type MutableReachabilityData interface {
ReachabilityData
AddChild(child *externalapi.DomainHash)
SetParent(parent *externalapi.DomainHash)
SetInterval(interval *ReachabilityInterval)
SetFutureCoveringSet(futureCoveringSet FutureCoveringTreeNodeSet)
}
// ReachabilityInterval represents an interval to be used within the
// tree reachability algorithm. See ReachabilityTreeNode for further
// details.
type ReachabilityInterval struct {
Start uint64
End uint64
}
// If this doesn't compile, it means the type definition has been changed, so it's
// an indication to update Equal and Clone accordingly.
var _ = &ReachabilityInterval{0, 0}
// Equal returns whether ri equals to other
func (ri *ReachabilityInterval) Equal(other *ReachabilityInterval) bool {
if ri == nil || other == nil {
return ri == other
}
if ri.Start != other.Start {
return false
}
if ri.End != other.End {
return false
}
return true
}
// Clone returns a clone of ReachabilityInterval
func (ri *ReachabilityInterval) Clone() *ReachabilityInterval {
return &ReachabilityInterval{
Start: ri.Start,
End: ri.End,
}
}
func (ri *ReachabilityInterval) String() string {
return fmt.Sprintf("[%d,%d]", ri.Start, ri.End)
}
// FutureCoveringTreeNodeSet represents a collection of blocks in the future of
// a certain block. Once a block B is added to the DAG, every block A_i in
// B's selected parent anticone must register B in its FutureCoveringTreeNodeSet. This allows
// to relatively quickly (O(log(|FutureCoveringTreeNodeSet|))) query whether B
// is a descendent (is in the "future") of any block that previously
// registered it.
//
// Note that FutureCoveringTreeNodeSet is meant to be queried only if B is not
// a reachability tree descendant of the block in question, as reachability
// tree queries are always O(1).
//
// See insertNode, hasAncestorOf, and isInPast for further details.
type FutureCoveringTreeNodeSet []*externalapi.DomainHash
// Clone returns a clone of FutureCoveringTreeNodeSet
func (fctns FutureCoveringTreeNodeSet) Clone() FutureCoveringTreeNodeSet {
//return fctns
return externalapi.CloneHashes(fctns)
}
// If this doesn't compile, it means the type definition has been changed, so it's
// an indication to update Equal and Clone accordingly.
var _ FutureCoveringTreeNodeSet = []*externalapi.DomainHash{}
// Equal returns whether fctns equals to other
func (fctns FutureCoveringTreeNodeSet) Equal(other FutureCoveringTreeNodeSet) bool {
return externalapi.HashesEqual(fctns, other)
}