Invalidate the cache in ArbitraryTransaction.onImportAsUnconfirmed() if we have the local data

This ensures that the local copy of a resource updates as soon as the transaction is broadcast.
This commit is contained in:
CalDescent 2021-11-03 21:33:29 +00:00
parent a61b0685f0
commit 6d031130b9
2 changed files with 32 additions and 18 deletions

View File

@ -385,6 +385,24 @@ public class ArbitraryDataManager extends Thread {
this.arbitraryDataCachedResources.put(resourceId, timestamp);
}
public void invalidateCache(ArbitraryTransactionData arbitraryTransactionData) {
if (arbitraryTransactionData.getName() != null) {
String resourceId = arbitraryTransactionData.getName().toLowerCase();
LOGGER.info("We have all data for transaction {}", Base58.encode(arbitraryTransactionData.getSignature()));
LOGGER.info("Clearing cache for name {}...", arbitraryTransactionData.getName());
if (this.arbitraryDataCachedResources.containsKey(resourceId)) {
this.arbitraryDataCachedResources.remove(resourceId);
}
// Also remove from the failed builds queue in case it previously failed due to missing chunks
ArbitraryDataBuildManager buildManager = ArbitraryDataBuildManager.getInstance();
if (buildManager.arbitraryDataFailedBuilds.containsKey(resourceId)) {
buildManager.arbitraryDataFailedBuilds.remove(resourceId);
}
}
}
// Network handlers
@ -502,21 +520,7 @@ public class ArbitraryDataManager extends Thread {
// We have all the chunks for this transaction, so we should invalidate the transaction's name's
// data cache so that it is rebuilt the next time we serve it
if (arbitraryTransactionData.getName() != null) {
String resourceId = arbitraryTransactionData.getName().toLowerCase();
LOGGER.info("We have all data for transaction {}", Base58.encode(transactionData.getSignature()));
LOGGER.info("Clearing cache for name {}...", arbitraryTransactionData.getName());
if (this.arbitraryDataCachedResources.containsKey(resourceId)) {
this.arbitraryDataCachedResources.remove(resourceId);
}
// Also remove from the failed builds queue in case it previously failed due to missing chunks
ArbitraryDataBuildManager buildManager = ArbitraryDataBuildManager.getInstance();
if (buildManager.arbitraryDataFailedBuilds.containsKey(resourceId)) {
buildManager.arbitraryDataFailedBuilds.remove(resourceId);
}
}
invalidateCache(arbitraryTransactionData);
// We also need to broadcast to the network that we are now hosting files for this transaction
// Use a null peer address to indicate our own

View File

@ -6,6 +6,7 @@ import java.util.Objects;
import java.util.stream.Collectors;
import org.qortal.account.Account;
import org.qortal.controller.arbitrary.ArbitraryDataManager;
import org.qortal.crypto.Crypto;
import org.qortal.crypto.MemoryPoW;
import org.qortal.data.PaymentData;
@ -212,6 +213,16 @@ public class ArbitraryTransaction extends Transaction {
arbitraryTransactionData.getFee());
}
@Override
protected void onImportAsUnconfirmed() throws DataException {
// Invalidate the cache for this name if we have the data already
if (arbitraryTransactionData.getName() != null) {
if (isDataLocal()) {
ArbitraryDataManager.getInstance().invalidateCache(arbitraryTransactionData);
}
}
}
@Override
public void preProcess() throws DataException {
// Nothing to do
@ -252,10 +263,9 @@ public class ArbitraryTransaction extends Transaction {
/** Returns arbitrary data payload, fetching from network if needed. Can block for a while! */
public byte[] fetchData() throws DataException {
// If local, read from file
if (isDataLocal())
if (isDataLocal()) {
return this.repository.getArbitraryRepository().fetchData(this.transactionData.getSignature());
// TODO If not local, attempt to fetch via network?
}
return null;
}