mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-02 22:27:19 +00:00
Use tmsp.Client interface
This commit is contained in:
+1
-16
@@ -2,23 +2,8 @@ package proxy
|
||||
|
||||
import (
|
||||
tmspcli "github.com/tendermint/tmsp/client"
|
||||
tmsp "github.com/tendermint/tmsp/types"
|
||||
)
|
||||
|
||||
type AppConn interface {
|
||||
SetResponseCallback(tmspcli.Callback)
|
||||
Error() error
|
||||
|
||||
EchoAsync(msg string) *tmspcli.ReqRes
|
||||
FlushAsync() *tmspcli.ReqRes
|
||||
AppendTxAsync(tx []byte) *tmspcli.ReqRes
|
||||
CheckTxAsync(tx []byte) *tmspcli.ReqRes
|
||||
CommitAsync() *tmspcli.ReqRes
|
||||
SetOptionAsync(key string, value string) *tmspcli.ReqRes
|
||||
|
||||
InfoSync() (info string, err error)
|
||||
FlushSync() error
|
||||
CommitSync() (res tmsp.Result)
|
||||
InitChainSync(validators []*tmsp.Validator) (err error)
|
||||
EndBlockSync(height uint64) (changedValidators []*tmsp.Validator, err error)
|
||||
tmspcli.Client
|
||||
}
|
||||
|
||||
@@ -1,133 +0,0 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
tmspcli "github.com/tendermint/tmsp/client"
|
||||
tmsp "github.com/tendermint/tmsp/types"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type localAppConn struct {
|
||||
mtx *sync.Mutex
|
||||
tmsp.Application
|
||||
tmspcli.Callback
|
||||
}
|
||||
|
||||
func NewLocalAppConn(mtx *sync.Mutex, app tmsp.Application) *localAppConn {
|
||||
return &localAppConn{
|
||||
mtx: mtx,
|
||||
Application: app,
|
||||
}
|
||||
}
|
||||
|
||||
func (app *localAppConn) SetResponseCallback(cb tmspcli.Callback) {
|
||||
app.mtx.Lock()
|
||||
defer app.mtx.Unlock()
|
||||
app.Callback = cb
|
||||
}
|
||||
|
||||
// TODO: change tmsp.Application to include Error()?
|
||||
func (app *localAppConn) Error() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *localAppConn) EchoAsync(msg string) *tmspcli.ReqRes {
|
||||
return app.callback(
|
||||
tmsp.RequestEcho(msg),
|
||||
tmsp.ResponseEcho(msg),
|
||||
)
|
||||
}
|
||||
|
||||
func (app *localAppConn) FlushAsync() *tmspcli.ReqRes {
|
||||
// Do nothing
|
||||
return NewReqRes(tmsp.RequestFlush(), nil)
|
||||
}
|
||||
|
||||
func (app *localAppConn) SetOptionAsync(key string, value string) *tmspcli.ReqRes {
|
||||
app.mtx.Lock()
|
||||
log := app.Application.SetOption(key, value)
|
||||
app.mtx.Unlock()
|
||||
return app.callback(
|
||||
tmsp.RequestSetOption(key, value),
|
||||
tmsp.ResponseSetOption(log),
|
||||
)
|
||||
}
|
||||
|
||||
func (app *localAppConn) AppendTxAsync(tx []byte) *tmspcli.ReqRes {
|
||||
app.mtx.Lock()
|
||||
res := app.Application.AppendTx(tx)
|
||||
app.mtx.Unlock()
|
||||
return app.callback(
|
||||
tmsp.RequestAppendTx(tx),
|
||||
tmsp.ResponseAppendTx(res.Code, res.Data, res.Log),
|
||||
)
|
||||
}
|
||||
|
||||
func (app *localAppConn) CheckTxAsync(tx []byte) *tmspcli.ReqRes {
|
||||
app.mtx.Lock()
|
||||
res := app.Application.CheckTx(tx)
|
||||
app.mtx.Unlock()
|
||||
return app.callback(
|
||||
tmsp.RequestCheckTx(tx),
|
||||
tmsp.ResponseCheckTx(res.Code, res.Data, res.Log),
|
||||
)
|
||||
}
|
||||
|
||||
func (app *localAppConn) CommitAsync() *tmspcli.ReqRes {
|
||||
app.mtx.Lock()
|
||||
res := app.Application.Commit()
|
||||
app.mtx.Unlock()
|
||||
return app.callback(
|
||||
tmsp.RequestCommit(),
|
||||
tmsp.ResponseCommit(res.Code, res.Data, res.Log),
|
||||
)
|
||||
}
|
||||
|
||||
func (app *localAppConn) InfoSync() (info string, err error) {
|
||||
app.mtx.Lock()
|
||||
info = app.Application.Info()
|
||||
app.mtx.Unlock()
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (app *localAppConn) FlushSync() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *localAppConn) CommitSync() (res tmsp.Result) {
|
||||
app.mtx.Lock()
|
||||
res = app.Application.Commit()
|
||||
app.mtx.Unlock()
|
||||
return res
|
||||
}
|
||||
|
||||
func (app *localAppConn) InitChainSync(validators []*tmsp.Validator) (err error) {
|
||||
app.mtx.Lock()
|
||||
if bcApp, ok := app.Application.(tmsp.BlockchainAware); ok {
|
||||
bcApp.InitChain(validators)
|
||||
}
|
||||
app.mtx.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *localAppConn) EndBlockSync(height uint64) (changedValidators []*tmsp.Validator, err error) {
|
||||
app.mtx.Lock()
|
||||
if bcApp, ok := app.Application.(tmsp.BlockchainAware); ok {
|
||||
changedValidators = bcApp.EndBlock(height)
|
||||
}
|
||||
app.mtx.Unlock()
|
||||
return changedValidators, nil
|
||||
}
|
||||
|
||||
//-------------------------------------------------------
|
||||
|
||||
func (app *localAppConn) callback(req *tmsp.Request, res *tmsp.Response) *tmspcli.ReqRes {
|
||||
app.Callback(req, res)
|
||||
return NewReqRes(req, res)
|
||||
}
|
||||
|
||||
func NewReqRes(req *tmsp.Request, res *tmsp.Response) *tmspcli.ReqRes {
|
||||
reqRes := tmspcli.NewReqRes(req)
|
||||
reqRes.Response = res
|
||||
reqRes.SetDone()
|
||||
return reqRes
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
// the application in general is not meant to be interfaced
|
||||
// with concurrent callers.
|
||||
type remoteAppConn struct {
|
||||
*tmspcli.Client
|
||||
tmspcli.Client
|
||||
}
|
||||
|
||||
func NewRemoteAppConn(addr string) (*remoteAppConn, error) {
|
||||
|
||||
@@ -76,12 +76,11 @@ func TestInfo(t *testing.T) {
|
||||
} else {
|
||||
t.Log("Connected")
|
||||
}
|
||||
proxy.Start()
|
||||
data, err := proxy.InfoSync()
|
||||
if err != nil {
|
||||
res := proxy.InfoSync()
|
||||
if res.IsErr() {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
if data != "size:0" {
|
||||
if string(res.Data) != "size:0" {
|
||||
t.Error("Expected ResponseInfo with one element 'size:0' but got something else")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user