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

Add DISABLE-TX-SUB configuration parameter #4979

Merged
merged 32 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ef65a93
Add DisableTxSub param
aditya1702 Jul 25, 2023
79562fb
Add tests
aditya1702 Jul 25, 2023
fdcdc65
Add test for ApplyFlags
aditya1702 Jul 26, 2023
a4c3640
Revert "Add tests"
aditya1702 Jul 26, 2023
88d50c7
Make changes - 1
aditya1702 Jul 26, 2023
b1d4c77
Merge branch 'master' into add-tx-disabled
aditya1702 Aug 1, 2023
0c72438
Update parameters_test.go
aditya1702 Aug 1, 2023
45bb55a
Add check for INGEST=false and refactor test for help output
aditya1702 Aug 2, 2023
bcca4f9
Update integration.go
aditya1702 Aug 2, 2023
4610cee
Check if integration tests have been enabled
aditya1702 Aug 2, 2023
8153de2
Remove INGEST=false condition for tx-submission
aditya1702 Aug 3, 2023
d3fa9e7
Merge branch 'master' into add-tx-disabled
aditya1702 Aug 9, 2023
9251635
Merge branch 'master' into add-tx-disabled
aditya1702 Aug 10, 2023
9103d40
Merge branch 'master' into add-tx-disabled
aditya1702 Aug 11, 2023
fe8984c
Update flags.go
aditya1702 Aug 14, 2023
1392018
Merge branch 'master' into add-tx-disabled
aditya1702 Aug 14, 2023
7304870
Merge branch 'master' into add-tx-disabled
aditya1702 Aug 15, 2023
43864dc
Make changes - 1
aditya1702 Aug 15, 2023
b0f03e0
Merge branch 'master' into add-tx-disabled
aditya1702 Aug 21, 2023
e31b120
Make changes - 2
aditya1702 Aug 21, 2023
43b8b74
Merge branch 'master' into add-tx-disabled
aditya1702 Aug 22, 2023
b165589
Merge branch 'add-tx-disabled' of https://github.com/aditya1702/go in…
aditya1702 Aug 22, 2023
a4ca08f
Make changes - 3
aditya1702 Aug 22, 2023
12ad799
Change type to String
aditya1702 Aug 22, 2023
7439eba
Make changes - 4
aditya1702 Aug 23, 2023
5377630
Merge branch 'master' into add-tx-disabled
aditya1702 Aug 25, 2023
3cc8813
Add tests for different configurations of DISABLE_TX_SUB
aditya1702 Aug 28, 2023
7d3b717
Update parameters_test.go
aditya1702 Aug 28, 2023
4c9125f
Add integration tests checking transaction submission
aditya1702 Aug 28, 2023
6e8cafc
Update txsub_test.go
aditya1702 Aug 28, 2023
8235160
Update parameters_test.go
aditya1702 Aug 28, 2023
b94e9cf
Update parameters_test.go
aditya1702 Aug 29, 2023
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
14 changes: 14 additions & 0 deletions services/horizon/internal/actions/submit_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type NetworkSubmitter interface {
type SubmitTransactionHandler struct {
Submitter NetworkSubmitter
NetworkPassphrase string
DisableTxSub bool
CoreStateGetter
}

Expand Down Expand Up @@ -155,6 +156,19 @@ func (handler SubmitTransactionHandler) GetResource(w HeaderWriter, r *http.Requ
return nil, hProblem.StaleHistory
}

if handler.DisableTxSub {
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
return nil, &problem.P{
Type: "transaction_submission_disabled",
Title: "Transaction Submission Disabled",
Status: http.StatusMethodNotAllowed,
Detail: "Transaction submission has been disabled for Horizon. " +
"To enable it again, set env variable DISABLE_TX_SUB to false.",
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
Extras: map[string]interface{}{
"envelope_xdr": raw,
},
}
}

submission := handler.Submitter.Submit(r.Context(), info.raw, info.parsed, info.hash)

select {
Expand Down
53 changes: 53 additions & 0 deletions services/horizon/internal/actions/submit_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"github.com/stretchr/testify/require"
)

const (
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
TransactionEnvelopeXDR = "AAAAAAGUcmKO5465JxTSLQOQljwk2SfqAJmZSG6JH6wtqpwhAAABLAAAAAAAAAABAAAAAAAAAAEAAAALaGVsbG8gd29ybGQAAAAAAwAAAAAAAAAAAAAAABbxCy3mLg3hiTqX4VUEEp60pFOrJNxYM1JtxXTwXhY2AAAAAAvrwgAAAAAAAAAAAQAAAAAW8Qst5i4N4Yk6l+FVBBKetKRTqyTcWDNSbcV08F4WNgAAAAAN4Lazj4x61AAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABLaqcIQAAAEBKwqWy3TaOxoGnfm9eUjfTRBvPf34dvDA0Nf+B8z4zBob90UXtuCqmQqwMCyH+okOI3c05br3khkH0yP4kCwcE"

Check failure on line 24 in services/horizon/internal/actions/submit_transaction_test.go

View workflow job for this annotation

GitHub Actions / golangci

line is 444 characters (lll)

Check failure on line 24 in services/horizon/internal/actions/submit_transaction_test.go

View workflow job for this annotation

GitHub Actions / golangci

line is 444 characters (lll)
)

func TestStellarCoreMalformedTx(t *testing.T) {
handler := SubmitTransactionHandler{}

Expand Down Expand Up @@ -153,3 +157,52 @@
_, err = handler.GetResource(w, request)
assert.Equal(t, hProblem.ClientDisconnected, err)
}

func TestDisableTxSubFlagSubmission(t *testing.T) {
mockSubmitChannel := make(chan txsub.Result)

mock := &coreStateGetterMock{}
mock.On("GetCoreState").Return(corestate.State{
Synced: true,
})

mockSubmitter := &networkSubmitterMock{}
mockSubmitter.On("Submit").Return(mockSubmitChannel)

handler := SubmitTransactionHandler{
Submitter: mockSubmitter,
NetworkPassphrase: network.PublicNetworkPassphrase,
DisableTxSub: true,
CoreStateGetter: mock,
}

form := url.Values{}
form.Set("tx", TransactionEnvelopeXDR)

var p = &problem.P{
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
Type: "transaction_submission_disabled",
Title: "Transaction Submission Disabled",
Status: http.StatusMethodNotAllowed,
Detail: "Transaction submission has been disabled for Horizon. " +
"To enable it again, set env variable DISABLE_TX_SUB to false.",
Extras: map[string]interface{}{
"envelope_xdr": TransactionEnvelopeXDR,
},
}

request, err := http.NewRequest(

Check failure on line 193 in services/horizon/internal/actions/submit_transaction_test.go

View workflow job for this annotation

GitHub Actions / golangci

should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)

Check failure on line 193 in services/horizon/internal/actions/submit_transaction_test.go

View workflow job for this annotation

GitHub Actions / golangci

should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)
"POST",
"https://horizon.stellar.org/transactions",
strings.NewReader(form.Encode()),
)

