-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
146 lines (117 loc) · 3.03 KB
/
sync.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
package DataSync
import (
"github.com/BASChain/go-bas/Bas_Ethereum"
"github.com/ethereum/go-ethereum/event"
"sync"
"time"
)
var lastSavingPoint = uint64(0)
var currentSavingPoint = uint64(0)
func moveToNewSavingPoint(blockNumber uint64) bool{
if currentSavingPoint == blockNumber {
return false
}
lastSavingPoint = currentSavingPoint;
currentSavingPoint = blockNumber;
logger.Info("[Data_Sync] saving point ", lastSavingPoint, "----->" , currentSavingPoint)
return true
}
func syncGap(from ,to uint64){
if from>to {
return
}
opts := Bas_Ethereum.GetLoopOpts(from,&to)
var waitGroup sync.WaitGroup
waitGroup.Add(12)
go loopOverRootChanged(opts,&waitGroup)
go loopOverSubChanged(opts,&waitGroup)
go loopOverDNSChanged(opts,&waitGroup)
go loopOverAdd(opts,&waitGroup)
go loopOverUpdate(opts,&waitGroup)
go loopOverExtend(opts,&waitGroup)
go loopOverTakeover(opts,&waitGroup)
go loopOverTransfer(opts,&waitGroup)
go loopOverTransferFrom(opts,&waitGroup)
go loopOverRemove(opts,&waitGroup)
go loopOverPaid(opts,&waitGroup)
go loopOverSettingChanged(opts,&waitGroup)
waitGroup.Wait()
syncDataByHandleQueue()
}
func SyncGapWithNoTrust(blockNumber uint64){
if moveToNewSavingPoint(blockNumber){
go syncGap(lastSavingPoint+1, blockNumber-1)
}
}
func syncGapToNewest(){
moveToNewSavingPoint(conn.GetLastBlockNumber())
syncGap(lastSavingPoint,currentSavingPoint)
}
func syncDataByHandleQueue(){
var waitGroup sync.WaitGroup
waitGroup.Add(4)
go loopOverQueueOwnership(&waitGroup)
go loopOverQueueRoot(&waitGroup)
go loopOverQueueSub(&waitGroup)
go loopOverQueueDns(&waitGroup)
waitGroup.Wait()
clearQueueO()
clearQueueD()
clearQueueR()
clearQueueS()
}
func ShowCachedNames(){
if DebugFlag{
for _,v := range Records{
logger.Info(string(v.Name), v.Owner.String())
}
}
}
var subs []event.Subscription
func watch(lastBlockNumber uint64){
var waitGroup sync.WaitGroup
opts := Bas_Ethereum.GetWatchOpts(lastBlockNumber)
subs = []event.Subscription{}
waitGroup.Add(12)
go watchRootChanged(opts,&subs,&waitGroup)
go watchSubChanged(opts,&subs,&waitGroup)
go watchDNSChanged(opts,&subs,&waitGroup)
go watchAdd(opts,&subs,&waitGroup)
go watchUpdate(opts,&subs,&waitGroup)
go watchExtend(opts,&subs,&waitGroup)
go watchTakeover(opts,&subs,&waitGroup)
go watchTransfer(opts,&subs,&waitGroup)
go watchTransferFrom(opts,&subs,&waitGroup)
go watchRemove(opts,&subs,&waitGroup)
go watchPaid(opts,&subs,&waitGroup)
go watchSettingChanged(opts,&subs,&waitGroup)
waitGroup.Wait()
ReWatch()
}
func unSubscriptAll(){
for _ ,s:= range subs {
s.Unsubscribe()
}
logger.Info("Unsubscribed all event watches")
}
func ReWatch(){
logger.Info("ReSyncing")
unSubscriptAll()
ResetConnAndService()
watch(currentSavingPoint)
}
var firstStart = true;
func Sync(){
Settings()
syncGapToNewest()
//make second fast sync to avoid new event during sync
go syncGapToNewest()
if firstStart {
go func() {
time.Sleep(time.Duration(120)*time.Second)
ShowCachedNames()
}()
firstStart = false
}
watch(currentSavingPoint)
}