tendermint/proxy/multi_app_conn.go

103 lines
2.4 KiB
Go
Raw Normal View History

2016-08-17 19:28:08 -07:00
package proxy
import (
2017-03-02 17:47:07 -08:00
cmn "github.com/tendermint/go-common"
2016-08-17 19:28:08 -07:00
cfg "github.com/tendermint/go-config"
)
2016-08-23 18:44:07 -07:00
//-----------------------------
// Tendermint's interface to the application consists of multiple connections
2016-08-22 13:00:08 -07:00
type AppConns interface {
2017-03-02 17:47:07 -08:00
cmn.Service
2016-08-22 13:00:08 -07:00
Mempool() AppConnMempool
Consensus() AppConnConsensus
Query() AppConnQuery
}
func NewAppConns(config cfg.Config, clientCreator ClientCreator, handshaker Handshaker) AppConns {
return NewMultiAppConn(config, clientCreator, handshaker)
2016-08-22 13:00:08 -07:00
}
2016-08-24 21:18:03 -07:00
//-----------------------------
// multiAppConn implements AppConns
type Handshaker interface {
Handshake(AppConns) error
}
// a multiAppConn is made of a few appConns (mempool, consensus, query)
2017-01-12 12:53:32 -08:00
// and manages their underlying abci clients, including the handshake
2016-08-23 18:44:07 -07:00
// which ensures the app and tendermint are synced.
// TODO: on app restart, clients must reboot together
2016-08-17 19:28:08 -07:00
type multiAppConn struct {
2017-03-02 17:47:07 -08:00
cmn.BaseService
2016-08-17 19:28:08 -07:00
config cfg.Config
handshaker Handshaker
2016-08-17 19:28:08 -07:00
mempoolConn *appConnMempool
consensusConn *appConnConsensus
2016-08-22 13:00:08 -07:00
queryConn *appConnQuery
clientCreator ClientCreator
2016-08-17 19:28:08 -07:00
}
2017-01-12 12:53:32 -08:00
// Make all necessary abci connections to the application
func NewMultiAppConn(config cfg.Config, clientCreator ClientCreator, handshaker Handshaker) *multiAppConn {
2016-08-17 19:28:08 -07:00
multiAppConn := &multiAppConn{
config: config,
handshaker: handshaker,
clientCreator: clientCreator,
2016-08-17 19:28:08 -07:00
}
2017-03-02 17:47:07 -08:00
multiAppConn.BaseService = *cmn.NewBaseService(log, "multiAppConn", multiAppConn)
2016-08-17 19:28:08 -07:00
return multiAppConn
}
// Returns the mempool connection
func (app *multiAppConn) Mempool() AppConnMempool {
return app.mempoolConn
}
// Returns the consensus Connection
func (app *multiAppConn) Consensus() AppConnConsensus {
return app.consensusConn
}
2016-08-23 18:44:07 -07:00
// Returns the query Connection
2016-08-22 13:00:08 -07:00
func (app *multiAppConn) Query() AppConnQuery {
return app.queryConn
}
2016-08-17 19:28:08 -07:00
func (app *multiAppConn) OnStart() error {
2016-08-22 13:00:08 -07:00
// query connection
2017-01-12 12:53:32 -08:00
querycli, err := app.clientCreator.NewABCIClient()
2016-08-22 13:00:08 -07:00
if err != nil {
return err
}
2016-08-25 11:16:28 -07:00
app.queryConn = NewAppConnQuery(querycli)
2016-08-22 13:00:08 -07:00
// mempool connection
2017-01-12 12:53:32 -08:00
memcli, err := app.clientCreator.NewABCIClient()
2016-08-17 19:28:08 -07:00
if err != nil {
return err
}
app.mempoolConn = NewAppConnMempool(memcli)
2016-08-22 13:00:08 -07:00
// consensus connection
2017-01-12 12:53:32 -08:00
concli, err := app.clientCreator.NewABCIClient()
2016-08-17 19:28:08 -07:00
if err != nil {
return err
}
app.consensusConn = NewAppConnConsensus(concli)
2016-08-23 18:44:07 -07:00
// ensure app is synced to the latest state
if app.handshaker != nil {
return app.handshaker.Handshake(app)
2016-08-23 18:44:07 -07:00
}
2016-08-17 19:28:08 -07:00
return nil
}