Browse Source

remove Result from the client package

plus make Client interface more consistent. All *Sync functions now
return an error as a second return param. Deliver/Check/Commit use Code
to indicate errors and have IsErr() func defined on ResponseXYZ structs.
pull/1780/head
Anton Kaliaev 7 years ago
parent
commit
fbe7234639
No known key found for this signature in database GPG Key ID: 7B6881D965918214
7 changed files with 136 additions and 185 deletions
  1. +10
    -10
      client/client.go
  2. +31
    -64
      client/grpc_client.go
  3. +26
    -26
      client/local_client.go
  4. +28
    -54
      client/socket_client.go
  5. +12
    -6
      cmd/abci-cli/abci-cli.go
  6. +22
    -25
      tests/test_app/app.go
  7. +7
    -0
      types/result.go

+ 10
- 10
client/client.go View File

@ -24,21 +24,21 @@ type Client interface {
CommitAsync() *ReqRes
FlushSync() error
EchoSync(msg string) (res types.Result)
InfoSync(types.RequestInfo) (resInfo types.ResponseInfo, err error)
SetOptionSync(key string, value string) (res types.Result)
DeliverTxSync(tx []byte) (res types.Result)
CheckTxSync(tx []byte) (res types.Result)
QuerySync(types.RequestQuery) (resQuery types.ResponseQuery, err error)
CommitSync() (res types.Result)
EchoSync(msg string) (*types.ResponseEcho, error)
InfoSync(types.RequestInfo) (*types.ResponseInfo, error)
SetOptionSync(key string, value string) (log string, err error)
DeliverTxSync(tx []byte) *types.ResponseDeliverTx
CheckTxSync(tx []byte) *types.ResponseCheckTx
QuerySync(types.RequestQuery) (*types.ResponseQuery, error)
CommitSync() *types.ResponseCommit
InitChainAsync(types.RequestInitChain) *ReqRes
BeginBlockAsync(types.RequestBeginBlock) *ReqRes
EndBlockAsync(height uint64) *ReqRes
InitChainSync(types.RequestInitChain) (err error)
BeginBlockSync(types.RequestBeginBlock) (err error)
EndBlockSync(height uint64) (resEndBlock types.ResponseEndBlock, err error)
InitChainSync(types.RequestInitChain) error
BeginBlockSync(types.RequestBeginBlock) error
EndBlockSync(height uint64) (*types.ResponseEndBlock, error)
}
//----------------------------------------


+ 31
- 64
client/grpc_client.go View File

