diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-02-27 10:41:01 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:57 +0800 |
commit | e83bcc1097d49b46b79131e546f1270b9192cc05 (patch) | |
tree | a1af659afd80e3744177637cc06b2f0662a0ced8 /vendor/github.com/dexon-foundation/dexon-consensus/common/event.go | |
parent | 22b38ce74c3da40b7b7f24ada0abdf8d5ba03c64 (diff) | |
download | dexon-e83bcc1097d49b46b79131e546f1270b9192cc05.tar.gz dexon-e83bcc1097d49b46b79131e546f1270b9192cc05.tar.zst dexon-e83bcc1097d49b46b79131e546f1270b9192cc05.zip |
core: sync to latest core (#214)
* vendor: sync to latest core
* fix for single chain
Diffstat (limited to 'vendor/github.com/dexon-foundation/dexon-consensus/common/event.go')
-rw-r--r-- | vendor/github.com/dexon-foundation/dexon-consensus/common/event.go | 75 |
1 files changed, 37 insertions, 38 deletions
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/common/event.go b/vendor/github.com/dexon-foundation/dexon-consensus/common/event.go index 6c6bf49d4..4e4e23bf3 100644 --- a/vendor/github.com/dexon-foundation/dexon-consensus/common/event.go +++ b/vendor/github.com/dexon-foundation/dexon-consensus/common/event.go @@ -20,26 +20,25 @@ package common import ( "container/heap" "sync" - "time" ) -type timeEventFn func(time.Time) +type heightEventFn func(uint64) -type timeEvent struct { - t time.Time - fn timeEventFn +type heightEvent struct { + h uint64 + fn heightEventFn } -// timeEvents implements a Min-Heap structure. -type timeEvents []timeEvent +// heightEvents implements a Min-Heap structure. +type heightEvents []heightEvent -func (h timeEvents) Len() int { return len(h) } -func (h timeEvents) Less(i, j int) bool { return h[i].t.Before(h[j].t) } -func (h timeEvents) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *timeEvents) Push(x interface{}) { - *h = append(*h, x.(timeEvent)) +func (h heightEvents) Len() int { return len(h) } +func (h heightEvents) Less(i, j int) bool { return h[i].h < h[j].h } +func (h heightEvents) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *heightEvents) Push(x interface{}) { + *h = append(*h, x.(heightEvent)) } -func (h *timeEvents) Pop() interface{} { +func (h *heightEvents) Pop() interface{} { old := *h n := len(old) x := old[n-1] @@ -49,54 +48,54 @@ func (h *timeEvents) Pop() interface{} { // Event implements the Observer pattern. type Event struct { - timeEvents timeEvents - timeEventsLock sync.Mutex + heightEvents heightEvents + heightEventsLock sync.Mutex } // NewEvent creates a new event instance. func NewEvent() *Event { - te := timeEvents{} - heap.Init(&te) + he := heightEvents{} + heap.Init(&he) return &Event{ - timeEvents: te, + heightEvents: he, } } -// RegisterTime to get notified on and after specific time. -func (e *Event) RegisterTime(t time.Time, fn timeEventFn) { - e.timeEventsLock.Lock() - defer e.timeEventsLock.Unlock() - heap.Push(&e.timeEvents, timeEvent{ - t: t, +// RegisterHeight to get notified on a specific height. +func (e *Event) RegisterHeight(h uint64, fn heightEventFn) { + e.heightEventsLock.Lock() + defer e.heightEventsLock.Unlock() + heap.Push(&e.heightEvents, heightEvent{ + h: h, fn: fn, }) } -// NotifyTime and trigger function callback. -func (e *Event) NotifyTime(t time.Time) { - fns := func() (fns []timeEventFn) { - e.timeEventsLock.Lock() - defer e.timeEventsLock.Unlock() - if len(e.timeEvents) == 0 { +// NotifyHeight and trigger function callback. +func (e *Event) NotifyHeight(h uint64) { + fns := func() (fns []heightEventFn) { + e.heightEventsLock.Lock() + defer e.heightEventsLock.Unlock() + if len(e.heightEvents) == 0 { return } - for !t.Before(e.timeEvents[0].t) { - te := heap.Pop(&e.timeEvents).(timeEvent) - fns = append(fns, te.fn) - if len(e.timeEvents) == 0 { + for h >= e.heightEvents[0].h { + he := heap.Pop(&e.heightEvents).(heightEvent) + fns = append(fns, he.fn) + if len(e.heightEvents) == 0 { return } } return }() for _, fn := range fns { - fn(t) + fn(h) } } // Reset clears all pending event func (e *Event) Reset() { - e.timeEventsLock.Lock() - defer e.timeEventsLock.Unlock() - e.timeEvents = timeEvents{} + e.heightEventsLock.Lock() + defer e.heightEventsLock.Unlock() + e.heightEvents = heightEvents{} } |