Skip to content

Commit

Permalink
[chore] Replace clock with specialized lib (smartcontractkit#12031)
Browse files Browse the repository at this point in the history
Co-authored-by: Jordan Krage <[email protected]>
  • Loading branch information
cedric-cordenier and jmank88 authored Feb 15, 2024
1 parent f8cdac1 commit 203ed2d
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 89 deletions.
4 changes: 2 additions & 2 deletions core/scripts/gateway/connector/run_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/services/gateway/connectionmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 7 additions & 8 deletions core/services/gateway/connectionmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = `
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions core/services/gateway/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -51,7 +52,7 @@ type gatewayConnector struct {

config *ConnectorConfig
codec api.Codec
clock utils.Clock
clock clockwork.Clock
nodeAddress []byte
signer Signer
handler GatewayConnectorHandler
Expand Down Expand Up @@ -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")
}
Expand Down
6 changes: 3 additions & 3 deletions core/services/gateway/connector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = `
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions core/services/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = `
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions core/services/ocr2/plugins/functions/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions core/services/s4/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions core/services/s4/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
29 changes: 0 additions & 29 deletions core/utils/clock.go

This file was deleted.

30 changes: 0 additions & 30 deletions core/utils/clock_test.go

This file was deleted.

Loading

0 comments on commit 203ed2d

Please sign in to comment.