Skip to content

Commit a8229f5

Browse files
committed
Merge remote-tracking branch 'origin/develop' into issue-1198
2 parents 4656503 + 6882f84 commit a8229f5

15 files changed

Lines changed: 441 additions & 205 deletions

aggsender/block_notifier_polling.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,28 @@ type BlockNotifierPolling struct {
4040
config ConfigBlockNotifierPolling
4141
mu sync.Mutex
4242
lastStatus *blockNotifierPollingInternalStatus
43-
types.GenericSubscriber[types.EventNewBlock]
43+
aggkitcommon.PubSub[types.EventNewBlock]
4444
}
4545

4646
// NewBlockNotifierPolling creates a new BlockNotifierPolling.
47-
// if param `subscriber` is nil a new GenericSubscriberImpl[types.EventNewBlock] will be created.
47+
// if param `subscriber` is nil a new GenericSubscriber[types.EventNewBlock] will be created.
4848
// To use this class you need to subscribe and each time that a new block appear the subscriber
4949
// will be notified through the channel. (check unit tests TestExploratoryBlockNotifierPolling
5050
// for more information)
5151
func NewBlockNotifierPolling(ethClient aggkittypes.BaseEthereumClienter,
5252
config ConfigBlockNotifierPolling,
5353
logger aggkitcommon.Logger,
54-
subscriber types.GenericSubscriber[types.EventNewBlock]) (*BlockNotifierPolling, error) {
54+
subscriber aggkitcommon.PubSub[types.EventNewBlock]) (*BlockNotifierPolling, error) {
5555
if subscriber == nil {
56-
subscriber = NewGenericSubscriberImpl[types.EventNewBlock]()
56+
subscriber = aggkitcommon.NewGenericSubscriber[types.EventNewBlock]()
5757
}
5858

5959
return &BlockNotifierPolling{
60-
ethClient: ethClient,
61-
blockFinality: config.BlockFinalityType,
62-
logger: logger,
63-
config: config,
64-
GenericSubscriber: subscriber,
60+
ethClient: ethClient,
61+
blockFinality: config.BlockFinalityType,
62+
logger: logger,
63+
config: config,
64+
PubSub: subscriber,
6565
}, nil
6666
}
6767

aggsender/epoch_notifier_per_block.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ type EpochNotifierPerBlock struct {
7474
lastStartingEpochBlock uint64
7575

7676
Config ConfigEpochNotifierPerBlock
77-
types.GenericSubscriber[types.EpochEvent]
77+
aggkitcommon.PubSub[types.EpochEvent]
7878
}
7979

8080
func NewEpochNotifierPerBlock(blockNotifier types.BlockNotifier,
8181
logger aggkitcommon.Logger,
8282
config ConfigEpochNotifierPerBlock,
83-
subscriber types.GenericSubscriber[types.EpochEvent]) (*EpochNotifierPerBlock, error) {
83+
subscriber aggkitcommon.PubSub[types.EpochEvent]) (*EpochNotifierPerBlock, error) {
8484
if subscriber == nil {
85-
subscriber = NewGenericSubscriberImpl[types.EpochEvent]()
85+
subscriber = aggkitcommon.NewGenericSubscriber[types.EpochEvent]()
8686
}
8787

8888
err := config.Validate()
@@ -94,7 +94,7 @@ func NewEpochNotifierPerBlock(blockNotifier types.BlockNotifier,
9494
logger: logger,
9595
lastStartingEpochBlock: config.StartingEpochBlock,
9696
Config: config,
97-
GenericSubscriber: subscriber,
97+
PubSub: subscriber,
9898
}, nil
9999
}
100100

aggsender/generic_subscriber_impl.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

aggsender/mocks/mock_generic_subscriber.go

Lines changed: 0 additions & 113 deletions
This file was deleted.

aggsender/types/generic_subscriber.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

bridgeservice/bridge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ func (b *BridgeService) InjectedL1InfoLeafHandler(c *gin.Context) {
784784
}
785785

