Skip to content

Commit 73f0586

Browse files
authored
Merge pull request #23 from bttcprotocol/release_1.0.2
Release 1.0.2
2 parents a3d6186 + 65885b4 commit 73f0586

File tree

13 files changed

+154
-147
lines changed

13 files changed

+154
-147
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
- name: Install Go
1010
uses: actions/setup-go@v1
1111
with:
12-
go-version: 1.13
12+
go-version: 1.16
1313
- name: "Build binaries"
1414
run: make build
1515
- name: "Run tests"

bridge/setu/listener/base.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Listener interface {
2828

2929
StartHeaderProcess(context.Context)
3030

31-
StartPolling(context.Context, time.Duration)
31+
StartPolling(context.Context, time.Duration, bool)
3232

3333
StartSubscription(context.Context, ethereum.Subscription)
3434

@@ -156,23 +156,33 @@ func (bl *BaseListener) StartHeaderProcess(ctx context.Context) {
156156
}
157157

158158
// startPolling starts polling
159-
func (bl *BaseListener) StartPolling(ctx context.Context, pollInterval time.Duration) {
159+
// needAlign is used to decide whether the ticker is align to 1970 UTC.
160+
// if true, the ticker will always tick as it begins at 1970 UTC.
161+
func (bl *BaseListener) StartPolling(ctx context.Context, pollInterval time.Duration, needAlign bool) {
160162
// How often to fire the passed in function in second
161163
interval := pollInterval
164+
firstInterval := interval
165+
if needAlign {
166+
now := time.Now()
167+
baseTime := time.Unix(0, 0)
168+
firstInterval = interval - (now.UTC().Sub(baseTime) % interval)
169+
}
162170

163171
// Setup the ticket and the channel to signal
164172
// the ending of the interval
165-
ticker := time.NewTicker(interval)
173+
ticker := time.NewTicker(firstInterval)
166174

167175
// start listening
168176
for {
169177
select {
170178
case <-ticker.C:
179+
ticker.Reset(interval)
171180
header, err := bl.chainClient.HeaderByNumber(ctx, nil)
172181
if err == nil && header != nil {
173182
// send data to channel
174183
bl.HeaderChannel <- header
175184
}
185+
176186
case <-ctx.Done():
177187
bl.Logger.Info("Polling stopped")
178188
ticker.Stop()

bridge/setu/listener/heimdall.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (hl *HeimdallListener) Start() error {
4747
}
4848

4949
hl.Logger.Info("Start polling for events", "pollInterval", pollInterval)
50-
hl.StartPolling(headerCtx, pollInterval)
50+
hl.StartPolling(headerCtx, pollInterval, false)
5151
return nil
5252
}
5353

@@ -57,13 +57,21 @@ func (hl *HeimdallListener) ProcessHeader(*types.Header) {
5757
}
5858

5959
// StartPolling - starts polling for heimdall events
60-
func (hl *HeimdallListener) StartPolling(ctx context.Context, pollInterval time.Duration) {
60+
// needAlign is used to decide whether the ticker is align to 1970 UTC.
61+
// if true, the ticker will always tick as it begins at 1970 UTC.
62+
func (hl *HeimdallListener) StartPolling(ctx context.Context, pollInterval time.Duration, needAlign bool) {
6163
// How often to fire the passed in function in second
6264
interval := pollInterval
65+
firstInterval := interval
66+
if needAlign {
67+
now := time.Now()
68+
baseTime := time.Unix(0, 0)
69+
firstInterval = interval - (now.UTC().Sub(baseTime) % interval)
70+
}
6371

6472
// Setup the ticket and the channel to signal
6573
// the ending of the interval
66-
ticker := time.NewTicker(interval)
74+
ticker := time.NewTicker(firstInterval)
6775

6876
// var eventTypes []string
6977
// eventTypes = append(eventTypes, "message.action='checkpoint'")
@@ -75,6 +83,7 @@ func (hl *HeimdallListener) StartPolling(ctx context.Context, pollInterval time.
7583
for {
7684
select {
7785
case <-ticker.C:
86+
ticker.Reset(interval)
7887
fromBlock, toBlock, err := hl.fetchFromAndToBlock()
7988
if err != nil {
8089
hl.Logger.Error("Error fetching fromBlock and toBlock...skipping events query", "error", err)

bridge/setu/listener/maticchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (ml *MaticChainListener) Start() error {
3939
if err != nil {
4040
// start go routine to poll for new header using client object
4141
ml.Logger.Info("Start polling for header blocks", "pollInterval", helper.GetConfig().CheckpointerPollInterval)
42-
go ml.StartPolling(ctx, helper.GetConfig().CheckpointerPollInterval)
42+
go ml.StartPolling(ctx, helper.GetConfig().CheckpointerPollInterval, true)
4343
} else {
4444
// start go routine to listen new header using subscription
4545
go ml.StartSubscription(ctx, subscription)

bridge/setu/listener/rootchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (rl *RootChainListener) Start() error {
108108
// start go routine to poll for new header using client object
109109
rl.Logger.Info("Start polling for root chain header blocks",
110110
"root", rl.rootChainType, "pollInterval", rl.pollInterval)
111-
go rl.StartPolling(ctx, rl.pollInterval)
111+
go rl.StartPolling(ctx, rl.pollInterval, false)
112112
} else {
113113
// start go routine to listen new header using subscription
114114
go rl.StartSubscription(ctx, subscription)

bridge/setu/listener/tron.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,41 @@ func (tl *TronListener) Start() error {
7171

7272
tl.Logger.Info("Start polling for events", "pollInterval", pollInterval)
7373
// poll for new header using client object
74-
go tl.StartPolling(headerCtx, pollInterval)
74+
go tl.StartPolling(headerCtx, pollInterval, false)
7575
return nil
7676
}
7777

7878
// startPolling starts polling
79-
func (tl *TronListener) StartPolling(ctx context.Context, pollInterval time.Duration) {
79+
// needAlign is used to decide whether the ticker is align to 1970 UTC.
80+
// if true, the ticker will always tick as it begins at 1970 UTC.
81+
func (tl *TronListener) StartPolling(ctx context.Context, pollInterval time.Duration, needAlign bool) {
8082
// How often to fire the passed in function in second
8183
interval := pollInterval
84+
firstInterval := interval
85+
86+
if needAlign {
87+
now := time.Now()
88+
baseTime := time.Unix(0, 0)
89+
firstInterval = interval - (now.UTC().Sub(baseTime) % interval)
90+
}
8291

8392
// Setup the ticket and the channel to signal
8493
// the ending of the interval
85-
ticker := time.NewTicker(interval)
94+
ticker := time.NewTicker(firstInterval)
8695

8796
// start listening
8897
for {
8998
select {
9099
case <-ticker.C:
100+
ticker.Reset(interval)
91101
headerNum, err := tl.contractConnector.GetTronLatestBlockNumber()
92102
if err == nil {
93103
// send data to channel
94104
tl.HeaderChannel <- &(ethTypes.Header{
95105
Number: big.NewInt(headerNum),
96106
})
97107
}
108+
98109
case <-ctx.Done():
99110
tl.Logger.Info("Polling stopped")
100111
ticker.Stop()
@@ -214,7 +225,7 @@ func (tl *TronListener) queryAndBroadcastEvents(chainManagerParams *chainmanager
214225
// topup has to be processed first before validator join. so adding delay.
215226
delay := util.TaskDelayBetweenEachVal
216227
tl.sendTaskWithDelay("sendValidatorJoinToHeimdall", selectedEvent.Name, logBytes, delay)
217-
} else if isCurrentValidator, delay := util.CalculateTaskDelayWithOffset(tl.cliCtx,1); isCurrentValidator {
228+
} else if isCurrentValidator, delay := util.CalculateTaskDelayWithOffset(tl.cliCtx, 1); isCurrentValidator {
218229
// topup has to be processed first before validator join. so adding delay.
219230
delay = delay + util.TaskDelayBetweenEachVal
220231
tl.sendTaskWithDelay("sendValidatorJoinToHeimdall", selectedEvent.Name, logBytes, delay)
@@ -237,7 +248,7 @@ func (tl *TronListener) queryAndBroadcastEvents(chainManagerParams *chainmanager
237248
}
238249
if bytes.Equal(event.SignerPubkey, pubkeyBytes) {
239250
tl.sendTaskWithDelay("sendSignerChangeToHeimdall", selectedEvent.Name, logBytes, 0)
240-
} else if isCurrentValidator, delay := util.CalculateTaskDelayWithOffset(tl.cliCtx,1); isCurrentValidator {
251+
} else if isCurrentValidator, delay := util.CalculateTaskDelayWithOffset(tl.cliCtx, 1); isCurrentValidator {
241252
tl.sendTaskWithDelay("sendSignerChangeToHeimdall", selectedEvent.Name, logBytes, delay)
242253
}
243254

@@ -248,7 +259,7 @@ func (tl *TronListener) queryAndBroadcastEvents(chainManagerParams *chainmanager
248259
}
249260
if util.IsEventSender(tl.cliCtx, event.ValidatorId.Uint64()) {
250261
tl.sendTaskWithDelay("sendUnstakeInitToHeimdall", selectedEvent.Name, logBytes, 0)
251-
} else if isCurrentValidator, delay := util.CalculateTaskDelayWithOffset(tl.cliCtx,1); isCurrentValidator {
262+
} else if isCurrentValidator, delay := util.CalculateTaskDelayWithOffset(tl.cliCtx, 1); isCurrentValidator {
252263
tl.sendTaskWithDelay("sendUnstakeInitToHeimdall", selectedEvent.Name, logBytes, delay)
253264
}
254265

@@ -264,7 +275,7 @@ func (tl *TronListener) queryAndBroadcastEvents(chainManagerParams *chainmanager
264275
}
265276
if bytes.Equal(event.User.Bytes(), helper.GetAddress()) {
266277
tl.sendTaskWithDelay("sendTopUpFeeToHeimdall", selectedEvent.Name, logBytes, 0)
267-
} else if isCurrentValidator, delay := util.CalculateTaskDelayWithOffset(tl.cliCtx,1); isCurrentValidator {
278+
} else if isCurrentValidator, delay := util.CalculateTaskDelayWithOffset(tl.cliCtx, 1); isCurrentValidator {
268279
tl.sendTaskWithDelay("sendTopUpFeeToHeimdall", selectedEvent.Name, logBytes, delay)
269280
}
270281

@@ -280,7 +291,7 @@ func (tl *TronListener) queryAndBroadcastEvents(chainManagerParams *chainmanager
280291
}
281292
if util.IsEventSender(tl.cliCtx, event.ValidatorId.Uint64()) {
282293
tl.sendTaskWithDelay("sendUnjailToHeimdall", selectedEvent.Name, logBytes, 0)
283-
} else if isCurrentValidator, delay := util.CalculateTaskDelayWithOffset(tl.cliCtx,1); isCurrentValidator {
294+
} else if isCurrentValidator, delay := util.CalculateTaskDelayWithOffset(tl.cliCtx, 1); isCurrentValidator {
284295
tl.sendTaskWithDelay("sendUnjailToHeimdall", selectedEvent.Name, logBytes, delay)
285296
}
286297

0 commit comments

Comments
 (0)