Skip to content

Commit

Permalink
Fix separator logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Apr 30, 2021
1 parent 60c50ab commit 4436d00
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ UNSAFE_QUORUM = true
QUALITY = "MEDIUM"

[[VALIDATORS]]
ADDRESS = "core-testnet1.stellar.org"
ADDRESS = "localhost:123"
HOME_DOMAIN = "testnet.stellar.org"
NAME = "sdf_testnet_1"
PUBLIC_KEY = "GDKXE2OZMJIPOSLNA6N6F2BVCI3O777I2OOC4BV7VOYUEHYX7RTRYA7Y"
Expand Down
2 changes: 1 addition & 1 deletion ingest/ledgerbackend/testdata/expected-online-core.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PEER_PORT = 12345
QUALITY = "MEDIUM"

[[VALIDATORS]]
ADDRESS = "core-testnet1.stellar.org"
ADDRESS = "localhost:123"
HOME_DOMAIN = "testnet.stellar.org"
NAME = "sdf_testnet_1"
PUBLIC_KEY = "GDKXE2OZMJIPOSLNA6N6F2BVCI3O777I2OOC4BV7VOYUEHYX7RTRYA7Y"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PEER_PORT = 12345
QUALITY = "MEDIUM"

[[VALIDATORS]]
ADDRESS = "core-testnet1.stellar.org"
ADDRESS = "localhost:123"
HOME_DOMAIN = "testnet.stellar.org"
NAME = "sdf_testnet_1"
PUBLIC_KEY = "GDKXE2OZMJIPOSLNA6N6F2BVCI3O777I2OOC4BV7VOYUEHYX7RTRYA7Y"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NETWORK_PASSPHRASE = "Public Global Stellar Network ; September 2015"
QUALITY = "MEDIUM"

[[VALIDATORS]]
ADDRESS = "core-testnet1.stellar.org"
ADDRESS = "localhost:123"
HOME_DOMAIN = "testnet.stellar.org"
NAME = "sdf_testnet_1"
PUBLIC_KEY = "GDKXE2OZMJIPOSLNA6N6F2BVCI3O777I2OOC4BV7VOYUEHYX7RTRYA7Y"
Expand Down
2 changes: 1 addition & 1 deletion ingest/ledgerbackend/testdata/sample-appendix.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ QUALITY="MEDIUM"
NAME="sdf_testnet_1"
HOME_DOMAIN="testnet.stellar.org"
PUBLIC_KEY="GDKXE2OZMJIPOSLNA6N6F2BVCI3O777I2OOC4BV7VOYUEHYX7RTRYA7Y"
ADDRESS="core-testnet1.stellar.org"
ADDRESS="localhost:123"
86 changes: 64 additions & 22 deletions ingest/ledgerbackend/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,14 @@ func unmarshalTreeNode(t *toml.Tree, key string, dest interface{}) error {
return tree.Unmarshal(dest)
}

