mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-13 21:31:23 +00:00
node: NewNode takes DBProvider and GenDocProvider
This commit is contained in:
@ -67,7 +67,15 @@ func NewRunNodeCmd(signerAndApp FuncSignerAndApp) *cobra.Command {
|
|||||||
|
|
||||||
// Create & start node
|
// Create & start node
|
||||||
privVal, clientCreator := signerAndApp(config)
|
privVal, clientCreator := signerAndApp(config)
|
||||||
n := node.NewNode(config, privVal, clientCreator, logger.With("module", "node"))
|
n, err := node.NewNode(config,
|
||||||
|
privVal,
|
||||||
|
clientCreator,
|
||||||
|
node.DefaultGenesisDocProviderFunc(config),
|
||||||
|
node.DefaultDBProvider,
|
||||||
|
logger.With("module", "node"))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to create node: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := n.Start(); err != nil {
|
if _, err := n.Start(); err != nil {
|
||||||
return fmt.Errorf("Failed to start node: %v", err)
|
return fmt.Errorf("Failed to start node: %v", err)
|
||||||
|
@ -268,7 +268,7 @@ func loadPrivValidator(config *cfg.Config) *types.PrivValidatorFS {
|
|||||||
|
|
||||||
func fixedConsensusStateDummy() *ConsensusState {
|
func fixedConsensusStateDummy() *ConsensusState {
|
||||||
stateDB := dbm.NewMemDB()
|
stateDB := dbm.NewMemDB()
|
||||||
state := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile())
|
state, _ := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile())
|
||||||
state.SetLogger(log.TestingLogger().With("module", "state"))
|
state.SetLogger(log.TestingLogger().With("module", "state"))
|
||||||
privValidator := loadPrivValidator(config)
|
privValidator := loadPrivValidator(config)
|
||||||
cs := newConsensusState(state, privValidator, dummy.NewDummyApplication())
|
cs := newConsensusState(state, privValidator, dummy.NewDummyApplication())
|
||||||
@ -338,7 +338,7 @@ func randConsensusNet(nValidators int, testName string, tickerFunc func() Timeou
|
|||||||
logger := consensusLogger()
|
logger := consensusLogger()
|
||||||
for i := 0; i < nValidators; i++ {
|
for i := 0; i < nValidators; i++ {
|
||||||
db := dbm.NewMemDB() // each state needs its own db
|
db := dbm.NewMemDB() // each state needs its own db
|
||||||
state := sm.MakeGenesisState(db, genDoc)
|
state, _ := sm.MakeGenesisState(db, genDoc)
|
||||||
state.SetLogger(logger.With("module", "state", "validator", i))
|
state.SetLogger(logger.With("module", "state", "validator", i))
|
||||||
state.Save()
|
state.Save()
|
||||||
thisConfig := ResetConfig(Fmt("%s_%d", testName, i))
|
thisConfig := ResetConfig(Fmt("%s_%d", testName, i))
|
||||||
@ -359,7 +359,7 @@ func randConsensusNetWithPeers(nValidators, nPeers int, testName string, tickerF
|
|||||||
css := make([]*ConsensusState, nPeers)
|
css := make([]*ConsensusState, nPeers)
|
||||||
for i := 0; i < nPeers; i++ {
|
for i := 0; i < nPeers; i++ {
|
||||||
db := dbm.NewMemDB() // each state needs its own db
|
db := dbm.NewMemDB() // each state needs its own db
|
||||||
state := sm.MakeGenesisState(db, genDoc)
|
state, _ := sm.MakeGenesisState(db, genDoc)
|
||||||
state.SetLogger(log.TestingLogger().With("module", "state"))
|
state.SetLogger(log.TestingLogger().With("module", "state"))
|
||||||
state.Save()
|
state.Save()
|
||||||
thisConfig := ResetConfig(Fmt("%s_%d", testName, i))
|
thisConfig := ResetConfig(Fmt("%s_%d", testName, i))
|
||||||
@ -414,7 +414,7 @@ func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.G
|
|||||||
func randGenesisState(numValidators int, randPower bool, minPower int64) (*sm.State, []*types.PrivValidatorFS) {
|
func randGenesisState(numValidators int, randPower bool, minPower int64) (*sm.State, []*types.PrivValidatorFS) {
|
||||||
genDoc, privValidators := randGenesisDoc(numValidators, randPower, minPower)
|
genDoc, privValidators := randGenesisDoc(numValidators, randPower, minPower)
|
||||||
db := dbm.NewMemDB()
|
db := dbm.NewMemDB()
|
||||||
s0 := sm.MakeGenesisState(db, genDoc)
|
s0, _ := sm.MakeGenesisState(db, genDoc)
|
||||||
s0.SetLogger(log.TestingLogger().With("module", "state"))
|
s0.SetLogger(log.TestingLogger().With("module", "state"))
|
||||||
s0.Save()
|
s0.Save()
|
||||||
return s0, privValidators
|
return s0, privValidators
|
||||||
|
@ -241,12 +241,15 @@ func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cfg.ConsensusCo
|
|||||||
|
|
||||||
// Get State
|
// Get State
|
||||||
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
||||||
state := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile())
|
state, err := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile())
|
||||||
|
if err != nil {
|
||||||
|
cmn.Exit(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
// Create proxyAppConn connection (consensus, mempool, query)
|
// Create proxyAppConn connection (consensus, mempool, query)
|
||||||
clientCreator := proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir())
|
clientCreator := proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir())
|
||||||
proxyApp := proxy.NewAppConns(clientCreator, NewHandshaker(state, blockStore))
|
proxyApp := proxy.NewAppConns(clientCreator, NewHandshaker(state, blockStore))
|
||||||
_, err := proxyApp.Start()
|
_, err = proxyApp.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmn.Exit(cmn.Fmt("Error starting proxy app conns: %v", err))
|
cmn.Exit(cmn.Fmt("Error starting proxy app conns: %v", err))
|
||||||
}
|
}
|
||||||
|
@ -558,7 +558,7 @@ func readPieceFromWAL(msgBytes []byte) (interface{}, error) {
|
|||||||
// fresh state and mock store
|
// fresh state and mock store
|
||||||
func stateAndStore(config *cfg.Config, pubKey crypto.PubKey) (*sm.State, *mockBlockStore) {
|
func stateAndStore(config *cfg.Config, pubKey crypto.PubKey) (*sm.State, *mockBlockStore) {
|
||||||
stateDB := dbm.NewMemDB()
|
stateDB := dbm.NewMemDB()
|
||||||
state := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile())
|
state, _ := sm.MakeGenesisStateFromFile(stateDB, config.GenesisFile())
|
||||||
state.SetLogger(log.TestingLogger().With("module", "state"))
|
state.SetLogger(log.TestingLogger().With("module", "state"))
|
||||||
|
|
||||||
store := NewMockBlockStore(config, state.Params())
|
store := NewMockBlockStore(config, state.Params())
|
||||||
|
128
node/node.go
128
node/node.go
@ -3,6 +3,7 @@ package node
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -10,11 +11,15 @@ import (
|
|||||||
abci "github.com/tendermint/abci/types"
|
abci "github.com/tendermint/abci/types"
|
||||||
crypto "github.com/tendermint/go-crypto"
|
crypto "github.com/tendermint/go-crypto"
|
||||||
wire "github.com/tendermint/go-wire"
|
wire "github.com/tendermint/go-wire"
|
||||||
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
|
dbm "github.com/tendermint/tmlibs/db"
|
||||||
|
"github.com/tendermint/tmlibs/log"
|
||||||
|
|
||||||
bc "github.com/tendermint/tendermint/blockchain"
|
bc "github.com/tendermint/tendermint/blockchain"
|
||||||
cfg "github.com/tendermint/tendermint/config"
|
cfg "github.com/tendermint/tendermint/config"
|
||||||
"github.com/tendermint/tendermint/consensus"
|
"github.com/tendermint/tendermint/consensus"
|
||||||
mempl "github.com/tendermint/tendermint/mempool"
|
mempl "github.com/tendermint/tendermint/mempool"
|
||||||
p2p "github.com/tendermint/tendermint/p2p"
|
"github.com/tendermint/tendermint/p2p"
|
||||||
"github.com/tendermint/tendermint/proxy"
|
"github.com/tendermint/tendermint/proxy"
|
||||||
rpccore "github.com/tendermint/tendermint/rpc/core"
|
rpccore "github.com/tendermint/tendermint/rpc/core"
|
||||||
grpccore "github.com/tendermint/tendermint/rpc/grpc"
|
grpccore "github.com/tendermint/tendermint/rpc/grpc"
|
||||||
@ -26,13 +31,44 @@ import (
|
|||||||
"github.com/tendermint/tendermint/state/txindex/null"
|
"github.com/tendermint/tendermint/state/txindex/null"
|
||||||
"github.com/tendermint/tendermint/types"
|
"github.com/tendermint/tendermint/types"
|
||||||
"github.com/tendermint/tendermint/version"
|
"github.com/tendermint/tendermint/version"
|
||||||
cmn "github.com/tendermint/tmlibs/common"
|
|
||||||
dbm "github.com/tendermint/tmlibs/db"
|
|
||||||
"github.com/tendermint/tmlibs/log"
|
|
||||||
|
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// DBContext specifies config information for loading a new DB.
|
||||||
|
type DBContext struct {
|
||||||
|
ID string
|
||||||
|
Config *cfg.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
// DBProvider takes a DBContext and returns an instantiated DB.
|
||||||
|
type DBProvider func(*DBContext) (dbm.DB, error)
|
||||||
|
|
||||||
|
// DefaultDBProvider returns a database using the DBBackend and DBDir
|
||||||
|
// specified in the ctx.Config.
|
||||||
|
func DefaultDBProvider(ctx *DBContext) (dbm.DB, error) {
|
||||||
|
return dbm.NewDB(ctx.ID, ctx.Config.DBBackend, ctx.Config.DBDir()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenesisDocProvider returns a GenesisDoc.
|
||||||
|
// It allows the GenesisDoc to be pulled from sources other than the
|
||||||
|
// filesystem, for instance from a distributed key-value store cluster.
|
||||||
|
type GenesisDocProvider func() (*types.GenesisDoc, error)
|
||||||
|
|
||||||
|
// DefaultGenesisDocProviderFunc returns a GenesisDocProvider that loads
|
||||||
|
// the GenesisDoc from the config.GenesisFile() on the filesystem.
|
||||||
|
func DefaultGenesisDocProviderFunc(config *cfg.Config) GenesisDocProvider {
|
||||||
|
return func() (*types.GenesisDoc, error) {
|
||||||
|
return types.GenesisDocFromFile(config.GenesisFile())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Node is the highest level interface to a full Tendermint node.
|
||||||
|
// It includes all configuration information and running services.
|
||||||
type Node struct {
|
type Node struct {
|
||||||
cmn.BaseService
|
cmn.BaseService
|
||||||
|
|
||||||
@ -58,24 +94,55 @@ type Node struct {
|
|||||||
txIndexer txindex.TxIndexer
|
txIndexer txindex.TxIndexer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeDefault(config *cfg.Config, logger log.Logger) *Node {
|
// NewNodeDefault returns a Tendermint node with default settings for the
|
||||||
|
// PrivValidator, ClientCreator, GenesisDoc, and DBProvider,
|
||||||
|
func NewNodeDefault(config *cfg.Config, logger log.Logger) (*Node, error) {
|
||||||
// Get PrivValidator
|
// Get PrivValidator
|
||||||
privValidator := types.LoadOrGenPrivValidatorFS(config.PrivValidatorFile())
|
privValidator := types.LoadOrGenPrivValidatorFS(config.PrivValidatorFile())
|
||||||
return NewNode(config, privValidator,
|
return NewNode(config,
|
||||||
proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), logger)
|
privValidator,
|
||||||
|
proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()),
|
||||||
|
DefaultGenesisDocProviderFunc(config),
|
||||||
|
DefaultDBProvider,
|
||||||
|
logger)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNode(config *cfg.Config, privValidator types.PrivValidator, clientCreator proxy.ClientCreator, logger log.Logger) *Node {
|
// NewNode returns a new, ready to go, Tendermint Node.
|
||||||
|
func NewNode(config *cfg.Config,
|
||||||
|
privValidator types.PrivValidator,
|
||||||
|
clientCreator proxy.ClientCreator,
|
||||||
|
genDocProvider GenesisDocProvider,
|
||||||
|
dbProvider DBProvider,
|
||||||
|
logger log.Logger) (*Node, error) {
|
||||||
|
|
||||||
// Get BlockStore
|
// Get BlockStore
|
||||||
blockStoreDB := dbm.NewDB("blockstore", config.DBBackend, config.DBDir())
|
blockStoreDB, err := dbProvider(&DBContext{"blockstore", config})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
blockStore := bc.NewBlockStore(blockStoreDB)
|
blockStore := bc.NewBlockStore(blockStoreDB)
|
||||||
|
|
||||||
consensusLogger := logger.With("module", "consensus")
|
consensusLogger := logger.With("module", "consensus")
|
||||||
stateLogger := logger.With("module", "state")
|
stateLogger := logger.With("module", "state")
|
||||||
|
|
||||||
// Get State
|
// Get State
|
||||||
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
stateDB, err := dbProvider(&DBContext{"state", config})
|
||||||
state := sm.GetState(stateDB, config.GenesisFile())
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
state := sm.LoadState(stateDB)
|
||||||
|
if state == nil {
|
||||||
|
genDoc, err := genDocProvider()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
state, err = sm.MakeGenesisState(stateDB, genDoc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
state.Save()
|
||||||
|
}
|
||||||
|
|
||||||
state.SetLogger(stateLogger)
|
state.SetLogger(stateLogger)
|
||||||
|
|
||||||
// Create the proxyApp, which manages connections (consensus, mempool, query)
|
// Create the proxyApp, which manages connections (consensus, mempool, query)
|
||||||
@ -85,7 +152,7 @@ func NewNode(config *cfg.Config, privValidator types.PrivValidator, clientCreato
|
|||||||
proxyApp := proxy.NewAppConns(clientCreator, handshaker)
|
proxyApp := proxy.NewAppConns(clientCreator, handshaker)
|
||||||
proxyApp.SetLogger(logger.With("module", "proxy"))
|
proxyApp.SetLogger(logger.With("module", "proxy"))
|
||||||
if _, err := proxyApp.Start(); err != nil {
|
if _, err := proxyApp.Start(); err != nil {
|
||||||
cmn.Exit(cmn.Fmt("Error starting proxy app connections: %v", err))
|
return nil, fmt.Errorf("Error starting proxy app connections: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// reload the state (it may have been updated by the handshake)
|
// reload the state (it may have been updated by the handshake)
|
||||||
@ -96,7 +163,10 @@ func NewNode(config *cfg.Config, privValidator types.PrivValidator, clientCreato
|
|||||||
var txIndexer txindex.TxIndexer
|
var txIndexer txindex.TxIndexer
|
||||||
switch config.TxIndex {
|
switch config.TxIndex {
|
||||||
case "kv":
|
case "kv":
|
||||||
store := dbm.NewDB("tx_index", config.DBBackend, config.DBDir())
|
store, err := dbProvider(&DBContext{"tx_index", config})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
txIndexer = kv.NewTxIndex(store)
|
txIndexer = kv.NewTxIndex(store)
|
||||||
default:
|
default:
|
||||||
txIndexer = &null.TxIndex{}
|
txIndexer = &null.TxIndex{}
|
||||||
@ -109,9 +179,8 @@ func NewNode(config *cfg.Config, privValidator types.PrivValidator, clientCreato
|
|||||||
// Make event switch
|
// Make event switch
|
||||||
eventSwitch := types.NewEventSwitch()
|
eventSwitch := types.NewEventSwitch()
|
||||||
eventSwitch.SetLogger(logger.With("module", "types"))
|
eventSwitch.SetLogger(logger.With("module", "types"))
|
||||||
_, err := eventSwitch.Start()
|
if _, err := eventSwitch.Start(); err != nil {
|
||||||
if err != nil {
|
return nil, fmt.Errorf("Failed to start switch: %v", err)
|
||||||
cmn.Exit(cmn.Fmt("Failed to start switch: %v", err))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decide whether to fast-sync or not
|
// Decide whether to fast-sync or not
|
||||||
@ -232,9 +301,10 @@ func NewNode(config *cfg.Config, privValidator types.PrivValidator, clientCreato
|
|||||||
txIndexer: txIndexer,
|
txIndexer: txIndexer,
|
||||||
}
|
}
|
||||||
node.BaseService = *cmn.NewBaseService(logger, "Node", node)
|
node.BaseService = *cmn.NewBaseService(logger, "Node", node)
|
||||||
return node
|
return node, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnStart starts the Node. It implements cmn.Service.
|
||||||
func (n *Node) OnStart() error {
|
func (n *Node) OnStart() error {
|
||||||
// Create & add listener
|
// Create & add listener
|
||||||
protocol, address := ProtocolAndAddress(n.config.P2P.ListenAddress)
|
protocol, address := ProtocolAndAddress(n.config.P2P.ListenAddress)
|
||||||
@ -270,6 +340,7 @@ func (n *Node) OnStart() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnStop stops the Node. It implements cmn.Service.
|
||||||
func (n *Node) OnStop() {
|
func (n *Node) OnStop() {
|
||||||
n.BaseService.OnStop()
|
n.BaseService.OnStop()
|
||||||
|
|
||||||
@ -285,6 +356,7 @@ func (n *Node) OnStop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RunForever waits for an interupt signal and stops the node.
|
||||||
func (n *Node) RunForever() {
|
func (n *Node) RunForever() {
|
||||||
// Sleep forever and then...
|
// Sleep forever and then...
|
||||||
cmn.TrapSignal(func() {
|
cmn.TrapSignal(func() {
|
||||||
@ -292,15 +364,15 @@ func (n *Node) RunForever() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the event switch to reactors, mempool, etc.
|
// SetEventSwitch adds the event switch to reactors, mempool, etc.
|
||||||
func SetEventSwitch(evsw types.EventSwitch, eventables ...types.Eventable) {
|
func SetEventSwitch(evsw types.EventSwitch, eventables ...types.Eventable) {
|
||||||
for _, e := range eventables {
|
for _, e := range eventables {
|
||||||
e.SetEventSwitch(evsw)
|
e.SetEventSwitch(evsw)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a Listener to accept inbound peer connections.
|
// AddListener adds a listener to accept inbound peer connections.
|
||||||
// Add listeners before starting the Node.
|
// It should be called before starting the Node.
|
||||||
// The first listener is the primary listener (in NodeInfo)
|
// The first listener is the primary listener (in NodeInfo)
|
||||||
func (n *Node) AddListener(l p2p.Listener) {
|
func (n *Node) AddListener(l p2p.Listener) {
|
||||||
n.sw.AddListener(l)
|
n.sw.AddListener(l)
|
||||||
@ -360,39 +432,48 @@ func (n *Node) startRPC() ([]net.Listener, error) {
|
|||||||
return listeners, nil
|
return listeners, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Switch returns the Node's Switch.
|
||||||
func (n *Node) Switch() *p2p.Switch {
|
func (n *Node) Switch() *p2p.Switch {
|
||||||
return n.sw
|
return n.sw
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BlockStore returns the Node's BlockStore.
|
||||||
func (n *Node) BlockStore() *bc.BlockStore {
|
func (n *Node) BlockStore() *bc.BlockStore {
|
||||||
return n.blockStore
|
return n.blockStore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConsensusState returns the Node's ConsensusState.
|
||||||
func (n *Node) ConsensusState() *consensus.ConsensusState {
|
func (n *Node) ConsensusState() *consensus.ConsensusState {
|
||||||
return n.consensusState
|
return n.consensusState
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConsensusReactor returns the Node's ConsensusReactor.
|
||||||
func (n *Node) ConsensusReactor() *consensus.ConsensusReactor {
|
func (n *Node) ConsensusReactor() *consensus.ConsensusReactor {
|
||||||
return n.consensusReactor
|
return n.consensusReactor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MempoolReactor returns the Node's MempoolReactor.
|
||||||
func (n *Node) MempoolReactor() *mempl.MempoolReactor {
|
func (n *Node) MempoolReactor() *mempl.MempoolReactor {
|
||||||
return n.mempoolReactor
|
return n.mempoolReactor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EventSwitch returns the Node's EventSwitch.
|
||||||
func (n *Node) EventSwitch() types.EventSwitch {
|
func (n *Node) EventSwitch() types.EventSwitch {
|
||||||
return n.evsw
|
return n.evsw
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX: for convenience
|
// PrivValidator returns the Node's PrivValidator.
|
||||||
|
// XXX: for convenience only!
|
||||||
func (n *Node) PrivValidator() types.PrivValidator {
|
func (n *Node) PrivValidator() types.PrivValidator {
|
||||||
return n.privValidator
|
return n.privValidator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenesisDoc returns the Node's GenesisDoc.
|
||||||
func (n *Node) GenesisDoc() *types.GenesisDoc {
|
func (n *Node) GenesisDoc() *types.GenesisDoc {
|
||||||
return n.genesisDoc
|
return n.genesisDoc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProxyApp returns the Node's AppConns, representing its connections to the ABCI application.
|
||||||
func (n *Node) ProxyApp() proxy.AppConns {
|
func (n *Node) ProxyApp() proxy.AppConns {
|
||||||
return n.proxyApp
|
return n.proxyApp
|
||||||
}
|
}
|
||||||
@ -442,15 +523,18 @@ func (n *Node) makeNodeInfo() *p2p.NodeInfo {
|
|||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// NodeInfo returns the Node's Info from the Switch.
|
||||||
func (n *Node) NodeInfo() *p2p.NodeInfo {
|
func (n *Node) NodeInfo() *p2p.NodeInfo {
|
||||||
return n.sw.NodeInfo()
|
return n.sw.NodeInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DialSeeds dials the given seeds on the Switch.
|
||||||
func (n *Node) DialSeeds(seeds []string) error {
|
func (n *Node) DialSeeds(seeds []string) error {
|
||||||
return n.sw.DialSeeds(n.addrBook, seeds)
|
return n.sw.DialSeeds(n.addrBook, seeds)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Defaults to tcp
|
// ProtocolAndAddress returns the transport protocol
|
||||||
|
// and the ip address from the given string. Defaults to tcp.
|
||||||
func ProtocolAndAddress(listenAddr string) (string, string) {
|
func ProtocolAndAddress(listenAddr string) (string, string) {
|
||||||
protocol, address := "tcp", listenAddr
|
protocol, address := "tcp", listenAddr
|
||||||
parts := strings.SplitN(address, "://", 2)
|
parts := strings.SplitN(address, "://", 2)
|
||||||
|
@ -4,15 +4,19 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
cfg "github.com/tendermint/tendermint/config"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/tendermint/tmlibs/log"
|
"github.com/tendermint/tmlibs/log"
|
||||||
|
|
||||||
|
cfg "github.com/tendermint/tendermint/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNodeStartStop(t *testing.T) {
|
func TestNodeStartStop(t *testing.T) {
|
||||||
config := cfg.ResetTestRoot("node_node_test")
|
config := cfg.ResetTestRoot("node_node_test")
|
||||||
|
|
||||||
// Create & start node
|
// Create & start node
|
||||||
n := NewNodeDefault(config, log.TestingLogger())
|
n, err := NewNodeDefault(config, log.TestingLogger())
|
||||||
|
assert.NoError(t, err, "expected no err on NewNodeDefault")
|
||||||
n.Start()
|
n.Start()
|
||||||
t.Logf("Started node %v", n.sw.NodeInfo())
|
t.Logf("Started node %v", n.sw.NodeInfo())
|
||||||
|
|
||||||
|
@ -81,6 +81,11 @@ func NewTendermint(app abci.Application) *nm.Node {
|
|||||||
privValidatorFile := config.PrivValidatorFile()
|
privValidatorFile := config.PrivValidatorFile()
|
||||||
privValidator := types.LoadOrGenPrivValidatorFS(privValidatorFile)
|
privValidator := types.LoadOrGenPrivValidatorFS(privValidatorFile)
|
||||||
papp := proxy.NewLocalClientCreator(app)
|
papp := proxy.NewLocalClientCreator(app)
|
||||||
node := nm.NewNode(config, privValidator, papp, logger)
|
node, err := nm.NewNode(config, privValidator, papp,
|
||||||
|
nm.DefaultGenesisDocProviderFunc(config),
|
||||||
|
nm.DefaultDBProvider, logger)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
@ -55,13 +55,14 @@ func makeTxs(blockNum int) (txs []types.Tx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func state() *State {
|
func state() *State {
|
||||||
return MakeGenesisState(dbm.NewMemDB(), &types.GenesisDoc{
|
s, _ := MakeGenesisState(dbm.NewMemDB(), &types.GenesisDoc{
|
||||||
ChainID: chainID,
|
ChainID: chainID,
|
||||||
Validators: []types.GenesisValidator{
|
Validators: []types.GenesisValidator{
|
||||||
types.GenesisValidator{privKey.PubKey(), 10000, "test"},
|
types.GenesisValidator{privKey.PubKey(), 10000, "test"},
|
||||||
},
|
},
|
||||||
AppHash: nil,
|
AppHash: nil,
|
||||||
})
|
})
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeBlock(num int, state *State) *types.Block {
|
func makeBlock(num int, state *State) *types.Block {
|
||||||
|
@ -68,14 +68,17 @@ type State struct {
|
|||||||
// GetState loads the most recent state from the database,
|
// GetState loads the most recent state from the database,
|
||||||
// or creates a new one from the given genesisFile and persists the result
|
// or creates a new one from the given genesisFile and persists the result
|
||||||
// to the database.
|
// to the database.
|
||||||
func GetState(stateDB dbm.DB, genesisFile string) *State {
|
func GetState(stateDB dbm.DB, genesisFile string) (*State, error) {
|
||||||
|
var err error
|
||||||
state := LoadState(stateDB)
|
state := LoadState(stateDB)
|
||||||
if state == nil {
|
if state == nil {
|
||||||
state = MakeGenesisStateFromFile(stateDB, genesisFile)
|
state, err = MakeGenesisStateFromFile(stateDB, genesisFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
state.Save()
|
state.Save()
|
||||||
}
|
}
|
||||||
|
return state, nil
|
||||||
return state
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadState loads the State from the database.
|
// LoadState loads the State from the database.
|
||||||
@ -316,25 +319,34 @@ func (vi *ValidatorsInfo) Bytes() []byte {
|
|||||||
// file.
|
// file.
|
||||||
//
|
//
|
||||||
// Used during replay and in tests.
|
// Used during replay and in tests.
|
||||||
func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State {
|
func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) (*State, error) {
|
||||||
|
genDoc, err := MakeGenesisDocFromFile(genDocFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return MakeGenesisState(db, genDoc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeGenesisDocFromFile reads and unmarshals genesis doc from the given file.
|
||||||
|
func MakeGenesisDocFromFile(genDocFile string) (*types.GenesisDoc, error) {
|
||||||
genDocJSON, err := ioutil.ReadFile(genDocFile)
|
genDocJSON, err := ioutil.ReadFile(genDocFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmn.Exit(cmn.Fmt("Couldn't read GenesisDoc file: %v", err))
|
return nil, fmt.Errorf("Couldn't read GenesisDoc file: %v", err)
|
||||||
}
|
}
|
||||||
genDoc, err := types.GenesisDocFromJSON(genDocJSON)
|
genDoc, err := types.GenesisDocFromJSON(genDocJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmn.Exit(cmn.Fmt("Error reading GenesisDoc: %v", err))
|
return nil, fmt.Errorf("Error reading GenesisDoc: %v", err)
|
||||||
}
|
}
|
||||||
return MakeGenesisState(db, genDoc)
|
return genDoc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeGenesisState creates state from types.GenesisDoc.
|
// MakeGenesisState creates state from types.GenesisDoc.
|
||||||
//
|
//
|
||||||
// Used in tests.
|
// Used in tests.
|
||||||
func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State {
|
func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) (*State, error) {
|
||||||
err := genDoc.ValidateAndComplete()
|
err := genDoc.ValidateAndComplete()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmn.Exit(cmn.Fmt("Error in genesis file: %v", err))
|
return nil, fmt.Errorf("Error in genesis file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make validators slice
|
// Make validators slice
|
||||||
@ -363,5 +375,5 @@ func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State {
|
|||||||
AppHash: genDoc.AppHash,
|
AppHash: genDoc.AppHash,
|
||||||
TxIndexer: &null.TxIndex{}, // we do not need indexer during replay and in tests
|
TxIndexer: &null.TxIndex{}, // we do not need indexer during replay and in tests
|
||||||
LastHeightValidatorsChanged: 1,
|
LastHeightValidatorsChanged: 1,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,8 @@ import (
|
|||||||
func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, *State) {
|
func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, *State) {
|
||||||
config := cfg.ResetTestRoot("state_")
|
config := cfg.ResetTestRoot("state_")
|
||||||
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
|
||||||
state := GetState(stateDB, config.GenesisFile())
|
state, err := GetState(stateDB, config.GenesisFile())
|
||||||
|
assert.NoError(t, err, "expected no error on GetState")
|
||||||
state.SetLogger(log.TestingLogger())
|
state.SetLogger(log.TestingLogger())
|
||||||
|
|
||||||
tearDown := func(t *testing.T) {}
|
tearDown := func(t *testing.T) {}
|
||||||
|
Reference in New Issue
Block a user