tendermint/proxy/local_app_conn.go

134 lines
3.0 KiB
Go
Raw Normal View History

package proxy
import (
2016-01-25 14:34:08 -08:00
tmspcli "github.com/tendermint/tmsp/client"
tmsp "github.com/tendermint/tmsp/types"
"sync"
)
type localAppConn struct {
mtx *sync.Mutex
tmsp.Application
2016-01-22 15:48:13 -08:00
tmspcli.Callback
}
func NewLocalAppConn(mtx *sync.Mutex, app tmsp.Application) *localAppConn {
return &localAppConn{
mtx: mtx,
Application: app,
}
}
2016-01-22 15:48:13 -08:00
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
}
2016-02-08 00:48:58 -08:00
func (app *localAppConn) EchoAsync(msg string) *tmspcli.ReqRes {
2016-02-26 23:03:31 -08:00
return app.callback(
2016-01-31 08:11:50 -08:00
tmsp.RequestEcho(msg),
tmsp.ResponseEcho(msg),
)
}
2016-02-08 00:48:58 -08:00
func (app *localAppConn) FlushAsync() *tmspcli.ReqRes {
// Do nothing
2016-02-26 23:03:31 -08:00
return NewReqRes(tmsp.RequestFlush(), nil)
}
2016-02-08 00:48:58 -08:00
func (app *localAppConn) SetOptionAsync(key string, value string) *tmspcli.ReqRes {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
log := app.Application.SetOption(key, value)
app.mtx.Unlock()
2016-02-26 23:03:31 -08:00
return app.callback(
2016-01-31 08:11:50 -08:00
tmsp.RequestSetOption(key, value),
tmsp.ResponseSetOption(log),
)
}
2016-02-08 00:48:58 -08:00
func (app *localAppConn) AppendTxAsync(tx []byte) *tmspcli.ReqRes {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
code, result, log := app.Application.AppendTx(tx)
app.mtx.Unlock()
2016-02-26 23:03:31 -08:00
return app.callback(
2016-01-31 08:11:50 -08:00
tmsp.RequestAppendTx(tx),
tmsp.ResponseAppendTx(code, result, log),
)
}
2016-02-08 00:48:58 -08:00
func (app *localAppConn) CheckTxAsync(tx []byte) *tmspcli.ReqRes {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
code, result, log := app.Application.CheckTx(tx)
app.mtx.Unlock()
2016-02-26 23:03:31 -08:00
return app.callback(
2016-01-31 08:11:50 -08:00
tmsp.RequestCheckTx(tx),
tmsp.ResponseCheckTx(code, result, log),
)
}
2016-02-14 13:11:01 -08:00
func (app *localAppConn) CommitAsync() *tmspcli.ReqRes {
app.mtx.Lock()
2016-02-14 13:11:01 -08:00
hash, log := app.Application.Commit()
app.mtx.Unlock()
2016-02-26 23:03:31 -08:00
return app.callback(
2016-02-14 13:11:01 -08:00
tmsp.RequestCommit(),
tmsp.ResponseCommit(hash, log),
)
}
2016-01-25 14:34:08 -08:00
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
}
2016-02-14 13:11:01 -08:00
func (app *localAppConn) CommitSync() (hash []byte, log string, err error) {
app.mtx.Lock()
2016-02-14 13:11:01 -08:00
hash, log = app.Application.Commit()
app.mtx.Unlock()
2016-01-25 14:34:08 -08:00
return hash, log, nil
}
2016-02-26 23:03:31 -08:00
2016-03-05 20:57:36 -08:00
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
}
2016-03-06 18:02:29 -08:00
func (app *localAppConn) EndBlockSync(height uint64) (changedValidators []*tmsp.Validator, err error) {
2016-03-05 20:57:36 -08:00
app.mtx.Lock()
if bcApp, ok := app.Application.(tmsp.BlockchainAware); ok {
2016-03-06 18:02:29 -08:00
changedValidators = bcApp.EndBlock(height)
2016-03-05 20:57:36 -08:00
}
app.mtx.Unlock()
return changedValidators, nil
}
2016-02-26 23:03:31 -08:00
//-------------------------------------------------------
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
}