forked from Qitmeer/qng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdagsync.go
221 lines (203 loc) · 4.72 KB
/
dagsync.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package meerdag
import (
"container/list"
"github.com/Qitmeer/qng/common/hash"
"sync"
)
// This parameter can be set according to the size of TCP package(1500) to ensure the transmission stability of the network
const MaxMainLocatorNum = 32
// Synchronization mode
type SyncMode byte
const (
DirectMode SyncMode = 0
SubDAGMode SyncMode = 1
ContinueMode SyncMode = 2
)
type DAGSync struct {
bd *MeerDAG
// The following fields are used to track the graph state being synced to from
// peers.
gsMtx sync.Mutex
gs *GraphState
}
// CalcSyncBlocks
func (ds *DAGSync) CalcSyncBlocks(gs *GraphState, locator []*hash.Hash, mode SyncMode, maxHashes uint) ([]*hash.Hash, *hash.Hash) {
ds.bd.stateLock.Lock()
defer ds.bd.stateLock.Unlock()
if mode == DirectMode {
result := []*hash.Hash{}
if len(locator) == 0 {
return result, nil
}
return ds.bd.sortBlock(locator), nil
} else if mode == ContinueMode {
result := []*hash.Hash{}
if len(locator) <= 0 {
return result, nil
}
point := ds.bd.getBlock(locator[0])
if point == nil {
return result, nil
}
if !ds.bd.isOnMainChain(point.GetID()) {
return result, nil
}
startBlock := ds.bd.getBlock(locator[1])
if startBlock == nil {
return result, nil
}
return ds.getBlockChainFromMain(startBlock, maxHashes), point.GetHash()
}
var point IBlock
for i := len(locator) - 1; i >= 0; i-- {
mainBlock := ds.bd.getBlock(locator[i])
if mainBlock == nil {
continue
}
if !ds.bd.isOnMainChain(mainBlock.GetID()) {
continue
}
point = mainBlock
break
}
if point == nil && len(locator) > 0 {
point = ds.bd.getBlock(locator[0])
if point != nil {
for !ds.bd.isOnMainChain(point.GetID()) {
if point.GetMainParent() == MaxId {
break
}
point = ds.bd.getBlockById(point.GetMainParent())
if point == nil {
break
}
}
}
}
if point == nil {
point = ds.bd.getGenesis()
}
return ds.getBlockChainFromMain(point, maxHashes), point.GetHash()
}
// GetMainLocator
func (ds *DAGSync) GetMainLocator(point *hash.Hash) []*hash.Hash {
ds.bd.stateLock.Lock()
defer ds.bd.stateLock.Unlock()
var endBlock IBlock
if point != nil {
endBlock = ds.bd.getBlock(point)
}
if endBlock != nil {
for !ds.bd.isOnMainChain(endBlock.GetID()) {
if endBlock.GetMainParent() == MaxId {
break
}
endBlock = ds.bd.getBlockById(endBlock.GetMainParent())
if endBlock == nil {
break
}
}
}
if endBlock == nil {
endBlock = ds.bd.getGenesis()
}
startBlock := ds.bd.getMainChainTip()
locator := list.New()
cur := startBlock
const DefaultMainLocatorNum = 10
for i := 0; i < DefaultMainLocatorNum; i++ {
if cur.GetID() == 0 ||
cur.GetMainParent() == MaxId ||
cur.GetID() <= endBlock.GetID() {
break
}
locator.PushFront(cur.GetHash())
cur = ds.bd.getBlockById(cur.GetMainParent())
if cur == nil {
break
}
}
if cur.GetID() > endBlock.GetID() {
halfStart := cur.GetID()
halfEnd := endBlock.GetID()
hlocator := []*hash.Hash{}
for locator.Len()+len(hlocator)+1 < MaxMainLocatorNum {
//for {
nextID := (halfStart - halfEnd) / 2
if nextID <= 0 {
break
}
nextID += halfEnd
if nextID == halfStart ||
nextID == halfEnd {
break
}
if !ds.bd.isOnMainChain(nextID) {
halfEnd++
continue
}
ib := ds.bd.getBlockById(nextID)
if ib == nil {
halfEnd++
continue
}
hlocator = append(hlocator, ib.GetHash())
halfEnd = nextID
}
if len(hlocator) > 0 {
for i := len(hlocator) - 1; i >= 0; i-- {
locator.PushFront(hlocator[i])
}
}
}
result := []*hash.Hash{endBlock.GetHash()}
for i := locator.Front(); i != nil; i = i.Next() {
result = append(result, i.Value.(*hash.Hash))
}
return result
}
func (ds *DAGSync) getBlockChainFromMain(point IBlock, maxHashes uint) []*hash.Hash {
mainTip := ds.bd.getMainChainTip()
result := []*hash.Hash{}
for i := point.GetOrder() + 1; i <= mainTip.GetOrder(); i++ {
block := ds.bd.getBlockByOrder(i)
if block == nil {
continue
}
result = append(result, block.GetHash())
if uint(len(result)) >= maxHashes {
break
}
}
rlen := len(result)
if uint(rlen) < maxHashes &&
rlen > 0 &&
result[rlen-1].IsEqual(mainTip.GetHash()) {
pdb, ok := ds.bd.GetInstance().(*Phantom)
if ok {
if !pdb.GetDiffAnticone().IsEmpty() {
da := pdb.GetDiffAnticone().SortList(false)
for _, id := range da {
block := ds.bd.getBlockById(id)
if block == nil {
continue
}
result = append(result, block.GetHash())
if uint(len(result)) >= maxHashes {
break
}
}
}
}
}
return result
}
func (ds *DAGSync) SetGraphState(gs *GraphState) {
ds.gsMtx.Lock()
defer ds.gsMtx.Unlock()
ds.gs = gs
}
// NewDAGSync
func NewDAGSync(bd *MeerDAG) *DAGSync {
return &DAGSync{bd: bd}
}