From 55e2ce9de26688c7674d6aed8dbe0405de9dfb98 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 20 Mar 2016 17:10:13 -0700 Subject: [PATCH] Group (code,data,log) return values into types.Result --- example/counter/counter.go | 24 ++++++++++++++++-------- example/dummy/dummy.go | 12 ++++++------ example/nil/nil_app.go | 12 ++++++------ server/server.go | 12 ++++++------ types/application.go | 6 +++--- types/result.go | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 types/result.go diff --git a/example/counter/counter.go b/example/counter/counter.go index f97786f8e..d6b9da2e5 100644 --- a/example/counter/counter.go +++ b/example/counter/counter.go @@ -29,29 +29,37 @@ func (app *CounterApplication) SetOption(key string, value string) (log string) return "" } -func (app *CounterApplication) AppendTx(tx []byte) (code types.CodeType, result []byte, log string) { +func (app *CounterApplication) AppendTx(tx []byte) types.Result { if app.serial { tx8 := make([]byte, 8) copy(tx8[len(tx8)-len(tx):], tx) txValue := binary.BigEndian.Uint64(tx8) if txValue != uint64(app.txCount) { - return types.CodeType_BadNonce, nil, fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue) + return types.Result{ + Code: types.CodeType_BadNonce, + Data: nil, + Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue), + } } } app.txCount += 1 - return types.CodeType_OK, nil, "" + return types.NewResultOK(nil, "") } -func (app *CounterApplication) CheckTx(tx []byte) (code types.CodeType, result []byte, log string) { +func (app *CounterApplication) CheckTx(tx []byte) types.Result { if app.serial { tx8 := make([]byte, 8) copy(tx8[len(tx8)-len(tx):], tx) txValue := binary.BigEndian.Uint64(tx8) if txValue < uint64(app.txCount) { - return types.CodeType_BadNonce, nil, fmt.Sprintf("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue) + return types.Result{ + Code: types.CodeType_BadNonce, + Data: nil, + Log: fmt.Sprintf("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue), + } } } - return types.CodeType_OK, nil, "" + return types.NewResultOK(nil, "") } func (app *CounterApplication) Commit() (hash []byte, log string) { @@ -66,6 +74,6 @@ func (app *CounterApplication) Commit() (hash []byte, log string) { } } -func (app *CounterApplication) Query(query []byte) (code types.CodeType, result []byte, log string) { - return types.CodeType_OK, nil, fmt.Sprintf("Query is not supported") +func (app *CounterApplication) Query(query []byte) types.Result { + return types.NewResultOK(nil, fmt.Sprintf("Query is not supported")) } diff --git a/example/dummy/dummy.go b/example/dummy/dummy.go index dc4441d9c..e2d4a09ec 100644 --- a/example/dummy/dummy.go +++ b/example/dummy/dummy.go @@ -28,18 +28,18 @@ func (app *DummyApplication) SetOption(key string, value string) (log string) { return "" } -func (app *DummyApplication) AppendTx(tx []byte) (code types.CodeType, result []byte, log string) { +func (app *DummyApplication) AppendTx(tx []byte) types.Result { parts := strings.Split(string(tx), "=") if len(parts) == 2 { app.state.Set([]byte(parts[0]), []byte(parts[1])) } else { app.state.Set(tx, tx) } - return types.CodeType_OK, nil, "" + return types.NewResultOK(nil, "") } -func (app *DummyApplication) CheckTx(tx []byte) (code types.CodeType, result []byte, log string) { - return types.CodeType_OK, nil, "" +func (app *DummyApplication) CheckTx(tx []byte) types.Result { + return types.NewResultOK(nil, "") } func (app *DummyApplication) Commit() (hash []byte, log string) { @@ -47,8 +47,8 @@ func (app *DummyApplication) Commit() (hash []byte, log string) { return hash, "" } -func (app *DummyApplication) Query(query []byte) (code types.CodeType, result []byte, log string) { +func (app *DummyApplication) Query(query []byte) types.Result { index, value, exists := app.state.Get(query) resStr := Fmt("Index=%v value=%v exists=%v", index, string(value), exists) - return types.CodeType_OK, []byte(resStr), "" + return types.NewResultOK([]byte(resStr), "") } diff --git a/example/nil/nil_app.go b/example/nil/nil_app.go index f417862be..6233ea5e0 100644 --- a/example/nil/nil_app.go +++ b/example/nil/nil_app.go @@ -19,18 +19,18 @@ func (app *NilApplication) SetOption(key string, value string) (log string) { return "" } -func (app *NilApplication) AppendTx(tx []byte) (code types.CodeType, result []byte, log string) { - return types.CodeType_OK, nil, "" +func (app *NilApplication) AppendTx(tx []byte) types.Result { + return types.NewResultOK(nil, "") } -func (app *NilApplication) CheckTx(tx []byte) (code types.CodeType, result []byte, log string) { - return types.CodeType_OK, nil, "" +func (app *NilApplication) CheckTx(tx []byte) types.Result { + return types.NewResultOK(nil, "") } func (app *NilApplication) Commit() (hash []byte, log string) { return []byte("nil"), "" } -func (app *NilApplication) Query(query []byte) (code types.CodeType, result []byte, log string) { - return types.CodeType_OK, nil, "" +func (app *NilApplication) Query(query []byte) types.Result { + return types.NewResultOK(nil, "") } diff --git a/server/server.go b/server/server.go index afef937f1..edc65989b 100644 --- a/server/server.go +++ b/server/server.go @@ -137,17 +137,17 @@ func (s *Server) handleRequest(req *types.Request, responses chan<- *types.Respo logStr := s.app.SetOption(req.Key, req.Value) responses <- types.ResponseSetOption(logStr) case types.MessageType_AppendTx: - code, result, logStr := s.app.AppendTx(req.Data) - responses <- types.ResponseAppendTx(code, result, logStr) + res := s.app.AppendTx(req.Data) + responses <- types.ResponseAppendTx(res.Code, res.Data, res.Log) case types.MessageType_CheckTx: - code, result, logStr := s.app.CheckTx(req.Data) - responses <- types.ResponseCheckTx(code, result, logStr) + res := s.app.CheckTx(req.Data) + responses <- types.ResponseCheckTx(res.Code, res.Data, res.Log) case types.MessageType_Commit: hash, logStr := s.app.Commit() responses <- types.ResponseCommit(hash, logStr) case types.MessageType_Query: - code, result, logStr := s.app.Query(req.Data) - responses <- types.ResponseQuery(code, result, logStr) + res := s.app.Query(req.Data) + responses <- types.ResponseQuery(res.Code, res.Data, res.Log) case types.MessageType_InitChain: if app, ok := s.app.(types.BlockchainAware); ok { app.InitChain(req.Validators) diff --git a/types/application.go b/types/application.go index 79c1f9fd0..1d65ba8bf 100644 --- a/types/application.go +++ b/types/application.go @@ -10,16 +10,16 @@ type Application interface { SetOption(key string, value string) (log string) // Append a tx - AppendTx(tx []byte) (code CodeType, result []byte, log string) + AppendTx(tx []byte) Result // Validate a tx for the mempool - CheckTx(tx []byte) (code CodeType, result []byte, log string) + CheckTx(tx []byte) Result // Return the application Merkle root hash Commit() (hash []byte, log string) // Query for state - Query(query []byte) (code CodeType, result []byte, log string) + Query(query []byte) Result } // Some applications can choose to implement BlockchainAware diff --git a/types/result.go b/types/result.go new file mode 100644 index 000000000..48af9f392 --- /dev/null +++ b/types/result.go @@ -0,0 +1,37 @@ +package types + +import ( + "fmt" +) + +type Result struct { + Code CodeType + Data []byte + Log string // Can be non-deterministic +} + +func NewResult(code CodeType, data []byte, log string) Result { + return Result{ + Code: code, + Data: data, + Log: log, + } +} + +func (res Result) IsOK() bool { + return res.Code == CodeType_OK +} + +func (res Result) Error() string { + return fmt.Sprintf("TMSP error code:%v, data:%X, log:%v", res.Code, res.Data, res.Log) +} + +//---------------------------------------- + +func NewResultOK(data []byte, log string) Result { + return Result{ + Code: CodeType_OK, + Data: data, + Log: log, + } +}