@@ -63,7 +63,6 @@ type ArchiveManager struct {
6363 codexConfig * params.CodexConfig
6464 isCodexClientStarted bool
6565 torrentTasks map [string ]metainfo.Hash
66- indexCidTasks map [string ]string
6766 historyArchiveDownloadTasks map [string ]* HistoryArchiveDownloadTask
6867 historyArchiveTasksWaitGroup sync.WaitGroup
6968 historyArchiveTasks sync.Map // stores `chan struct{}`
@@ -86,7 +85,6 @@ func NewArchiveManager(amc *ArchiveManagerConfig) *ArchiveManager {
8685 torrentConfig : amc .TorrentConfig ,
8786 codexConfig : amc .CodexConfig ,
8887 torrentTasks : make (map [string ]metainfo.Hash ),
89- indexCidTasks : make (map [string ]string ),
9088 historyArchiveDownloadTasks : make (map [string ]* HistoryArchiveDownloadTask ),
9189
9290 logger : amc .Logger ,
@@ -279,6 +277,16 @@ func (m *ArchiveManager) Stop() error {
279277 return nil
280278}
281279
280+ func (m * ArchiveManager ) GetCodexClient () * CodexClient {
281+ return m .codexClient
282+ }
283+
284+ func (m * ArchiveManager ) SetCodexClient (client * CodexClient ) {
285+ m .codexClient = client
286+ m .ArchiveFileManager .codexClient = client
287+ m .isCodexClientStarted = true
288+ }
289+
282290func (m * ArchiveManager ) torrentClientStarted () bool {
283291 return m .torrentClient != nil
284292}
@@ -471,6 +479,7 @@ func (m *ArchiveManager) StartHistoryArchiveTasksInterval(community *Community,
471479 }
472480 case <- cancel :
473481 m .UnseedHistoryArchiveTorrent (community .ID ())
482+ m .UnseedHistoryArchiveIndexCid (community .ID ())
474483 m .historyArchiveTasks .Delete (id )
475484 m .historyArchiveTasksWaitGroup .Done ()
476485 return
@@ -551,17 +560,25 @@ func (m *ArchiveManager) UnseedHistoryArchiveTorrent(communityID types.HexBytes)
551560}
552561
553562func (m * ArchiveManager ) UnseedHistoryArchiveIndexCid (communityID types.HexBytes ) {
554- id := communityID .String ()
563+ // Remove local index file
564+ err := m .removeCodexIndexFile (communityID )
565+ if err != nil {
566+ m .logger .Error ("failed to remove local index file" , zap .Error (err ))
567+ }
555568
556- if cid , exists := m .indexCidTasks [id ]; exists {
557- m .logger .Debug ("Unseeding index CID for community" , zap .String ("id" , id ), zap .String ("cid" , cid ))
558- // ToDo: consider "unpinning" the index Cid, so that it is no longer advertised on DHT
559- // For now, we remove it from tracking and could delete the local index file
560- delete (m .indexCidTasks , id )
569+ // get currently advertised index Cid
570+ cid , err := m .GetHistoryArchiveIndexCid (communityID )
561571
562- // Optional: Remove local index file if we want to clean up storage
563- // indexFilePath := m.ArchiveFileManager.codexArchiveIndexFile(id)
564- // os.Remove(indexFilePath)
572+ if err != nil {
573+ m .logger .Debug ("failed to get history archive index CID" , zap .Error (err ))
574+ return
575+ }
576+
577+ m .logger .Debug ("Unseeding index CID for community" , zap .String ("id" , communityID .String ()), zap .String ("cid" , cid ))
578+
579+ err = m .codexClient .RemoveCid (cid )
580+ if err != nil {
581+ m .logger .Error ("failed to remove CID from Codex" , zap .Error (err ))
565582 }
566583}
567584
@@ -763,14 +780,13 @@ func (m *ArchiveManager) DownloadHistoryArchivesByIndexCid(communityID types.Hex
763780 Cancelled : false ,
764781 }
765782
766- m .indexCidTasks [id ] = indexCid
767783 timeout := time .After (20 * time .Second )
768784
769785 // Create separate cancel channel for the index downloader to avoid channel competition
770786 indexDownloaderCancel := make (chan struct {})
771787
772788 // Create index downloader with path to index file using helper function
773- indexFilePath := m .ArchiveFileManager . codexArchiveIndexFile ( id )
789+ indexFilePath := m .codexArchiveIndexFilePath ( communityID )
774790 indexDownloader := NewCodexIndexDownloader (m .codexClient , indexCid , indexFilePath , indexDownloaderCancel , m .logger )
775791
776792 m .logger .Debug ("fetching history index from Codex" , zap .String ("indexCid" , indexCid ))
@@ -817,8 +833,13 @@ func (m *ArchiveManager) DownloadHistoryArchivesByIndexCid(communityID types.Hex
817833 }
818834
819835 if indexDownloader .IsDownloadComplete () {
836+ err := m .writeCodexIndexCidToFile (communityID , indexCid )
837+ if err != nil {
838+ m .logger .Error ("failed to write Codex index CID to file" , zap .Error (err ))
839+ return nil , err
840+ }
820841
821- index , err := m .ArchiveFileManager . CodexLoadHistoryArchiveIndexFromFile (m .identity , communityID )
842+ index , err := m .CodexLoadHistoryArchiveIndexFromFile (m .identity , communityID )
822843 if err != nil {
823844 return nil , err
824845 }
0 commit comments