From a7febe53812a8792a45f66c2d7f62a54065843aa Mon Sep 17 00:00:00 2001 From: Cedric Cordenier Date: Wed, 14 Feb 2024 18:25:43 +0000 Subject: [PATCH] [chore] Replace clock with specialized lib --- .../gateway/connector/run_connector.go | 4 +-- core/scripts/go.mod | 1 + core/services/gateway/connectionmanager.go | 6 ++-- .../gateway/connectionmanager_test.go | 15 +++++----- core/services/gateway/connector/connector.go | 5 ++-- .../gateway/connector/connector_test.go | 6 ++-- core/services/gateway/gateway.go | 4 +-- .../gateway_integration_test.go | 4 +-- .../services/ocr2/plugins/functions/plugin.go | 6 ++-- core/services/s4/storage.go | 7 +++-- core/services/s4/storage_test.go | 5 ++-- core/utils/clock.go | 29 ------------------ core/utils/clock_test.go | 30 ------------------- go.mod | 1 + integration-tests/go.mod | 1 + integration-tests/go.sum | 2 ++ 16 files changed, 37 insertions(+), 89 deletions(-) delete mode 100644 core/utils/clock.go delete mode 100644 core/utils/clock_test.go diff --git a/core/scripts/gateway/connector/run_connector.go b/core/scripts/gateway/connector/run_connector.go index c6ad187461c..8d74bb88aec 100644 --- a/core/scripts/gateway/connector/run_connector.go +++ b/core/scripts/gateway/connector/run_connector.go @@ -9,13 +9,13 @@ import ( "os/signal" "github.com/ethereum/go-ethereum/crypto" + "github.com/jonboulle/clockwork" "github.com/pelletier/go-toml/v2" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/api" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/common" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/connector" - "github.com/smartcontractkit/chainlink/v2/core/utils" ) // Script to run Connector outside of the core node. @@ -69,7 +69,7 @@ func main() { sampleKey, _ := crypto.HexToECDSA("cd47d3fafdbd652dd2b66c6104fa79b372c13cb01f4a4fbfc36107cce913ac1d") lggr, _ := logger.NewLogger() client := &client{privateKey: sampleKey, lggr: lggr} - connector, _ := connector.NewGatewayConnector(&cfg, client, client, utils.NewRealClock(), lggr) + connector, _ := connector.NewGatewayConnector(&cfg, client, client, clockwork.NewRealClock(), lggr) client.connector = connector ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt) diff --git a/core/scripts/go.mod b/core/scripts/go.mod index 511e02c5554..e8070fdde05 100644 --- a/core/scripts/go.mod +++ b/core/scripts/go.mod @@ -13,6 +13,7 @@ require ( github.com/google/uuid v1.4.0 github.com/jmoiron/sqlx v1.3.5 github.com/joho/godotenv v1.4.0 + github.com/jonboulle/clockwork v0.4.0 github.com/manyminds/api2go v0.0.0-20171030193247-e7b693844a6f github.com/montanaflynn/stats v0.7.1 github.com/olekukonko/tablewriter v0.0.5 diff --git a/core/services/gateway/connectionmanager.go b/core/services/gateway/connectionmanager.go index a3c39211c6e..7438b0042d2 100644 --- a/core/services/gateway/connectionmanager.go +++ b/core/services/gateway/connectionmanager.go @@ -11,6 +11,7 @@ import ( "time" "github.com/gorilla/websocket" + "github.com/jonboulle/clockwork" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "go.uber.org/multierr" @@ -24,7 +25,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/gateway/handlers" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/network" "github.com/smartcontractkit/chainlink/v2/core/services/job" - "github.com/smartcontractkit/chainlink/v2/core/utils" ) var promKeepalivesSent = promauto.NewGaugeVec(prometheus.GaugeOpts{ @@ -47,7 +47,7 @@ type connectionManager struct { config *config.ConnectionManagerConfig dons map[string]*donConnectionManager wsServer network.WebSocketServer - clock utils.Clock + clock clockwork.Clock connAttempts map[string]*connAttempt connAttemptCounter uint64 connAttemptsMu sync.Mutex @@ -89,7 +89,7 @@ type connAttempt struct { timestamp uint32 } -func NewConnectionManager(gwConfig *config.GatewayConfig, clock utils.Clock, lggr logger.Logger) (ConnectionManager, error) { +func NewConnectionManager(gwConfig *config.GatewayConfig, clock clockwork.Clock, lggr logger.Logger) (ConnectionManager, error) { codec := &api.JsonRPCCodec{} dons := make(map[string]*donConnectionManager) for _, donConfig := range gwConfig.Dons { diff --git a/core/services/gateway/connectionmanager_test.go b/core/services/gateway/connectionmanager_test.go index b176837d9ca..1750e975889 100644 --- a/core/services/gateway/connectionmanager_test.go +++ b/core/services/gateway/connectionmanager_test.go @@ -4,8 +4,8 @@ import ( "crypto/ecdsa" "fmt" "testing" - "time" + "github.com/jonboulle/clockwork" "github.com/stretchr/testify/require" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" @@ -15,7 +15,6 @@ import ( gc "github.com/smartcontractkit/chainlink/v2/core/services/gateway/common" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/config" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/network" - "github.com/smartcontractkit/chainlink/v2/core/utils" ) const defaultConfig = ` @@ -44,7 +43,7 @@ func TestConnectionManager_NewConnectionManager_ValidConfig(t *testing.T) { tomlConfig := parseTOMLConfig(t, defaultConfig) - _, err := gateway.NewConnectionManager(tomlConfig, utils.NewFixedClock(time.Now()), logger.TestLogger(t)) + _, err := gateway.NewConnectionManager(tomlConfig, clockwork.NewFakeClock(), logger.TestLogger(t)) require.NoError(t, err) } @@ -86,7 +85,7 @@ Address = "0x68902D681c28119f9b2531473a417088bf008E59" fullConfig := ` [nodeServerConfig] Path = "/node"` + config - _, err := gateway.NewConnectionManager(parseTOMLConfig(t, fullConfig), utils.NewFixedClock(time.Now()), logger.TestLogger(t)) + _, err := gateway.NewConnectionManager(parseTOMLConfig(t, fullConfig), clockwork.NewFakeClock(), logger.TestLogger(t)) require.Error(t, err) }) } @@ -128,7 +127,7 @@ func TestConnectionManager_StartHandshake(t *testing.T) { config, nodes := newTestConfig(t, 4) unrelatedNode := gc.NewTestNodes(t, 1)[0] - clock := utils.NewFixedClock(time.Now()) + clock := clockwork.NewFakeClock() mgr, err := gateway.NewConnectionManager(config, clock, logger.TestLogger(t)) require.NoError(t, err) @@ -181,7 +180,7 @@ func TestConnectionManager_FinalizeHandshake(t *testing.T) { t.Parallel() config, nodes := newTestConfig(t, 4) - clock := utils.NewFixedClock(time.Now()) + clock := clockwork.NewFakeClock() mgr, err := gateway.NewConnectionManager(config, clock, logger.TestLogger(t)) require.NoError(t, err) @@ -215,7 +214,7 @@ func TestConnectionManager_SendToNode_Failures(t *testing.T) { t.Parallel() config, nodes := newTestConfig(t, 2) - clock := utils.NewFixedClock(time.Now()) + clock := clockwork.NewFakeClock() mgr, err := gateway.NewConnectionManager(config, clock, logger.TestLogger(t)) require.NoError(t, err) @@ -233,7 +232,7 @@ func TestConnectionManager_CleanStartClose(t *testing.T) { config, _ := newTestConfig(t, 2) config.ConnectionManagerConfig.HeartbeatIntervalSec = 1 - clock := utils.NewFixedClock(time.Now()) + clock := clockwork.NewFakeClock() mgr, err := gateway.NewConnectionManager(config, clock, logger.TestLogger(t)) require.NoError(t, err) diff --git a/core/services/gateway/connector/connector.go b/core/services/gateway/connector/connector.go index a05652607ba..4e9de2df40e 100644 --- a/core/services/gateway/connector/connector.go +++ b/core/services/gateway/connector/connector.go @@ -9,6 +9,7 @@ import ( "time" "github.com/gorilla/websocket" + "github.com/jonboulle/clockwork" "github.com/smartcontractkit/chainlink-common/pkg/services" "github.com/smartcontractkit/chainlink-common/pkg/utils/hex" @@ -51,7 +52,7 @@ type gatewayConnector struct { config *ConnectorConfig codec api.Codec - clock utils.Clock + clock clockwork.Clock nodeAddress []byte signer Signer handler GatewayConnectorHandler @@ -79,7 +80,7 @@ type gatewayState struct { wsClient network.WebSocketClient } -func NewGatewayConnector(config *ConnectorConfig, signer Signer, handler GatewayConnectorHandler, clock utils.Clock, lggr logger.Logger) (GatewayConnector, error) { +func NewGatewayConnector(config *ConnectorConfig, signer Signer, handler GatewayConnectorHandler, clock clockwork.Clock, lggr logger.Logger) (GatewayConnector, error) { if config == nil || signer == nil || handler == nil || clock == nil || lggr == nil { return nil, errors.New("nil dependency") } diff --git a/core/services/gateway/connector/connector_test.go b/core/services/gateway/connector/connector_test.go index 1c2c6d26b10..3dd782c626a 100644 --- a/core/services/gateway/connector/connector_test.go +++ b/core/services/gateway/connector/connector_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/jonboulle/clockwork" "github.com/pelletier/go-toml/v2" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -15,7 +16,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/gateway/connector" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/connector/mocks" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/network" - "github.com/smartcontractkit/chainlink/v2/core/utils" ) const defaultConfig = ` @@ -43,7 +43,7 @@ func parseTOMLConfig(t *testing.T, tomlConfig string) *connector.ConnectorConfig func newTestConnector(t *testing.T, config *connector.ConnectorConfig, now time.Time) (connector.GatewayConnector, *mocks.Signer, *mocks.GatewayConnectorHandler) { signer := mocks.NewSigner(t) handler := mocks.NewGatewayConnectorHandler(t) - clock := utils.NewFixedClock(now) + clock := clockwork.NewFakeClock() connector, err := connector.NewGatewayConnector(config, signer, handler, clock, logger.TestLogger(t)) require.NoError(t, err) return connector, signer, handler @@ -104,7 +104,7 @@ URL = "ws://localhost:8081/node" signer := mocks.NewSigner(t) handler := mocks.NewGatewayConnectorHandler(t) - clock := utils.NewFixedClock(time.Now()) + clock := clockwork.NewFakeClock() for name, config := range invalidCases { config := config t.Run(name, func(t *testing.T) { diff --git a/core/services/gateway/gateway.go b/core/services/gateway/gateway.go index 79ddf0a5c69..93ecc474bec 100644 --- a/core/services/gateway/gateway.go +++ b/core/services/gateway/gateway.go @@ -9,6 +9,7 @@ import ( "go.uber.org/multierr" "github.com/ethereum/go-ethereum/common" + "github.com/jonboulle/clockwork" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" @@ -20,7 +21,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/gateway/handlers" gw_net "github.com/smartcontractkit/chainlink/v2/core/services/gateway/network" "github.com/smartcontractkit/chainlink/v2/core/services/job" - "github.com/smartcontractkit/chainlink/v2/core/utils" ) var promRequest = promauto.NewCounterVec(prometheus.CounterOpts{ @@ -55,7 +55,7 @@ type gateway struct { func NewGatewayFromConfig(config *config.GatewayConfig, handlerFactory HandlerFactory, lggr logger.Logger) (Gateway, error) { codec := &api.JsonRPCCodec{} httpServer := gw_net.NewHttpServer(&config.UserServerConfig, lggr) - connMgr, err := NewConnectionManager(config, utils.NewRealClock(), lggr) + connMgr, err := NewConnectionManager(config, clockwork.NewRealClock(), lggr) if err != nil { return nil, err } diff --git a/core/services/gateway/integration_tests/gateway_integration_test.go b/core/services/gateway/integration_tests/gateway_integration_test.go index a2064b7a591..7f4a2ab58fa 100644 --- a/core/services/gateway/integration_tests/gateway_integration_test.go +++ b/core/services/gateway/integration_tests/gateway_integration_test.go @@ -11,6 +11,7 @@ import ( "sync/atomic" "testing" + "github.com/jonboulle/clockwork" "github.com/onsi/gomega" "github.com/pelletier/go-toml/v2" "github.com/stretchr/testify/require" @@ -23,7 +24,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/gateway/common" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/config" "github.com/smartcontractkit/chainlink/v2/core/services/gateway/connector" - "github.com/smartcontractkit/chainlink/v2/core/utils" ) const gatewayConfigTemplate = ` @@ -152,7 +152,7 @@ func TestIntegration_Gateway_NoFullNodes_BasicConnectionAndMessage(t *testing.T) // Launch Connector client := &client{privateKey: nodeKeys.PrivateKey} - connector, err := connector.NewGatewayConnector(parseConnectorConfig(t, nodeConfigTemplate, nodeKeys.Address, nodeUrl), client, client, utils.NewRealClock(), lggr) + connector, err := connector.NewGatewayConnector(parseConnectorConfig(t, nodeConfigTemplate, nodeKeys.Address, nodeUrl), client, client, clockwork.NewRealClock(), lggr) require.NoError(t, err) client.connector = connector servicetest.Run(t, connector) diff --git a/core/services/ocr2/plugins/functions/plugin.go b/core/services/ocr2/plugins/functions/plugin.go index 15e36f07b09..a49ce4be90a 100644 --- a/core/services/ocr2/plugins/functions/plugin.go +++ b/core/services/ocr2/plugins/functions/plugin.go @@ -8,6 +8,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/jmoiron/sqlx" + "github.com/jonboulle/clockwork" "github.com/pkg/errors" "github.com/smartcontractkit/libocr/commontypes" @@ -32,7 +33,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/pg" evmrelayTypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types" "github.com/smartcontractkit/chainlink/v2/core/services/s4" - "github.com/smartcontractkit/chainlink/v2/core/utils" ) type FunctionsServicesConfig struct { @@ -100,7 +100,7 @@ func NewFunctionsServices(functionsOracleArgs, thresholdOracleArgs, s4OracleArgs var s4Storage s4.Storage if pluginConfig.S4Constraints != nil { - s4Storage = s4.NewStorage(conf.Logger, *pluginConfig.S4Constraints, s4ORM, utils.NewRealClock()) + s4Storage = s4.NewStorage(conf.Logger, *pluginConfig.S4Constraints, s4ORM, clockwork.NewRealClock()) } offchainTransmitter := functions.NewOffchainTransmitter(DefaultOffchainTransmitterChannelSize) @@ -202,7 +202,7 @@ func NewConnector(pluginConfig *config.PluginConfig, ethKeystore keystore.Eth, c if err != nil { return nil, err } - connector, err := connector.NewGatewayConnector(pluginConfig.GatewayConnectorConfig, handler, handler, utils.NewRealClock(), lggr) + connector, err := connector.NewGatewayConnector(pluginConfig.GatewayConnectorConfig, handler, handler, clockwork.NewRealClock(), lggr) if err != nil { return nil, err } diff --git a/core/services/s4/storage.go b/core/services/s4/storage.go index 7c9a92d1f68..02ba9c7bd50 100644 --- a/core/services/s4/storage.go +++ b/core/services/s4/storage.go @@ -3,10 +3,11 @@ package s4 import ( "context" + "github.com/jonboulle/clockwork" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/pg" - "github.com/smartcontractkit/chainlink/v2/core/utils" "github.com/ethereum/go-ethereum/common" ) @@ -70,12 +71,12 @@ type storage struct { lggr logger.Logger contraints Constraints orm ORM - clock utils.Clock + clock clockwork.Clock } var _ Storage = (*storage)(nil) -func NewStorage(lggr logger.Logger, contraints Constraints, orm ORM, clock utils.Clock) Storage { +func NewStorage(lggr logger.Logger, contraints Constraints, orm ORM, clock clockwork.Clock) Storage { return &storage{ lggr: lggr.Named("S4Storage"), contraints: contraints, diff --git a/core/services/s4/storage_test.go b/core/services/s4/storage_test.go index 199e3e6924b..b643609f449 100644 --- a/core/services/s4/storage_test.go +++ b/core/services/s4/storage_test.go @@ -4,12 +4,13 @@ import ( "testing" "time" + "github.com/jonboulle/clockwork" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/s4" "github.com/smartcontractkit/chainlink/v2/core/services/s4/mocks" - "github.com/smartcontractkit/chainlink/v2/core/utils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -27,7 +28,7 @@ var ( func setupTestStorage(t *testing.T, now time.Time) (*mocks.ORM, s4.Storage) { logger := logger.TestLogger(t) orm := mocks.NewORM(t) - clock := utils.NewFixedClock(now) + clock := clockwork.NewFakeClock() storage := s4.NewStorage(logger, constraints, orm, clock) return orm, storage } diff --git a/core/utils/clock.go b/core/utils/clock.go deleted file mode 100644 index 0734c8a6a84..00000000000 --- a/core/utils/clock.go +++ /dev/null @@ -1,29 +0,0 @@ -package utils - -import "time" - -type Clock interface { - Now() time.Time -} - -type realClock struct{} - -func NewRealClock() Clock { - return &realClock{} -} - -func (realClock) Now() time.Time { - return time.Now() -} - -type fixedClock struct { - now time.Time -} - -func NewFixedClock(now time.Time) Clock { - return &fixedClock{now: now} -} - -func (fc fixedClock) Now() time.Time { - return fc.now -} diff --git a/core/utils/clock_test.go b/core/utils/clock_test.go deleted file mode 100644 index 9c4e7645dee..00000000000 --- a/core/utils/clock_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package utils_test - -import ( - "testing" - "time" - - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" - "github.com/smartcontractkit/chainlink/v2/core/utils" - - "github.com/stretchr/testify/assert" -) - -func TestNewRealClock(t *testing.T) { - t.Parallel() - - clock := utils.NewRealClock() - now := clock.Now() - time.Sleep(testutils.TestInterval) - interval := time.Since(now) - assert.GreaterOrEqual(t, interval, testutils.TestInterval) -} - -func TestNewFixedClock(t *testing.T) { - t.Parallel() - - now := time.Now() - clock := utils.NewFixedClock(now) - time.Sleep(testutils.TestInterval) - assert.Equal(t, now, clock.Now()) -} diff --git a/go.mod b/go.mod index ddb45496be3..10c8cfd70bf 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( github.com/jackc/pgtype v1.14.0 github.com/jackc/pgx/v4 v4.18.1 github.com/jmoiron/sqlx v1.3.5 + github.com/jonboulle/clockwork v0.4.0 github.com/jpillora/backoff v1.0.0 github.com/kylelemons/godebug v1.1.0 github.com/leanovate/gopter v0.2.10-0.20210127095200-9abe2343507a diff --git a/integration-tests/go.mod b/integration-tests/go.mod index 226572aee98..dc043705154 100644 --- a/integration-tests/go.mod +++ b/integration-tests/go.mod @@ -283,6 +283,7 @@ require ( github.com/jackpal/go-nat-pmp v1.0.2 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/jmhodges/levigo v1.0.0 // indirect + github.com/jonboulle/clockwork v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect diff --git a/integration-tests/go.sum b/integration-tests/go.sum index a012eca8fca..39882e4b649 100644 --- a/integration-tests/go.sum +++ b/integration-tests/go.sum @@ -1070,6 +1070,8 @@ github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqx github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= +github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=