[#1208] innerring: Disallow to tick timer twice on the same height

Provide current heights as an argument to ticker.
Zero height disables any checks, thus corresponding to the old
behaviour. If non-zero height is used, ignore the tick if the height
is less than the timer tick state.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-30 13:16:41 +03:00 committed by Alex Vanin
parent c0e65dadaf
commit 4770cb8bf6
4 changed files with 68 additions and 8 deletions

View file

@ -27,6 +27,8 @@ type BlockTimer struct {
cur, tgt uint32
last uint32
h BlockTickHandler
ps []BlockTimer
@ -159,13 +161,18 @@ func (t *BlockTimer) reset() {
// Tick ticks one block in the BlockTimer.
//
// Executes all callbacks which are awaiting execution at the new block.
func (t *BlockTimer) Tick() {
func (t *BlockTimer) Tick(h uint32) {
t.mtx.Lock()
t.tick()
t.tick(h)
t.mtx.Unlock()
}
func (t *BlockTimer) tick() {
func (t *BlockTimer) tick(h uint32) {
if h != 0 && t.last == h {
return
}
t.last = h
t.cur++
if t.cur == t.tgt {
@ -182,6 +189,6 @@ func (t *BlockTimer) tick() {
}
for i := range t.ps {
t.ps[i].tick()
t.ps[i].tick(h)
}
}