From 1a67bf4950d439ff0c98dbf57f5dbcdb535f0607 Mon Sep 17 00:00:00 2001 From: Cedric Date: Fri, 9 Jun 2023 13:30:34 +0100 Subject: [PATCH] [BCF-2306/2307/2308] Move and rename P2P config fields (#9538) --- .../evm/config/mocks/chain_scoped_config.go | 350 +----------------- core/config/app_config.go | 4 +- core/config/p2p_config.go | 27 +- core/config/p2p_v1_config.go | 25 +- core/config/p2p_v2_config.go | 15 +- core/services/chainlink/config_general.go | 111 +----- core/services/chainlink/config_p2p.go | 143 +++++++ core/services/chainlink/config_p2p_test.go | 60 +++ .../chainlink/mocks/general_config.go | 350 +----------------- core/services/job/orm.go | 2 - core/services/ocr/delegate.go | 8 +- core/services/ocr/validate.go | 2 - core/services/ocr2/delegate.go | 2 +- core/services/ocrcommon/peer_wrapper.go | 50 +-- 14 files changed, 275 insertions(+), 874 deletions(-) create mode 100644 core/services/chainlink/config_p2p.go create mode 100644 core/services/chainlink/config_p2p_test.go diff --git a/core/chains/evm/config/mocks/chain_scoped_config.go b/core/chains/evm/config/mocks/chain_scoped_config.go index 2812429cb93..dcbe63466ea 100644 --- a/core/chains/evm/config/mocks/chain_scoped_config.go +++ b/core/chains/evm/config/mocks/chain_scoped_config.go @@ -9,8 +9,6 @@ import ( common "github.com/ethereum/go-ethereum/common" - commontypes "github.com/smartcontractkit/libocr/commontypes" - coreconfig "github.com/smartcontractkit/chainlink/v2/core/config" ethkey "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey" @@ -19,14 +17,6 @@ import ( models "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/models" - net "net" - - networking "github.com/smartcontractkit/libocr/networking" - - p2pkey "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey" - - storemodels "github.com/smartcontractkit/chainlink/v2/core/store/models" - time "time" uuid "github.com/google/uuid" @@ -1684,104 +1674,22 @@ func (_m *ChainScopedConfig) OperatorFactoryAddress() string { return r0 } -// P2PAnnounceIP provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PAnnounceIP() net.IP { +// P2P provides a mock function with given fields: +func (_m *ChainScopedConfig) P2P() coreconfig.P2P { ret := _m.Called() - var r0 net.IP - if rf, ok := ret.Get(0).(func() net.IP); ok { + var r0 coreconfig.P2P + if rf, ok := ret.Get(0).(func() coreconfig.P2P); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(net.IP) + r0 = ret.Get(0).(coreconfig.P2P) } } return r0 } -// P2PAnnouncePort provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PAnnouncePort() uint16 { - ret := _m.Called() - - var r0 uint16 - if rf, ok := ret.Get(0).(func() uint16); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(uint16) - } - - return r0 -} - -// P2PBootstrapCheckInterval provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PBootstrapCheckInterval() time.Duration { - ret := _m.Called() - - var r0 time.Duration - if rf, ok := ret.Get(0).(func() time.Duration); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(time.Duration) - } - - return r0 -} - -// P2PBootstrapPeers provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PBootstrapPeers() ([]string, error) { - ret := _m.Called() - - var r0 []string - var r1 error - if rf, ok := ret.Get(0).(func() ([]string, error)); ok { - return rf() - } - if rf, ok := ret.Get(0).(func() []string); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]string) - } - } - - if rf, ok := ret.Get(1).(func() error); ok { - r1 = rf() - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// P2PDHTAnnouncementCounterUserPrefix provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PDHTAnnouncementCounterUserPrefix() uint32 { - ret := _m.Called() - - var r0 uint32 - if rf, ok := ret.Get(0).(func() uint32); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(uint32) - } - - return r0 -} - -// P2PDHTLookupInterval provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PDHTLookupInterval() int { - ret := _m.Called() - - var r0 int - if rf, ok := ret.Get(0).(func() int); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(int) - } - - return r0 -} - // P2PEnabled provides a mock function with given fields: func (_m *ChainScopedConfig) P2PEnabled() bool { ret := _m.Called() @@ -1796,254 +1704,6 @@ func (_m *ChainScopedConfig) P2PEnabled() bool { return r0 } -// P2PIncomingMessageBufferSize provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PIncomingMessageBufferSize() int { - ret := _m.Called() - - var r0 int - if rf, ok := ret.Get(0).(func() int); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(int) - } - - return r0 -} - -// P2PListenIP provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PListenIP() net.IP { - ret := _m.Called() - - var r0 net.IP - if rf, ok := ret.Get(0).(func() net.IP); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(net.IP) - } - } - - return r0 -} - -// P2PListenPort provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PListenPort() uint16 { - ret := _m.Called() - - var r0 uint16 - if rf, ok := ret.Get(0).(func() uint16); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(uint16) - } - - return r0 -} - -// P2PListenPortRaw provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PListenPortRaw() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// P2PNetworkingStack provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PNetworkingStack() networking.NetworkingStack { - ret := _m.Called() - - var r0 networking.NetworkingStack - if rf, ok := ret.Get(0).(func() networking.NetworkingStack); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(networking.NetworkingStack) - } - - return r0 -} - -// P2PNetworkingStackRaw provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PNetworkingStackRaw() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// P2PNewStreamTimeout provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PNewStreamTimeout() time.Duration { - ret := _m.Called() - - var r0 time.Duration - if rf, ok := ret.Get(0).(func() time.Duration); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(time.Duration) - } - - return r0 -} - -// P2POutgoingMessageBufferSize provides a mock function with given fields: -func (_m *ChainScopedConfig) P2POutgoingMessageBufferSize() int { - ret := _m.Called() - - var r0 int - if rf, ok := ret.Get(0).(func() int); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(int) - } - - return r0 -} - -// P2PPeerID provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PPeerID() p2pkey.PeerID { - ret := _m.Called() - - var r0 p2pkey.PeerID - if rf, ok := ret.Get(0).(func() p2pkey.PeerID); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(p2pkey.PeerID) - } - - return r0 -} - -// P2PPeerIDRaw provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PPeerIDRaw() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// P2PPeerstoreWriteInterval provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PPeerstoreWriteInterval() time.Duration { - ret := _m.Called() - - var r0 time.Duration - if rf, ok := ret.Get(0).(func() time.Duration); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(time.Duration) - } - - return r0 -} - -// P2PV2AnnounceAddresses provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PV2AnnounceAddresses() []string { - ret := _m.Called() - - var r0 []string - if rf, ok := ret.Get(0).(func() []string); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]string) - } - } - - return r0 -} - -// P2PV2Bootstrappers provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PV2Bootstrappers() []commontypes.BootstrapperLocator { - ret := _m.Called() - - var r0 []commontypes.BootstrapperLocator - if rf, ok := ret.Get(0).(func() []commontypes.BootstrapperLocator); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]commontypes.BootstrapperLocator) - } - } - - return r0 -} - -// P2PV2BootstrappersRaw provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PV2BootstrappersRaw() []string { - ret := _m.Called() - - var r0 []string - if rf, ok := ret.Get(0).(func() []string); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]string) - } - } - - return r0 -} - -// P2PV2DeltaDial provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PV2DeltaDial() storemodels.Duration { - ret := _m.Called() - - var r0 storemodels.Duration - if rf, ok := ret.Get(0).(func() storemodels.Duration); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(storemodels.Duration) - } - - return r0 -} - -// P2PV2DeltaReconcile provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PV2DeltaReconcile() storemodels.Duration { - ret := _m.Called() - - var r0 storemodels.Duration - if rf, ok := ret.Get(0).(func() storemodels.Duration); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(storemodels.Duration) - } - - return r0 -} - -// P2PV2ListenAddresses provides a mock function with given fields: -func (_m *ChainScopedConfig) P2PV2ListenAddresses() []string { - ret := _m.Called() - - var r0 []string - if rf, ok := ret.Get(0).(func() []string); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]string) - } - } - - return r0 -} - // Password provides a mock function with given fields: func (_m *ChainScopedConfig) Password() coreconfig.Password { ret := _m.Called() diff --git a/core/config/app_config.go b/core/config/app_config.go index 4f44b5596c0..1620c4a898a 100644 --- a/core/config/app_config.go +++ b/core/config/app_config.go @@ -33,9 +33,6 @@ type AppConfig interface { FeatureFlags OCR1Config OCR2Config - P2PNetworking - P2PV1Networking - P2PV2Networking Pyroscope Secrets @@ -53,6 +50,7 @@ type AppConfig interface { Explorer() Explorer Password() Password Prometheus() Prometheus + P2P() P2P } type DatabaseBackupMode string diff --git a/core/config/p2p_config.go b/core/config/p2p_config.go index 99ee8206833..40649b5538b 100644 --- a/core/config/p2p_config.go +++ b/core/config/p2p_config.go @@ -1,28 +1,17 @@ package config import ( - "time" - ocrnetworking "github.com/smartcontractkit/libocr/networking" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey" ) -// P2PNetworking is a subset of global config relevant to p2p networking. -type P2PNetworking interface { - P2PNetworkingStack() (n ocrnetworking.NetworkingStack) - P2PNetworkingStackRaw() string - P2PPeerID() p2pkey.PeerID - P2PPeerIDRaw() string - P2PIncomingMessageBufferSize() int - P2POutgoingMessageBufferSize() int -} - -type P2PDeprecated interface { - // DEPRECATED - HERE FOR BACKWARDS COMPATIBILITY - ocrNewStreamTimeout() time.Duration - ocrBootstrapCheckInterval() time.Duration - ocrDHTLookupInterval() int - ocrIncomingMessageBufferSize() int - ocrOutgoingMessageBufferSize() int +type P2P interface { + V2() V2 + V1() V1 + NetworkStack() (n ocrnetworking.NetworkingStack) + PeerID() p2pkey.PeerID + IncomingMessageBufferSize() int + OutgoingMessageBufferSize() int + TraceLogging() bool } diff --git a/core/config/p2p_v1_config.go b/core/config/p2p_v1_config.go index a0d7ab9f0f5..2e138285182 100644 --- a/core/config/p2p_v1_config.go +++ b/core/config/p2p_v1_config.go @@ -5,17 +5,16 @@ import ( "time" ) -// P2PV1Networking is a subset of global config relevant to p2p v1 networking. -type P2PV1Networking interface { - P2PAnnounceIP() net.IP - P2PAnnouncePort() uint16 - P2PBootstrapPeers() ([]string, error) - P2PDHTAnnouncementCounterUserPrefix() uint32 - P2PListenIP() net.IP - P2PListenPort() uint16 - P2PListenPortRaw() string - P2PNewStreamTimeout() time.Duration - P2PBootstrapCheckInterval() time.Duration - P2PDHTLookupInterval() int - P2PPeerstoreWriteInterval() time.Duration +type V1 interface { + Enabled() bool + AnnounceIP() net.IP + AnnouncePort() uint16 + DefaultBootstrapPeers() ([]string, error) + DHTAnnouncementCounterUserPrefix() uint32 + ListenIP() net.IP + ListenPort() uint16 + NewStreamTimeout() time.Duration + BootstrapCheckInterval() time.Duration + DHTLookupInterval() int + PeerstoreWriteInterval() time.Duration } diff --git a/core/config/p2p_v2_config.go b/core/config/p2p_v2_config.go index 6d74ff21330..7b4a3c05fc4 100644 --- a/core/config/p2p_v2_config.go +++ b/core/config/p2p_v2_config.go @@ -6,12 +6,11 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/store/models" ) -// P2PV2Networking is a subset of global config relevant to p2p v2 networking. -type P2PV2Networking interface { - P2PV2AnnounceAddresses() []string - P2PV2Bootstrappers() (locators []ocrcommontypes.BootstrapperLocator) - P2PV2BootstrappersRaw() []string - P2PV2DeltaDial() models.Duration - P2PV2DeltaReconcile() models.Duration - P2PV2ListenAddresses() []string +type V2 interface { + Enabled() bool + AnnounceAddresses() []string + DefaultBootstrappers() (locators []ocrcommontypes.BootstrapperLocator) + DeltaDial() models.Duration + DeltaReconcile() models.Duration + ListenAddresses() []string } diff --git a/core/services/chainlink/config_general.go b/core/services/chainlink/config_general.go index cb00f1a92de..62db42073f7 100644 --- a/core/services/chainlink/config_general.go +++ b/core/services/chainlink/config_general.go @@ -4,11 +4,9 @@ import ( _ "embed" "fmt" "math/big" - "net" "net/url" "os" "path/filepath" - "strconv" "strings" "sync" "time" @@ -17,7 +15,6 @@ import ( "go.uber.org/multierr" "go.uber.org/zap/zapcore" - "github.com/smartcontractkit/libocr/commontypes" ocrnetworking "github.com/smartcontractkit/libocr/networking" "github.com/smartcontractkit/chainlink/v2/core/chains/cosmos" @@ -510,6 +507,10 @@ func (g *generalConfig) OCR2SimulateTransactions() bool { return *g.c.OCR2.SimulateTransactions } +func (g *generalConfig) P2P() config.P2P { + return &p2p{c: g.c.P2P} +} + func (g *generalConfig) P2PNetworkingStack() (n ocrnetworking.NetworkingStack) { return g.c.P2P.NetworkStack() } @@ -534,110 +535,6 @@ func (g *generalConfig) P2POutgoingMessageBufferSize() int { return int(*g.c.P2P.OutgoingMessageBufferSize) } -func (g *generalConfig) P2PAnnounceIP() net.IP { - return *g.c.P2P.V1.AnnounceIP -} - -func (g *generalConfig) P2PAnnouncePort() uint16 { - return *g.c.P2P.V1.AnnouncePort -} - -func (g *generalConfig) P2PBootstrapPeers() ([]string, error) { - p := *g.c.P2P.V1.DefaultBootstrapPeers - if p == nil { - p = []string{} - } - return p, nil -} - -func (g *generalConfig) P2PDHTAnnouncementCounterUserPrefix() uint32 { - return *g.c.P2P.V1.DHTAnnouncementCounterUserPrefix -} - -func (g *generalConfig) P2PListenIP() net.IP { - return *g.c.P2P.V1.ListenIP -} - -func (g *generalConfig) P2PListenPort() uint16 { - v1 := g.c.P2P.V1 - p := *v1.ListenPort - return p -} - -func (g *generalConfig) P2PListenPortRaw() string { - p := *g.c.P2P.V1.ListenPort - if p == 0 { - return "" - } - return strconv.Itoa(int(p)) -} - -func (g *generalConfig) P2PNewStreamTimeout() time.Duration { - return g.c.P2P.V1.NewStreamTimeout.Duration() -} - -func (g *generalConfig) P2PBootstrapCheckInterval() time.Duration { - return g.c.P2P.V1.BootstrapCheckInterval.Duration() -} - -func (g *generalConfig) P2PDHTLookupInterval() int { - return int(*g.c.P2P.V1.DHTLookupInterval) -} - -func (g *generalConfig) P2PPeerstoreWriteInterval() time.Duration { - return g.c.P2P.V1.PeerstoreWriteInterval.Duration() -} - -func (g *generalConfig) P2PV2AnnounceAddresses() []string { - if v := g.c.P2P.V2.AnnounceAddresses; v != nil { - return *v - } - return nil -} - -func (g *generalConfig) P2PV2Bootstrappers() (locators []commontypes.BootstrapperLocator) { - if v := g.c.P2P.V2.DefaultBootstrappers; v != nil { - return *v - } - return nil -} - -func (g *generalConfig) P2PV2BootstrappersRaw() (s []string) { - if v := g.c.P2P.V2.DefaultBootstrappers; v != nil { - for _, b := range *v { - t, err := b.MarshalText() - if err != nil { - // log panic matches old behavior - only called for UI presentation - panic(fmt.Sprintf("Failed to marshal bootstrapper: %v", err)) - } - s = append(s, string(t)) - } - } - return -} - -func (g *generalConfig) P2PV2DeltaDial() models.Duration { - if v := g.c.P2P.V2.DeltaDial; v != nil { - return *v - } - return models.Duration{} -} - -func (g *generalConfig) P2PV2DeltaReconcile() models.Duration { - if v := g.c.P2P.V2.DeltaReconcile; v != nil { - return *v - - } - return models.Duration{} -} - -func (g *generalConfig) P2PV2ListenAddresses() []string { - if v := g.c.P2P.V2.ListenAddresses; v != nil { - return *v - } - return nil -} - func (g *generalConfig) PyroscopeServerAddress() string { return *g.c.Pyroscope.ServerAddress } diff --git a/core/services/chainlink/config_p2p.go b/core/services/chainlink/config_p2p.go new file mode 100644 index 00000000000..698a90bcde4 --- /dev/null +++ b/core/services/chainlink/config_p2p.go @@ -0,0 +1,143 @@ +package chainlink + +import ( + "net" + "time" + + "github.com/smartcontractkit/libocr/commontypes" + ocrnetworking "github.com/smartcontractkit/libocr/networking" + + "github.com/smartcontractkit/chainlink/v2/core/config" + v2types "github.com/smartcontractkit/chainlink/v2/core/config/v2" + "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey" + "github.com/smartcontractkit/chainlink/v2/core/store/models" +) + +type p2p struct { + c v2types.P2P +} + +func (p *p2p) NetworkStack() (n ocrnetworking.NetworkingStack) { + return p.c.NetworkStack() +} + +func (p *p2p) PeerID() p2pkey.PeerID { + return *p.c.PeerID +} + +func (p *p2p) TraceLogging() bool { + return *p.c.TraceLogging +} + +func (p *p2p) IncomingMessageBufferSize() int { + return int(*p.c.IncomingMessageBufferSize) +} + +func (p *p2p) OutgoingMessageBufferSize() int { + return int(*p.c.OutgoingMessageBufferSize) +} + +func (p *p2p) V2() config.V2 { + return &p2pv2{p.c.V2} +} + +func (p *p2p) V1() config.V1 { + return &p2pv1{p.c.V1} +} + +type p2pv1 struct { + c v2types.P2PV1 +} + +func (v *p2pv1) Enabled() bool { + return *v.c.Enabled +} + +func (v *p2pv1) AnnounceIP() net.IP { + return *v.c.AnnounceIP +} + +func (v *p2pv1) AnnouncePort() uint16 { + return *v.c.AnnouncePort +} + +func (v *p2pv1) DefaultBootstrapPeers() ([]string, error) { + p := *v.c.DefaultBootstrapPeers + if p == nil { + p = []string{} + } + return p, nil +} + +func (v *p2pv1) DHTAnnouncementCounterUserPrefix() uint32 { + return *v.c.DHTAnnouncementCounterUserPrefix +} + +func (v *p2pv1) ListenIP() net.IP { + return *v.c.ListenIP +} + +func (v *p2pv1) ListenPort() uint16 { + p := *v.c.ListenPort + return p +} + +func (v *p2pv1) NewStreamTimeout() time.Duration { + return v.c.NewStreamTimeout.Duration() +} + +func (v *p2pv1) BootstrapCheckInterval() time.Duration { + return v.c.BootstrapCheckInterval.Duration() +} + +func (v *p2pv1) DHTLookupInterval() int { + return int(*v.c.DHTLookupInterval) +} + +func (v *p2pv1) PeerstoreWriteInterval() time.Duration { + return v.c.PeerstoreWriteInterval.Duration() +} + +type p2pv2 struct { + c v2types.P2PV2 +} + +func (v *p2pv2) Enabled() bool { + return *v.c.Enabled +} + +func (v *p2pv2) AnnounceAddresses() []string { + if a := v.c.AnnounceAddresses; a != nil { + return *a + } + return nil +} + +func (v *p2pv2) DefaultBootstrappers() (locators []commontypes.BootstrapperLocator) { + if d := v.c.DefaultBootstrappers; d != nil { + return *d + } + return nil +} + +func (v *p2pv2) DeltaDial() models.Duration { + if d := v.c.DeltaDial; d != nil { + return *d + } + return models.Duration{} +} + +func (v *p2pv2) DeltaReconcile() models.Duration { + if d := v.c.DeltaReconcile; d != nil { + return *d + + } + return models.Duration{} +} + +func (v *p2pv2) ListenAddresses() []string { + if l := v.c.ListenAddresses; l != nil { + return *l + } + return nil +} diff --git a/core/services/chainlink/config_p2p_test.go b/core/services/chainlink/config_p2p_test.go new file mode 100644 index 00000000000..d6adfe7051c --- /dev/null +++ b/core/services/chainlink/config_p2p_test.go @@ -0,0 +1,60 @@ +package chainlink + +import ( + "testing" + "time" + + "github.com/smartcontractkit/libocr/commontypes" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestP2PConfig(t *testing.T) { + opts := GeneralConfigOpts{ + ConfigStrings: []string{fullTOML}, + } + cfg, err := opts.New() + require.NoError(t, err) + + p2p := cfg.P2P() + assert.Equal(t, "p2p_12D3KooWMoejJznyDuEk5aX6GvbjaG12UzeornPCBNzMRqdwrFJw", p2p.PeerID().String()) + assert.Equal(t, 13, p2p.IncomingMessageBufferSize()) + assert.Equal(t, 17, p2p.OutgoingMessageBufferSize()) + assert.True(t, p2p.TraceLogging()) + + v1 := p2p.V1() + assert.False(t, v1.Enabled()) + assert.Equal(t, "1.2.3.4", v1.AnnounceIP().String()) + assert.Equal(t, uint16(1234), v1.AnnouncePort()) + assert.Equal(t, time.Minute, v1.BootstrapCheckInterval()) + p, err := v1.DefaultBootstrapPeers() + require.NoError(t, err) + assert.Equal(t, []string{"foo", "bar", "should", "these", "be", "typed"}, p) + assert.Equal(t, uint32(4321), v1.DHTAnnouncementCounterUserPrefix()) + assert.Equal(t, 9, v1.DHTLookupInterval()) + assert.Equal(t, "4.3.2.1", v1.ListenIP().String()) + assert.Equal(t, uint16(9), v1.ListenPort()) + assert.Equal(t, time.Second, v1.NewStreamTimeout()) + assert.Equal(t, time.Minute, v1.PeerstoreWriteInterval()) + + v2 := p2p.V2() + assert.True(t, v2.Enabled()) + assert.Equal(t, []string{"a", "b", "c"}, v2.AnnounceAddresses()) + assert.ElementsMatch( + t, + []commontypes.BootstrapperLocator{ + { + PeerID: "12D3KooWMoejJznyDuEk5aX6GvbjaG12UzeornPCBNzMRqdwrFJw", + Addrs: []string{"test:99"}, + }, + { + PeerID: "12D3KooWMoejJznyDuEk5aX6GvbjaG12UzeornPCBNzMRqdwrFJw", + Addrs: []string{"foo:42", "bar:10"}, + }, + }, + v2.DefaultBootstrappers(), + ) + assert.Equal(t, time.Minute, v2.DeltaDial().Duration()) + assert.Equal(t, time.Second, v2.DeltaReconcile().Duration()) + assert.Equal(t, []string{"foo", "bar"}, v2.ListenAddresses()) +} diff --git a/core/services/chainlink/mocks/general_config.go b/core/services/chainlink/mocks/general_config.go index 1b8ae1f6ee6..43f2c4b383b 100644 --- a/core/services/chainlink/mocks/general_config.go +++ b/core/services/chainlink/mocks/general_config.go @@ -5,8 +5,6 @@ package mocks import ( big "math/big" - commontypes "github.com/smartcontractkit/libocr/commontypes" - config "github.com/smartcontractkit/chainlink/v2/core/config" cosmos "github.com/smartcontractkit/chainlink/v2/core/chains/cosmos" @@ -17,18 +15,10 @@ import ( models "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/models" - net "net" - - networking "github.com/smartcontractkit/libocr/networking" - - p2pkey "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey" - solana "github.com/smartcontractkit/chainlink/v2/core/chains/solana" starknet "github.com/smartcontractkit/chainlink/v2/core/chains/starknet" - storemodels "github.com/smartcontractkit/chainlink/v2/core/store/models" - time "time" uuid "github.com/google/uuid" @@ -774,104 +764,22 @@ func (_m *GeneralConfig) OCRTransmitterAddress() (ethkey.EIP55Address, error) { return r0, r1 } -// P2PAnnounceIP provides a mock function with given fields: -func (_m *GeneralConfig) P2PAnnounceIP() net.IP { +// P2P provides a mock function with given fields: +func (_m *GeneralConfig) P2P() config.P2P { ret := _m.Called() - var r0 net.IP - if rf, ok := ret.Get(0).(func() net.IP); ok { + var r0 config.P2P + if rf, ok := ret.Get(0).(func() config.P2P); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(net.IP) + r0 = ret.Get(0).(config.P2P) } } return r0 } -// P2PAnnouncePort provides a mock function with given fields: -func (_m *GeneralConfig) P2PAnnouncePort() uint16 { - ret := _m.Called() - - var r0 uint16 - if rf, ok := ret.Get(0).(func() uint16); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(uint16) - } - - return r0 -} - -// P2PBootstrapCheckInterval provides a mock function with given fields: -func (_m *GeneralConfig) P2PBootstrapCheckInterval() time.Duration { - ret := _m.Called() - - var r0 time.Duration - if rf, ok := ret.Get(0).(func() time.Duration); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(time.Duration) - } - - return r0 -} - -// P2PBootstrapPeers provides a mock function with given fields: -func (_m *GeneralConfig) P2PBootstrapPeers() ([]string, error) { - ret := _m.Called() - - var r0 []string - var r1 error - if rf, ok := ret.Get(0).(func() ([]string, error)); ok { - return rf() - } - if rf, ok := ret.Get(0).(func() []string); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]string) - } - } - - if rf, ok := ret.Get(1).(func() error); ok { - r1 = rf() - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// P2PDHTAnnouncementCounterUserPrefix provides a mock function with given fields: -func (_m *GeneralConfig) P2PDHTAnnouncementCounterUserPrefix() uint32 { - ret := _m.Called() - - var r0 uint32 - if rf, ok := ret.Get(0).(func() uint32); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(uint32) - } - - return r0 -} - -// P2PDHTLookupInterval provides a mock function with given fields: -func (_m *GeneralConfig) P2PDHTLookupInterval() int { - ret := _m.Called() - - var r0 int - if rf, ok := ret.Get(0).(func() int); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(int) - } - - return r0 -} - // P2PEnabled provides a mock function with given fields: func (_m *GeneralConfig) P2PEnabled() bool { ret := _m.Called() @@ -886,254 +794,6 @@ func (_m *GeneralConfig) P2PEnabled() bool { return r0 } -// P2PIncomingMessageBufferSize provides a mock function with given fields: -func (_m *GeneralConfig) P2PIncomingMessageBufferSize() int { - ret := _m.Called() - - var r0 int - if rf, ok := ret.Get(0).(func() int); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(int) - } - - return r0 -} - -// P2PListenIP provides a mock function with given fields: -func (_m *GeneralConfig) P2PListenIP() net.IP { - ret := _m.Called() - - var r0 net.IP - if rf, ok := ret.Get(0).(func() net.IP); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(net.IP) - } - } - - return r0 -} - -// P2PListenPort provides a mock function with given fields: -func (_m *GeneralConfig) P2PListenPort() uint16 { - ret := _m.Called() - - var r0 uint16 - if rf, ok := ret.Get(0).(func() uint16); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(uint16) - } - - return r0 -} - -// P2PListenPortRaw provides a mock function with given fields: -func (_m *GeneralConfig) P2PListenPortRaw() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// P2PNetworkingStack provides a mock function with given fields: -func (_m *GeneralConfig) P2PNetworkingStack() networking.NetworkingStack { - ret := _m.Called() - - var r0 networking.NetworkingStack - if rf, ok := ret.Get(0).(func() networking.NetworkingStack); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(networking.NetworkingStack) - } - - return r0 -} - -// P2PNetworkingStackRaw provides a mock function with given fields: -func (_m *GeneralConfig) P2PNetworkingStackRaw() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// P2PNewStreamTimeout provides a mock function with given fields: -func (_m *GeneralConfig) P2PNewStreamTimeout() time.Duration { - ret := _m.Called() - - var r0 time.Duration - if rf, ok := ret.Get(0).(func() time.Duration); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(time.Duration) - } - - return r0 -} - -// P2POutgoingMessageBufferSize provides a mock function with given fields: -func (_m *GeneralConfig) P2POutgoingMessageBufferSize() int { - ret := _m.Called() - - var r0 int - if rf, ok := ret.Get(0).(func() int); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(int) - } - - return r0 -} - -// P2PPeerID provides a mock function with given fields: -func (_m *GeneralConfig) P2PPeerID() p2pkey.PeerID { - ret := _m.Called() - - var r0 p2pkey.PeerID - if rf, ok := ret.Get(0).(func() p2pkey.PeerID); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(p2pkey.PeerID) - } - - return r0 -} - -// P2PPeerIDRaw provides a mock function with given fields: -func (_m *GeneralConfig) P2PPeerIDRaw() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// P2PPeerstoreWriteInterval provides a mock function with given fields: -func (_m *GeneralConfig) P2PPeerstoreWriteInterval() time.Duration { - ret := _m.Called() - - var r0 time.Duration - if rf, ok := ret.Get(0).(func() time.Duration); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(time.Duration) - } - - return r0 -} - -// P2PV2AnnounceAddresses provides a mock function with given fields: -func (_m *GeneralConfig) P2PV2AnnounceAddresses() []string { - ret := _m.Called() - - var r0 []string - if rf, ok := ret.Get(0).(func() []string); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]string) - } - } - - return r0 -} - -// P2PV2Bootstrappers provides a mock function with given fields: -func (_m *GeneralConfig) P2PV2Bootstrappers() []commontypes.BootstrapperLocator { - ret := _m.Called() - - var r0 []commontypes.BootstrapperLocator - if rf, ok := ret.Get(0).(func() []commontypes.BootstrapperLocator); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]commontypes.BootstrapperLocator) - } - } - - return r0 -} - -// P2PV2BootstrappersRaw provides a mock function with given fields: -func (_m *GeneralConfig) P2PV2BootstrappersRaw() []string { - ret := _m.Called() - - var r0 []string - if rf, ok := ret.Get(0).(func() []string); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]string) - } - } - - return r0 -} - -// P2PV2DeltaDial provides a mock function with given fields: -func (_m *GeneralConfig) P2PV2DeltaDial() storemodels.Duration { - ret := _m.Called() - - var r0 storemodels.Duration - if rf, ok := ret.Get(0).(func() storemodels.Duration); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(storemodels.Duration) - } - - return r0 -} - -// P2PV2DeltaReconcile provides a mock function with given fields: -func (_m *GeneralConfig) P2PV2DeltaReconcile() storemodels.Duration { - ret := _m.Called() - - var r0 storemodels.Duration - if rf, ok := ret.Get(0).(func() storemodels.Duration); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(storemodels.Duration) - } - - return r0 -} - -// P2PV2ListenAddresses provides a mock function with given fields: -func (_m *GeneralConfig) P2PV2ListenAddresses() []string { - ret := _m.Called() - - var r0 []string - if rf, ok := ret.Get(0).(func() []string); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]string) - } - } - - return r0 -} - // Password provides a mock function with given fields: func (_m *GeneralConfig) Password() config.Password { ret := _m.Called() diff --git a/core/services/job/orm.go b/core/services/job/orm.go index afdc057e3ca..7885e85fc1b 100644 --- a/core/services/job/orm.go +++ b/core/services/job/orm.go @@ -24,7 +24,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/null" "github.com/smartcontractkit/chainlink/v2/core/services/keystore" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey" - "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey" medianconfig "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/median/config" "github.com/smartcontractkit/chainlink/v2/core/services/pg" "github.com/smartcontractkit/chainlink/v2/core/services/pipeline" @@ -699,7 +698,6 @@ func LoadEnvConfigVarsDR(cfg DRSpecConfig, drs DirectRequestSpec) *DirectRequest } type OCRSpecConfig interface { - P2PPeerID() p2pkey.PeerID OCRBlockchainTimeout() time.Duration OCRContractConfirmations() uint16 OCRContractPollInterval() time.Duration diff --git a/core/services/ocr/delegate.go b/core/services/ocr/delegate.go index e07ff13257e..832ed8ba560 100644 --- a/core/services/ocr/delegate.go +++ b/core/services/ocr/delegate.go @@ -147,7 +147,7 @@ func (d *Delegate) ServicesForSpec(jb job.Job) (services []job.ServiceCtx, err e if concreteSpec.P2PBootstrapPeers != nil { v1BootstrapPeers = concreteSpec.P2PBootstrapPeers } else { - v1BootstrapPeers, err = chain.Config().P2PBootstrapPeers() + v1BootstrapPeers, err = chain.Config().P2P().V1().DefaultBootstrapPeers() if err != nil { return nil, err } @@ -161,7 +161,7 @@ func (d *Delegate) ServicesForSpec(jb job.Job) (services []job.ServiceCtx, err e // present in job spec, and p2pv2Bootstrappers = []. So even if an empty list is // passed explicitly, this will still fall back to using the V2 bootstappers defined // in P2P.V2.DefaultBootstrappers config var. Only a non-empty list will override the default list. - v2Bootstrappers = peerWrapper.Config().P2PV2Bootstrappers() + v2Bootstrappers = peerWrapper.Config().P2P().V2().DefaultBootstrappers() } ocrLogger := logger.NewOCRWrapper(lggr, chain.Config().OCRTraceLogging(), func(msg string) { @@ -193,7 +193,7 @@ func (d *Delegate) ServicesForSpec(jb job.Job) (services []job.ServiceCtx, err e } else { // In V1 or V1V2 mode, p2pv1BootstrapPeers must be defined either in // node config or in job spec - if peerWrapper.Config().P2PNetworkingStack() != ocrnetworking.NetworkingStackV2 { + if peerWrapper.Config().P2P().NetworkStack() != ocrnetworking.NetworkingStackV2 { if len(v1BootstrapPeers) < 1 { return nil, errors.New("Need at least one v1 bootstrap peer defined") } @@ -201,7 +201,7 @@ func (d *Delegate) ServicesForSpec(jb job.Job) (services []job.ServiceCtx, err e // In V1V2 or V2 mode, p2pv2Bootstrappers must be defined either in // node config or in job spec - if peerWrapper.Config().P2PNetworkingStack() != ocrnetworking.NetworkingStackV1 { + if peerWrapper.Config().P2P().NetworkStack() != ocrnetworking.NetworkingStackV1 { if len(v2Bootstrappers) < 1 { return nil, errors.New("Need at least one v2 bootstrap peer defined") } diff --git a/core/services/ocr/validate.go b/core/services/ocr/validate.go index e234c327e7a..cd8c757be20 100644 --- a/core/services/ocr/validate.go +++ b/core/services/ocr/validate.go @@ -15,7 +15,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/config" "github.com/smartcontractkit/chainlink/v2/core/services/job" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey" - "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey" "github.com/smartcontractkit/chainlink/v2/core/services/ocrcommon" ) @@ -31,7 +30,6 @@ type ValidationConfig interface { OCRObservationGracePeriod() time.Duration OCRObservationTimeout() time.Duration OCRTransmitterAddress() (ethkey.EIP55Address, error) - P2PPeerID() p2pkey.PeerID OCRCaptureEATelemetry() bool } diff --git a/core/services/ocr2/delegate.go b/core/services/ocr2/delegate.go index 660f8b59598..6be514f76f9 100644 --- a/core/services/ocr2/delegate.go +++ b/core/services/ocr2/delegate.go @@ -339,7 +339,7 @@ func (d *Delegate) ServicesForSpec(jb job.Job) ([]job.ServiceCtx, error) { "DatabaseTimeout", lc.DatabaseTimeout, ) - bootstrapPeers, err := ocrcommon.GetValidatedBootstrapPeers(spec.P2PV2Bootstrappers, peerWrapper.Config().P2PV2Bootstrappers()) + bootstrapPeers, err := ocrcommon.GetValidatedBootstrapPeers(spec.P2PV2Bootstrappers, peerWrapper.Config().P2P().V2().DefaultBootstrappers()) if err != nil { return nil, err } diff --git a/core/services/ocrcommon/peer_wrapper.go b/core/services/ocrcommon/peer_wrapper.go index f677448879a..d316f04cce5 100644 --- a/core/services/ocrcommon/peer_wrapper.go +++ b/core/services/ocrcommon/peer_wrapper.go @@ -27,10 +27,8 @@ import ( ) type PeerWrapperConfig interface { - config.P2PNetworking - config.P2PV1Networking - config.P2PV2Networking OCRTraceLogging() bool + P2P() config.P2P } type ( @@ -67,16 +65,16 @@ type ( ) func ValidatePeerWrapperConfig(config PeerWrapperConfig) error { - switch config.P2PNetworkingStack() { + switch config.P2P().NetworkStack() { case ocrnetworking.NetworkingStackV1: // Note: If P2PListenPort isn't set, the peer wrapper will generate a random one itself. return nil case ocrnetworking.NetworkingStackV2: - if len(config.P2PV2ListenAddresses()) == 0 { + if len(config.P2P().V2().ListenAddresses()) == 0 { return errors.New("networking stack v2 selected but no P2P.V2.ListenAddresses specified") } case ocrnetworking.NetworkingStackV1V2: - if len(config.P2PV2ListenAddresses()) == 0 { + if len(config.P2P().V2().ListenAddresses()) == 0 { return errors.New("networking stack v1v2 selected but no P2P.V2.ListenAddresses specified") } default: @@ -134,21 +132,22 @@ func (p *SingletonPeerWrapper) peerConfig() (ocrnetworking.PeerConfig, error) { if ks, err := p.keyStore.P2P().GetAll(); err == nil && len(ks) == 0 { return ocrnetworking.PeerConfig{}, errors.Errorf("No P2P keys found in keystore. Peer wrapper will not be fully initialized") } - key, err := p.keyStore.P2P().GetOrFirst(p.config.P2PPeerID()) + key, err := p.keyStore.P2P().GetOrFirst(p.config.P2P().PeerID()) if err != nil { return ocrnetworking.PeerConfig{}, err } p.PeerID = key.PeerID() - p2pPort := p.config.P2PListenPort() + v1 := p.config.P2P().V1() + p2pPort := v1.ListenPort() // We need to start the peer store wrapper if v1 is required. // Also fallback to listen params if announce params not specified. - ns := p.config.P2PNetworkingStack() + ns := p.config.P2P().NetworkStack() // NewPeer requires that these are both set or unset, otherwise it will error out. - v1AnnounceIP, v1AnnouncePort := p.config.P2PAnnounceIP(), p.config.P2PAnnouncePort() + v1AnnounceIP, v1AnnouncePort := v1.AnnounceIP(), v1.AnnouncePort() var peerStore p2ppeerstore.Peerstore if ns == ocrnetworking.NetworkingStackV1 || ns == ocrnetworking.NetworkingStackV1V2 { - p.pstoreWrapper, err = NewPeerstoreWrapper(p.db, p.config.P2PPeerstoreWriteInterval(), p.PeerID, p.lggr, p.dbConfig) + p.pstoreWrapper, err = NewPeerstoreWrapper(p.db, v1.PeerstoreWriteInterval(), p.PeerID, p.lggr, p.dbConfig) if err != nil { return ocrnetworking.PeerConfig{}, errors.Wrap(err, "could not make new pstorewrapper") } @@ -191,37 +190,38 @@ func (p *SingletonPeerWrapper) peerConfig() (ocrnetworking.PeerConfig, error) { discovererDB = NewDiscovererDatabase(p.db.DB, p2ppeer.ID(p.PeerID)) } + config := p.config.P2P() peerConfig := ocrnetworking.PeerConfig{ - NetworkingStack: p.config.P2PNetworkingStack(), + NetworkingStack: config.NetworkStack(), PrivKey: key.PrivKey, Logger: logger.NewOCRWrapper(p.lggr, p.config.OCRTraceLogging(), func(string) {}), // V1 config - V1ListenIP: p.config.P2PListenIP(), + V1ListenIP: config.V1().ListenIP(), V1ListenPort: p2pPort, V1AnnounceIP: v1AnnounceIP, V1AnnouncePort: v1AnnouncePort, V1Peerstore: peerStore, - V1DHTAnnouncementCounterUserPrefix: p.config.P2PDHTAnnouncementCounterUserPrefix(), + V1DHTAnnouncementCounterUserPrefix: config.V1().DHTAnnouncementCounterUserPrefix(), // V2 config - V2ListenAddresses: p.config.P2PV2ListenAddresses(), - V2AnnounceAddresses: p.config.P2PV2AnnounceAddresses(), // NewPeer will handle the fallback to listen addresses for us. - V2DeltaReconcile: p.config.P2PV2DeltaReconcile().Duration(), - V2DeltaDial: p.config.P2PV2DeltaDial().Duration(), + V2ListenAddresses: config.V2().ListenAddresses(), + V2AnnounceAddresses: config.V2().AnnounceAddresses(), // NewPeer will handle the fallback to listen addresses for us. + V2DeltaReconcile: config.V2().DeltaReconcile().Duration(), + V2DeltaDial: config.V2().DeltaDial().Duration(), V2DiscovererDatabase: discovererDB, V1EndpointConfig: ocrnetworking.EndpointConfigV1{ - IncomingMessageBufferSize: p.config.P2PIncomingMessageBufferSize(), - OutgoingMessageBufferSize: p.config.P2POutgoingMessageBufferSize(), - NewStreamTimeout: p.config.P2PNewStreamTimeout(), - DHTLookupInterval: p.config.P2PDHTLookupInterval(), - BootstrapCheckInterval: p.config.P2PBootstrapCheckInterval(), + IncomingMessageBufferSize: config.IncomingMessageBufferSize(), + OutgoingMessageBufferSize: config.OutgoingMessageBufferSize(), + NewStreamTimeout: config.V1().NewStreamTimeout(), + DHTLookupInterval: config.V1().DHTLookupInterval(), + BootstrapCheckInterval: config.V1().BootstrapCheckInterval(), }, V2EndpointConfig: ocrnetworking.EndpointConfigV2{ - IncomingMessageBufferSize: p.config.P2PIncomingMessageBufferSize(), - OutgoingMessageBufferSize: p.config.P2POutgoingMessageBufferSize(), + IncomingMessageBufferSize: config.IncomingMessageBufferSize(), + OutgoingMessageBufferSize: config.OutgoingMessageBufferSize(), }, }