Skip to content
Snippets Groups Projects
Commit 5149db8e authored by Silas Davis's avatar Silas Davis
Browse files

update code to work against newer tendermint protobuf etc

parent e4f8c940
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,7 @@ import (
"sync"
tmsp_client "github.com/tendermint/tmsp/client"
tmsp_types "github.com/tendermint/tmsp/types"
tmsp_types "github.com/tendermint/tmsp/types"
manager_types "github.com/eris-ltd/eris-db/manager/types"
)
......@@ -68,13 +68,13 @@ func (app *localClient) Stop() bool {
func (app *localClient) FlushAsync() *tmsp_client.ReqRes {
// Do nothing
return newLocalReqRes(tmsp_types.RequestFlush(), nil)
return newLocalReqRes(tmsp_types.ToRequestFlush(), nil)
}
func (app *localClient) EchoAsync(msg string) *tmsp_client.ReqRes {
return app.callback(
tmsp_types.RequestEcho(msg),
tmsp_types.ResponseEcho(msg),
tmsp_types.ToRequestEcho(msg),
tmsp_types.ToResponseEcho(msg),
)
}
......@@ -83,8 +83,8 @@ func (app *localClient) InfoAsync() *tmsp_client.ReqRes {
info := app.Application.Info()
app.mtx.Unlock()
return app.callback(
tmsp_types.RequestInfo(),
tmsp_types.ResponseInfo(info),
tmsp_types.ToRequestInfo(),
tmsp_types.ToResponseInfo(info),
)
}
......@@ -93,8 +93,8 @@ func (app *localClient) SetOptionAsync(key string, value string) *tmsp_client.Re
log := app.Application.SetOption(key, value)
app.mtx.Unlock()
return app.callback(
tmsp_types.RequestSetOption(key, value),
tmsp_types.ResponseSetOption(log),
tmsp_types.ToRequestSetOption(key, value),
tmsp_types.ToResponseSetOption(log),
)
}
......@@ -103,8 +103,8 @@ func (app *localClient) AppendTxAsync(tx []byte) *tmsp_client.ReqRes {
res := app.Application.AppendTx(tx)
app.mtx.Unlock()
return app.callback(
tmsp_types.RequestAppendTx(tx),
tmsp_types.ResponseAppendTx(res.Code, res.Data, res.Log),
tmsp_types.ToRequestAppendTx(tx),
tmsp_types.ToResponseAppendTx(res.Code, res.Data, res.Log),
)
}
......@@ -113,8 +113,8 @@ func (app *localClient) CheckTxAsync(tx []byte) *tmsp_client.ReqRes {
res := app.Application.CheckTx(tx)
app.mtx.Unlock()
return app.callback(
tmsp_types.RequestCheckTx(tx),
tmsp_types.ResponseCheckTx(res.Code, res.Data, res.Log),
tmsp_types.ToRequestCheckTx(tx),
tmsp_types.ToResponseCheckTx(res.Code, res.Data, res.Log),
)
}
......@@ -123,8 +123,8 @@ func (app *localClient) QueryAsync(tx []byte) *tmsp_client.ReqRes {
res := app.Application.Query(tx)
app.mtx.Unlock()
return app.callback(
tmsp_types.RequestQuery(tx),
tmsp_types.ResponseQuery(res.Code, res.Data, res.Log),
tmsp_types.ToRequestQuery(tx),
tmsp_types.ToResponseQuery(res.Code, res.Data, res.Log),
)
}
......@@ -133,8 +133,8 @@ func (app *localClient) CommitAsync() *tmsp_client.ReqRes {
res := app.Application.Commit()
app.mtx.Unlock()
return app.callback(
tmsp_types.RequestCommit(),
tmsp_types.ResponseCommit(res.Code, res.Data, res.Log),
tmsp_types.ToRequestCommit(),
tmsp_types.ToResponseCommit(res.Code, res.Data, res.Log),
)
}
......@@ -144,8 +144,8 @@ func (app *localClient) InitChainAsync(validators []*tmsp_types.Validator) *tmsp
bcApp.InitChain(validators)
}
reqRes := app.callback(
tmsp_types.RequestInitChain(validators),
tmsp_types.ResponseInitChain(),
tmsp_types.ToRequestInitChain(validators),
tmsp_types.ToResponseInitChain(),
)
app.mtx.Unlock()
return reqRes
......@@ -158,8 +158,8 @@ func (app *localClient) BeginBlockAsync(height uint64) *tmsp_client.ReqRes {
}
app.mtx.Unlock()
return app.callback(
tmsp_types.RequestBeginBlock(height),
tmsp_types.ResponseBeginBlock(),
tmsp_types.ToRequestBeginBlock(height),
tmsp_types.ToResponseBeginBlock(),
)
}
......@@ -171,8 +171,8 @@ func (app *localClient) EndBlockAsync(height uint64) *tmsp_client.ReqRes {
}
app.mtx.Unlock()
return app.callback(
tmsp_types.RequestEndBlock(height),
tmsp_types.ResponseEndBlock(validators),
tmsp_types.ToRequestEndBlock(height),
tmsp_types.ToResponseEndBlock(validators),
)
}
......
......@@ -20,27 +20,27 @@
package tendermint
import (
"fmt"
"path"
"strings"
"sync"
node "github.com/tendermint/tendermint/node"
proxy "github.com/tendermint/tendermint/proxy"
p2p "github.com/tendermint/go-p2p"
tendermint_types "github.com/tendermint/tendermint/types"
log "github.com/eris-ltd/eris-logger"
config "github.com/eris-ltd/eris-db/config"
definitions "github.com/eris-ltd/eris-db/definitions"
manager_types "github.com/eris-ltd/eris-db/manager/types"
// files "github.com/eris-ltd/eris-db/files"
"fmt"
"path"
"strings"
"sync"
p2p "github.com/tendermint/go-p2p"
node "github.com/tendermint/tendermint/node"
proxy "github.com/tendermint/tendermint/proxy"
tendermint_types "github.com/tendermint/tendermint/types"
log "github.com/eris-ltd/eris-logger"
config "github.com/eris-ltd/eris-db/config"
definitions "github.com/eris-ltd/eris-db/definitions"
manager_types "github.com/eris-ltd/eris-db/manager/types"
// files "github.com/eris-ltd/eris-db/files"
)
type TendermintNode struct {
tmintNode *node.Node
tmintConfig *TendermintConfig
tmintNode *node.Node
tmintConfig *TendermintConfig
}
// NOTE [ben] Compiler check to ensure TendermintNode successfully implements
......@@ -48,94 +48,93 @@ type TendermintNode struct {
var _ definitions.ConsensusEngine = (*TendermintNode)(nil)
func NewTendermintNode(moduleConfig *config.ModuleConfig,
application manager_types.Application) (*TendermintNode, error) {
// re-assert proper configuration for module
if moduleConfig.Version != GetTendermintVersion().GetMinorVersionString() {
return nil, fmt.Errorf("Version string %s did not match %s",
moduleConfig.Version, GetTendermintVersion().GetMinorVersionString())
}
// loading the module has ensured the working and data directory
// for tendermint have been created, but the config files needs
// to be written in tendermint's root directory.
// NOTE: [ben] as elsewhere Sub panics if config file does not have this
// subtree. To shield in go-routine, or PR to viper.
tendermintConfigViper := moduleConfig.Config.Sub("configuration")
if tendermintConfigViper == nil {
return nil,
fmt.Errorf("Failed to extract Tendermint configuration subtree.")
}
// wrap a copy of the viper config in a tendermint/go-config interface
tmintConfig := GetTendermintConfig(tendermintConfigViper)
// complete the tendermint configuration with default flags
tmintConfig.AssertTendermintDefaults(moduleConfig.ChainId,
moduleConfig.WorkDir, moduleConfig.DataDir, moduleConfig.RootDir)
privateValidatorFilePath := path.Join(moduleConfig.RootDir,
moduleConfig.Config.GetString("private_validator_file"))
if moduleConfig.Config.GetString("private_validator_file") == "" {
return nil, fmt.Errorf("No private validator file provided.")
}
// override tendermint configurations to force consistency with overruling
// settings
tmintConfig.AssertTendermintConsistency(moduleConfig,
privateValidatorFilePath)
log.WithFields(log.Fields{
"chainId": tmintConfig.GetString("chain_id"),
"genesisFile": tmintConfig.GetString("genesis_file"),
"nodeLocalAddress": tmintConfig.GetString("node_laddr"),
"moniker": tmintConfig.GetString("moniker"),
"seeds": tmintConfig.GetString("seeds"),
"fastSync": tmintConfig.GetBool("fast_sync"),
"rpcLocalAddress": tmintConfig.GetString("rpc_laddr"),
"databaseDirectory": tmintConfig.GetString("db_dir"),
"privateValidatorFile": tmintConfig.GetString("priv_validator_file"),
"privValFile": moduleConfig.Config.GetString("private_validator_file"),
}).Debug("Loaded Tendermint sub-configuration")
// TODO: [ben] do not "or Generate Validator keys", rather fail directly
// TODO: [ben] implement the signer for Private validator over eris-keys
// TODO: [ben] copy from rootDir to tendermint workingDir;
privateValidator := tendermint_types.LoadOrGenPrivValidator(
path.Join(moduleConfig.RootDir,
moduleConfig.Config.GetString("private_validator_file")))
newNode := node.NewNode(tmintConfig, privateValidator, func(addr string,
hash []byte) proxy.AppConn {
return NewLocalClient(new(sync.Mutex), application)
})
listener := p2p.NewDefaultListener("tcp", tmintConfig.GetString("node_laddr"),
tmintConfig.GetBool("skip_upnp"))
newNode.AddListener(listener)
// TODO: [ben] delay starting the node to a different function, to hand
// control over events to Core
if err := newNode.Start(); err != nil {
newNode.Stop()
return nil, fmt.Errorf("Failed to start Tendermint consensus node: %v", err)
}
log.WithFields(log.Fields{
"nodeAddress": tmintConfig.GetString("node_laddr"),
"transportProtocol": "tcp",
"upnp": !tmintConfig.GetBool("skip_upnp"),
"moniker": tmintConfig.GetString("moniker"),
}).Info("Tendermint consensus node started")
// If seedNode is provided by config, dial out.
if tmintConfig.GetString("seeds") != "" {
seeds := strings.Split(tmintConfig.GetString("seeds"), ",")
newNode.DialSeeds(seeds)
log.WithFields(log.Fields{
"seeds": seeds,
}).Debug("Tendermint node called seeds")
}
return &TendermintNode{
tmintNode: newNode,
tmintConfig: tmintConfig,
}, nil
application manager_types.Application) (*TendermintNode, error) {
// re-assert proper configuration for module
if moduleConfig.Version != GetTendermintVersion().GetMinorVersionString() {
return nil, fmt.Errorf("Version string %s did not match %s",
moduleConfig.Version, GetTendermintVersion().GetMinorVersionString())
}
// loading the module has ensured the working and data directory
// for tendermint have been created, but the config files needs
// to be written in tendermint's root directory.
// NOTE: [ben] as elsewhere Sub panics if config file does not have this
// subtree. To shield in go-routine, or PR to viper.
tendermintConfigViper := moduleConfig.Config.Sub("configuration")
if tendermintConfigViper == nil {
return nil,
fmt.Errorf("Failed to extract Tendermint configuration subtree.")
}
// wrap a copy of the viper config in a tendermint/go-config interface
tmintConfig := GetTendermintConfig(tendermintConfigViper)
// complete the tendermint configuration with default flags
tmintConfig.AssertTendermintDefaults(moduleConfig.ChainId,
moduleConfig.WorkDir, moduleConfig.DataDir, moduleConfig.RootDir)
privateValidatorFilePath := path.Join(moduleConfig.RootDir,
moduleConfig.Config.GetString("private_validator_file"))
if moduleConfig.Config.GetString("private_validator_file") == "" {
return nil, fmt.Errorf("No private validator file provided.")
}
// override tendermint configurations to force consistency with overruling
// settings
tmintConfig.AssertTendermintConsistency(moduleConfig,
privateValidatorFilePath)
log.WithFields(log.Fields{
"chainId": tmintConfig.GetString("chain_id"),
"genesisFile": tmintConfig.GetString("genesis_file"),
"nodeLocalAddress": tmintConfig.GetString("node_laddr"),
"moniker": tmintConfig.GetString("moniker"),
"seeds": tmintConfig.GetString("seeds"),
"fastSync": tmintConfig.GetBool("fast_sync"),
"rpcLocalAddress": tmintConfig.GetString("rpc_laddr"),
"databaseDirectory": tmintConfig.GetString("db_dir"),
"privateValidatorFile": tmintConfig.GetString("priv_validator_file"),
"privValFile": moduleConfig.Config.GetString("private_validator_file"),
}).Debug("Loaded Tendermint sub-configuration")
// TODO: [ben] do not "or Generate Validator keys", rather fail directly
// TODO: [ben] implement the signer for Private validator over eris-keys
// TODO: [ben] copy from rootDir to tendermint workingDir;
privateValidator := tendermint_types.LoadOrGenPrivValidator(
path.Join(moduleConfig.RootDir,
moduleConfig.Config.GetString("private_validator_file")))
newNode := node.NewNode(tmintConfig, privateValidator, func(_, _ string,
hash []byte) proxy.AppConn {
return NewLocalClient(new(sync.Mutex), application)
})
listener := p2p.NewDefaultListener("tcp", tmintConfig.GetString("node_laddr"),
tmintConfig.GetBool("skip_upnp"))
newNode.AddListener(listener)
// TODO: [ben] delay starting the node to a different function, to hand
// control over events to Core
if err := newNode.Start(); err != nil {
newNode.Stop()
return nil, fmt.Errorf("Failed to start Tendermint consensus node: %v", err)
}
log.WithFields(log.Fields{
"nodeAddress": tmintConfig.GetString("node_laddr"),
"transportProtocol": "tcp",
"upnp": !tmintConfig.GetBool("skip_upnp"),
"moniker": tmintConfig.GetString("moniker"),
}).Info("Tendermint consensus node started")
// If seedNode is provided by config, dial out.
if tmintConfig.GetString("seeds") != "" {
seeds := strings.Split(tmintConfig.GetString("seeds"), ",")
newNode.DialSeeds(seeds)
log.WithFields(log.Fields{
"seeds": seeds,
}).Debug("Tendermint node called seeds")
}
return &TendermintNode{
tmintNode: newNode,
tmintConfig: tmintConfig,
}, nil
}
//------------------------------------------------------------------------------
// Helper functions
......
......@@ -93,7 +93,7 @@ func (s *Server) acceptConnectionsRoutine() {
fmt.Println("Accepted a new connection")
}
closeConn := make(chan error, 2) // Push to signal connection closed
closeConn := make(chan error, 2) // Push to signal connection closed
responses := make(chan *tmsp_types.Response, 1000) // A channel to buffer responses
// Read requests from conn and deal with them
......@@ -145,45 +145,46 @@ func (s *Server) handleRequests(closeConn chan error, conn net.Conn, responses c
}
func (s *Server) handleRequest(req *tmsp_types.Request, responses chan<- *tmsp_types.Response) {
switch req.Type {
case tmsp_types.MessageType_Echo:
responses <- tmsp_types.ResponseEcho(string(req.Data))
case tmsp_types.MessageType_Flush:
responses <- tmsp_types.ResponseFlush()
case tmsp_types.MessageType_Info:
switch r := req.Value.(type) {
case *tmsp_types.Request_Echo:
responses <- tmsp_types.ToResponseEcho(r.Echo.Message)
case *tmsp_types.Request_Flush:
responses <- tmsp_types.ToResponseFlush()
case *tmsp_types.Request_Info:
data := s.app.Info()
responses <- tmsp_types.ResponseInfo(data)
case tmsp_types.MessageType_SetOption:
logStr := s.app.SetOption(req.Key, req.Value)
responses <- tmsp_types.ResponseSetOption(logStr)
case tmsp_types.MessageType_AppendTx:
res := s.app.AppendTx(req.Data)
responses <- tmsp_types.ResponseAppendTx(res.Code, res.Data, res.Log)
case tmsp_types.MessageType_CheckTx:
res := s.app.CheckTx(req.Data)
responses <- tmsp_types.ResponseCheckTx(res.Code, res.Data, res.Log)
case tmsp_types.MessageType_Commit:
responses <- tmsp_types.ToResponseInfo(data)
case *tmsp_types.Request_SetOption:
so := r.SetOption
logStr := s.app.SetOption(so.Key, so.Value)
responses <- tmsp_types.ToResponseSetOption(logStr)
case *tmsp_types.Request_AppendTx:
res := s.app.AppendTx(r.AppendTx.Tx)
responses <- tmsp_types.ToResponseAppendTx(res.Code, res.Data, res.Log)
case *tmsp_types.Request_CheckTx:
res := s.app.CheckTx(r.CheckTx.Tx)
responses <- tmsp_types.ToResponseCheckTx(res.Code, res.Data, res.Log)
case *tmsp_types.Request_Commit:
res := s.app.Commit()
responses <- tmsp_types.ResponseCommit(res.Code, res.Data, res.Log)
case tmsp_types.MessageType_Query:
res := s.app.Query(req.Data)
responses <- tmsp_types.ResponseQuery(res.Code, res.Data, res.Log)
case tmsp_types.MessageType_InitChain:
if app, ok := s.app.(manager_types.BlockchainAware); ok {
app.InitChain(req.Validators)
responses <- tmsp_types.ResponseInitChain()
responses <- tmsp_types.ToResponseCommit(res.Code, res.Data, res.Log)
case *tmsp_types.Request_Query:
res := s.app.Query(r.Query.Query)
responses <- tmsp_types.ToResponseQuery(res.Code, res.Data, res.Log)
case *tmsp_types.Request_InitChain:
if app, ok := s.app.(tmsp_types.BlockchainAware); ok {
app.InitChain(r.InitChain.Validators)
responses <- tmsp_types.ToResponseInitChain()
} else {
responses <- tmsp_types.ResponseInitChain()
responses <- tmsp_types.ToResponseInitChain()
}
case tmsp_types.MessageType_EndBlock:
if app, ok := s.app.(manager_types.BlockchainAware); ok {
validators := app.EndBlock(req.Height)
responses <- tmsp_types.ResponseEndBlock(validators)
case *tmsp_types.Request_EndBlock:
if app, ok := s.app.(tmsp_types.BlockchainAware); ok {
validators := app.EndBlock(r.EndBlock.Height)
responses <- tmsp_types.ToResponseEndBlock(validators)
} else {
responses <- tmsp_types.ResponseEndBlock(nil)
responses <- tmsp_types.ToResponseEndBlock(nil)
}
default:
responses <- tmsp_types.ResponseException("Unknown request")
responses <- tmsp_types.ToResponseException("Unknown request")
}
}
......@@ -198,10 +199,10 @@ func (s *Server) handleResponses(closeConn chan error, responses <-chan *tmsp_ty
closeConn <- fmt.Errorf("Error in handleResponses: %v", err.Error())
return
}
if res.Type == tmsp_types.MessageType_Flush {
if _, ok := res.Value.(*tmsp_types.Response_Flush); ok {
err = bufWriter.Flush()
if err != nil {
closeConn <- fmt.Errorf("Error in handleResponses: %v", err.Error())
closeConn <- fmt.Errorf("Error in handleValue: %v", err.Error())
return
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment