@@ -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