func (c *CaptiveCoreToml) unmarshal(text string) error {
func (c *CaptiveCoreToml) unmarshal(text string, params CaptiveCoreTomlParams) error {
var body captiveCoreTomlValues
quorumSetEntries := map[string]QuorumSet{}
historyEntries := map[string]History{}
separator := nonSubstring(text)
separator, err := generateSeparator(text, params)
if err != nil {
return errors.Wrap(err, "could not generate separator")
}
// The toml library has trouble with nested tables so we need to flatten all nested
// QUORUM_SET and HISTORY tables as a workaround.
// In Marshall() we apply the inverse process to unflatten the nested tables.
Expand Down Expand Up @@ -246,32 +249,76 @@ type CaptiveCoreTomlParams struct {

func NewCaptiveCoreTomlFromFile(configPath string, params CaptiveCoreTomlParams) (*CaptiveCoreToml, error) {
var captiveCoreToml CaptiveCoreToml
text, err := ioutil.ReadFile(configPath)
contents, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, errors.Wrap(err, "could not load toml path")
}

if err = captiveCoreToml.unmarshal(string(text)); err != nil {
if err = captiveCoreToml.unmarshal(string(contents), params); err != nil {
return nil, errors.Wrap(err, "could not unmarshall captive core toml")
}

if err = captiveCoreToml.validate(params); err != nil {
return nil, errors.Wrap(err, "invalid captive core toml")
}

captiveCoreToml.setDefaults(params)
setDefaults(&captiveCoreToml.captiveCoreTomlValues, captiveCoreToml.tree, params, captiveCoreToml.separator)
return &captiveCoreToml, nil
}

func generateSeparator(initialText string, params CaptiveCoreTomlParams) (string, error) {
var buf strings.Builder
buf.WriteString(initialText)

encoder := toml.NewEncoder(&buf)

tree, err := toml.TreeFromMap(map[string]interface{}{})
if err != nil {
return "", err
}
var defaultValues captiveCoreTomlValues
setDefaults(&defaultValues, tree, params, ".")

if err = encoder.Encode(defaultValues); err != nil {
return "", errors.Wrap(err, "could not encode toml file")
}

if err = encoder.Encode(defaultValues.HistoryEntries); err != nil {
return "", errors.Wrap(err, "could not serialize history archive tables")
}

if err = encoder.Encode(fakeQuorumSet()); err != nil {
return "", errors.Wrap(err, "could not serialize quorum set")
}

return nonSubstring(buf.String()), nil
}

func NewCaptiveCoreToml(params CaptiveCoreTomlParams) (*CaptiveCoreToml, error) {
var captiveCoreToml CaptiveCoreToml
var err error

captiveCoreToml.separator, err = generateSeparator("", params)
if err != nil {
return nil, errors.Wrap(err, "could not generate separator")
}
captiveCoreToml.tree, err = toml.TreeFromMap(map[string]interface{}{})
captiveCoreToml.separator = "---"
captiveCoreToml.setDefaults(params)
return &captiveCoreToml, err
if err != nil {
return nil, err
}

setDefaults(&captiveCoreToml.captiveCoreTomlValues, captiveCoreToml.tree, params, captiveCoreToml.separator)
return &captiveCoreToml, nil
}

func fakeQuorumSet() map[string]QuorumSet {
return map[string]QuorumSet{
"QUORUM_SET": QuorumSet{
ThresholdPercent: 100,
Validators: []string{"GCZBOIAY4HLKAJVNJORXZOZRAY2BJDBZHKPBHZCRAIUR5IHC2UHBGCQR"},
},
}
}
func (c *CaptiveCoreToml) CatchupToml() *CaptiveCoreToml {
offline := *c
offline.RunStandalone = true
Expand All @@ -283,48 +330,43 @@ func (c *CaptiveCoreToml) CatchupToml() *CaptiveCoreToml {
if !c.QuorumSetIsConfigured() {
// Add a fictional quorum -- necessary to convince core to start up;
// but not used at all for our purposes. Pubkey here is just random.
offline.QuorumSetEntries = map[string]QuorumSet{
"QUORUM_SET": QuorumSet{
ThresholdPercent: 100,
Validators: []string{"GCZBOIAY4HLKAJVNJORXZOZRAY2BJDBZHKPBHZCRAIUR5IHC2UHBGCQR"},
},
}
offline.QuorumSetEntries = fakeQuorumSet()
}
return &offline
}

func (c *CaptiveCoreToml) setDefaults(params CaptiveCoreTomlParams) {
if !c.tree.Has("NETWORK_PASSPHRASE") {
func setDefaults(c *captiveCoreTomlValues, tree *toml.Tree, params CaptiveCoreTomlParams, separator string) {
if !tree.Has("NETWORK_PASSPHRASE") {
c.NetworkPassphrase = params.NetworkPassphrase
}

if def := c.tree.Has("HTTP_PORT"); !def && params.HTTPPort != nil {
if def := tree.Has("HTTP_PORT"); !def && params.HTTPPort != nil {
c.HTTPPort = *params.HTTPPort
} else if !def && params.HTTPPort == nil {
c.HTTPPort = defaultHTTPPort
}

if def := c.tree.Has("PEER_PORT"); !def && params.PeerPort != nil {
if def := tree.Has("PEER_PORT"); !def && params.PeerPort != nil {
c.PeerPort = *params.PeerPort
}

if def := c.tree.Has("LOG_FILE_PATH"); !def && params.LogPath != nil {
if def := tree.Has("LOG_FILE_PATH"); !def && params.LogPath != nil {
c.LogFilePath = *params.LogPath
} else if !def && params.LogPath == nil {
c.LogFilePath = defaultLogFilePath
}

if !c.tree.Has("FAILURE_SAFETY") {
if !tree.Has("FAILURE_SAFETY") {
c.FailureSafety = defaultFailureSafety
}
if !c.tree.Has("DISABLE_XDR_FSYNC") {
if !tree.Has("DISABLE_XDR_FSYNC") {
c.DisableXDRFsync = defaultDisableXDRFsync
}

if !c.historyIsConfigured() {
c.HistoryEntries = map[string]History{}
for i, val := range params.HistoryArchiveURLs {
c.HistoryEntries[fmt.Sprintf("HISTORY%sh%d", c.separator, i)] = History{
c.HistoryEntries[fmt.Sprintf("HISTORY%sh%d", separator, i)] = History{
Get: fmt.Sprintf("curl -sf %s/{0} -o {1}", val),
}
}
Expand Down

0 comments on commit 4436d00

Please sign in to comment.