From c220338f0364b4dbd10239cfd5e90a65bc127617 Mon Sep 17 00:00:00 2001 From: Chengxuan Xing Date: Mon, 9 Dec 2024 14:07:22 +0000 Subject: [PATCH] adding migration Signed-off-by: Chengxuan Xing --- config.md | 6 ++-- internal/ethereum/ethereum.go | 25 ++++++++++++---- internal/ethereum/ethereum_test.go | 38 +++++++++++++++++++++++++ internal/msgs/en_config_descriptions.go | 6 ++-- 4 files changed, 64 insertions(+), 11 deletions(-) diff --git a/config.md b/config.md index be65e4b..c8807d3 100644 --- a/config.md +++ b/config.md @@ -70,7 +70,7 @@ |connectionTimeout|The maximum amount of time that a connection is allowed to remain with no data transmitted|[`time.Duration`](https://pkg.go.dev/time#Duration)|`30s` |dataFormat|Configure the JSON data format for query output and events|map,flat_array,self_describing|`map` |expectContinueTimeout|See [ExpectContinueTimeout in the Go docs](https://pkg.go.dev/net/http#Transport)|[`time.Duration`](https://pkg.go.dev/time#Duration)|`1s` -|gasEstimationFactor|The factor to apply to the gas estimation to determine the gas limit|float|`1.5` +|gasEstimationFactor|The factor to apply to the gas estimation to determine the gas limit|`float32`|`1.5` |headers|Adds custom headers to HTTP requests|`map[string]string`|`` |hederaCompatibilityMode|Compatibility mode for Hedera, allowing non-standard block header hashes to be processed|`boolean`|`false` |idleTimeout|The max duration to hold a HTTP keepalive connection between calls|[`time.Duration`](https://pkg.go.dev/time#Duration)|`475ms` @@ -95,7 +95,7 @@ |Key|Description|Type|Default Value| |---|-----------|----|-------------| -|factor|Factor to increase the delay by, between each block indexing http requests retry to the RPC endpoint|`int`|`2` +|factor|Factor to increase the delay by, between each block indexing http requests retry to the RPC endpoint|`float32`|`2` |initialDelay|Initial delay for retrying block indexing http requests to the RPC endpoint|[`time.Duration`](https://pkg.go.dev/time#Duration)|`100ms` |maxDelay|Maximum delay for between each block indexing http requests retry to the RPC endpoint|[`time.Duration`](https://pkg.go.dev/time#Duration)|`30s` @@ -123,7 +123,7 @@ |count|The maximum number of times to retry|`int`|`5` |enabled|Enables retries|`boolean`|`false` |errorStatusCodeRegex|The regex that the error response status code must match to trigger retry|`string`|`` -|factor| use 'connector.blockListenerRetry.factor' instead|`int`|`` +|factor| use 'connector.blockListenerRetry.factor' instead|`float32`|`` |initWaitTime|The initial retry delay|[`time.Duration`](https://pkg.go.dev/time#Duration)|`250ms` |initialDelay| use 'connector.blockListenerRetry.initialDelay' instead|[`time.Duration`](https://pkg.go.dev/time#Duration)|`` |maxDelay| use 'connector.blockListenerRetry.maxDelay' instead|[`time.Duration`](https://pkg.go.dev/time#Duration)|`` diff --git a/internal/ethereum/ethereum.go b/internal/ethereum/ethereum.go index c2b5ee9..8736c28 100644 --- a/internal/ethereum/ethereum.go +++ b/internal/ethereum/ethereum.go @@ -66,12 +66,27 @@ func NewEthereumConnector(ctx context.Context, conf config.Section) (cc ffcapi.A eventBlockTimestamps: conf.GetBool(EventsBlockTimestamps), eventFilterPollingInterval: conf.GetDuration(EventsFilterPollingInterval), traceTXForRevertReason: conf.GetBool(TraceTXForRevertReason), - retry: &retry.Retry{ - InitialDelay: conf.GetDuration(RetryInitDelay), - MaximumDelay: conf.GetDuration(RetryMaxDelay), - Factor: conf.GetFloat64(RetryFactor), - }, + retry: &retry.Retry{}, } + + if !conf.IsSet(DeprecatedRetryInitDelay) || (conf.IsSet(DeprecatedRetryInitDelay) && conf.IsSet(RetryInitDelay)) { + c.retry.InitialDelay = conf.GetDuration(RetryInitDelay) + } else { + c.retry.InitialDelay = conf.GetDuration(DeprecatedRetryInitDelay) + } + + if !conf.IsSet(DeprecatedRetryFactor) || (conf.IsSet(DeprecatedRetryFactor) && conf.IsSet(RetryFactor)) { + c.retry.Factor = conf.GetFloat64(RetryFactor) + } else { + c.retry.Factor = conf.GetFloat64(DeprecatedRetryFactor) + } + + if !conf.IsSet(DeprecatedRetryMaxDelay) || (conf.IsSet(DeprecatedRetryMaxDelay) && conf.IsSet(RetryMaxDelay)) { + c.retry.MaximumDelay = conf.GetDuration(RetryMaxDelay) + } else { + c.retry.MaximumDelay = conf.GetDuration(DeprecatedRetryMaxDelay) + } + if c.catchupThreshold < c.catchupPageSize { log.L(ctx).Warnf("Catchup threshold %d must be at least as large as the catchup page size %d (overridden to %d)", c.catchupThreshold, c.catchupPageSize, c.catchupPageSize) c.catchupThreshold = c.catchupPageSize diff --git a/internal/ethereum/ethereum_test.go b/internal/ethereum/ethereum_test.go index c37575b..b69376f 100644 --- a/internal/ethereum/ethereum_test.go +++ b/internal/ethereum/ethereum_test.go @@ -19,6 +19,7 @@ package ethereum import ( "context" "testing" + "time" "github.com/hyperledger/firefly-common/pkg/config" "github.com/hyperledger/firefly-common/pkg/ffresty" @@ -153,3 +154,40 @@ func TestConnectorInit(t *testing.T) { cc, err = NewEthereumConnector(context.Background(), conf) assert.Regexp(t, "FF23051", err) } +func TestNewEthereumConnector(t *testing.T) { + // Test deprecated fields + config.RootConfigReset() + conf := config.RootSection("unittest") + InitConfig(conf) + conf.Set(ffresty.HTTPConfigURL, "http://localhost:8545") + + // check default + cc, err := NewEthereumConnector(context.Background(), conf) + assert.NoError(t, err) + assert.NotNil(t, cc) + assert.Equal(t, 100*time.Millisecond, cc.(*ethConnector).retry.InitialDelay) + assert.Equal(t, 2.0, cc.(*ethConnector).retry.Factor) + assert.Equal(t, 30*time.Second, cc.(*ethConnector).retry.MaximumDelay) + + // check default + conf.Set(DeprecatedRetryInitDelay, "100ms") + conf.Set(DeprecatedRetryFactor, 2.0) + conf.Set(DeprecatedRetryMaxDelay, "30s") + cc, err = NewEthereumConnector(context.Background(), conf) + assert.NoError(t, err) + assert.NotNil(t, cc) + assert.Equal(t, 100*time.Millisecond, cc.(*ethConnector).retry.InitialDelay) + assert.Equal(t, 2.0, cc.(*ethConnector).retry.Factor) + assert.Equal(t, 30*time.Second, cc.(*ethConnector).retry.MaximumDelay) + + // check new values set + conf.Set(RetryInitDelay, "10s") + conf.Set(RetryFactor, 4.0) + conf.Set(RetryMaxDelay, "30s") + cc, err = NewEthereumConnector(context.Background(), conf) + assert.NoError(t, err) + assert.NotNil(t, cc) + assert.Equal(t, 10*time.Second, cc.(*ethConnector).retry.InitialDelay) + assert.Equal(t, 4.0, cc.(*ethConnector).retry.Factor) + assert.Equal(t, 30*time.Second, cc.(*ethConnector).retry.MaximumDelay) +} diff --git a/internal/msgs/en_config_descriptions.go b/internal/msgs/en_config_descriptions.go index f8a317b..b4fa35c 100644 --- a/internal/msgs/en_config_descriptions.go +++ b/internal/msgs/en_config_descriptions.go @@ -30,14 +30,14 @@ var ( ConfigEthereumURL = ffc("config.connector.url", "URL of JSON/RPC endpoint for the Ethereum node/gateway", "string") ConfigEthereumWSEnabled = ffc("config.connector.ws.enabled", "When true a WebSocket is established for block listening, in addition to the HTTP RPC connections used for other functions", i18n.BooleanType) ConfigEthereumDataFormat = ffc("config.connector.dataFormat", "Configure the JSON data format for query output and events", "map,flat_array,self_describing") - ConfigEthereumGasEstimationFactor = ffc("config.connector.gasEstimationFactor", "The factor to apply to the gas estimation to determine the gas limit", "float") + ConfigEthereumGasEstimationFactor = ffc("config.connector.gasEstimationFactor", "The factor to apply to the gas estimation to determine the gas limit", i18n.FloatType) ConfigBlockCacheSize = ffc("config.connector.blockCacheSize", "Maximum of blocks to hold in the block info cache", i18n.IntType) ConfigBlockPollingInterval = ffc("config.connector.blockPollingInterval", "Interval for polling to check for new blocks", i18n.TimeDurationType) ConfigBlockIndexRetryInitialDelay = ffc("config.connector.blockListenerRetry.initialDelay", "Initial delay for retrying block indexing http requests to the RPC endpoint", i18n.TimeDurationType) - ConfigBlockIndexRetryFactor = ffc("config.connector.blockListenerRetry.factor", "Factor to increase the delay by, between each block indexing http requests retry to the RPC endpoint", i18n.IntType) + ConfigBlockIndexRetryFactor = ffc("config.connector.blockListenerRetry.factor", "Factor to increase the delay by, between each block indexing http requests retry to the RPC endpoint", i18n.FloatType) ConfigBlockIndexRetryMaxDelay = ffc("config.connector.blockListenerRetry.maxDelay", "Maximum delay for between each block indexing http requests retry to the RPC endpoint", i18n.TimeDurationType) DeprecatedConfigBlockIndexRetryInitialDelay = ffc("config.connector.retry.initialDelay", " use 'connector.blockListenerRetry.initialDelay' instead", i18n.TimeDurationType) - DeprecatedConfigBlockIndexRetryFactor = ffc("config.connector.retry.factor", " use 'connector.blockListenerRetry.factor' instead", i18n.IntType) + DeprecatedConfigBlockIndexRetryFactor = ffc("config.connector.retry.factor", " use 'connector.blockListenerRetry.factor' instead", i18n.FloatType) DeprecatedConfigBlockIndexRetryMaxDelay = ffc("config.connector.retry.maxDelay", " use 'connector.blockListenerRetry.maxDelay' instead", i18n.TimeDurationType) ConfigEventsBlockTimestamps = ffc("config.connector.events.blockTimestamps", "Whether to include the block timestamps in the event information", i18n.BooleanType) ConfigEventsCatchupPageSize = ffc("config.connector.events.catchupPageSize", "Number of blocks to query per poll when catching up to the head of the blockchain", i18n.IntType)