tendermint/proxy/multi_app_conn.go

172 lines
4.6 KiB
Go
Raw Normal View History

2016-08-17 19:28:08 -07:00
package proxy
import (
2016-08-23 18:44:07 -07:00
"bytes"
"fmt"
2016-08-17 19:28:08 -07:00
. "github.com/tendermint/go-common"
cfg "github.com/tendermint/go-config"
"github.com/tendermint/tendermint/types"
2016-08-17 19:28:08 -07:00
)
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 {
Service
2016-08-22 13:00:08 -07:00
Mempool() AppConnMempool
Consensus() AppConnConsensus
Query() AppConnQuery
}
func NewAppConns(config cfg.Config, clientCreator ClientCreator, state State, blockStore BlockStore) AppConns {
return NewMultiAppConn(config, clientCreator, state, blockStore)
2016-08-22 13:00:08 -07:00
}
2016-08-24 21:18:03 -07:00
//-----------------------------
// multiAppConn implements AppConns
// a multiAppConn is made of a few appConns (mempool, consensus, query)
2016-08-23 18:44:07 -07:00
// and manages their underlying tmsp clients, including the handshake
// 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 {
2016-10-28 12:14:24 -07:00
BaseService
2016-08-17 19:28:08 -07:00
config cfg.Config
state State
blockStore BlockStore
mempoolConn *appConnMempool
consensusConn *appConnConsensus
2016-08-22 13:00:08 -07:00
queryConn *appConnQuery
clientCreator ClientCreator
2016-08-17 19:28:08 -07:00
}
// Make all necessary tmsp connections to the application
func NewMultiAppConn(config cfg.Config, clientCreator ClientCreator, state State, blockStore BlockStore) *multiAppConn {
2016-08-17 19:28:08 -07:00
multiAppConn := &multiAppConn{
config: config,
state: state,
blockStore: blockStore,
clientCreator: clientCreator,
2016-08-17 19:28:08 -07:00
}
2016-10-28 12:14:24 -07:00
multiAppConn.BaseService = *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-10-28 12:14:24 -07:00
app.BaseService.OnStart()
2016-08-17 19:28:08 -07:00
2016-08-22 13:00:08 -07:00
// query connection
querycli, err := app.clientCreator.NewTMSPClient()
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
memcli, err := app.clientCreator.NewTMSPClient()
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
concli, err := app.clientCreator.NewTMSPClient()
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
return app.Handshake()
}
// TODO: retry the handshake once if it fails the first time
2016-08-24 21:18:03 -07:00
// ... let Info take an argument determining its behaviour
2016-08-23 18:44:07 -07:00
func (app *multiAppConn) Handshake() error {
2016-08-24 21:18:03 -07:00
// handshake is done via info request on the query conn
2016-08-23 18:44:07 -07:00
res, tmspInfo, blockInfo, configInfo := app.queryConn.InfoSync()
if res.IsErr() {
return fmt.Errorf("Error calling Info. Code: %v; Data: %X; Log: %s", res.Code, res.Data, res.Log)
}
if blockInfo == nil {
log.Warn("blockInfo is nil, aborting handshake")
return nil
}
log.Notice("TMSP Handshake", "height", blockInfo.BlockHeight, "block_hash", blockInfo.BlockHash, "app_hash", blockInfo.AppHash)
// TODO: check overflow or change pb to int32
blockHeight := int(blockInfo.BlockHeight)
blockHash := blockInfo.BlockHash
appHash := blockInfo.AppHash
if tmspInfo != nil {
// TODO: check tmsp version (or do this in the tmspcli?)
_ = tmspInfo
}
2016-08-24 21:18:03 -07:00
// last block (nil if we starting from 0)
2016-08-23 18:44:07 -07:00
var header *types.Header
var partsHeader types.PartSetHeader
2016-08-24 21:18:03 -07:00
// replay all blocks after blockHeight
// if blockHeight == 0, we will replay everything
2016-08-23 18:44:07 -07:00
if blockHeight != 0 {
blockMeta := app.blockStore.LoadBlockMeta(blockHeight)
if blockMeta == nil {
return fmt.Errorf("Handshake error. Could not find block #%d", blockHeight)
}
// check block hash
if !bytes.Equal(blockMeta.Hash, blockHash) {
return fmt.Errorf("Handshake error. Block hash at height %d does not match. Got %X, expected %X", blockHeight, blockHash, blockMeta.Hash)
}
// NOTE: app hash should be in the next block ...
2016-08-23 18:44:07 -07:00
// check app hash
/*if !bytes.Equal(blockMeta.Header.AppHash, appHash) {
2016-08-23 18:44:07 -07:00
return fmt.Errorf("Handshake error. App hash at height %d does not match. Got %X, expected %X", blockHeight, appHash, blockMeta.Header.AppHash)
}*/
2016-08-23 18:44:07 -07:00
header = blockMeta.Header
partsHeader = blockMeta.PartsHeader
}
if configInfo != nil {
// TODO: set config info
_ = configInfo
}
2016-08-17 19:28:08 -07:00
2016-08-23 18:44:07 -07:00
// replay blocks up to the latest in the blockstore
err := app.state.ReplayBlocks(appHash, header, partsHeader, app.consensusConn, app.blockStore)
2016-08-23 18:44:07 -07:00
if err != nil {
return fmt.Errorf("Error on replay: %v", err)
}
2016-08-17 19:28:08 -07:00
// TODO: (on restart) replay mempool
return nil
}