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

exp/services/captivecore: Captive Core API fixes #3232

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions exp/services/captivecore/internal/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type rangeRequest struct {
readyDuration int
valid bool
ready bool
sync.RWMutex
sync.Mutex
}

// CaptiveCoreAPI manages a shared captive core subprocess and exposes an API for
Expand Down Expand Up @@ -141,8 +141,8 @@ func (c *CaptiveCoreAPI) PrepareRange(ledgerRange ledgerbackend.Range) (ledgerba

// GetLatestLedgerSequence determines the latest ledger sequence available on the captive core instance.
func (c *CaptiveCoreAPI) GetLatestLedgerSequence() (ledgerbackend.LatestLedgerSequenceResponse, error) {
c.activeRequest.RLock()
defer c.activeRequest.RUnlock()
c.activeRequest.Lock()
defer c.activeRequest.Unlock()

if !c.activeRequest.valid {
return ledgerbackend.LatestLedgerSequenceResponse{}, ErrMissingPrepareRange
Expand All @@ -152,13 +152,16 @@ func (c *CaptiveCoreAPI) GetLatestLedgerSequence() (ledgerbackend.LatestLedgerSe
}

seq, err := c.core.GetLatestLedgerSequence()
if err != nil {
c.activeRequest.valid = false
}
return ledgerbackend.LatestLedgerSequenceResponse{Sequence: seq}, err
}

// GetLedger fetches the ledger with the given sequence number from the captive core instance.
func (c *CaptiveCoreAPI) GetLedger(sequence uint32) (ledgerbackend.LedgerResponse, error) {
c.activeRequest.RLock()
defer c.activeRequest.RUnlock()
c.activeRequest.Lock()
defer c.activeRequest.Unlock()

if !c.activeRequest.valid {
return ledgerbackend.LedgerResponse{}, ErrMissingPrepareRange
Expand All @@ -168,6 +171,9 @@ func (c *CaptiveCoreAPI) GetLedger(sequence uint32) (ledgerbackend.LedgerRespons
}

present, ledger, err := c.core.GetLedger(sequence)
if err != nil {
c.activeRequest.valid = false
}
return ledgerbackend.LedgerResponse{
Present: present,
Ledger: ledgerbackend.Base64Ledger(ledger),
Expand Down
5 changes: 3 additions & 2 deletions exp/services/captivecore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func main() {
Run: func(_ *cobra.Command, _ []string) {
configOpts.Require()
configOpts.SetValues()
logger.Level = logLevel
logger.SetLevel(logLevel)

captiveConfig := ledgerbackend.CaptiveCoreConfig{
StellarCoreBinaryPath: binaryPath,
Expand All @@ -124,7 +124,8 @@ func main() {
if err != nil {
logger.WithError(err).Fatal("Could not create captive core instance")
}
api := internal.NewCaptiveCoreAPI(core, logger)
core.SetStellarCoreLogger(logger.WithField("subservice", "stellar-core"))
api := internal.NewCaptiveCoreAPI(core, logger.WithField("subservice", "api"))

supporthttp.Run(supporthttp.Config{
ListenAddr: fmt.Sprintf(":%d", port),
Expand Down