require.NoError(t, err)
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
ctx, cancel := context.WithCancel(request.Context())
cancel()
request = request.WithContext(ctx)

w := httptest.NewRecorder()
_, err = handler.GetResource(w, request)
assert.Equal(t, p, err)
}
35 changes: 18 additions & 17 deletions services/horizon/internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,22 +512,24 @@ func (a *App) init() error {
initTxSubMetrics(a)

routerConfig := httpx.RouterConfig{
DBSession: a.historyQ.SessionInterface,
TxSubmitter: a.submitter,
RateQuota: a.config.RateQuota,
BehindCloudflare: a.config.BehindCloudflare,
BehindAWSLoadBalancer: a.config.BehindAWSLoadBalancer,
SSEUpdateFrequency: a.config.SSEUpdateFrequency,
StaleThreshold: a.config.StaleThreshold,
ConnectionTimeout: a.config.ConnectionTimeout,
NetworkPassphrase: a.config.NetworkPassphrase,
MaxPathLength: a.config.MaxPathLength,
MaxAssetsPerPathRequest: a.config.MaxAssetsPerPathRequest,
PathFinder: a.paths,
PrometheusRegistry: a.prometheusRegistry,
CoreGetter: a,
HorizonVersion: a.horizonVersion,
FriendbotURL: a.config.FriendbotURL,
DBSession: a.historyQ.SessionInterface,
TxSubmitter: a.submitter,
RateQuota: a.config.RateQuota,
BehindCloudflare: a.config.BehindCloudflare,
BehindAWSLoadBalancer: a.config.BehindAWSLoadBalancer,
SSEUpdateFrequency: a.config.SSEUpdateFrequency,
StaleThreshold: a.config.StaleThreshold,
ConnectionTimeout: a.config.ConnectionTimeout,
NetworkPassphrase: a.config.NetworkPassphrase,
MaxPathLength: a.config.MaxPathLength,
MaxAssetsPerPathRequest: a.config.MaxAssetsPerPathRequest,
PathFinder: a.paths,
PrometheusRegistry: a.prometheusRegistry,
CoreGetter: a,
HorizonVersion: a.horizonVersion,
FriendbotURL: a.config.FriendbotURL,
EnableIngestionFiltering: a.config.EnableIngestionFiltering,
DisableTxSub: a.config.DisableTxSub,
HealthCheck: healthCheck{
session: a.historyQ.SessionInterface,
ctx: a.ctx,
Expand All @@ -537,7 +539,6 @@ func (a *App) init() error {
},
cache: newHealthCache(healthCacheTTL),
},
EnableIngestionFiltering: a.config.EnableIngestionFiltering,
}

if a.primaryHistoryQ != nil {
Expand Down
3 changes: 2 additions & 1 deletion services/horizon/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@ type Config struct {
// RoundingSlippageFilter excludes trades from /trade_aggregations with rounding slippage >x bps
RoundingSlippageFilter int
// Stellar network: 'testnet' or 'pubnet'
Network string
Network string
DisableTxSub bool
}
40 changes: 29 additions & 11 deletions services/horizon/internal/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ const (
// HistoryArchiveURLsFlagName is the command line flag for specifying the history archive URLs
HistoryArchiveURLsFlagName = "history-archive-urls"
// NetworkFlagName is the command line flag for specifying the "network"
NetworkFlagName = "network"
EnableIngestionFilteringFlag = "exp-enable-ingestion-filtering"
NetworkFlagName = "network"
EnableIngestionFilteringFlagName = "exp-enable-ingestion-filtering"
DisableTxSubFlagName = "disable-tx-sub"
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved

captiveCoreMigrationHint = "If you are migrating from Horizon 1.x.y, start with the Migration Guide here: https://developers.stellar.org/docs/run-api-server/migrating/"
// StellarPubnet is a constant representing the Stellar public network
Expand Down Expand Up @@ -146,6 +147,15 @@ func Flags() (*Config, support.ConfigOptions) {
Usage: "path to stellar core binary, look for the stellar-core binary in $PATH by default.",
ConfigKey: &config.CaptiveCoreBinaryPath,
},
&support.ConfigOption{
Name: DisableTxSubFlagName,
OptType: types.Bool,
FlagDefault: false,
Required: false,
Usage: "disables the transaction submission functionality of Horizon.",
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
ConfigKey: &config.DisableTxSub,
Hidden: true,
},
&support.ConfigOption{
Name: captiveCoreConfigAppendPathName,
OptType: types.String,
Expand Down Expand Up @@ -211,7 +221,7 @@ func Flags() (*Config, support.ConfigOptions) {
ConfigKey: &config.EnableCaptiveCoreIngestion,
},
&support.ConfigOption{
Name: EnableIngestionFilteringFlag,
Name: EnableIngestionFilteringFlagName,
OptType: types.Bool,
FlagDefault: true,
Required: false,
Expand Down Expand Up @@ -251,7 +261,7 @@ func Flags() (*Config, support.ConfigOptions) {
if existingValue == "" || existingValue == "." {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("Unable to determine the current directory: %s", err)
return fmt.Errorf("unable to determine the current directory: %s", err)
}
existingValue = cwd
}
Expand Down Expand Up @@ -388,7 +398,7 @@ func Flags() (*Config, support.ConfigOptions) {
CustomSetValue: func(co *support.ConfigOption) error {
ll, err := logrus.ParseLevel(viper.GetString(co.Name))
if err != nil {
return fmt.Errorf("Could not parse log-level: %v", viper.GetString(co.Name))
return fmt.Errorf("could not parse log-level: %v", viper.GetString(co.Name))
}
*(co.ConfigKey.(*logrus.Level)) = ll
return nil
Expand Down Expand Up @@ -798,6 +808,14 @@ func setCaptiveCoreConfiguration(config *Config) error {
config.StellarCoreURL = fmt.Sprintf("http://localhost:%d", config.CaptiveCoreToml.HTTPPort)
}

if !config.DisableTxSub {
if config.Network == "" && (config.NetworkPassphrase == "" || len(config.HistoryArchiveURLs) == 0) {
return fmt.Errorf("invalid config: --%s set to false but no --%s flag or "+
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
"ingestion parameters set (--%s and --%s)", DisableTxSubFlagName, NetworkFlagName,
NetworkPassphraseFlagName, HistoryArchiveURLsFlagName)
}
}

return nil
}

Expand All @@ -820,8 +838,6 @@ func ApplyFlags(config *Config, flags support.ConfigOptions, options ApplyOption
config.Ingest = true
}

config.EnableIngestionFiltering = true
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved

if config.Ingest {
// Migrations should be checked as early as possible. Apply and check
// only on ingesting instances which are required to have write-access
Expand Down Expand Up @@ -849,11 +865,12 @@ func ApplyFlags(config *Config, flags support.ConfigOptions, options ApplyOption
if viper.GetString(CaptiveCoreConfigPathName) != "" {
captiveCoreConfigFlag = CaptiveCoreConfigPathName
}
return fmt.Errorf("Invalid config: one or more captive core params passed (--%s or --%s) but --ingest not set. "+captiveCoreMigrationHint,
return fmt.Errorf("invalid config: one or more captive core params passed (--%s or --%s) but --ingest not set"+captiveCoreMigrationHint,
StellarCoreBinaryPathName, captiveCoreConfigFlag)
}
if config.StellarCoreDatabaseURL != "" {
return fmt.Errorf("Invalid config: --%s passed but --ingest not set. ", StellarCoreDBURLFlagName)
return fmt.Errorf("invalid config: --%s passed but --ingest not setcccccbgkvrnudnhglgvtnbcnunfghkgkjrdvrcekflli"+
"", StellarCoreDBURLFlagName)
}
}

Expand All @@ -863,7 +880,7 @@ func ApplyFlags(config *Config, flags support.ConfigOptions, options ApplyOption
if err == nil {
log.DefaultLogger.SetOutput(logFile)
} else {
return fmt.Errorf("Failed to open file to log: %s", err)
return fmt.Errorf("failed to open file to log: %s", err)
}
}

Expand All @@ -878,7 +895,8 @@ func ApplyFlags(config *Config, flags support.ConfigOptions, options ApplyOption
}

if config.BehindCloudflare && config.BehindAWSLoadBalancer {
return fmt.Errorf("Invalid config: Only one option of --behind-cloudflare and --behind-aws-load-balancer is allowed. If Horizon is behind both, use --behind-cloudflare only.")
return fmt.Errorf("invalid config: Only one option of --behind-cloudflare and --behind-aws-load-balancer is allowed." +
" If Horizon is behind both, use --behind-cloudflare only")
}

return nil
Expand Down
32 changes: 32 additions & 0 deletions services/horizon/internal/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

support "github.com/stellar/go/support/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -131,3 +132,34 @@ func Test_createCaptiveCoreConfig(t *testing.T) {
})
}
}

func TestInvalidConfigOutputFromApplyFlags(t *testing.T) {
_, flags := Flags()

tests := []struct {
name string
config Config
flags support.ConfigOptions
options ApplyOptions
errStr string
}{
{
name: "disable-tx-sub set to false and no network and ingestion params set",
config: Config{
NetworkPassphrase: "",
HistoryArchiveURLs: []string{},
DisableTxSub: false,
},
flags: flags,
options: ApplyOptions{RequireCaptiveCoreConfig: true, AlwaysIngest: false},
errStr: "invalid config: --disable-tx-sub set to false but ingestion has been disabled." +
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
"Set INGEST=true to enable transaction submission functionality",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := ApplyFlags(&tt.config, tt.flags, tt.options)
assert.Error(t, e, tt.errStr)
})
}
}
2 changes: 2 additions & 0 deletions services/horizon/internal/httpx/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type RouterConfig struct {
FriendbotURL *url.URL
HealthCheck http.Handler
EnableIngestionFiltering bool
DisableTxSub bool
}

type Router struct {
Expand Down Expand Up @@ -319,6 +320,7 @@ func (r *Router) addRoutes(config *RouterConfig, rateLimiter *throttled.HTTPRate
r.Method(http.MethodPost, "/transactions", ObjectActionHandler{actions.SubmitTransactionHandler{
Submitter: config.TxSubmitter,
NetworkPassphrase: config.NetworkPassphrase,
DisableTxSub: config.DisableTxSub,
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
CoreStateGetter: config.CoreGetter,
}})

Expand Down
4 changes: 2 additions & 2 deletions services/horizon/internal/integration/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func TestDeprecatedOutputForIngestionFilteringFlag(t *testing.T) {
"the same no-filtering result. Remove usage of this flag in all cases.")
}

func TestHelpOutputForNoIngestionFilteringFlag(t *testing.T) {
func TestHelpOutput(t *testing.T) {
config, flags := horizon.Flags()

horizonCmd := &cobra.Command{
Expand Down Expand Up @@ -461,9 +461,9 @@ func TestHelpOutputForNoIngestionFilteringFlag(t *testing.T) {
if err := horizonCmd.Execute(); err != nil {
fmt.Println(err)
}

output := writer.(*bytes.Buffer).String()
assert.NotContains(t, output, "--exp-enable-ingestion-filtering")
assert.NotContains(t, output, "--disable-tx-sub")
aditya1702 marked this conversation as resolved.
Show resolved Hide resolved
}

// validateNoBucketDirPath ensures the Stellar Core auto-generated configuration
Expand Down
Loading