tendermint/client/local_client.go

231 lines
5.9 KiB
Go
Raw Normal View History

2017-01-12 12:47:55 -08:00
package abcicli
2016-03-24 10:19:48 -07:00
import (
"sync"
2017-01-12 12:47:55 -08:00
types "github.com/tendermint/abci/types"
2017-04-21 15:25:13 -07:00
cmn "github.com/tendermint/tmlibs/common"
2016-03-24 10:19:48 -07:00
)
var _ Client = (*localClient)(nil)
2016-03-24 10:19:48 -07:00
type localClient struct {
2017-01-23 20:26:17 -08:00
cmn.BaseService
2016-03-24 10:19:48 -07:00
mtx *sync.Mutex
types.Application
Callback
}
func NewLocalClient(mtx *sync.Mutex, app types.Application) *localClient {
if mtx == nil {
mtx = new(sync.Mutex)
}
cli := &localClient{
2016-03-24 10:19:48 -07:00
mtx: mtx,
Application: app,
}
2017-04-27 13:37:18 -07:00
cli.BaseService = *cmn.NewBaseService(nil, "localClient", cli)
return cli
2016-03-24 10:19:48 -07:00
}
func (app *localClient) SetResponseCallback(cb Callback) {
app.mtx.Lock()
defer app.mtx.Unlock()
app.Callback = cb
}
// TODO: change types.Application to include Error()?
func (app *localClient) Error() error {
return nil
}
func (app *localClient) FlushAsync() *ReqRes {
// Do nothing
2016-05-13 23:22:32 -07:00
return newLocalReqRes(types.ToRequestFlush(), nil)
2016-03-24 10:19:48 -07:00
}
func (app *localClient) EchoAsync(msg string) *ReqRes {
return app.callback(
2016-05-13 23:22:32 -07:00
types.ToRequestEcho(msg),
types.ToResponseEcho(msg),
2016-03-24 10:19:48 -07:00
)
}
2017-09-22 08:10:39 -07:00
func (app *localClient) InfoAsync(req types.RequestInfo) *ReqRes {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.Info(types.ToParamsInfo(req))
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return app.callback(
2017-09-22 08:10:39 -07:00
types.ToRequestInfo(req),
types.ToResponseInfo(types.FromResultInfo(res)),
2016-03-24 10:19:48 -07:00
)
}
func (app *localClient) SetOptionAsync(req types.RequestSetOption) *ReqRes {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.SetOption(types.ToParamsSetOption(req))
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return app.callback(
types.ToRequestSetOption(req),
types.ToResponseSetOption(types.FromResultSetOption(res)),
2016-03-24 10:19:48 -07:00
)
}
2017-01-12 12:27:08 -08:00
func (app *localClient) DeliverTxAsync(tx []byte) *ReqRes {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
2017-01-12 12:27:08 -08:00
res := app.Application.DeliverTx(tx)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return app.callback(
2017-01-12 12:27:08 -08:00
types.ToRequestDeliverTx(tx),
types.ToResponseDeliverTx(types.FromResultDeliverTx(res)),
2016-03-24 10:19:48 -07:00
)
}
func (app *localClient) CheckTxAsync(tx []byte) *ReqRes {
app.mtx.Lock()
res := app.Application.CheckTx(tx)
app.mtx.Unlock()
return app.callback(
2016-05-13 23:22:32 -07:00
types.ToRequestCheckTx(tx),
types.ToResponseCheckTx(types.FromResultCheckTx(res)),
2016-03-24 10:19:48 -07:00
)
}
func (app *localClient) QueryAsync(req types.RequestQuery) *ReqRes {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.Query(types.ToParamsQuery(req))
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return app.callback(
types.ToRequestQuery(req),
types.ToResponseQuery(types.FromResultQuery(res)),
2016-03-24 10:19:48 -07:00
)
}
func (app *localClient) CommitAsync() *ReqRes {
app.mtx.Lock()
res := app.Application.Commit()
app.mtx.Unlock()
return app.callback(
2016-05-13 23:22:32 -07:00
types.ToRequestCommit(),
types.ToResponseCommit(types.FromResultCommit(res)),
2016-03-24 10:19:48 -07:00
)
}
func (app *localClient) InitChainAsync(req types.RequestInitChain) *ReqRes {
2016-03-26 22:35:23 -07:00
app.mtx.Lock()
res := app.Application.InitChain(types.ToParamsInitChain(req))
2016-03-26 22:35:23 -07:00
reqRes := app.callback(
types.ToRequestInitChain(req),
types.ToResponseInitChain(types.FromResultInitChain(res)),
2016-03-26 22:35:23 -07:00
)
app.mtx.Unlock()
return reqRes
}
func (app *localClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes {
2016-03-26 22:35:23 -07:00
app.mtx.Lock()
res := app.Application.BeginBlock(types.ToParamsBeginBlock(req))
2016-03-26 22:35:23 -07:00
app.mtx.Unlock()
return app.callback(
types.ToRequestBeginBlock(req),
types.ToResponseBeginBlock(types.FromResultBeginBlock(res)),
2016-03-26 22:35:23 -07:00
)
}
func (app *localClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes {
2016-03-26 22:35:23 -07:00
app.mtx.Lock()
res := app.Application.EndBlock(types.ToParamsEndBlock(req))
2016-03-26 22:35:23 -07:00
app.mtx.Unlock()
return app.callback(
types.ToRequestEndBlock(req),
types.ToResponseEndBlock(types.FromResultEndBlock(res)),
2016-03-26 22:35:23 -07:00
)
}
2016-03-24 10:19:48 -07:00
//-------------------------------------------------------
func (app *localClient) FlushSync() error {
return nil
}
func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) {
return &types.ResponseEcho{msg}, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := types.FromResultInfo(app.Application.Info(types.ToParamsInfo(req)))
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := types.FromResultSetOption(app.Application.SetOption(types.ToParamsSetOption(req)))
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := types.FromResultDeliverTx(app.Application.DeliverTx(tx))
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) CheckTxSync(tx []byte) (*types.ResponseCheckTx, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := types.FromResultCheckTx(app.Application.CheckTx(tx))
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := types.FromResultQuery(app.Application.Query(types.ToParamsQuery(req)))
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) CommitSync() (*types.ResponseCommit, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := types.FromResultCommit(app.Application.Commit())
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := types.FromResultInitChain(app.Application.InitChain(types.ToParamsInitChain(req)))
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) {
2016-03-26 22:35:23 -07:00
app.mtx.Lock()
res := types.FromResultBeginBlock(app.Application.BeginBlock(types.ToParamsBeginBlock(req)))
2016-03-26 22:35:23 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-26 22:35:23 -07:00
}
func (app *localClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := types.FromResultEndBlock(app.Application.EndBlock(types.ToParamsEndBlock(req)))
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
//-------------------------------------------------------
func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes {
app.Callback(req, res)
return newLocalReqRes(req, res)
}
func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes {
reqRes := NewReqRes(req)
reqRes.Response = res
reqRes.SetDone()
return reqRes
}