mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-27 19:37:08 +00:00
types: add note about ReadMessage having no cap
This commit is contained in:
@@ -75,8 +75,6 @@ func (BaseApplication) EndBlock(req RequestEndBlock) ResponseEndBlock {
|
||||
|
||||
//-------------------------------------------------------
|
||||
|
||||
var _ Application = (*GRPCApplication)(nil)
|
||||
|
||||
// GRPCApplication is a GRPC wrapper for Application
|
||||
type GRPCApplication struct {
|
||||
app Application
|
||||
|
||||
+25
-25
@@ -7,6 +7,31 @@ import (
|
||||
wire "github.com/tendermint/go-wire"
|
||||
)
|
||||
|
||||
// WriteMessage writes a length-delimited protobuf message.
|
||||
func WriteMessage(msg proto.Message, w io.Writer) error {
|
||||
bz, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var n int
|
||||
wire.WriteByteSlice(bz, w, &n, &err)
|
||||
return err
|
||||
}
|
||||
|
||||
// ReadMessage reads a length delimited protobuf message.
|
||||
func ReadMessage(r io.Reader, msg proto.Message) error {
|
||||
var n int
|
||||
var err error
|
||||
bz := wire.ReadByteSlice(r, 0, &n, &err) //XXX: no max
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = proto.Unmarshal(bz, msg)
|
||||
return err
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
func ToRequestEcho(message string) *Request {
|
||||
return &Request{
|
||||
Value: &Request_Echo{&RequestEcho{message}},
|
||||
@@ -146,28 +171,3 @@ func ToResponseEndBlock(res ResponseEndBlock) *Response {
|
||||
Value: &Response_EndBlock{&res},
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
// Write proto message, length delimited
|
||||
func WriteMessage(msg proto.Message, w io.Writer) error {
|
||||
bz, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var n int
|
||||
wire.WriteByteSlice(bz, w, &n, &err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Read proto message, length delimited
|
||||
func ReadMessage(r io.Reader, msg proto.Message) error {
|
||||
var n int
|
||||
var err error
|
||||
bz := wire.ReadByteSlice(r, 0, &n, &err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = proto.Unmarshal(bz, msg)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWriteReadMessage(t *testing.T) {
|
||||
cases := []proto.Message{
|
||||
&RequestEcho{"hello"},
|
||||
// TODO: add the rest
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
buf := new(bytes.Buffer)
|
||||
err := WriteMessage(c, buf)
|
||||
assert.Nil(t, err)
|
||||
|
||||
msg := new(RequestEcho)
|
||||
err = ReadMessage(buf, msg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, c, msg)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user