Browse Source

updates from go-rpc, go-event-meter

pull/1943/head
Ethan Buchman 9 years ago
parent
commit
e6e9f3ac6b
4 changed files with 42 additions and 59 deletions
  1. +3
    -5
      handlers/handlers.go
  2. +10
    -35
      handlers/routes.go
  3. +1
    -11
      main.go
  4. +28
    -8
      types/types.go

+ 3
- 5
handlers/handlers.go View File

@ -11,15 +11,12 @@ import (
"github.com/tendermint/netmon/types"
)
type NetMonResultInterface interface{}
type NetMonResult struct {
Result NetMonResultInterface
type NetMonResult interface {
}
// for wire.readReflect
var _ = wire.RegisterInterface(
struct{ NetMonResultInterface }{},
struct{ NetMonResult }{},
wire.ConcreteType{&types.ChainAndValidatorIDs{}, 0x01},
wire.ConcreteType{&types.ChainStatus{}, 0x02},
wire.ConcreteType{&types.Validator{}, 0x03},
@ -81,6 +78,7 @@ func (tn *TendermintNetwork) GetChain(chainID string) (*types.ChainStatus, error
if !ok {
return nil, fmt.Errorf("Unknown chain %s", chainID)
}
fmt.Println("CHAIN:", chain)
return chain, nil
}


+ 10
- 35
handlers/routes.go View File

@ -22,56 +22,31 @@ func Routes(network *TendermintNetwork) map[string]*rpc.RPCFunc {
}
func StatusResult(network *TendermintNetwork) interface{} {
return func() (*NetMonResult, error) {
r, err := network.Status()
if err != nil {
return nil, err
} else {
return &NetMonResult{r}, nil
}
return func() (NetMonResult, error) {
return network.Status()
}
}
func GetChainResult(network *TendermintNetwork) interface{} {
return func(chain string) (*NetMonResult, error) {
r, err := network.GetChain(chain)
if err != nil {
return nil, err
} else {
return &NetMonResult{r}, nil
}
return func(chain string) (NetMonResult, error) {
return network.GetChain(chain)
}
}
func GetValidatorSetResult(network *TendermintNetwork) interface{} {
return func(valSetID string) (*NetMonResult, error) {
r, err := network.GetValidatorSet(valSetID)
if err != nil {
return nil, err
} else {
return &NetMonResult{r}, nil
}
return func(valSetID string) (NetMonResult, error) {
return network.GetValidatorSet(valSetID)
}
}
func GetValidatorResult(network *TendermintNetwork) interface{} {
return func(valSetID, valID string) (*NetMonResult, error) {
r, err := network.GetValidator(valSetID, valID)
if err != nil {
return nil, err
} else {
return &NetMonResult{r}, nil
}
return func(valSetID, valID string) (NetMonResult, error) {
return network.GetValidator(valSetID, valID)
}
}
func GetMeterResult(network *TendermintNetwork) interface{} {
return func(chainID, valID, eventID string) (*NetMonResult, error) {
r, err := network.GetMeter(chainID, valID, eventID)
if err != nil {
return nil, err
} else {
return &NetMonResult{r}, nil
}
return func(chainID, valID, eventID string) (NetMonResult, error) {
return network.GetMeter(chainID, valID, eventID)
}
}

+ 1
- 11
main.go View File

@ -20,10 +20,7 @@ import (
"github.com/tendermint/go-events"
pcm "github.com/tendermint/go-process"
"github.com/tendermint/go-rpc/server"
"github.com/tendermint/go-rpc/types"
"github.com/tendermint/go-wire"
tmcfg "github.com/tendermint/tendermint/config/tendermint"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
)
@ -85,7 +82,7 @@ func cmdMonitor(c *cli.Context) {
if err := v.NewEventMeter(); err != nil {
Exit(err.Error())
}
if err := v.EventMeter().Subscribe(tmtypes.EventStringNewBlock(), func(metric *eventmeter.EventMetric, data interface{}) {
if err := v.EventMeter().Subscribe(tmtypes.EventStringNewBlock(), func(metric *eventmeter.EventMetric, data events.EventData) {
// TODO: update chain status with block and metric
// chainStatus.NewBlock(data.(tmtypes.EventDataNewBlock).Block)
}); err != nil {
@ -100,13 +97,6 @@ func cmdMonitor(c *cli.Context) {
// the routes are functions on the network object
routes := handlers.Routes(network)
// register the result objects with wire
wire.RegisterInterface(
struct{ rpctypes.Result }{},
wire.ConcreteType{&events.EventResult{}, 0x1},
wire.ConcreteType{&ctypes.TendermintResult{}, 0x2},
wire.ConcreteType{&handlers.NetMonResult{}, 0x3},
)
// serve http and ws
mux := http.NewServeMux()
wm := rpcserver.NewWebsocketManager(routes, nil) // TODO: evsw


+ 28
- 8
types/types.go View File

@ -1,11 +1,16 @@
package types
import (
"encoding/json"
"fmt"
"reflect"
"sync"
"github.com/tendermint/go-event-meter"
"github.com/tendermint/go-crypto"
"github.com/tendermint/go-event-meter"
"github.com/tendermint/go-events"
"github.com/tendermint/go-wire"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)
//---------------------------------------------
@ -59,24 +64,39 @@ type BlockchainStatus struct {
mtx sync.Mutex
Height int `json:"height"`
MeanBlockTime float64 `json:"mean_block_time"`
TxThroughput float64 `json:"tx_throughput"`
MeanBlockTime float64 `json:"mean_block_time" wire:"unsafe"`
TxThroughput float64 `json:"tx_throughput" wire:"unsafe"`
BlockchainSize int64 `json:"blockchain_size"` // how might we get StateSize ?
}
// validator on a chain
type ChainValidator struct {
*Validator
Addr string `json:"addr"` // do we want multiple addrs?
Index int `json:"index"`
*Validator `json:"validator"`
Addr string `json:"addr"` // do we want multiple addrs?
Index int `json:"index"`
em *eventmeter.EventMeter // holds a ws connection to the val
Latency float64 `json:"latency,omitempty"`
Latency float64 `json:"latency,omitempty" wire:"unsafe"`
}
func unmarshalEvent(b json.RawMessage) (string, events.EventData, error) {
var err error
result := new(ctypes.TMResult)
wire.ReadJSONPtr(result, b, &err)
if err != nil {
return "", nil, err
}
event, ok := (*result).(*ctypes.ResultEvent)
if !ok {
return "", nil, fmt.Errorf("Result is not type *ctypes.ResultEvent. Got %v", reflect.TypeOf(*result))
}
return event.Name, event.Data, nil
}
func (cv *ChainValidator) NewEventMeter() error {
em := eventmeter.NewEventMeter(fmt.Sprintf("ws://%s/websocket", cv.Addr))
em := eventmeter.NewEventMeter(fmt.Sprintf("ws://%s/websocket", cv.Addr), unmarshalEvent)
if err := em.Start(); err != nil {
return err
}


Loading…
Cancel
Save