Browse Source

Use tmsp.Client interface

pull/206/merge
Jae Kwon 9 years ago
parent
commit
a8ee0377d5
8 changed files with 16 additions and 163 deletions
  1. +2
    -1
      cmd/tendermint/flags.go
  2. +3
    -3
      consensus/common_test.go
  3. +3
    -3
      mempool/mempool_test.go
  4. +3
    -2
      node/node.go
  5. +1
    -16
      proxy/app_conn.go
  6. +0
    -133
      proxy/local_app_conn.go
  7. +1
    -1
      proxy/remote_app_conn.go
  8. +3
    -4
      proxy/remote_app_conn_test.go

+ 2
- 1
cmd/tendermint/flags.go View File

@ -30,7 +30,8 @@ func parseFlags(config cfg.Config, args []string) {
flags.BoolVar(&skipUPNP, "skip_upnp", config.GetBool("skip_upnp"), "Skip UPNP configuration") flags.BoolVar(&skipUPNP, "skip_upnp", config.GetBool("skip_upnp"), "Skip UPNP configuration")
flags.StringVar(&rpcLaddr, "rpc_laddr", config.GetString("rpc_laddr"), "RPC listen address. Port required") flags.StringVar(&rpcLaddr, "rpc_laddr", config.GetString("rpc_laddr"), "RPC listen address. Port required")
flags.StringVar(&logLevel, "log_level", config.GetString("log_level"), "Log level") flags.StringVar(&logLevel, "log_level", config.GetString("log_level"), "Log level")
flags.StringVar(&proxyApp, "proxy_app", config.GetString("proxy_app"), "Proxy app address")
flags.StringVar(&proxyApp, "proxy_app", config.GetString("proxy_app"),
"Proxy app address, or 'nilapp' or 'dummy' for local testing.")
flags.Parse(args) flags.Parse(args)
if printHelp { if printHelp {
flags.PrintDefaults() flags.PrintDefaults()


+ 3
- 3
consensus/common_test.go View File

@ -13,9 +13,9 @@ import (
bc "github.com/tendermint/tendermint/blockchain" bc "github.com/tendermint/tendermint/blockchain"
"github.com/tendermint/tendermint/config/tendermint_test" "github.com/tendermint/tendermint/config/tendermint_test"
mempl "github.com/tendermint/tendermint/mempool" mempl "github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/proxy"
sm "github.com/tendermint/tendermint/state" sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
tmspcli "github.com/tendermint/tmsp/client"
"github.com/tendermint/tmsp/example/counter" "github.com/tendermint/tmsp/example/counter"
) )
@ -313,8 +313,8 @@ func newConsensusState(state *sm.State, pv *types.PrivValidator) *ConsensusState
// one for mempool, one for consensus // one for mempool, one for consensus
mtx, app := new(sync.Mutex), counter.NewCounterApplication(false) mtx, app := new(sync.Mutex), counter.NewCounterApplication(false)
proxyAppConnMem := proxy.NewLocalAppConn(mtx, app)
proxyAppConnCon := proxy.NewLocalAppConn(mtx, app)
proxyAppConnMem := tmspcli.NewLocalClient(mtx, app)
proxyAppConnCon := tmspcli.NewLocalClient(mtx, app)
// Make Mempool // Make Mempool
mempool := mempl.NewMempool(proxyAppConnMem) mempool := mempl.NewMempool(proxyAppConnMem)


+ 3
- 3
mempool/mempool_test.go View File

@ -6,8 +6,8 @@ import (
"testing" "testing"
"github.com/tendermint/tendermint/config/tendermint_test" "github.com/tendermint/tendermint/config/tendermint_test"
"github.com/tendermint/tendermint/proxy"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
tmspcli "github.com/tendermint/tmsp/client"
"github.com/tendermint/tmsp/example/counter" "github.com/tendermint/tmsp/example/counter"
) )
@ -20,8 +20,8 @@ func TestSerialReap(t *testing.T) {
app := counter.NewCounterApplication(true) app := counter.NewCounterApplication(true)
app.SetOption("serial", "on") app.SetOption("serial", "on")
mtx := new(sync.Mutex) mtx := new(sync.Mutex)
appConnMem := proxy.NewLocalAppConn(mtx, app)
appConnCon := proxy.NewLocalAppConn(mtx, app)
appConnMem := tmspcli.NewLocalClient(mtx, app)
appConnCon := tmspcli.NewLocalClient(mtx, app)
mempool := NewMempool(appConnMem) mempool := NewMempool(appConnMem)
appendTxsRange := func(start, end int) { appendTxsRange := func(start, end int) {


+ 3
- 2
node/node.go View File

@ -25,6 +25,7 @@ import (
sm "github.com/tendermint/tendermint/state" sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
"github.com/tendermint/tendermint/version" "github.com/tendermint/tendermint/version"
tmspcli "github.com/tendermint/tmsp/client"
"github.com/tendermint/tmsp/example/dummy" "github.com/tendermint/tmsp/example/dummy"
"github.com/tendermint/tmsp/example/nil" "github.com/tendermint/tmsp/example/nil"
) )
@ -244,11 +245,11 @@ func getProxyApp(addr string, hash []byte) (proxyAppConn proxy.AppConn) {
case "nilapp": case "nilapp":
app := nilapp.NewNilApplication() app := nilapp.NewNilApplication()
mtx := new(sync.Mutex) mtx := new(sync.Mutex)
proxyAppConn = proxy.NewLocalAppConn(mtx, app)
proxyAppConn = tmspcli.NewLocalClient(mtx, app)
case "dummy": case "dummy":
app := dummy.NewDummyApplication() app := dummy.NewDummyApplication()
mtx := new(sync.Mutex) mtx := new(sync.Mutex)
proxyAppConn = proxy.NewLocalAppConn(mtx, app)
proxyAppConn = tmspcli.NewLocalClient(mtx, app)
default: default:
// Run forever in a loop // Run forever in a loop
remoteApp, err := proxy.NewRemoteAppConn(addr) remoteApp, err := proxy.NewRemoteAppConn(addr)


+ 1
- 16
proxy/app_conn.go View File

@ -2,23 +2,8 @@ package proxy
import ( import (
tmspcli "github.com/tendermint/tmsp/client" tmspcli "github.com/tendermint/tmsp/client"
tmsp "github.com/tendermint/tmsp/types"
) )
type AppConn interface { 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
} }

+ 0
- 133
proxy/local_app_conn.go View File

@ -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
}

+ 1
- 1
proxy/remote_app_conn.go View File

@ -8,7 +8,7 @@ import (
// the application in general is not meant to be interfaced // the application in general is not meant to be interfaced
// with concurrent callers. // with concurrent callers.
type remoteAppConn struct { type remoteAppConn struct {
*tmspcli.Client
tmspcli.Client
} }
func NewRemoteAppConn(addr string) (*remoteAppConn, error) { func NewRemoteAppConn(addr string) (*remoteAppConn, error) {


+ 3
- 4
proxy/remote_app_conn_test.go View File

@ -76,12 +76,11 @@ func TestInfo(t *testing.T) {
} else { } else {
t.Log("Connected") t.Log("Connected")
} }
proxy.Start()
data, err := proxy.InfoSync()
if err != nil {
res := proxy.InfoSync()
if res.IsErr() {
t.Errorf("Unexpected error: %v", err) 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") t.Error("Expected ResponseInfo with one element 'size:0' but got something else")
} }
} }

Loading…
Cancel
Save