Skip to content

Commit f151882

Browse files
committed
rename some types
1 parent 9b70d29 commit f151882

File tree

6 files changed

+91
-94
lines changed

6 files changed

+91
-94
lines changed

network/hybridNetwork.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ type HybridP2PNetwork struct {
3434
wsNetwork *WebsocketNetwork
3535

3636
useP2PAddress bool
37-
meshStrategy meshStrategy
37+
mesher mesher
3838
}
3939

4040
// NewHybridP2PNetwork constructs a GossipNode that combines P2PNetwork and WebsocketNetwork
4141
// Hybrid mode requires both P2P and WS to be running in server (NetAddress set) or client (NetAddress empty) mode.
42-
func NewHybridP2PNetwork(log logging.Logger, cfg config.Local, datadir string, phonebookAddresses []string, genesisInfo GenesisInfo, nodeInfo NodeInfo, meshCreator MeshStrategyCreator) (*HybridP2PNetwork, error) {
42+
func NewHybridP2PNetwork(log logging.Logger, cfg config.Local, datadir string, phonebookAddresses []string, genesisInfo GenesisInfo, nodeInfo NodeInfo, meshCreator MeshCreator) (*HybridP2PNetwork, error) {
4343
if err := cfg.ValidateP2PHybridConfig(); err != nil {
4444
return nil, err
4545
}
@@ -49,15 +49,15 @@ func NewHybridP2PNetwork(log logging.Logger, cfg config.Local, datadir string, p
4949
p2pcfg.IncomingConnectionsLimit = cfg.P2PHybridIncomingConnectionsLimit
5050
identityTracker := NewIdentityTracker()
5151

52-
var subnetMeshCreator MeshStrategyCreator = meshCreator
53-
var hybridMeshCreator MeshStrategyCreator = &noopMeshStrategyCreator{}
54-
_, isHybridMeshCreator := meshCreator.(*HybridRelayMeshStrategyCreator)
52+
var subnetMeshCreator MeshCreator = meshCreator
53+
var hybridMeshCreator MeshCreator = &noopMeshCreator{}
54+
_, isHybridMeshCreator := meshCreator.(*HybridRelayMeshCreator)
5555
if meshCreator == nil && cfg.IsHybridServer() || isHybridMeshCreator {
56-
// no mesh strategy provided and this node is a listening/relaying node
57-
// then override and use hybrid relay meshing strategy
58-
// or, if a hybrid relay strategy requested explicitly, do the same
59-
subnetMeshCreator = &noopMeshStrategyCreator{}
60-
hybridMeshCreator = &HybridRelayMeshStrategyCreator{}
56+
// no mesh creator provided and this node is a listening/relaying node
57+
// then override and use hybrid relay meshing
58+
// or, if a hybrid relay meshing requested explicitly, do the same
59+
subnetMeshCreator = &noopMeshCreator{}
60+
hybridMeshCreator = &HybridRelayMeshCreator{}
6161
}
6262

6363
p2pnet, err := NewP2PNetwork(log, p2pcfg, datadir, phonebookAddresses, genesisInfo, nodeInfo, &identityOpts{tracker: identityTracker}, subnetMeshCreator)
@@ -74,17 +74,17 @@ func NewHybridP2PNetwork(log logging.Logger, cfg config.Local, datadir string, p
7474
return nil, err
7575
}
7676

77-
hybridMeshStrategy, err := hybridMeshCreator.create(
77+
hybridMesh, err := hybridMeshCreator.create(
7878
withWebsocketNetwork(wsnet),
7979
withP2PNetwork(p2pnet))
8080
if err != nil {
81-
return nil, fmt.Errorf("failed to create hybrid mesh strategy: %w", err)
81+
return nil, fmt.Errorf("failed to create hybrid mesh: %w", err)
8282
}
8383

8484
hn := &HybridP2PNetwork{
85-
p2pNetwork: p2pnet,
86-
wsNetwork: wsnet,
87-
meshStrategy: hybridMeshStrategy,
85+
p2pNetwork: p2pnet,
86+
wsNetwork: wsnet,
87+
mesher: hybridMesh,
8888
}
8989

9090
return hn, nil
@@ -204,7 +204,7 @@ func (n *HybridP2PNetwork) Start() error {
204204
if err != nil {
205205
return err
206206
}
207-
n.meshStrategy.start()
207+
n.mesher.start()
208208
return nil
209209
}
210210

@@ -215,7 +215,7 @@ func (n *HybridP2PNetwork) Stop() {
215215
return nil
216216
})
217217

218-
n.meshStrategy.stop()
218+
n.mesher.stop()
219219
}
220220

221221
// RegisterHandlers adds to the set of given message handlers.

network/hybridNetwork_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestHybridNetwork_DuplicateConn(t *testing.T) {
4747

4848
relayCfg := cfg
4949
relayCfg.ForceRelayMessages = true
50-
netA, err := NewHybridP2PNetwork(log.With("node", "netA"), relayCfg, p2pKeyDir, nil, genesisInfo, &nopeNodeInfo{}, &BaseMeshStrategyCreator{})
50+
netA, err := NewHybridP2PNetwork(log.With("node", "netA"), relayCfg, p2pKeyDir, nil, genesisInfo, &nopeNodeInfo{}, &BaseMeshCreator{})
5151
require.NoError(t, err)
5252

5353
err = netA.Start()
@@ -66,7 +66,7 @@ func TestHybridNetwork_DuplicateConn(t *testing.T) {
6666
relayCfg.NetAddress = addr
6767
relayCfg.PublicAddress = addr
6868
relayCfg.P2PHybridNetAddress = "127.0.0.1:0"
69-
netA, err = NewHybridP2PNetwork(log.With("node", "netA"), relayCfg, p2pKeyDir, nil, genesisInfo, &nopeNodeInfo{}, &BaseMeshStrategyCreator{})
69+
netA, err = NewHybridP2PNetwork(log.With("node", "netA"), relayCfg, p2pKeyDir, nil, genesisInfo, &nopeNodeInfo{}, &BaseMeshCreator{})
7070
require.NoError(t, err)
7171

7272
err = netA.Start()
@@ -87,14 +87,14 @@ func TestHybridNetwork_DuplicateConn(t *testing.T) {
8787

8888
phoneBookAddresses := []string{multiAddrStr, addr}
8989

90-
netB, err := NewHybridP2PNetwork(log.With("node", "netB"), cfg, "", phoneBookAddresses, genesisInfo, &nopeNodeInfo{}, &BaseMeshStrategyCreator{})
90+
netB, err := NewHybridP2PNetwork(log.With("node", "netB"), cfg, "", phoneBookAddresses, genesisInfo, &nopeNodeInfo{}, &BaseMeshCreator{})
9191
require.NoError(t, err)
9292
// for netB start the p2p network first
9393
err = netB.p2pNetwork.Start()
9494
require.NoError(t, err)
9595
defer netB.Stop()
9696

97-
netC, err := NewHybridP2PNetwork(log.With("node", "netC"), cfg, "", phoneBookAddresses, genesisInfo, &nopeNodeInfo{}, &BaseMeshStrategyCreator{})
97+
netC, err := NewHybridP2PNetwork(log.With("node", "netC"), cfg, "", phoneBookAddresses, genesisInfo, &nopeNodeInfo{}, &BaseMeshCreator{})
9898
require.NoError(t, err)
9999
// for netC start the ws network first
100100
err = netC.wsNetwork.Start()
@@ -193,12 +193,12 @@ func TestHybridNetwork_ValidateConfig(t *testing.T) {
193193
cfg.P2PHybridNetAddress = ""
194194
genesisInfo := GenesisInfo{genesisID, "net"}
195195

196-
_, err := NewHybridP2PNetwork(logging.TestingLog(t), cfg, "", nil, genesisInfo, &nopeNodeInfo{}, &BaseMeshStrategyCreator{})
196+
_, err := NewHybridP2PNetwork(logging.TestingLog(t), cfg, "", nil, genesisInfo, &nopeNodeInfo{}, &BaseMeshCreator{})
197197
require.ErrorContains(t, err, "both NetAddress and P2PHybridNetAddress")
198198

199199
cfg.NetAddress = ""
200200
cfg.P2PHybridNetAddress = ":0"
201-
_, err = NewHybridP2PNetwork(logging.TestingLog(t), cfg, "", nil, genesisInfo, &nopeNodeInfo{}, &BaseMeshStrategyCreator{})
201+
_, err = NewHybridP2PNetwork(logging.TestingLog(t), cfg, "", nil, genesisInfo, &nopeNodeInfo{}, &BaseMeshCreator{})
202202
require.ErrorContains(t, err, "both NetAddress and P2PHybridNetAddress")
203203
}
204204

network/mesh.go

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ import (
2828

2929
const meshThreadInterval = time.Minute
3030

31-
type meshStrategy interface {
32-
meshThread()
31+
type mesher interface {
3332
start()
3433
stop()
3534
}
3635

37-
type baseMeshStrategy struct {
36+
type baseMesher struct {
3837
wg sync.WaitGroup
3938
meshConfig
4039
}
@@ -44,18 +43,18 @@ type meshConfig struct {
4443
meshUpdateRequests chan meshRequest
4544
meshThreadInterval time.Duration
4645
backoff backoff.BackoffStrategy
47-
netMesh func() bool
48-
peerStatReport func()
46+
netMeshFn func() bool
47+
peerStatReporter func()
4948
closer func()
5049

5150
// wsnet and p2pnet are used in hybrid relay mode
5251
wsnet *WebsocketNetwork
5352
p2pnet *P2PNetwork
5453
}
5554

56-
type meshStrategyOption func(*meshConfig)
55+
type meshOption func(*meshConfig)
5756

58-
func withMeshExpJitterBackoff() meshStrategyOption {
57+
func withMeshExpJitterBackoff() meshOption {
5958
return func(cfg *meshConfig) {
6059
// Add exponential backoff with jitter to the mesh thread to handle new networks startup
6160
// when no DNS or DHT peers are available.
@@ -66,61 +65,61 @@ func withMeshExpJitterBackoff() meshStrategyOption {
6665
cfg.backoff = eb
6766
}
6867
}
69-
func withMeshNetMesh(netMesh func() bool) meshStrategyOption {
68+
func withMeshNetMeshFn(netMeshFn func() bool) meshOption {
7069
return func(cfg *meshConfig) {
71-
cfg.netMesh = netMesh
70+
cfg.netMeshFn = netMeshFn
7271
}
7372
}
74-
func withMeshPeerStatReport(peerStatReport func()) meshStrategyOption {
73+
func withMeshPeerStatReporter(peerStatReporter func()) meshOption {
7574
return func(cfg *meshConfig) {
76-
cfg.peerStatReport = peerStatReport
75+
cfg.peerStatReporter = peerStatReporter
7776
}
7877
}
79-
func withMeshCloser(closer func()) meshStrategyOption {
78+
func withMeshCloser(closer func()) meshOption {
8079
return func(cfg *meshConfig) {
8180
cfg.closer = closer
8281
}
8382
}
8483

85-
func withMeshUpdateRequest(ch chan meshRequest) meshStrategyOption {
84+
func withMeshUpdateRequest(ch chan meshRequest) meshOption {
8685
return func(cfg *meshConfig) {
8786
cfg.meshUpdateRequests = ch
8887
}
8988
}
9089

91-
func withMeshUpdateInterval(d time.Duration) meshStrategyOption {
90+
func withMeshUpdateInterval(d time.Duration) meshOption {
9291
return func(cfg *meshConfig) {
9392
cfg.meshThreadInterval = d
9493
}
9594
}
9695

97-
func withContext(ctx context.Context) meshStrategyOption {
96+
func withContext(ctx context.Context) meshOption {
9897
return func(cfg *meshConfig) {
9998
cfg.ctx = ctx
10099
}
101100
}
102101

103-
func withWebsocketNetwork(wsnet *WebsocketNetwork) meshStrategyOption {
102+
func withWebsocketNetwork(wsnet *WebsocketNetwork) meshOption {
104103
return func(cfg *meshConfig) {
105104
cfg.wsnet = wsnet
106105
}
107106
}
108107

109-
func withP2PNetwork(p2pnet *P2PNetwork) meshStrategyOption {
108+
func withP2PNetwork(p2pnet *P2PNetwork) meshOption {
110109
return func(cfg *meshConfig) {
111110
cfg.p2pnet = p2pnet
112111
}
113112
}
114113

115-
func newBaseMeshStrategy(opts ...meshStrategyOption) (*baseMeshStrategy, error) {
114+
func newBaseMesher(opts ...meshOption) (*baseMesher, error) {
116115
var cfg meshConfig
117116
for _, opt := range opts {
118117
opt(&cfg)
119118
}
120119
if cfg.ctx == nil {
121120
return nil, errors.New("context is not set")
122121
}
123-
if cfg.netMesh == nil {
122+
if cfg.netMeshFn == nil {
124123
return nil, errors.New("mesh function is not set")
125124
}
126125
if cfg.meshUpdateRequests == nil {
@@ -130,12 +129,12 @@ func newBaseMeshStrategy(opts ...meshStrategyOption) (*baseMeshStrategy, error)
130129
cfg.meshThreadInterval = meshThreadInterval
131130
}
132131

133-
return &baseMeshStrategy{
132+
return &baseMesher{
134133
meshConfig: cfg,
135134
}, nil
136135
}
137136

138-
func (m *baseMeshStrategy) meshThread() {
137+
func (m *baseMesher) meshThread() {
139138
defer m.wg.Done()
140139

141140
timer := time.NewTicker(m.meshThreadInterval)
@@ -150,7 +149,7 @@ func (m *baseMeshStrategy) meshThread() {
150149
return
151150
}
152151

153-
hasPeers := m.netMesh()
152+
hasPeers := m.netMeshFn()
154153
if m.backoff != nil {
155154
if hasPeers {
156155
// found something, reset timer to the configured value
@@ -168,41 +167,41 @@ func (m *baseMeshStrategy) meshThread() {
168167
// send the currently connected peers information to the
169168
// telemetry server; that would allow the telemetry server
170169
// to construct a cross-node map of all the nodes interconnections.
171-
m.peerStatReport()
170+
m.peerStatReporter()
172171
}
173172
}
174173

175-
func (m *baseMeshStrategy) start() {
174+
func (m *baseMesher) start() {
176175
m.wg.Add(1)
177176
go m.meshThread()
178177
}
179178

180-
func (m *baseMeshStrategy) stop() {
179+
func (m *baseMesher) stop() {
181180
m.wg.Wait()
182181
if m.closer != nil {
183182
m.closer()
184183
}
185184
}
186185

187-
// MeshStrategyCreator is an interface for creating mesh strategies.
188-
type MeshStrategyCreator interface {
189-
create(opts ...meshStrategyOption) (meshStrategy, error)
186+
// MeshCreator is an interface for creating mesh strategies.
187+
type MeshCreator interface {
188+
create(opts ...meshOption) (mesher, error)
190189
}
191190

192-
// BaseMeshStrategyCreator is a creator for the base mesh strategy used in our standard WS or P2P implementations:
191+
// BaseMeshCreator is a creator for the base mesh strategy used in our standard WS or P2P implementations:
193192
// run a mesh thread that periodically checks for new peers.
194-
type BaseMeshStrategyCreator struct {
193+
type BaseMeshCreator struct {
195194
}
196195

197-
func (c *BaseMeshStrategyCreator) create(opts ...meshStrategyOption) (meshStrategy, error) {
198-
return newBaseMeshStrategy(opts...)
196+
func (c *BaseMeshCreator) create(opts ...meshOption) (mesher, error) {
197+
return newBaseMesher(opts...)
199198
}
200199

201-
// HybridRelayMeshStrategyCreator is a creator for the hybrid relay mesh strategy used in hybrid relays:
200+
// HybridRelayMeshCreator is a creator for the hybrid relay mesh strategy used in hybrid relays:
202201
// always use wsnet nodes
203-
type HybridRelayMeshStrategyCreator struct{}
202+
type HybridRelayMeshCreator struct{}
204203

205-
func (c *HybridRelayMeshStrategyCreator) create(opts ...meshStrategyOption) (meshStrategy, error) {
204+
func (c *HybridRelayMeshCreator) create(opts ...meshOption) (mesher, error) {
206205
var cfg meshConfig
207206
for _, opt := range opts {
208207
opt(&cfg)
@@ -215,12 +214,12 @@ func (c *HybridRelayMeshStrategyCreator) create(opts ...meshStrategyOption) (mes
215214
out := make(chan meshRequest, 5)
216215
var wg sync.WaitGroup
217216

218-
creator := BaseMeshStrategyCreator{}
217+
creator := BaseMeshCreator{}
219218
ctx := cfg.wsnet.ctx
220-
strategy, err := creator.create(
219+
mesh, err := creator.create(
221220
withContext(ctx),
222-
withMeshNetMesh(cfg.wsnet.meshThreadInner),
223-
withMeshPeerStatReport(func() {
221+
withMeshNetMeshFn(cfg.wsnet.meshThreadInner),
222+
withMeshPeerStatReporter(func() {
224223
cfg.p2pnet.peerStater.sendPeerConnectionsTelemetryStatus(cfg.wsnet)
225224
cfg.p2pnet.peerStater.sendPeerConnectionsTelemetryStatus(cfg.p2pnet)
226225
}),
@@ -256,20 +255,18 @@ func (c *HybridRelayMeshStrategyCreator) create(opts ...meshStrategyOption) (mes
256255
}
257256
}()
258257

259-
return strategy, nil
258+
return mesh, nil
260259
}
261260

262-
type noopMeshStrategyCreator struct{}
261+
type noopMeshCreator struct{}
263262

264-
func (c *noopMeshStrategyCreator) create(opts ...meshStrategyOption) (meshStrategy, error) {
265-
return &noopMeshStrategy{}, nil
263+
func (c *noopMeshCreator) create(opts ...meshOption) (mesher, error) {
264+
return &noopMesh{}, nil
266265
}
267266

268-
type noopMeshStrategy struct{}
267+
type noopMesh struct{}
269268

270-
func (m *noopMeshStrategy) meshThread() {
269+
func (m *noopMesh) start() {
271270
}
272-
func (m *noopMeshStrategy) start() {
273-
}
274-
func (m *noopMeshStrategy) stop() {
271+
func (m *noopMesh) stop() {
275272
}

0 commit comments

Comments
 (0)