forked from Qortal/qortal
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:
parent
35dba27a55
commit
878394535e
@ -688,12 +688,16 @@ public class ArbitraryResource {
|
|||||||
|
|
||||||
ArbitraryDataResource resource = new ArbitraryDataResource(name, ResourceIdType.NAME, service, identifier);
|
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) {
|
if (metadata != null) {
|
||||||
return new String(metadata, StandardCharsets.UTF_8);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -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()) {
|
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||||
// Find latest transaction
|
// Find latest transaction
|
||||||
ArbitraryTransactionData latestTransaction = repository.getArbitraryRepository()
|
ArbitraryTransactionData latestTransaction = repository.getArbitraryRepository()
|
||||||
@ -87,7 +87,7 @@ public class ArbitraryMetadataManager {
|
|||||||
byte[] metadataHash = latestTransaction.getMetadataHash();
|
byte[] metadataHash = latestTransaction.getMetadataHash();
|
||||||
if (metadataHash == null) {
|
if (metadataHash == null) {
|
||||||
// This resource doesn't have metadata
|
// This resource doesn't have metadata
|
||||||
return null;
|
throw new IllegalArgumentException("This resource doesn't have metadata");
|
||||||
}
|
}
|
||||||
|
|
||||||
ArbitraryDataFile metadataFile = ArbitraryDataFile.fromHash(metadataHash, signature);
|
ArbitraryDataFile metadataFile = ArbitraryDataFile.fromHash(metadataHash, signature);
|
||||||
@ -97,7 +97,7 @@ public class ArbitraryMetadataManager {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Request from network
|
// Request from network
|
||||||
this.fetchArbitraryMetadata(latestTransaction);
|
return this.fetchArbitraryMetadata(latestTransaction, useRateLimiter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,20 +111,25 @@ public class ArbitraryMetadataManager {
|
|||||||
|
|
||||||
// Request metadata from network
|
// 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();
|
byte[] signature = arbitraryTransactionData.getSignature();
|
||||||
String signature58 = Base58.encode(signature);
|
String signature58 = Base58.encode(signature);
|
||||||
|
|
||||||
// Require an NTP sync
|
// Require an NTP sync
|
||||||
Long now = NTP.getTime();
|
Long now = NTP.getTime();
|
||||||
if (now == null) {
|
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 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);
|
LOGGER.trace("Skipping metadata request for signature {} due to rate limit", signature58);
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
this.addToSignatureRequests(signature58, true, false);
|
this.addToSignatureRequests(signature58, true, false);
|
||||||
|
|
||||||
@ -162,14 +167,22 @@ public class ArbitraryMetadataManager {
|
|||||||
|
|
||||||
requestEntry = arbitraryMetadataRequests.get(id);
|
requestEntry = arbitraryMetadataRequests.get(id);
|
||||||
if (requestEntry == null)
|
if (requestEntry == null)
|
||||||
return false;
|
return null;
|
||||||
|
|
||||||
if (requestEntry.getA() == null)
|
if (requestEntry.getA() == null)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
totalWait += singleWait;
|
totalWait += singleWait;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
try {
|
||||||
|
ArbitraryDataFile metadataFile = ArbitraryDataFile.fromHash(metadataHash, signature);
|
||||||
|
return metadataFile.getBytes();
|
||||||
|
} catch (DataException e) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user