diff --git a/dot/network/notifications.go b/dot/network/notifications.go index f58636276d..a938a64661 100644 --- a/dot/network/notifications.go +++ b/dot/network/notifications.go @@ -227,10 +227,6 @@ func (s *Service) handleHandshake(info *notificationsProtocol, stream network.St logger.Tracef("receiver: sent handshake to peer %s using protocol %s", peer, info.protocolID) - // if err := stream.CloseWrite(); err != nil { - // return fmt.Errorf("failed to close stream for writing: %s", err) - // } - return nil } diff --git a/dot/sync/fullsync_handle_block.go b/dot/sync/block_importer.go similarity index 98% rename from dot/sync/fullsync_handle_block.go rename to dot/sync/block_importer.go index be9ee14a50..3e6ab4898f 100644 --- a/dot/sync/fullsync_handle_block.go +++ b/dot/sync/block_importer.go @@ -80,7 +80,7 @@ func newBlockImporter(cfg *FullSyncConfig) *blockImporter { } } -func (b *blockImporter) handle(bd *types.BlockData, origin BlockOrigin) (imported bool, err error) { +func (b *blockImporter) importBlock(bd *types.BlockData, origin BlockOrigin) (imported bool, err error) { blockAlreadyExists, err := b.blockState.HasHeader(bd.Hash) if err != nil && !errors.Is(err, database.ErrNotFound) { return false, err diff --git a/dot/sync/fullsync.go b/dot/sync/fullsync.go index 749dc77ebe..a0930d548d 100644 --- a/dot/sync/fullsync.go +++ b/dot/sync/fullsync.go @@ -44,8 +44,8 @@ type FullSyncConfig struct { RequestMaker network.RequestMaker } -type Importer interface { - handle(*types.BlockData, BlockOrigin) (imported bool, err error) +type importer interface { + importBlock(*types.BlockData, BlockOrigin) (imported bool, err error) } // FullSyncStrategy protocol is the "default" protocol. @@ -61,7 +61,7 @@ type FullSyncStrategy struct { numOfTasks int startedAt time.Time syncedBlocks int - importer Importer + blockImporter importer } func NewFullSyncStrategy(cfg *FullSyncConfig) *FullSyncStrategy { @@ -74,7 +74,7 @@ func NewFullSyncStrategy(cfg *FullSyncConfig) *FullSyncStrategy { reqMaker: cfg.RequestMaker, blockState: cfg.BlockState, numOfTasks: cfg.NumOfTasks, - importer: newBlockImporter(cfg), + blockImporter: newBlockImporter(cfg), unreadyBlocks: newUnreadyBlocks(), requestQueue: &requestsQueue[*messages.BlockRequestMessage]{ queue: list.New(), @@ -183,8 +183,8 @@ func (f *FullSyncStrategy) Process(results []*SyncTaskResult) ( // disjoint fragments are pieces of the chain that could not be imported right now // because is blocks too far ahead or blocks that belongs to forks - orderedFragments := sortFragmentsOfChain(readyBlocks) - orderedFragments = mergeFragmentsOfChain(orderedFragments) + sortFragmentsOfChain(readyBlocks) + orderedFragments := mergeFragmentsOfChain(readyBlocks) nextBlocksToImport := make([]*types.BlockData, 0) disjointFragments := make([][]*types.BlockData, 0) @@ -206,7 +206,7 @@ func (f *FullSyncStrategy) Process(results []*SyncTaskResult) ( // this loop goal is to import ready blocks as well as update the highestFinalized header for len(nextBlocksToImport) > 0 || len(disjointFragments) > 0 { for _, blockToImport := range nextBlocksToImport { - imported, err := f.importer.handle(blockToImport, networkInitialSync) + imported, err := f.blockImporter.importBlock(blockToImport, networkInitialSync) if err != nil { return false, nil, nil, fmt.Errorf("while handling ready block: %w", err) } @@ -486,9 +486,9 @@ resultLoop: // note that we have fragments with single blocks, fragments with fork (in case of 8) // after sorting these fragments we end up with: // [ {1, 2, 3, 4, 5} {6, 7, 8, 9, 10} {8} {11, 12, 13, 14, 15, 16} {17} ] -func sortFragmentsOfChain(fragments [][]*types.BlockData) [][]*types.BlockData { +func sortFragmentsOfChain(fragments [][]*types.BlockData) { if len(fragments) == 0 { - return nil + return } slices.SortFunc(fragments, func(a, b []*types.BlockData) int { @@ -500,8 +500,6 @@ func sortFragmentsOfChain(fragments [][]*types.BlockData) [][]*types.BlockData { } return 1 }) - - return fragments } // mergeFragmentsOfChain expects a sorted slice of fragments and merges those diff --git a/dot/sync/fullsync_test.go b/dot/sync/fullsync_test.go index 4d766d4baa..6f11abd290 100644 --- a/dot/sync/fullsync_test.go +++ b/dot/sync/fullsync_test.go @@ -20,6 +20,8 @@ import ( _ "embed" ) +type mockBlockImporter struct{} + //go:embed testdata/westend_blocks.yaml var rawWestendBlocks []byte @@ -234,9 +236,9 @@ func TestFullSyncProcess(t *testing.T) { Return(false, nil). Times(2) - mockImporter := NewMockImporter(ctrl) + mockImporter := NewMockimporter(ctrl) mockImporter.EXPECT(). - handle(gomock.AssignableToTypeOf(&types.BlockData{}), networkInitialSync). + importBlock(gomock.AssignableToTypeOf(&types.BlockData{}), networkInitialSync). Return(true, nil). Times(10 + 128 + 128) @@ -245,7 +247,7 @@ func TestFullSyncProcess(t *testing.T) { } fs := NewFullSyncStrategy(cfg) - fs.importer = mockImporter + fs.blockImporter = mockImporter done, _, _, err := fs.Process(syncTaskResults) require.NoError(t, err) diff --git a/dot/sync/mock_importer.go b/dot/sync/mock_importer.go new file mode 100644 index 0000000000..6fb953b8b6 --- /dev/null +++ b/dot/sync/mock_importer.go @@ -0,0 +1,55 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: fullsync.go +// +// Generated by this command: +// +// mockgen -destination=mock_importer.go -source=fullsync.go -package=sync +// + +// Package sync is a generated GoMock package. +package sync + +import ( + reflect "reflect" + + types "github.com/ChainSafe/gossamer/dot/types" + gomock "go.uber.org/mock/gomock" +) + +// Mockimporter is a mock of importer interface. +type Mockimporter struct { + ctrl *gomock.Controller + recorder *MockimporterMockRecorder +} + +// MockimporterMockRecorder is the mock recorder for Mockimporter. +type MockimporterMockRecorder struct { + mock *Mockimporter +} + +// NewMockimporter creates a new mock instance. +func NewMockimporter(ctrl *gomock.Controller) *Mockimporter { + mock := &Mockimporter{ctrl: ctrl} + mock.recorder = &MockimporterMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *Mockimporter) EXPECT() *MockimporterMockRecorder { + return m.recorder +} + +// importBlock mocks base method. +func (m *Mockimporter) importBlock(arg0 *types.BlockData, arg1 BlockOrigin) (bool, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "importBlock", arg0, arg1) + ret0, _ := ret[0].(bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// importBlock indicates an expected call of importBlock. +func (mr *MockimporterMockRecorder) importBlock(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "importBlock", reflect.TypeOf((*Mockimporter)(nil).importBlock), arg0, arg1) +} diff --git a/dot/sync/mocks_generate_test.go b/dot/sync/mocks_generate_test.go index 894b5747f6..a8f52d172f 100644 --- a/dot/sync/mocks_generate_test.go +++ b/dot/sync/mocks_generate_test.go @@ -3,5 +3,6 @@ package sync -//go:generate mockgen -destination=mocks_test.go -package=$GOPACKAGE . Telemetry,BlockState,StorageState,TransactionState,BabeVerifier,FinalityGadget,BlockImportHandler,Network,Importer +//go:generate mockgen -destination=mocks_test.go -package=$GOPACKAGE . Telemetry,BlockState,StorageState,TransactionState,BabeVerifier,FinalityGadget,BlockImportHandler,Network //go:generate mockgen -destination=mock_request_maker.go -package $GOPACKAGE github.com/ChainSafe/gossamer/dot/network RequestMaker +//go:generate mockgen -destination=mock_importer.go -source=fullsync.go -package=sync diff --git a/dot/sync/mocks_test.go b/dot/sync/mocks_test.go index 6ad35f501c..ef04c575d7 100644 --- a/dot/sync/mocks_test.go +++ b/dot/sync/mocks_test.go @@ -1,9 +1,9 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: github.com/ChainSafe/gossamer/dot/sync (interfaces: Telemetry,BlockState,StorageState,TransactionState,BabeVerifier,FinalityGadget,BlockImportHandler,Network,Importer) +// Source: github.com/ChainSafe/gossamer/dot/sync (interfaces: Telemetry,BlockState,StorageState,TransactionState,BabeVerifier,FinalityGadget,BlockImportHandler,Network) // // Generated by this command: // -// mockgen -destination=mocks_test.go -package=sync . Telemetry,BlockState,StorageState,TransactionState,BabeVerifier,FinalityGadget,BlockImportHandler,Network,Importer +// mockgen -destination=mocks_test.go -package=sync . Telemetry,BlockState,StorageState,TransactionState,BabeVerifier,FinalityGadget,BlockImportHandler,Network // // Package sync is a generated GoMock package. @@ -731,41 +731,3 @@ func (mr *MockNetworkMockRecorder) ReportPeer(arg0, arg1 any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReportPeer", reflect.TypeOf((*MockNetwork)(nil).ReportPeer), arg0, arg1) } - -// MockImporter is a mock of Importer interface. -type MockImporter struct { - ctrl *gomock.Controller - recorder *MockImporterMockRecorder -} - -// MockImporterMockRecorder is the mock recorder for MockImporter. -type MockImporterMockRecorder struct { - mock *MockImporter -} - -// NewMockImporter creates a new mock instance. -func NewMockImporter(ctrl *gomock.Controller) *MockImporter { - mock := &MockImporter{ctrl: ctrl} - mock.recorder = &MockImporterMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockImporter) EXPECT() *MockImporterMockRecorder { - return m.recorder -} - -// handle mocks base method. -func (m *MockImporter) handle(arg0 *types.BlockData, arg1 BlockOrigin) (bool, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "handle", arg0, arg1) - ret0, _ := ret[0].(bool) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// handle indicates an expected call of handle. -func (mr *MockImporterMockRecorder) handle(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "handle", reflect.TypeOf((*MockImporter)(nil).handle), arg0, arg1) -}