tendermint/handlers/callbacks.go

42 lines
1.4 KiB
Go
Raw Normal View History

2016-01-15 23:31:57 -05:00
package handlers
import (
2016-01-21 23:05:39 -05:00
"github.com/tendermint/netmon/Godeps/_workspace/src/github.com/tendermint/go-event-meter"
"github.com/tendermint/netmon/Godeps/_workspace/src/github.com/tendermint/go-events"
2016-01-15 23:31:57 -05:00
2016-01-27 00:27:24 -05:00
"github.com/tendermint/netmon/types"
2016-01-21 23:05:39 -05:00
tmtypes "github.com/tendermint/netmon/Godeps/_workspace/src/github.com/tendermint/tendermint/types"
2016-01-15 23:31:57 -05:00
)
2016-01-27 00:27:24 -05:00
/*
Each chain-validator gets an eventmeter which maintains the websocket
Certain pre-defined events may update the netmon state: latency pongs, new blocks
TODO: config changes for new validators and changing ip/port
*/
2016-01-15 23:31:57 -05:00
// implements eventmeter.EventCallbackFunc
2016-01-27 00:27:24 -05:00
// updates validator and possibly chain with new block
func (tn *TendermintNetwork) newBlockCallback(chainState *types.ChainState, val *types.ValidatorState) eventmeter.EventCallbackFunc {
2016-01-15 23:31:57 -05:00
return func(metric *eventmeter.EventMetric, data events.EventData) {
block := data.(tmtypes.EventDataNewBlock).Block
2016-01-27 00:27:24 -05:00
// these functions are thread safe
// we should run them concurrently
2016-01-15 23:31:57 -05:00
// update height for validator
2016-01-27 00:27:24 -05:00
val.NewBlock(block)
2016-01-15 23:31:57 -05:00
// possibly update height and mean block time for chain
2016-01-27 00:27:24 -05:00
chainState.NewBlock(block)
2016-01-15 23:31:57 -05:00
}
}
// implements eventmeter.EventLatencyFunc
2016-01-27 00:27:24 -05:00
func (tn *TendermintNetwork) latencyCallback(chain *types.ChainState, val *types.ValidatorState) eventmeter.LatencyCallbackFunc {
2016-01-15 23:31:57 -05:00
return func(latency float64) {
2016-01-27 00:27:24 -05:00
oldLatency := val.UpdateLatency(latency)
chain.UpdateLatency(oldLatency, latency)
2016-01-15 23:31:57 -05:00
}
}