diff --git a/glide.lock b/glide.lock index 805b7b256..e79c02920 100644 --- a/glide.lock +++ b/glide.lock @@ -1,10 +1,14 @@ -hash: 1fbe2d780f4901d78d2e986a694e6335ae07715317c7d680db64e1d69f08aff7 -updated: 2018-01-05T22:07:26.119496005-05:00 +hash: e0a7a8b1023ecddcba757af1136c2a8b642909026252d6cde389c0120f283abb +updated: 2018-01-14T18:26:26.193727554-05:00 imports: - name: github.com/btcsuite/btcd version: c7588cbf7690cd9f047a28efa2dcd8f2435a4e5e subpackages: - btcec +- name: github.com/davecgh/go-spew + version: 04cdfd42973bb9c8589fd6a731800cf222fde1a9 + subpackages: + - spew - name: github.com/go-kit/kit version: e2b298466b32c7cd5579a9b9b07e968fc9d9452c subpackages: @@ -67,15 +71,15 @@ imports: - edwards25519 - extra25519 - name: github.com/tendermint/go-crypto - version: 3ebe3250ff67e8af92f00ec3dc427c14a7eb1066 + version: 32741be2126500d600cede1e2016bbbe2754cb46 - name: github.com/tendermint/go-wire - version: 27be46e25124ddf775e23317a83647ce62a93f6b + version: b93ebdd4f306833936c243561ec30af3455dc764 subpackages: - data - name: github.com/tendermint/iavl - version: ed0bbf0acc51e75f72c76099293113c2d67199b9 + version: 1dfe265ab4b491418e88e1da6577a8ad594fc989 - name: github.com/tendermint/tmlibs - version: 1afc0340069d38e71f137bc9e4fa4fadf12f76df + version: f2bc5432c31974bb3f5b1a80a53c38e37d481b8d subpackages: - common - db @@ -131,16 +135,12 @@ imports: - tap - transport testImports: -- name: github.com/davecgh/go-spew - version: 04cdfd42973bb9c8589fd6a731800cf222fde1a9 - subpackages: - - spew - name: github.com/pmezard/go-difflib version: d8ed2627bdf02c080bf22230dbb337003b7aba2d subpackages: - difflib - name: github.com/stretchr/testify - version: 2aa2c176b9dab406a6970f6a55f513e8a8c8b18f + version: b91bfb9ebec76498946beb6af7c0230c7cc7ba6c subpackages: - assert - require diff --git a/glide.yaml b/glide.yaml index 89b38239a..3923b1cdd 100644 --- a/glide.yaml +++ b/glide.yaml @@ -7,15 +7,15 @@ import: - package: github.com/spf13/cobra version: v0.0.1 - package: github.com/tendermint/go-crypto - version: develop + version: sdk2 - package: github.com/tendermint/go-wire - version: develop + version: sdk2 subpackages: - data - package: github.com/tendermint/iavl version: sdk2 - package: github.com/tendermint/tmlibs - version: develop + version: sdk2 subpackages: - common - db diff --git a/types/messages.go b/types/messages.go index 5ce234cf1..be49461ad 100644 --- a/types/messages.go +++ b/types/messages.go @@ -1,33 +1,47 @@ package types import ( + "bufio" + "encoding/binary" "io" "github.com/gogo/protobuf/proto" wire "github.com/tendermint/go-wire" ) -// WriteMessage writes a length-delimited protobuf message. +const ( + maxMsgSize = 104857600 // 100MB +) + +// WriteMessage writes a varint 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 + return wire.EncodeByteSlice(w, bz) } -// ReadMessage reads a length delimited protobuf message. +// ReadMessage reads a varint 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 + return readProtoMsg(r, msg, maxMsgSize) +} + +func readProtoMsg(r io.Reader, msg proto.Message, maxSize int) error { + reader := bufio.NewReader(r) + length64, err := binary.ReadVarint(reader) if err != nil { return err } - err = proto.Unmarshal(bz, msg) - return err + length := int(length64) + if length < 0 || length > maxSize { + return io.ErrShortBuffer + } + buf := make([]byte, length) + if _, err := io.ReadFull(reader, buf); err != nil { + return err + } + return proto.Unmarshal(buf, msg) } //---------------------------------------- diff --git a/types/messages_test.go b/types/messages_test.go index 34d958e7d..990a81483 100644 --- a/types/messages_test.go +++ b/types/messages_test.go @@ -32,6 +32,26 @@ func TestMarshalJSON(t *testing.T) { assert.Equal(t, r1, r2) } +func TestWriteReadMessageSimple(t *testing.T) { + cases := []proto.Message{ + &RequestEcho{ + Message: "Hello", + }, + } + + 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) + } +} + func TestWriteReadMessage(t *testing.T) { cases := []proto.Message{ &Header{