feat: add support to check the local store and inject it 1st as a signed location before going into the network

This commit is contained in:
Derrick Hammer 2024-03-02 05:23:39 -05:00
parent ed79c80def
commit 1a4890a6c0
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 28 additions and 0 deletions

View File

@ -61,6 +61,18 @@ func (s *StorageLocationProviderImpl) Start() error {
s.timeout = time.Now().Add(s.timeoutDuration) s.timeout = time.Now().Add(s.timeoutDuration)
s.isTimedOut = false s.isTimedOut = false
s.mutex.Unlock() s.mutex.Unlock()
local := s.checkLocal()
if local != nil {
s.availableNodes = append([]*encoding.NodeId{local.NodeId()}, s.availableNodes...)
nodeIdStr, err := local.NodeId().ToString()
if err != nil {
s.logger.Error("Error decoding node id", zap.Error(err))
return err
}
s.uris[nodeIdStr] = local.Location()
}
go func() { go func() {
requestSent := false requestSent := false
@ -214,6 +226,22 @@ func (s *StorageLocationProviderImpl) Downvote(uri storage.SignedStorageLocation
return nil return nil
} }
func (s *StorageLocationProviderImpl) checkLocal() storage.SignedStorageLocation {
if s.services.Storage().ProviderStore() != nil {
if s.services.Storage().ProviderStore().CanProvide(s.hash, s.types) {
location, _ := s.services.Storage().ProviderStore().Provide(s.hash, s.types)
message := storage.PrepareProvideMessage(s.services.P2P().Config().KeyPair, s.hash, location)
location.SetProviderMessage(message)
return storage.NewSignedStorageLocation(s.services.P2P().NodeId(), location)
}
}
return nil
}
func NewStorageLocationProvider(params StorageLocationProviderParams) *StorageLocationProviderImpl { func NewStorageLocationProvider(params StorageLocationProviderParams) *StorageLocationProviderImpl {
if params.LocationTypes == nil { if params.LocationTypes == nil {
params.LocationTypes = []types.StorageLocationType{ params.LocationTypes = []types.StorageLocationType{