786786
if err != nil {
787-
b.logger.Errorf("failed to get L1 info tree leaf (network id=%d, leaf index=%d): %v", networkID, l1InfoTreeIndex, err)
787+
b.logger.Debugf("failed to get L1 info tree leaf (network id=%d, leaf index=%d): %v", networkID, l1InfoTreeIndex, err)
788788
c.JSON(http.StatusInternalServerError,
789789
gin.H{"error": fmt.Sprintf("failed to get L1 info tree leaf (network id=%d, leaf index=%d), error: %s",
790790
networkID, l1InfoTreeIndex, err)})

bridgesync/bridgesync.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ func NewL1(
7070
rd ReorgDetector,
7171
ethClient aggkittypes.EthClienter,
7272
originNetwork uint32,
73-
syncFullClaims bool,
7473
) (*BridgeSync, error) {
7574
return newBridgeSync(
7675
ctx,
@@ -80,7 +79,7 @@ func NewL1(
8079
ethClient,
8180
L1BridgeSyncer,
8281
originNetwork,
83-
syncFullClaims,
82+
false,
8483
)
8584
}
8685

@@ -405,6 +404,11 @@ func (s *BridgeSync) OriginNetwork() uint32 {
405404
return s.originNetwork
406405
}
407406

407+
// SubscribeToSync allows a subscriber to receive block notifications
408+
func (s *BridgeSync) SubscribeToSync(subscriberID string) <-chan sync.Block {
409+
return s.driver.SubscribeToNewBlocks(subscriberID)
410+
}
411+
408412
type LastReorg struct {
409413
DetectedAt int64 `json:"detected_at"`
410414
FromBlock uint64 `json:"from_block"`

bridgesync/bridgesync_test.go

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ func TestNewLx(t *testing.T) {
7575
mockReorgDetector,
7676
mockEthClient,
7777
originNetwork,
78-
false,
7978
)
8079

8180
require.NoError(t, err)
@@ -766,3 +765,111 @@ func TestBridgeSync_GetLastRoot(t *testing.T) {
766765
require.NotEqual(t, common.Hash{}, root.Hash)
767766
})
768767
}
768+
769+
func TestBridgeSync_SubscribeToSync(t *testing.T) {
770+
const (
771+
syncBlockChunkSize = uint64(100)
772+
initialBlock = uint64(0)
773+
waitForNewBlocksPeriod = time.Second * 10
774+
retryAfterErrorPeriod = time.Second * 5
775+
maxRetryAttemptsAfterError = 3
776+
originNetwork = uint32(1)
777+
)
778+
779+
var (
780+
blockFinalityType = aggkittypes.SafeBlock
781+
ctx = context.Background()
782+
dbPath = path.Join(t.TempDir(), "TestSubscribeToSync.sqlite")
783+
bridge = common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678")
784+
)
785+
786+
mockEthClient := mocksethclient.NewEthClienter(t)
787+
mockEthClient.EXPECT().CallContract(mock.Anything, mock.Anything, mock.Anything).Return(
788+
common.FromHex("0x000000000000000000000000000000000000000000000000000000000000002a"), nil).Once()
789+
mockEthClient.EXPECT().
790+
CallContract(
791+
mock.Anything,
792+
mock.Anything,
793+
mock.Anything,
794+
).
795+
Return(common.LeftPadBytes(common.HexToAddress("0x3c351e10").Bytes(), 32), nil).
796+
Maybe()
797+
mockReorgDetector := mocksbridgesync.NewReorgDetector(t)
798+
799+
mockReorgDetector.EXPECT().Subscribe(mock.Anything).Return(nil, nil)
800+
mockReorgDetector.EXPECT().GetFinalizedBlockType().Return(blockFinalityType)
801+
mockReorgDetector.EXPECT().String().Return("mockReorgDetector")
802+
803+
dbQueryTimeout := 30 * time.Second
804+
805+
bridgeSyncCfg := Config{
806+
DBPath: dbPath,
807+
BridgeAddr: bridge,
808+
BlockFinality: aggkittypes.LatestBlock,
809+
SyncBlockChunkSize: syncBlockChunkSize,
810+
InitialBlockNum: initialBlock,
811+
WaitForNewBlocksPeriod: cfgtypes.NewDuration(waitForNewBlocksPeriod),
812+
RetryAfterErrorPeriod: cfgtypes.NewDuration(retryAfterErrorPeriod),
813+
MaxRetryAttemptsAfterError: maxRetryAttemptsAfterError,
814+
RequireStorageContentCompatibility: false,
815+
DBQueryTimeout: cfgtypes.NewDuration(dbQueryTimeout),
816+
}
817+
818+
s, err := NewL2(
819+
ctx,
820+
bridgeSyncCfg,
821+
mockReorgDetector,
822+
mockEthClient,
823+
originNetwork,
824+
false,
825+
)
826+
require.NoError(t, err)
827+
828+
t.Run("subscribe to sync with valid parameters", func(t *testing.T) {
829+
subscriberID := "test-subscriber"
830+
831+
blockChan := s.SubscribeToSync(subscriberID)
832+
require.NotNil(t, blockChan)
833+
834+
// Verify the channel is not closed immediately
835+
select {
836+
case _, ok := <-blockChan:
837+
if !ok {
838+
t.Fatal("channel should not be closed immediately")
839+
}
840+
default:
841+
// Expected - no blocks available initially
842+
}
843+
})
844+
845+
t.Run("subscribe with empty subscriber ID", func(t *testing.T) {
846+
subscriberID := ""
847+
848+
blockChan := s.SubscribeToSync(subscriberID)
849+
require.NotNil(t, blockChan)
850+
})
851+
852+
t.Run("multiple subscribers", func(t *testing.T) {
853+
subscriber1ID := "subscriber-1"
854+
subscriber2ID := "subscriber-2"
855+
856+
blockChan1 := s.SubscribeToSync(subscriber1ID)
857+
blockChan2 := s.SubscribeToSync(subscriber2ID)
858+
859+
require.NotNil(t, blockChan1)
860+
require.NotNil(t, blockChan2)
861+
862+
// Channels should be different instances
863+
require.NotEqual(t, blockChan1, blockChan2)
864+
})
865+
866+
t.Run("subscribe with same subscriber ID multiple times", func(t *testing.T) {
867+
subscriberID := "duplicate-subscriber"
868+
869+
blockChan1 := s.SubscribeToSync(subscriberID)
870+
blockChan2 := s.SubscribeToSync(subscriberID)
871+
872+
require.NotNil(t, blockChan1)
873+
require.NotNil(t, blockChan2)
874+
})
875+
}

0 commit comments

Comments
 (0)