Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services/horizon: Refactor Captive Core configuration flags #3262

Merged
merged 15 commits into from
Dec 4, 2020
Merged
26 changes: 18 additions & 8 deletions exp/services/captivecore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (

func main() {
var port int
var networkPassphrase, binaryPath, configPath, dbURL string
var networkPassphrase, binaryPath, configAddendumPath, dbURL string
var historyArchiveURLs []string
var stellarCoreHTTPPort uint
var logLevel logrus.Level
logger := supportlog.New()

Expand Down Expand Up @@ -51,12 +52,12 @@ func main() {
ConfigKey: &binaryPath,
},
&config.ConfigOption{
Name: "stellar-core-config-path",
Name: "captive-core-addendum-path",
OptType: types.String,
FlagDefault: "",
Required: false,
Usage: "path to stellar core config file",
ConfigKey: &configPath,
Usage: "path to an addendum for the Stellar Core configuration file used by captive core. It must, at least, include enough details to define a quorum set",
ConfigKey: &configAddendumPath,
},
&config.ConfigOption{
Name: "history-archive-urls",
Expand Down Expand Up @@ -94,6 +95,14 @@ func main() {
Required: false,
Usage: "horizon postgres database to connect with",
},
&config.ConfigOption{
Name: "stellar-captive-core-http-port",
ConfigKey: &stellarCoreHTTPPort,
OptType: types.Uint,
FlagDefault: uint(11626),
Required: false,
Usage: "HTTP port for captive core to listen on (0 disables the HTTP server)",
},
}
cmd := &cobra.Command{
Use: "captivecore",
Expand All @@ -104,10 +113,11 @@ func main() {
logger.SetLevel(logLevel)

captiveConfig := ledgerbackend.CaptiveCoreConfig{
StellarCoreBinaryPath: binaryPath,
StellarCoreConfigPath: configPath,
NetworkPassphrase: networkPassphrase,
HistoryArchiveURLs: historyArchiveURLs,
BinaryPath: binaryPath,
AddendumPath: configAddendumPath,
NetworkPassphrase: networkPassphrase,
HistoryArchiveURLs: historyArchiveURLs,
HTTPPort: stellarCoreHTTPPort,
}

var dbConn *db.Session
Expand Down
8 changes: 4 additions & 4 deletions exp/tools/captive-core-start-tester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func main() {
func check(ledger uint32) bool {
c, err := ledgerbackend.NewCaptive(
ledgerbackend.CaptiveCoreConfig{
StellarCoreBinaryPath: "stellar-core",
StellarCoreConfigPath: "stellar-core-standalone2.cfg",
NetworkPassphrase: "Standalone Network ; February 2017",
HistoryArchiveURLs: []string{"http://localhost:1570"},
BinaryPath: "stellar-core",
AddendumPath: "stellar-core-standalone2.cfg",
NetworkPassphrase: "Standalone Network ; February 2017",
HistoryArchiveURLs: []string{"http://localhost:1570"},
},
)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions ingest/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ func Example_changes() {
// Requires Stellar-Core 13.2.0+
backend, err := ledgerbackend.NewCaptive(
ledgerbackend.CaptiveCoreConfig{
StellarCoreBinaryPath: "/bin/stellar-core",
StellarCoreConfigPath: "/opt/stellar-core.cfg",
NetworkPassphrase: networkPassphrase,
HistoryArchiveURLs: []string{archiveURL},
BinaryPath: "/bin/stellar-core",
AddendumPath: "/opt/stellar-core.cfg",
NetworkPassphrase: networkPassphrase,
HistoryArchiveURLs: []string{archiveURL},
},
)
if err != nil {
Expand Down
47 changes: 21 additions & 26 deletions ingest/ledgerbackend/captive_core_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func roundDownToFirstReplayAfterCheckpointStart(ledger uint32) uint32 {
// keep ledger state in RAM. It requires around 3GB of RAM as of August 2020.
// * When a UnboundedRange is prepared it runs Stellar-Core catchup mode to
// sync with the first ledger and then runs it in a normal mode. This
// requires the configPath to be provided because a database connection is
// requires the coreConfigAddendumPath to be provided because a database connection is
// required and quorum set needs to be selected.
//
// The database requirement for UnboundedRange will soon be removed when some
Expand Down Expand Up @@ -62,12 +62,9 @@ func roundDownToFirstReplayAfterCheckpointStart(ledger uint32) uint32 {
//
// Requires Stellar-Core v13.2.0+.
type CaptiveStellarCore struct {
executablePath string
configPath string
networkPassphrase string
historyURLs []string
archive historyarchive.ArchiveInterface
ledgerHashStore TrustedLedgerHashStore
coreConfigAddendumPath string
archive historyarchive.ArchiveInterface
ledgerHashStore TrustedLedgerHashStore
2opremio marked this conversation as resolved.
Show resolved Hide resolved

// Quick note on how shutdown works:
// If Stellar-Core exits, the exit signal is "catched" by bufferedLedgerMetaReader
Expand All @@ -79,7 +76,7 @@ type CaptiveStellarCore struct {
stellarCoreRunner stellarCoreRunnerInterface

// For testing
stellarCoreRunnerFactory func(configPath string) (stellarCoreRunnerInterface, error)
stellarCoreRunnerFactory func(mode stellarCoreRunnerMode, captiveCoreAddendumPath string) (stellarCoreRunnerInterface, error)

// Defines if the blocking mode (off by default) is on or off. In blocking mode,
// calling GetLedger blocks until the requested ledger is available. This is useful
Expand All @@ -105,21 +102,23 @@ type CaptiveStellarCore struct {

// CaptiveCoreConfig contains all the parameters required to create a CaptiveStellarCore instance
type CaptiveCoreConfig struct {
// StellarCoreBinaryPath is the file path to the Stellar Core binary
StellarCoreBinaryPath string
// StellarCoreConfigPath is the file path to the Stellar Core configuration file used by captive core
StellarCoreConfigPath string
// BinaryPath is the file path to the Stellar Core binary
BinaryPath string
// AddendumPath is the file path to an addendum for the Stellar Core configuration file used by captive core
AddendumPath string
// NetworkPassphrase is the Stellar network passphrase used by captive core when connecting to the Stellar network
NetworkPassphrase string
// HistoryArchiveURLs are a list of history archive urls
HistoryArchiveURLs []string
// LedgerHashStore is an optional store used to obtain hashes for ledger sequences from a trusted source
LedgerHashStore TrustedLedgerHashStore
// HTTPPort is the TCP port to listen for requests (defaults to 0, which disables the HTTP server)
HTTPPort uint
}

// NewCaptive returns a new CaptiveStellarCore.
//
// All parameters are required, except configPath which is not required when
// All parameters are required, except coreConfigAddendumPath which is not required when
// working with BoundedRanges only.
func NewCaptive(config CaptiveCoreConfig) (*CaptiveStellarCore, error) {
archive, err := historyarchive.Connect(
Expand All @@ -134,19 +133,18 @@ func NewCaptive(config CaptiveCoreConfig) (*CaptiveStellarCore, error) {

c := &CaptiveStellarCore{
archive: archive,
executablePath: config.StellarCoreBinaryPath,
configPath: config.StellarCoreConfigPath,
historyURLs: config.HistoryArchiveURLs,
networkPassphrase: config.NetworkPassphrase,
coreConfigAddendumPath: config.AddendumPath,
waitIntervalPrepareRange: time.Second,
ledgerHashStore: config.LedgerHashStore,
}
c.stellarCoreRunnerFactory = func(configPath2 string) (stellarCoreRunnerInterface, error) {
c.stellarCoreRunnerFactory = func(mode stellarCoreRunnerMode, addendumPath string) (stellarCoreRunnerInterface, error) {
runner, innerErr := newStellarCoreRunner(
config.StellarCoreBinaryPath,
configPath2,
config.BinaryPath,
addendumPath,
config.NetworkPassphrase,
config.HTTPPort,
config.HistoryArchiveURLs,
mode,
)
if innerErr != nil {
return runner, innerErr
Expand Down Expand Up @@ -192,8 +190,8 @@ func (c *CaptiveStellarCore) openOfflineReplaySubprocess(from, to uint32) error
}

if c.stellarCoreRunner == nil {
// configPath is empty in an offline mode because it's generated
c.stellarCoreRunner, err = c.stellarCoreRunnerFactory("")
// coreConfigAddendumPath is empty in an offline mode because it's generated
c.stellarCoreRunner, err = c.stellarCoreRunnerFactory(stellarCoreRunnerModeOffline, "")
if err != nil {
return errors.Wrap(err, "error creating stellar-core runner")
}
Expand Down Expand Up @@ -245,10 +243,7 @@ func (c *CaptiveStellarCore) openOnlineReplaySubprocess(from uint32) error {
}

if c.stellarCoreRunner == nil {
if c.configPath == "" {
return errors.New("stellar-core config file path cannot be empty in an online mode")
}
c.stellarCoreRunner, err = c.stellarCoreRunnerFactory(c.configPath)
c.stellarCoreRunner, err = c.stellarCoreRunnerFactory(stellarCoreRunnerModeOnline, c.coreConfigAddendumPath)
if err != nil {
return errors.Wrap(err, "error creating stellar-core runner")
}
Expand Down
Loading