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.

75 lines
2.2 KiB

  1. // Package node provides a high level wrapper around tendermint services.
  2. package node
  3. import (
  4. "context"
  5. "fmt"
  6. abciclient "github.com/tendermint/tendermint/abci/client"
  7. "github.com/tendermint/tendermint/config"
  8. "github.com/tendermint/tendermint/libs/log"
  9. "github.com/tendermint/tendermint/libs/service"
  10. "github.com/tendermint/tendermint/privval"
  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(
  18. ctx context.Context,
  19. conf *config.Config,
  20. logger log.Logger,
  21. ) (service.Service, error) {
  22. return newDefaultNode(ctx, conf, logger)
  23. }
  24. // New constructs a tendermint node. The ClientCreator makes it
  25. // possible to construct an ABCI application that runs in the same
  26. // process as the tendermint node. The final option is a pointer to a
  27. // Genesis document: if the value is nil, the genesis document is read
  28. // from the file specified in the config, and otherwise the node uses
  29. // value of the final argument.
  30. func New(
  31. ctx context.Context,
  32. conf *config.Config,
  33. logger log.Logger,
  34. cf abciclient.Creator,
  35. gen *types.GenesisDoc,
  36. ) (service.Service, error) {
  37. nodeKey, err := types.LoadOrGenNodeKey(conf.NodeKeyFile())
  38. if err != nil {
  39. return nil, fmt.Errorf("failed to load or gen node key %s: %w", conf.NodeKeyFile(), err)
  40. }
  41. var genProvider genesisDocProvider
  42. switch gen {
  43. case nil:
  44. genProvider = defaultGenesisDocProviderFunc(conf)
  45. default:
  46. genProvider = func() (*types.GenesisDoc, error) { return gen, nil }
  47. }
  48. switch conf.Mode {
  49. case config.ModeFull, config.ModeValidator:
  50. pval, err := privval.LoadOrGenFilePV(conf.PrivValidator.KeyFile(), conf.PrivValidator.StateFile())
  51. if err != nil {
  52. return nil, err
  53. }
  54. return makeNode(
  55. ctx,
  56. conf,
  57. pval,
  58. nodeKey,
  59. cf,
  60. genProvider,
  61. config.DefaultDBProvider,
  62. logger)
  63. case config.ModeSeed:
  64. return makeSeedNode(ctx, conf, config.DefaultDBProvider, nodeKey, genProvider, logger)
  65. default:
  66. return nil, fmt.Errorf("%q is not a valid mode", conf.Mode)
  67. }
  68. }