Example #1
0
func (c *ClientJSON) RequestResponse(s rpctypes.RPCRequest) (b []byte, err error) {
	b = wire.JSONBytes(s)
	buf := bytes.NewBuffer(b)
	resp, err := http.Post(c.addr, "text/json", buf)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	return ioutil.ReadAll(resp.Body)
}
Example #2
0
func (privVal *PrivValidator) save() {
	if privVal.filePath == "" {
		PanicSanity("Cannot save PrivValidator: filePath not set")
	}
	jsonBytes := wire.JSONBytes(privVal)
	err := WriteFileAtomic(privVal.filePath, jsonBytes)
	if err != nil {
		// `@; BOOM!!!
		PanicCrisis(err)
	}
}
Example #3
0
func DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) {
	roundState := consensusState.GetRoundState()
	peerRoundStates := []string{}
	for _, peer := range p2pSwitch.Peers().List() {
		// TODO: clean this up?
		peerState := peer.Data.Get(types.PeerStateKey).(*cm.PeerState)
		peerRoundState := peerState.GetRoundState()
		peerRoundStateStr := peer.Key + ":" + string(wire.JSONBytes(peerRoundState))
		peerRoundStates = append(peerRoundStates, peerRoundStateStr)
	}
	return &ctypes.ResultDumpConsensusState{roundState.String(), peerRoundStates}, nil
}
Example #4
0
// Get the current consensus state.
func (this *consensus) State() (*ConsensusState, error) {
	roundState := this.consensusState.GetRoundState()
	peerRoundStates := []string{}
	for _, peer := range this.p2pSwitch.Peers().List() {
		// TODO: clean this up?
		peerState := peer.Data.Get(cm.PeerStateKey).(*cm.PeerState)
		peerRoundState := peerState.GetRoundState()
		peerRoundStateStr := peer.Key + ":" + string(wire.JSONBytes(peerRoundState))
		peerRoundStates = append(peerRoundStates, peerRoundStateStr)
	}
	return FromRoundState(roundState), nil
}
Example #5
0
func (tx *BondTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
	wire.WriteTo([]byte(Fmt(`{"chain_id":%s`, jsonEscape(chainID))), w, n, err)
	wire.WriteTo([]byte(Fmt(`,"tx":[%v,{"inputs":[`, TxTypeBond)), w, n, err)
	for i, in := range tx.Inputs {
		in.WriteSignBytes(w, n, err)
		if i != len(tx.Inputs)-1 {
			wire.WriteTo([]byte(","), w, n, err)
		}
	}
	wire.WriteTo([]byte(Fmt(`],"pub_key":`)), w, n, err)
	wire.WriteTo(wire.JSONBytes(tx.PubKey), w, n, err)
	wire.WriteTo([]byte(`,"unbond_to":[`), w, n, err)
	for i, out := range tx.UnbondTo {
		out.WriteSignBytes(w, n, err)
		if i != len(tx.UnbondTo)-1 {
			wire.WriteTo([]byte(","), w, n, err)
		}
	}
	wire.WriteTo([]byte(`]}]}`), w, n, err)
}
Example #6
0
func Call(remote string, method string, params []interface{}, dest interface{}) (interface{}, error) {
	// Make request and get responseBytes
	request := RPCRequest{
		JSONRPC: "2.0",
		Method:  method,
		Params:  params,
		ID:      "",
	}
	requestBytes := wire.JSONBytes(request)
	requestBuf := bytes.NewBuffer(requestBytes)
	log.Info(Fmt("RPC request to %v: %v", remote, string(requestBytes)))
	httpResponse, err := http.Post(remote, "text/json", requestBuf)
	if err != nil {
		return dest, err
	}
	defer httpResponse.Body.Close()
	responseBytes, err := ioutil.ReadAll(httpResponse.Body)
	if err != nil {
		return dest, err
	}
	log.Info(Fmt("RPC response: %v", string(responseBytes)))

	// Parse response into JSONResponse
	response := RPCResponse{}
	err = json.Unmarshal(responseBytes, &response)
	if err != nil {
		return dest, err
	}
	// Parse response into dest
	resultJSONObject := response.Result
	errorStr := response.Error
	if errorStr != "" {
		return dest, errors.New(errorStr)
	}
	dest = wire.ReadJSONObject(dest, resultJSONObject, &err)
	return dest, err
}
Example #7
0
// Encode to a byte array.
func (this *TCodec) EncodeBytes(v interface{}) ([]byte, error) {
	return wire.JSONBytes(v), nil
}