@ -240,104 +240,71 @@ func (cli *grpcClient) finishAsyncCall(req *types.Request, res *types.Response)
return reqres
}
func (cli *grpcClient) checkErrGetResult() types.Result {
if err := cli.Error(); err != nil {
// StopForError should already have been called if error is set
return types.ErrInternalError.SetLog(err.Error())
}
return types.Result{}
}
//----------------------------------------
func (cli *grpcClient) EchoSync(msg string) (res types.Result) {
reqres := cli.EchoAsync(msg)
if res := cli.checkErrGetResult(); res.IsErr() {
return res
}
resp := reqres.Response.GetEcho()
return types.NewResultOK([]byte(resp.Message), "")
}
func (cli *grpcClient) FlushSync() error {
return nil
}
func (cli *grpcClient) InfoSync(req types.RequestInfo) (resInfo types.ResponseInfo, err error) {
func (cli *grpcClient) EchoSync(msg string) (*types.ResponseEcho, error) {
reqres := cli.EchoAsync(msg)
// StopForError should already have been called if error is set
return reqres.Response.GetEcho(), cli.Error()
}
func (cli *grpcClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
reqres := cli.InfoAsync(req)
if err = cli.Error(); err != nil {
return resInfo, err
}
if info := reqres.Response.GetInfo(); info != nil {
return *info, nil
}
return resInfo, nil
return reqres.Response.GetInfo(), cli.Error()
}
func (cli *grpcClient) SetOptionSync(key string, value string) (res types.Result) {
func (cli *grpcClient) SetOptionSync(key string, value string) (log string, err error) {
reqres := cli.SetOptionAsync(key, value)
if res := cli.checkErrGetResult(); res.IsErr() {
return res
if err := cli.Error(); err != nil {
return "", err
}
resp := reqres.Response.GetSetOption()
return types.Result{Code: OK, Data: nil, Log: resp.Log}
return reqres.Response.GetSetOption().Log, nil
}
func (cli *grpcClient) DeliverTxSync(tx []byte) (res types.Result) {
func (cli *grpcClient) DeliverTxSync(tx []byte) *types.ResponseDeliverTx {
reqres := cli.DeliverTxAsync(tx)
if res := cli.checkErrGetResult(); res.IsErr() {
return res
if err := cli.Error(); err != nil {
return &types.ResponseDeliverTx{Code: types.CodeType_InternalError, Log: err.Error()}
}
resp := reqres.Response.GetDeliverTx()
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
return reqres.Response.GetDeliverTx()
}
func (cli *grpcClient) CheckTxSync(tx []byte) (res types.Result) {
func (cli *grpcClient) CheckTxSync(tx []byte) *types.ResponseCheckTx {
reqres := cli.CheckTxAsync(tx)
if res := cli.checkErrGetResult(); res.IsErr() {
return res
if err := cli.Error(); err != nil {
return &types.ResponseCheckTx{Code: types.CodeType_InternalError, Log: err.Error()}
}
resp := reqres.Response.GetCheckTx()
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
return reqres.Response.GetCheckTx()
}
func (cli *grpcClient) QuerySync(reqQuery types.RequestQuery) (resQuery types.ResponseQuery, err error) {
reqres := cli.QueryAsync(reqQuery)
if err = cli.Error(); err != nil {
return resQuery, err
}
if resQuery_ := reqres.Response.GetQuery(); resQuery_ != nil {
return *resQuery_, nil
}
return resQuery, nil
func (cli *grpcClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) {
reqres := cli.QueryAsync(req)
return reqres.Response.GetQuery(), cli.Error()
}
func (cli *grpcClient) CommitSync() (res types.Result) {
func (cli *grpcClient) CommitSync() *types.ResponseCommit {
reqres := cli.CommitAsync()
if res := cli.checkErrGetResult(); res.IsErr() {
return res
if err := cli.Error(); err != nil {
return &types.ResponseCommit{Code: types.CodeType_InternalError, Log: err.Error()}
}
resp := reqres.Response.GetCommit()
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
return reqres.Response.GetCommit()
}
func (cli *grpcClient) InitChainSync(params types.RequestInitChain) (err error) {
func (cli *grpcClient) InitChainSync(params types.RequestInitChain) error {
cli.InitChainAsync(params)
return cli.Error()
}
func (cli *grpcClient) BeginBlockSync(params types.RequestBeginBlock) (err error) {
func (cli *grpcClient) BeginBlockSync(params types.RequestBeginBlock) error {
cli.BeginBlockAsync(params)
return cli.Error()
}
func (cli *grpcClient) EndBlockSync(height uint64) (resEndBlock types.ResponseEndBlock, err error) {
func (cli *grpcClient) EndBlockSync(height uint64) (*types.ResponseEndBlock, error) {
reqres := cli.EndBlockAsync(height)
if err := cli.Error(); err != nil {
return resEndBlock, err
}
if blk := reqres.Response.GetEndBlock(); blk != nil {
return *blk, nil
}
return resEndBlock, nil
return reqres.Response.GetEndBlock(), cli.Error()
}

+ 26
- 26
client/local_client.go View File

@ -146,71 +146,71 @@ func (app *localClient) FlushSync() error {
return nil
}
func (app *localClient) EchoSync(msg string) (res types.Result) {
return types.OK.SetData([]byte(msg))
func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) {
return &types.ResponseEcho{msg}, nil
}
func (app *localClient) InfoSync(req types.RequestInfo) (resInfo types.ResponseInfo, err error) {
func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
app.mtx.Lock()
defer app.mtx.Unlock()
resInfo = app.Application.Info(req)
return resInfo, nil
res := app.Application.Info(req)
app.mtx.Unlock()
return &res, nil
}
func (app *localClient) SetOptionSync(key string, value string) (res types.Result) {
func (app *localClient) SetOptionSync(key string, value string) (log string, err error) {
app.mtx.Lock()
log := app.Application.SetOption(key, value)
log = app.Application.SetOption(key, value)
app.mtx.Unlock()
return types.OK.SetLog(log)
return log, nil
}
func (app *localClient) DeliverTxSync(tx []byte) (res types.ResponseDeliverTx) {
func (app *localClient) DeliverTxSync(tx []byte) *types.ResponseDeliverTx {
app.mtx.Lock()
res = app.Application.DeliverTx(tx)
res := app.Application.DeliverTx(tx)
app.mtx.Unlock()
return res
return &res
}
func (app *localClient) CheckTxSync(tx []byte) (res types.ResponseCheckTx) {
func (app *localClient) CheckTxSync(tx []byte) *types.ResponseCheckTx {
app.mtx.Lock()
res = app.Application.CheckTx(tx)
res := app.Application.CheckTx(tx)
app.mtx.Unlock()
return res
return &res
}
func (app *localClient) QuerySync(reqQuery types.RequestQuery) (resQuery types.ResponseQuery, err error) {
func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) {
app.mtx.Lock()
resQuery = app.Application.Query(reqQuery)
res := app.Application.Query(req)
app.mtx.Unlock()
return resQuery, nil
return &res, nil
}
func (app *localClient) CommitSync() (res types.ResponseCommit) {
func (app *localClient) CommitSync() *types.ResponseCommit {
app.mtx.Lock()
res = app.Application.Commit()
res := app.Application.Commit()
app.mtx.Unlock()
return res
return &res
}
func (app *localClient) InitChainSync(params types.RequestInitChain) (err error) {
func (app *localClient) InitChainSync(params types.RequestInitChain) error {
app.mtx.Lock()
app.Application.InitChain(params)
app.mtx.Unlock()
return nil
}
func (app *localClient) BeginBlockSync(params types.RequestBeginBlock) (err error) {
func (app *localClient) BeginBlockSync(params types.RequestBeginBlock) error {
app.mtx.Lock()
app.Application.BeginBlock(params)
app.mtx.Unlock()
return nil
}
func (app *localClient) EndBlockSync(height uint64) (resEndBlock types.ResponseEndBlock, err error) {
func (app *localClient) EndBlockSync(height uint64) (*types.ResponseEndBlock, error) {
app.mtx.Lock()
resEndBlock = app.Application.EndBlock(height)
res := app.Application.EndBlock(height)
app.mtx.Unlock()
return resEndBlock, nil
return &res, nil
}
//-------------------------------------------------------


+ 28
- 54
client/socket_client.go View File

@ -271,111 +271,85 @@ func (cli *socketClient) EndBlockAsync(height uint64) *ReqRes {
//----------------------------------------
func (cli *socketClient) EchoSync(msg string) (res types.Result) {
reqres := cli.queueRequest(types.ToRequestEcho(msg))
cli.FlushSync()
if err := cli.Error(); err != nil {
return types.ErrInternalError.SetLog(err.Error())
}
resp := reqres.Response.GetEcho()
return types.Result{Code: OK, Data: []byte(resp.Message)}
}
func (cli *socketClient) FlushSync() error {
reqRes := cli.queueRequest(types.ToRequestFlush())
if err := cli.Error(); err != nil {
return types.ErrInternalError.SetLog(err.Error())
return err
}
reqRes.Wait() // NOTE: if we don't flush the queue, its possible to get stuck here
return cli.Error()
}
func (cli *socketClient) InfoSync(req types.RequestInfo) (resInfo types.ResponseInfo, err error) {
func (cli *socketClient) EchoSync(msg string) (*types.ResponseEcho, error) {
reqres := cli.queueRequest(types.ToRequestEcho(msg))
cli.FlushSync()
return reqres.Response.GetEcho(), cli.Error()
}
func (cli *socketClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
reqres := cli.queueRequest(types.ToRequestInfo(req))
cli.FlushSync()
if err := cli.Error(); err != nil {
return resInfo, err
}
if resInfo_ := reqres.Response.GetInfo(); resInfo_ != nil {
return *resInfo_, nil
}
return resInfo, nil
return reqres.Response.GetInfo(), cli.Error()
}
func (cli *socketClient) SetOptionSync(key string, value string) (res types.Result) {
func (cli *socketClient) SetOptionSync(key string, value string) (log string, err error) {
reqres := cli.queueRequest(types.ToRequestSetOption(key, value))
cli.FlushSync()
if err := cli.Error(); err != nil {
return types.ErrInternalError.SetLog(err.Error())
return "", err
}
resp := reqres.Response.GetSetOption()
return types.Result{Code: OK, Data: nil, Log: resp.Log}
return reqres.Response.GetSetOption().Log, nil
}
func (cli *socketClient) DeliverTxSync(tx []byte) (res types.Result) {
func (cli *socketClient) DeliverTxSync(tx []byte) *types.ResponseDeliverTx {
reqres := cli.queueRequest(types.ToRequestDeliverTx(tx))
cli.FlushSync()
if err := cli.Error(); err != nil {
return types.ErrInternalError.SetLog(err.Error())
return &types.ResponseDeliverTx{Code: types.CodeType_InternalError, Log: err.Error()}
}
resp := reqres.Response.GetDeliverTx()
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
return reqres.Response.GetDeliverTx()
}
func (cli *socketClient) CheckTxSync(tx []byte) (res types.Result) {
func (cli *socketClient) CheckTxSync(tx []byte) *types.ResponseCheckTx {
reqres := cli.queueRequest(types.ToRequestCheckTx(tx))
cli.FlushSync()
if err := cli.Error(); err != nil {
return types.ErrInternalError.SetLog(err.Error())
return &types.ResponseCheckTx{Code: types.CodeType_InternalError, Log: err.Error()}
}
resp := reqres.Response.GetCheckTx()
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
return reqres.Response.GetCheckTx()
}
func (cli *socketClient) QuerySync(reqQuery types.RequestQuery) (resQuery types.ResponseQuery, err error) {
reqres := cli.queueRequest(types.ToRequestQuery(reqQuery))
func (cli *socketClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) {
reqres := cli.queueRequest(types.ToRequestQuery(req))
cli.FlushSync()
if err := cli.Error(); err != nil {
return resQuery, err
}
if resQuery_ := reqres.Response.GetQuery(); resQuery_ != nil {
return *resQuery_, nil
}
return resQuery, nil
return reqres.Response.GetQuery(), cli.Error()
}
func (cli *socketClient) CommitSync() (res types.Result) {
func (cli *socketClient) CommitSync() *types.ResponseCommit {
reqres := cli.queueRequest(types.ToRequestCommit())
cli.FlushSync()
if err := cli.Error(); err != nil {
return types.ErrInternalError.SetLog(err.Error())
return &types.ResponseCommit{Code: types.CodeType_InternalError, Log: err.Error()}
}
resp := reqres.Response.GetCommit()
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
return reqres.Response.GetCommit()
}
func (cli *socketClient) InitChainSync(params types.RequestInitChain) (err error) {
func (cli *socketClient) InitChainSync(params types.RequestInitChain) error {
cli.queueRequest(types.ToRequestInitChain(params))
cli.FlushSync()
return cli.Error()
}
func (cli *socketClient) BeginBlockSync(params types.RequestBeginBlock) (err error) {
func (cli *socketClient) BeginBlockSync(params types.RequestBeginBlock) error {
cli.queueRequest(types.ToRequestBeginBlock(params))
cli.FlushSync()
return cli.Error()
}
func (cli *socketClient) EndBlockSync(height uint64) (resEndBlock types.ResponseEndBlock, err error) {
func (cli *socketClient) EndBlockSync(height uint64) (*types.ResponseEndBlock, error) {
reqres := cli.queueRequest(types.ToRequestEndBlock(height))
cli.FlushSync()
if err := cli.Error(); err != nil {
return resEndBlock, err
}
if blk := reqres.Response.GetEndBlock(); blk != nil {
return *blk, nil
}
return resEndBlock, nil
return reqres.Response.GetEndBlock(), cli.Error()
}
//----------------------------------------


+ 12
- 6
cmd/abci-cli/abci-cli.go View File

@ -327,9 +327,12 @@ func cmdConsole(cmd *cobra.Command, args []string) error {
// Have the application echo a message
func cmdEcho(cmd *cobra.Command, args []string) error {
resEcho := client.EchoSync(args[0])
res, err := client.EchoSync(args[0])
if err != nil {
return err
}
printResponse(cmd, args, response{
Data: resEcho.Data,
Data: []byte(res.Message),
})
return nil
}
@ -340,21 +343,24 @@ func cmdInfo(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
version = args[0]
}
resInfo, err := client.InfoSync(types.RequestInfo{version})
res, err := client.InfoSync(types.RequestInfo{version})
if err != nil {
return err
}
printResponse(cmd, args, response{
Data: []byte(resInfo.Data),
Data: []byte(res.Data),
})
return nil
}
// Set an option on the application
func cmdSetOption(cmd *cobra.Command, args []string) error {
resSetOption := client.SetOptionSync(args[0], args[1])
log, err := client.SetOptionSync(args[0], args[1])
if err != nil {
return err
}
printResponse(cmd, args, response{
Log: resSetOption.Log,
Log: log,
})
return nil
}


+ 22
- 25
tests/test_app/app.go View File

@ -23,7 +23,7 @@ func startApp(abciApp string) *process.Process {
os.Stdout,
)
if err != nil {
panic("running abci_app: " + err.Error())
panicf("running abci_app: %v", err)
}
// TODO a better way to handle this?
@ -41,57 +41,54 @@ func startClient(abciType string) abcicli.Client {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
client.SetLogger(logger.With("module", "abcicli"))
if _, err := client.Start(); err != nil {
panic("connecting to abci_app: " + err.Error())
panicf("connecting to abci_app: %v", err.Error())
}
return client
}
func setOption(client abcicli.Client, key, value string) {
res := client.SetOptionSync(key, value)
_, _, log := res.Code, res.Data, res.Log
if res.IsErr() {
panic(fmt.Sprintf("setting %v=%v: \nlog: %v", key, value, log))
_, err := client.SetOptionSync(key, value)
if err != nil {
panicf("setting %v=%v: \nerr: %v", key, value, err)
}
}
func commit(client abcicli.Client, hashExp []byte) {
res := client.CommitSync()
_, data, _ := res.Code, res.Data, res.Log
if res.IsErr() {
panic(fmt.Sprintf("committing err %v\n", res))
panicf("committing err %v\n", res)
}
if !bytes.Equal(res.Data, hashExp) {
panic(fmt.Sprintf("Commit hash was unexpected. Got %X expected %X",
data, hashExp))
panicf("Commit hash was unexpected. Got %X expected %X", res.Data, hashExp)
}
}
func deliverTx(client abcicli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
res := client.DeliverTxSync(txBytes)
code, data, log := res.Code, res.Data, res.Log
if code != codeExp {
panic(fmt.Sprintf("DeliverTx response code was unexpected. Got %v expected %v. Log: %v",
code, codeExp, log))
if res.Code != codeExp {
panicf("DeliverTx response code was unexpected. Got %v expected %v. Log: %v", res.Code, codeExp, res.Log)
}
if !bytes.Equal(data, dataExp) {
panic(fmt.Sprintf("DeliverTx response data was unexpected. Got %X expected %X",
data, dataExp))
if !bytes.Equal(res.Data, dataExp) {
panicf("DeliverTx response data was unexpected. Got %X expected %X", res.Data, dataExp)
}
}
/*func checkTx(client abcicli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
res := client.CheckTxSync(txBytes)
code, data, log := res.Code, res.Data, res.Log
if res.IsErr() {
panic(fmt.Sprintf("checking tx %X: %v\nlog: %v", txBytes, log))
panicf("checking tx %X: %v\nlog: %v", txBytes, res.Log)
}
if code != codeExp {
panic(fmt.Sprintf("CheckTx response code was unexpected. Got %v expected %v. Log: %v",
code, codeExp, log))
if res.Code != codeExp {
panicf("CheckTx response code was unexpected. Got %v expected %v. Log: %v",
res.Code, codeExp, res.Log)
}
if !bytes.Equal(data, dataExp) {
panic(fmt.Sprintf("CheckTx response data was unexpected. Got %X expected %X",
data, dataExp))
if !bytes.Equal(res.Data, dataExp) {
panicf("CheckTx response data was unexpected. Got %X expected %X",
res.Data, dataExp)
}
}*/
func panicf(format string, a ...interface{}) {
panic(fmt.Sprintf(format, a))
}

+ 7
- 0
types/result.go View File

@ -106,6 +106,7 @@ func (r *ResponseCheckTx) Result() Result {
}
}
// IsErr returns true if Code is something other than OK.
func (r ResponseCheckTx) IsErr() bool {
return r.Code != CodeType_OK
}
@ -120,6 +121,7 @@ func (r *ResponseDeliverTx) Result() Result {
}
}
// IsErr returns true if Code is something other than OK.
func (r ResponseDeliverTx) IsErr() bool {
return r.Code != CodeType_OK
}
@ -145,3 +147,8 @@ func (r *ResponseQuery) Result() *ResultQuery {
Log: r.Log,
}
}
// IsErr returns true if Code is something other than OK.
func (r ResponseCommit) IsErr() bool {
return r.Code != CodeType_OK
}

Loading…
Cancel
Save