Improvements relating to fetching metadata

- Rate limiter is disabled when using the API
- fetchArbitraryMetadata() returns the actual metadata content rather than a boolean
- Exceptions are thrown on certain errors, rather than returning null
This commit is contained in:
CalDescent 2022-02-26 16:10:26 +00:00
parent 35dba27a55
commit 878394535e
2 changed files with 30 additions and 13 deletions

View File

@ -688,12 +688,16 @@ public class ArbitraryResource {
ArbitraryDataResource resource = new ArbitraryDataResource(name, ResourceIdType.NAME, service, identifier);
byte[] metadata = ArbitraryMetadataManager.getInstance().fetchMetadata(resource);
try {
byte[] metadata = ArbitraryMetadataManager.getInstance().fetchMetadata(resource, false);
if (metadata != null) {
return new String(metadata, StandardCharsets.UTF_8);
}
} catch (IllegalArgumentException e) {
throw ApiExceptionFactory.INSTANCE.createCustomException(request, ApiError.INVALID_CRITERIA, e.getMessage());
}
return null;
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA);
}

View File

@ -75,7 +75,7 @@ public class ArbitraryMetadataManager {
}
public byte[] fetchMetadata(ArbitraryDataResource arbitraryDataResource) {
public byte[] fetchMetadata(ArbitraryDataResource arbitraryDataResource, boolean useRateLimiter) {
try (final Repository repository = RepositoryManager.getRepository()) {
// Find latest transaction
ArbitraryTransactionData latestTransaction = repository.getArbitraryRepository()
@ -87,7 +87,7 @@ public class ArbitraryMetadataManager {
byte[] metadataHash = latestTransaction.getMetadataHash();
if (metadataHash == null) {
// This resource doesn't have metadata
return null;
throw new IllegalArgumentException("This resource doesn't have metadata");
}
ArbitraryDataFile metadataFile = ArbitraryDataFile.fromHash(metadataHash, signature);
@ -97,7 +97,7 @@ public class ArbitraryMetadataManager {
}
else {
// Request from network
this.fetchArbitraryMetadata(latestTransaction);
return this.fetchArbitraryMetadata(latestTransaction, useRateLimiter);
}
}
@ -111,20 +111,25 @@ public class ArbitraryMetadataManager {
// Request metadata from network
public boolean fetchArbitraryMetadata(ArbitraryTransactionData arbitraryTransactionData) {
public byte[] fetchArbitraryMetadata(ArbitraryTransactionData arbitraryTransactionData, boolean useRateLimiter) {
byte[] metadataHash = arbitraryTransactionData.getMetadataHash();
if (metadataHash == null) {
return null;
}
byte[] signature = arbitraryTransactionData.getSignature();
String signature58 = Base58.encode(signature);
// Require an NTP sync
Long now = NTP.getTime();
if (now == null) {
return false;
return null;
}
// If we've already tried too many times in a short space of time, make sure to give up
if (!this.shouldMakeMetadataRequestForSignature(signature58)) {
if (useRateLimiter && !this.shouldMakeMetadataRequestForSignature(signature58)) {
LOGGER.trace("Skipping metadata request for signature {} due to rate limit", signature58);
return false;
return null;
}
this.addToSignatureRequests(signature58, true, false);
@ -162,14 +167,22 @@ public class ArbitraryMetadataManager {
requestEntry = arbitraryMetadataRequests.get(id);
if (requestEntry == null)
return false;
return null;
if (requestEntry.getA() == null)
break;
totalWait += singleWait;
}
return true;
try {
ArbitraryDataFile metadataFile = ArbitraryDataFile.fromHash(metadataHash, signature);
return metadataFile.getBytes();
} catch (DataException e) {
// Do nothing
}
return null;
}