tendermint/types/types.go

80 lines
1.7 KiB
Go
Raw Normal View History

2016-01-12 16:50:06 -05:00
package rpctypes
import (
2016-01-13 18:37:35 -05:00
"encoding/json"
2016-01-12 16:50:06 -05:00
"github.com/tendermint/go-events"
2016-01-13 18:37:35 -05:00
"github.com/tendermint/go-wire"
2016-01-12 16:50:06 -05:00
)
type RPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID string `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
func NewRPCRequest(id string, method string, params []interface{}) RPCRequest {
return RPCRequest{
JSONRPC: "2.0",
ID: id,
Method: method,
Params: params,
}
}
//----------------------------------------
/*
Result is a generic interface.
Applications should register type-bytes like so:
var _ = wire.RegisterInterface(
struct{ Result }{},
wire.ConcreteType{&ResultGenesis{}, ResultTypeGenesis},
wire.ConcreteType{&ResultBlockchainInfo{}, ResultTypeBlockchainInfo},
...
)
*/
type Result interface {
}
//----------------------------------------
type RPCResponse struct {
2016-01-13 18:37:35 -05:00
JSONRPC string `json:"jsonrpc"`
ID string `json:"id"`
Result *json.RawMessage `json:"result"`
Error string `json:"error"`
2016-01-12 16:50:06 -05:00
}
2016-01-13 18:37:35 -05:00
func NewRPCResponse(id string, res interface{}, err string) RPCResponse {
2016-01-13 22:16:56 -05:00
var raw *json.RawMessage
if res != nil {
rawMsg := json.RawMessage(wire.JSONBytes(res))
raw = &rawMsg
}
2016-01-12 16:50:06 -05:00
return RPCResponse{
JSONRPC: "2.0",
ID: id,
2016-01-13 22:16:56 -05:00
Result: raw,
2016-01-12 16:50:06 -05:00
Error: err,
}
}
//----------------------------------------
// *wsConnection implements this interface.
type WSRPCConnection interface {
GetRemoteAddr() string
GetEventSwitch() *events.EventSwitch
WriteRPCResponse(resp RPCResponse)
TryWriteRPCResponse(resp RPCResponse) bool
}
// websocket-only RPCFuncs take this as the first parameter.
type WSRPCContext struct {
Request RPCRequest
WSRPCConnection
}