2016-01-28 19:53:22 -08:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2016-10-10 02:58:13 -04:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2016-01-28 19:53:22 -08:00
|
|
|
)
|
|
|
|
|
2016-12-22 21:51:58 -05:00
|
|
|
// XXX: WARNING: these functions can halt the consensus as firing events is synchronous.
|
|
|
|
// Make sure to read off the channels, and in the case of subscribeToEventRespond, to write back on it
|
|
|
|
|
2016-11-23 18:20:46 -05:00
|
|
|
// NOTE: if chanCap=0, this blocks on the event being consumed
|
2016-10-10 02:58:13 -04:00
|
|
|
func subscribeToEvent(evsw types.EventSwitch, receiver, eventID string, chanCap int) chan interface{} {
|
2016-06-26 00:40:53 -04:00
|
|
|
// listen for event
|
2016-01-28 19:53:22 -08:00
|
|
|
ch := make(chan interface{}, chanCap)
|
2016-10-10 02:58:13 -04:00
|
|
|
types.AddListenerForEvent(evsw, receiver, eventID, func(data types.TMEventData) {
|
2016-01-28 19:53:22 -08:00
|
|
|
ch <- data
|
|
|
|
})
|
|
|
|
return ch
|
|
|
|
}
|
2016-11-23 18:20:46 -05:00
|
|
|
|
|
|
|
// NOTE: this blocks on receiving a response after the event is consumed
|
|
|
|
func subscribeToEventRespond(evsw types.EventSwitch, receiver, eventID string) chan interface{} {
|
|
|
|
// listen for event
|
|
|
|
ch := make(chan interface{})
|
|
|
|
types.AddListenerForEvent(evsw, receiver, eventID, func(data types.TMEventData) {
|
|
|
|
ch <- data
|
|
|
|
<-ch
|
|
|
|
})
|
|
|
|
return ch
|
|
|
|
}
|
2017-06-28 11:12:45 -04:00
|
|
|
|
|
|
|
func discardFromChan(ch chan interface{}, n int) {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
<-ch
|
|
|
|
}
|
|
|
|
}
|