-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1810 from ardriveapp/PE-6517-implement-pre-sync-v…
…erification-of-snapshot-existence PE-6517: feat(snapshots)
- Loading branch information
Showing
5 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'package:ardrive/services/config/config_service.dart'; | ||
import 'package:ardrive/utils/logger.dart'; | ||
import 'package:ardrive/utils/snapshots/snapshot_item.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
class SnapshotValidationService { | ||
final ConfigService _configService; | ||
|
||
SnapshotValidationService({ | ||
required ConfigService configService, | ||
}) : _configService = configService; | ||
|
||
Future<List<SnapshotItem>> validateSnapshotItems( | ||
List<SnapshotItem> snapshotItems, | ||
) async { | ||
List<SnapshotItem> snapshotsVerified = []; | ||
|
||
final futures = snapshotItems.map((snapshotItem) async { | ||
final appConfig = _configService.config; | ||
|
||
try { | ||
final snapshotValidation = await http.head( | ||
Uri.parse( | ||
'${appConfig.defaultArweaveGatewayUrl}/${snapshotItem.txId}'), | ||
); | ||
|
||
logger.d('Validating snapshot ${snapshotItem.txId}'); | ||
|
||
if (snapshotValidation.statusCode == 200) { | ||
if (snapshotValidation.headers['content-length'] != null) { | ||
int lenght = | ||
int.parse(snapshotValidation.headers['content-length']!); | ||
|
||
final headers = { | ||
'Range': 'bytes=${lenght - 8}-$lenght', | ||
}; | ||
|
||
final validationRequest = await http.get( | ||
Uri.parse( | ||
'${appConfig.defaultArweaveGatewayUrl}${snapshotItem.txId}'), | ||
headers: headers, | ||
); | ||
|
||
logger.d( | ||
'Validation request status code: ${validationRequest.statusCode}'); | ||
} | ||
logger.d('Snapshot ${snapshotItem.txId} is valid'); | ||
|
||
snapshotsVerified.add(snapshotItem); | ||
} | ||
} catch (e) { | ||
logger.w('Error while validating snapshot. ${snapshotItem.txId}'); | ||
} | ||
}); | ||
|
||
await Future.wait(futures); | ||
|
||
snapshotItems.clear(); | ||
snapshotItems = snapshotsVerified; | ||
|
||
return snapshotItems; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters