You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

  1. // Package node provides a high level wrapper around tendermint services.
  2. package node
  3. import (
  4. "fmt"
  5. "github.com/tendermint/tendermint/config"
  6. "github.com/tendermint/tendermint/internal/p2p"
  7. "github.com/tendermint/tendermint/libs/log"
  8. "github.com/tendermint/tendermint/libs/service"
  9. "github.com/tendermint/tendermint/privval"
  10. "github.com/tendermint/tendermint/proxy"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. // NewDefault constructs a tendermint node service for use in go
  14. // process that host their own process-local tendermint node. This is
  15. // equivalent to running tendermint in it's own process communicating
  16. // to an external ABCI application.
  17. func NewDefault(conf *config.Config, logger log.Logger) (service.Service, error) {
  18. return newDefaultNode(conf, logger)
  19. }
  20. // New constructs a tendermint node. The ClientCreator makes it
  21. // possible to construct an ABCI application that runs in the same
  22. // process as the tendermint node. The final option is a pointer to a
  23. // Genesis document: if the value is nil, the genesis document is read
  24. // from the file specified in the config, and otherwise the node uses
  25. // value of the final argument.
  26. func New(conf *config.Config,
  27. logger log.Logger,
  28. cf proxy.ClientCreator,
  29. gen *types.GenesisDoc,
  30. ) (service.Service, error) {
  31. nodeKey, err := p2p.LoadOrGenNodeKey(conf.NodeKeyFile())
  32. if err != nil {
  33. return nil, fmt.Errorf("failed to load or gen node key %s: %w", conf.NodeKeyFile(), err)
  34. }
  35. var genProvider genesisDocProvider
  36. switch gen {
  37. case nil:
  38. genProvider = defaultGenesisDocProviderFunc(conf)
  39. default:
  40. genProvider = func() (*types.GenesisDoc, error) { return gen, nil }
  41. }
  42. switch conf.Mode {
  43. case config.ModeFull, config.ModeValidator:
  44. pval, err := privval.LoadOrGenFilePV(conf.PrivValidatorKeyFile(), conf.PrivValidatorStateFile())
  45. if err != nil {
  46. return nil, err
  47. }
  48. return makeNode(conf,
  49. pval,
  50. nodeKey,
  51. cf,
  52. genProvider,
  53. config.DefaultDBProvider,
  54. logger)
  55. case config.ModeSeed:
  56. return makeSeedNode(conf, config.DefaultDBProvider, nodeKey, genProvider, logger)
  57. default:
  58. return nil, fmt.Errorf("%q is not a valid mode", conf.Mode)
  59. }
  60. }