From f288e58c8181d3053b5b8aca017f885a812849e9 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 20 Dec 2015 08:18:39 -0800 Subject: [PATCH] Prefix TMSP messages with length --- consensus/common_test.go | 2 +- mempool/mempool_test.go | 2 +- node/node_test.go | 2 +- proxy/remote_app_context.go | 14 +++++++++++--- proxy/remote_app_context_test.go | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/consensus/common_test.go b/consensus/common_test.go index 94b30dd68..c0b82c3cf 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -16,7 +16,7 @@ import ( sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" - "github.com/tendermint/tmsp/example" + "github.com/tendermint/tmsp/example/golang" ) var chainID string diff --git a/mempool/mempool_test.go b/mempool/mempool_test.go index a3c5cff01..bdf326fa4 100644 --- a/mempool/mempool_test.go +++ b/mempool/mempool_test.go @@ -6,7 +6,7 @@ import ( "github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/types" - "github.com/tendermint/tmsp/example" + "github.com/tendermint/tmsp/example/golang" tmsp "github.com/tendermint/tmsp/types" ) diff --git a/node/node_test.go b/node/node_test.go index 49e7d3ddf..135d5444b 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -7,7 +7,7 @@ import ( . "github.com/tendermint/go-common" "github.com/tendermint/go-p2p" _ "github.com/tendermint/tendermint/config/tendermint_test" - "github.com/tendermint/tmsp/example" + "github.com/tendermint/tmsp/example/golang" "github.com/tendermint/tmsp/server" ) diff --git a/proxy/remote_app_context.go b/proxy/remote_app_context.go index dd96abe74..df638f3e5 100644 --- a/proxy/remote_app_context.go +++ b/proxy/remote_app_context.go @@ -2,6 +2,7 @@ package proxy import ( "bufio" + "bytes" "container/list" "errors" "fmt" @@ -83,7 +84,7 @@ func (app *remoteAppContext) Error() error { func (app *remoteAppContext) sendRequestsRoutine() { for { - var n int + var n, n2 int var err error select { case <-app.QuitService.Quit: @@ -92,7 +93,10 @@ func (app *remoteAppContext) sendRequestsRoutine() { app.willSendReq(reqres) - wire.WriteBinary(reqres.Request, app.bufWriter, &n, &err) + buf := new(bytes.Buffer) + wire.WriteBinary(reqres.Request, buf, &n2, &err) // Length prefix + wire.WriteVarint(buf.Len(), app.bufWriter, &n, &err) + wire.WriteTo(buf.Bytes(), app.bufWriter, &n, &err) if err != nil { app.StopForError(err) return @@ -113,13 +117,17 @@ func (app *remoteAppContext) recvResponseRoutine() { r := bufio.NewReader(app.conn) // Buffer reads for { var res tmsp.Response - var n int + var n, n2 int var err error + size := wire.ReadVarint(r, &n2, &err) wire.ReadBinaryPtr(&res, r, maxResponseSize, &n, &err) if err != nil { app.StopForError(err) return } + if size != n { + app.StopForError(fmt.Errorf("Prefixed length not equal to actual length. %v vs %v", size, n)) + } switch res := res.(type) { case tmsp.ResponseException: app.StopForError(errors.New(res.Error)) diff --git a/proxy/remote_app_context_test.go b/proxy/remote_app_context_test.go index 15e97eee7..1242b6c83 100644 --- a/proxy/remote_app_context_test.go +++ b/proxy/remote_app_context_test.go @@ -7,7 +7,7 @@ import ( . "github.com/tendermint/go-common" "github.com/tendermint/go-logio" - example "github.com/tendermint/tmsp/example" + "github.com/tendermint/tmsp/example/golang" "github.com/tendermint/tmsp/server" )