From d54dff5767e5d9b0a692099d73e208c6ff351eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Tue, 5 Nov 2024 12:51:40 +0100 Subject: [PATCH] refactor: gsoc handler param soc as reference --- pkg/api/gsoc_test.go | 6 +++--- pkg/gsoc/gsoc.go | 4 ++-- pkg/gsoc/gsoc_test.go | 8 ++++---- pkg/pullsync/pullsync.go | 6 +++--- pkg/pullsync/pullsync_test.go | 2 +- pkg/pushsync/pushsync.go | 6 +++--- pkg/pushsync/pushsync_test.go | 20 ++++++++++---------- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/pkg/api/gsoc_test.go b/pkg/api/gsoc_test.go index 5d0a70cc9c4..edef7a39842 100644 --- a/pkg/api/gsoc_test.go +++ b/pkg/api/gsoc_test.go @@ -45,7 +45,7 @@ func TestGsocWebsocketSingleHandler(t *testing.T) { socCh := soc.New(id, ch) ch, _ = socCh.Sign(signer) socCh, _ = soc.FromChunk(ch) - g.Handle(*socCh) + g.Handle(socCh) go expectMessage(t, cl, respC, payload) if err := <-respC; err != nil { @@ -87,7 +87,7 @@ func TestGsocWebsocketMultiHandler(t *testing.T) { t.Fatal(err) } - g.Handle(*socCh) + g.Handle(socCh) go expectMessage(t, cl, respC, payload) go expectMessage(t, cl2, respC, payload) @@ -125,7 +125,7 @@ func TestGsocPong(t *testing.T) { ch, _ = socCh.Sign(signer) socCh, _ = soc.FromChunk(ch) - g.Handle(*socCh) + g.Handle(socCh) go expectMessage(t, cl, respC, nil) if err := <-respC; err == nil || !strings.Contains(err.Error(), "i/o timeout") { diff --git a/pkg/gsoc/gsoc.go b/pkg/gsoc/gsoc.go index e342de9f549..4aecc27f5f6 100644 --- a/pkg/gsoc/gsoc.go +++ b/pkg/gsoc/gsoc.go @@ -14,7 +14,7 @@ import ( type Listener interface { Subscribe(address [32]byte, handler handler) (cleanup func()) - Handle(c soc.SOC) + Handle(c *soc.SOC) Close() error } @@ -56,7 +56,7 @@ func (l *listener) Subscribe(address [32]byte, handler handler) (cleanup func()) } // Handle is called by push/pull sync and passes the chunk its registered handler -func (l *listener) Handle(c soc.SOC) { +func (l *listener) Handle(c *soc.SOC) { addr, err := c.Address() if err != nil { return // no handler diff --git a/pkg/gsoc/gsoc_test.go b/pkg/gsoc/gsoc_test.go index 0bab1b39bfb..989225df068 100644 --- a/pkg/gsoc/gsoc_test.go +++ b/pkg/gsoc/gsoc_test.go @@ -66,7 +66,7 @@ func TestRegister(t *testing.T) { socCh2, _ = soc.FromChunk(ch2) // trigger soc upload on address1, check that only h1 is called - g.Handle(*socCh1) + g.Handle(socCh1) waitHandlerCallback(t, &msgChan, 1) @@ -76,7 +76,7 @@ func TestRegister(t *testing.T) { // register another handler on the first address cleanup := g.Subscribe([32]byte(address1.Bytes()), h3) - g.Handle(*socCh1) + g.Handle(socCh1) waitHandlerCallback(t, &msgChan, 2) @@ -86,7 +86,7 @@ func TestRegister(t *testing.T) { cleanup() // remove the last handler - g.Handle(*socCh1) + g.Handle(socCh1) waitHandlerCallback(t, &msgChan, 1) @@ -94,7 +94,7 @@ func TestRegister(t *testing.T) { ensureCalls(t, &h2Calls, 0) ensureCalls(t, &h3Calls, 1) - g.Handle(*socCh2) + g.Handle(socCh2) waitHandlerCallback(t, &msgChan, 1) diff --git a/pkg/pullsync/pullsync.go b/pkg/pullsync/pullsync.go index a917169e0d6..1f8d55d4495 100644 --- a/pkg/pullsync/pullsync.go +++ b/pkg/pullsync/pullsync.go @@ -71,7 +71,7 @@ type Syncer struct { store storer.Reserve quit chan struct{} unwrap func(swarm.Chunk) - gsocHandler func(soc.SOC) + gsocHandler func(*soc.SOC) validStamp postage.ValidStampFn intervalsSF singleflight.Group[string, *collectAddrsResult] syncInProgress atomic.Int32 @@ -88,7 +88,7 @@ func New( streamer p2p.Streamer, store storer.Reserve, unwrap func(swarm.Chunk), - gsocHandler func(soc.SOC), + gsocHandler func(*soc.SOC), validStamp postage.ValidStampFn, logger log.Logger, maxPage uint64, @@ -360,7 +360,7 @@ func (s *Syncer) Sync(ctx context.Context, peer swarm.Address, bin uint8, start if cac.Valid(chunk) { go s.unwrap(chunk) } else if chunk, err := soc.FromChunk(chunk); err == nil { - s.gsocHandler(*chunk) + s.gsocHandler(chunk) } else { s.logger.Debug("invalid cac/soc chunk", "error", swarm.ErrInvalidChunk, "peer_address", peer, "chunk", chunk) chunkErr = errors.Join(chunkErr, swarm.ErrInvalidChunk) diff --git a/pkg/pullsync/pullsync_test.go b/pkg/pullsync/pullsync_test.go index 68d9e04ecbc..fc80bae137f 100644 --- a/pkg/pullsync/pullsync_test.go +++ b/pkg/pullsync/pullsync_test.go @@ -354,7 +354,7 @@ func newPullSyncWithStamperValidator( storage := mock.NewReserve(o...) logger := log.Noop unwrap := func(swarm.Chunk) {} - socHandler := func(soc.SOC) {} + socHandler := func(*soc.SOC) {} ps := pullsync.New( s, storage, diff --git a/pkg/pushsync/pushsync.go b/pkg/pushsync/pushsync.go index 22348100dae..4c7983660cf 100644 --- a/pkg/pushsync/pushsync.go +++ b/pkg/pushsync/pushsync.go @@ -85,7 +85,7 @@ type PushSync struct { store Storer topologyDriver topology.Driver unwrap func(swarm.Chunk) - gsocHandler func(soc.SOC) + gsocHandler func(*soc.SOC) logger log.Logger accounting accounting.Interface pricer pricer.Interface @@ -115,7 +115,7 @@ func New( topology topology.Driver, fullNode bool, unwrap func(swarm.Chunk), - gsocHandler func(soc.SOC), + gsocHandler func(*soc.SOC), validStamp postage.ValidStampFn, logger log.Logger, accounting accounting.Interface, @@ -229,7 +229,7 @@ func (ps *PushSync) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream) if cac.Valid(chunk) { go ps.unwrap(chunk) } else if chunk, err := soc.FromChunk(chunk); err == nil { - ps.gsocHandler(*chunk) + ps.gsocHandler(chunk) } else { return swarm.ErrInvalidChunk } diff --git a/pkg/pushsync/pushsync_test.go b/pkg/pushsync/pushsync_test.go index 73296496753..8773c693de0 100644 --- a/pkg/pushsync/pushsync_test.go +++ b/pkg/pushsync/pushsync_test.go @@ -145,7 +145,7 @@ func TestSocListener(t *testing.T) { } sch2 = sch2.WithStamp(chunk2.Stamp()) expectedPayload := chunk1.Data() - gsocListener := func(soc soc.SOC) { + gsocListener := func(soc *soc.SOC) { if !bytes.Equal(soc.WrappedChunk().Data(), expectedPayload) { t.Fatalf("unexpected SOC payload on GSOC listener. got %s, want %s", soc.WrappedChunk().Data(), expectedPayload) } @@ -468,7 +468,7 @@ func TestPushChunkToClosestErrorAttemptRetry(t *testing.T) { }), ) - psPivot, pivotStorer := createPushSyncNodeWithAccounting(t, pivotNode, defaultPrices, recorder, nil, defaultSigner(chunk), pivotAccounting, log.Noop, func(soc.SOC) {}, mock.WithPeers(peer1, peer2, peer3, peer4)) + psPivot, pivotStorer := createPushSyncNodeWithAccounting(t, pivotNode, defaultPrices, recorder, nil, defaultSigner(chunk), pivotAccounting, log.Noop, func(*soc.SOC) {}, mock.WithPeers(peer1, peer2, peer3, peer4)) // Trigger the sending of chunk to the closest node receipt, err := psPivot.PushChunkToClosest(context.Background(), chunk) @@ -645,15 +645,15 @@ func TestPropagateErrMsg(t *testing.T) { captureLogger := log.NewLogger("test", log.WithSink(buf)) // Create the closest peer - psClosestPeer, _ := createPushSyncNodeWithAccounting(t, closestPeer, defaultPrices, nil, nil, faultySigner, accountingmock.NewAccounting(), log.Noop, func(soc.SOC) {}, mock.WithClosestPeerErr(topology.ErrWantSelf)) + psClosestPeer, _ := createPushSyncNodeWithAccounting(t, closestPeer, defaultPrices, nil, nil, faultySigner, accountingmock.NewAccounting(), log.Noop, func(*soc.SOC) {}, mock.WithClosestPeerErr(topology.ErrWantSelf)) // creating the pivot peer - psPivot, _ := createPushSyncNodeWithAccounting(t, pivotPeer, defaultPrices, nil, nil, defaultSigner(chunk), accountingmock.NewAccounting(), log.Noop, func(soc.SOC) {}, mock.WithPeers(closestPeer)) + psPivot, _ := createPushSyncNodeWithAccounting(t, pivotPeer, defaultPrices, nil, nil, defaultSigner(chunk), accountingmock.NewAccounting(), log.Noop, func(*soc.SOC) {}, mock.WithPeers(closestPeer)) combinedRecorder := streamtest.New(streamtest.WithProtocols(psPivot.Protocol(), psClosestPeer.Protocol()), streamtest.WithBaseAddr(triggerPeer)) // Creating the trigger peer - psTriggerPeer, _ := createPushSyncNodeWithAccounting(t, triggerPeer, defaultPrices, combinedRecorder, nil, defaultSigner(chunk), accountingmock.NewAccounting(), captureLogger, func(soc.SOC) {}, mock.WithPeers(pivotPeer)) + psTriggerPeer, _ := createPushSyncNodeWithAccounting(t, triggerPeer, defaultPrices, combinedRecorder, nil, defaultSigner(chunk), accountingmock.NewAccounting(), captureLogger, func(*soc.SOC) {}, mock.WithPeers(pivotPeer)) _, err := psTriggerPeer.PushChunkToClosest(context.Background(), chunk) if err == nil { @@ -829,7 +829,7 @@ func createPushSyncNode( ) (*pushsync.PushSync, *testStorer, accounting.Interface) { t.Helper() mockAccounting := accountingmock.NewAccounting() - ps, mstorer := createPushSyncNodeWithAccounting(t, addr, prices, recorder, unwrap, signer, mockAccounting, log.Noop, func(soc.SOC) {}, mockOpts...) + ps, mstorer := createPushSyncNodeWithAccounting(t, addr, prices, recorder, unwrap, signer, mockAccounting, log.Noop, func(*soc.SOC) {}, mockOpts...) return ps, mstorer, mockAccounting } @@ -838,7 +838,7 @@ func createGsocPushSyncNode( addr swarm.Address, prices pricerParameters, recorder *streamtest.Recorder, - gsocListener func(soc.SOC), + gsocListener func(*soc.SOC), signer crypto.Signer, mockOpts ...mock.Option, ) (*pushsync.PushSync, *testStorer, accounting.Interface) { @@ -878,7 +878,7 @@ func createPushSyncNodeWithRadius( radiusFunc := func() (uint8, error) { return radius, nil } - ps := pushsync.New(addr, 1, blockHash.Bytes(), recorderDisconnecter, storer, radiusFunc, mockTopology, true, unwrap, func(soc.SOC) {}, validStamp, log.Noop, accountingmock.NewAccounting(), mockPricer, signer, nil, -1) + ps := pushsync.New(addr, 1, blockHash.Bytes(), recorderDisconnecter, storer, radiusFunc, mockTopology, true, unwrap, func(*soc.SOC) {}, validStamp, log.Noop, accountingmock.NewAccounting(), mockPricer, signer, nil, -1) t.Cleanup(func() { ps.Close() }) return ps, storer @@ -893,7 +893,7 @@ func createPushSyncNodeWithAccounting( signer crypto.Signer, acct accounting.Interface, logger log.Logger, - gsocListener func(soc.SOC), + gsocListener func(*soc.SOC), mockOpts ...mock.Option, ) (*pushsync.PushSync, *testStorer) { t.Helper() @@ -910,7 +910,7 @@ func createPushSyncNodeWithAccounting( unwrap = func(swarm.Chunk) {} } if gsocListener == nil { - gsocListener = func(soc.SOC) {} + gsocListener = func(*soc.SOC) {} } validStamp := func(ch swarm.Chunk) (swarm.Chunk, error) {