From dca14e03401fb262c5f36371502503741747451c Mon Sep 17 00:00:00 2001 From: Peter Broadhurst Date: Mon, 30 Jun 2025 16:21:07 -0400 Subject: [PATCH] Add accessor to RPC interface, and coverage Signed-off-by: Peter Broadhurst --- cmd/evmconnect.go | 5 ++-- go.mod | 2 +- go.sum | 2 ++ internal/ethereum/ethereum.go | 38 ++++++++++++++++-------------- internal/ethereum/ethereum_test.go | 25 ++++++++++++++++++++ 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/cmd/evmconnect.go b/cmd/evmconnect.go index ec1638e..cf8373b 100644 --- a/cmd/evmconnect.go +++ b/cmd/evmconnect.go @@ -28,7 +28,6 @@ import ( "github.com/hyperledger/firefly-common/pkg/log" "github.com/hyperledger/firefly-evmconnect/internal/ethereum" fftmcmd "github.com/hyperledger/firefly-transaction-manager/cmd" - "github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi" "github.com/hyperledger/firefly-transaction-manager/pkg/fftm" txhandlerfactory "github.com/hyperledger/firefly-transaction-manager/pkg/txhandler/registry" "github.com/hyperledger/firefly-transaction-manager/pkg/txhandler/simple" @@ -36,6 +35,8 @@ import ( "github.com/spf13/cobra" ) +type EthereumConnector ethereum.Connector + var sigs = make(chan os.Signal, 1) var rootCmd = &cobra.Command{ @@ -77,7 +78,7 @@ func InitConfig() { txhandlerfactory.RegisterHandler(&simple.TransactionHandlerFactory{}) } -func NewEthereumConnector(ctx context.Context, conf config.Section) (cc ffcapi.API, err error) { +func NewEthereumConnector(ctx context.Context, conf config.Section) (EthereumConnector, error) { return ethereum.NewEthereumConnector(ctx, conf) } diff --git a/go.mod b/go.mod index 6c004cf..cb7aa1c 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.23.0 require ( github.com/hashicorp/golang-lru v1.0.2 - github.com/hyperledger/firefly-common v1.5.1 + github.com/hyperledger/firefly-common v1.5.6-0.20250630201730-e234335c0381 github.com/hyperledger/firefly-signer v1.1.21 github.com/hyperledger/firefly-transaction-manager v1.3.20 github.com/sirupsen/logrus v1.9.3 diff --git a/go.sum b/go.sum index 4f2ef8c..fafc7f5 100644 --- a/go.sum +++ b/go.sum @@ -102,6 +102,8 @@ github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/hyperledger/firefly-common v1.5.1 h1:/DREi1ye1HfYr3GDLBhXuugeMzT4zg9EN2uTYFlVY6M= github.com/hyperledger/firefly-common v1.5.1/go.mod h1:1Xawm5PUhxT7k+CL/Kr3i1LE3cTTzoQwZMLimvlW8rs= +github.com/hyperledger/firefly-common v1.5.6-0.20250630201730-e234335c0381 h1:4mkvcaVwq9bopPQ7ZNfJqFiN2QA32cPSGUHSyEGSFno= +github.com/hyperledger/firefly-common v1.5.6-0.20250630201730-e234335c0381/go.mod h1:1Xawm5PUhxT7k+CL/Kr3i1LE3cTTzoQwZMLimvlW8rs= github.com/hyperledger/firefly-signer v1.1.21 h1:r7cTOw6e/6AtiXLf84wZy6Z7zppzlc191HokW2hv4N4= github.com/hyperledger/firefly-signer v1.1.21/go.mod h1:axrlSQeKrd124UdHF5L3MkTjb5DeTcbJxJNCZ3JmcWM= github.com/hyperledger/firefly-transaction-manager v1.3.20 h1:Rs9ZW4XyOk3+mH5XBmcP9yFAeKMBtcJyOPCq4N/MnXY= diff --git a/internal/ethereum/ethereum.go b/internal/ethereum/ethereum.go index 4cefdab..62fdb27 100644 --- a/internal/ethereum/ethereum.go +++ b/internal/ethereum/ethereum.go @@ -58,7 +58,12 @@ type ethConnector struct { txCache *lru.Cache } -func NewEthereumConnector(ctx context.Context, conf config.Section) (cc ffcapi.API, err error) { +type Connector interface { + ffcapi.API + RPC() rpcbackend.RPC +} + +func NewEthereumConnector(ctx context.Context, conf config.Section) (cc Connector, err error) { c := ðConnector{ eventStreams: make(map[fftypes.UUID]*eventStream), catchupPageSize: conf.GetInt64(EventsCatchupPageSize), @@ -70,23 +75,9 @@ func NewEthereumConnector(ctx context.Context, conf config.Section) (cc ffcapi.A 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) - } + c.retry.InitialDelay = withDeprecatedConfFallback(conf, conf.GetDuration, DeprecatedRetryInitDelay, RetryInitDelay) + c.retry.Factor = withDeprecatedConfFallback(conf, conf.GetFloat64, DeprecatedRetryFactor, RetryFactor) + c.retry.MaximumDelay = withDeprecatedConfFallback(conf, conf.GetDuration, DeprecatedRetryMaxDelay, RetryMaxDelay) 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) @@ -152,6 +143,10 @@ func NewEthereumConnector(ctx context.Context, conf config.Section) (cc ffcapi.A return c, nil } +func (c *ethConnector) RPC() rpcbackend.RPC { + return c.backend +} + // WaitClosed can be called after cancelling all the contexts, to wait for everything to close down func (c *ethConnector) WaitClosed() { if c.blockListener != nil { @@ -161,3 +156,10 @@ func (c *ethConnector) WaitClosed() { <-s.streamLoopDone } } + +func withDeprecatedConfFallback[T any](conf config.Section, getter func(string) T, deprecatedKey, newKey string) T { + if !conf.IsSet(deprecatedKey) || (conf.IsSet(deprecatedKey) && conf.IsSet(newKey)) { + return getter(newKey) + } + return getter(deprecatedKey) +} diff --git a/internal/ethereum/ethereum_test.go b/internal/ethereum/ethereum_test.go index b6f5651..cf35995 100644 --- a/internal/ethereum/ethereum_test.go +++ b/internal/ethereum/ethereum_test.go @@ -29,6 +29,7 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) func strPtr(s string) *string { return &s } @@ -59,6 +60,8 @@ func newTestConnectorWithNoBlockerFilterDefaultMocks(t *testing.T, confSetup ... ctx, done := context.WithCancel(context.Background()) cc, err := NewEthereumConnector(ctx, conf) assert.NoError(t, err) + assert.NotNil(t, cc.RPC()) + c := cc.(*ethConnector) c.backend = mRPC c.blockListener.backend = mRPC @@ -193,3 +196,25 @@ func TestNewEthereumConnectorConfig(t *testing.T) { assert.Equal(t, 4.0, cc.(*ethConnector).retry.Factor) assert.Equal(t, 30*time.Second, cc.(*ethConnector).retry.MaximumDelay) } + +func TestWithDeprecatedConfFallback(t *testing.T) { + + config.RootConfigReset() + conf := config.RootSection("tdcf") + conf.AddKnownKey("deprecatedKey") + conf.AddKnownKey("newKey") + + conf.Set("deprecatedKey", 1111) + require.Equal(t, 1111, withDeprecatedConfFallback(conf, conf.GetInt, "deprecatedKey", "newKey")) + + conf.Set("newKey", 2222) + require.Equal(t, 2222, withDeprecatedConfFallback(conf, conf.GetInt, "deprecatedKey", "newKey")) + + config.RootConfigReset() + conf = config.RootSection("tdcf") + conf.AddKnownKey("deprecatedKey") + conf.AddKnownKey("newKey") + conf.Set("newKey", 2222) + require.Equal(t, 2222, withDeprecatedConfFallback(conf, conf.GetInt, "deprecatedKey", "newKey")) + +}