API refactors to avoid generic unhandled states.

This commit is contained in:
CalDescent 2021-07-04 13:38:20 +01:00
parent 60415b9222
commit a742fecf9c
2 changed files with 86 additions and 86 deletions

View File

@ -275,50 +275,48 @@ public class ArbitraryResource {
LOGGER.info("Whole file digest: {}", dataFile.base58Digest()); LOGGER.info("Whole file digest: {}", dataFile.base58Digest());
int chunkCount = dataFile.split(DataFile.CHUNK_SIZE); int chunkCount = dataFile.split(DataFile.CHUNK_SIZE);
if (chunkCount > 0) { if (chunkCount == 0) {
LOGGER.info(String.format("Successfully split into %d chunk%s", chunkCount, (chunkCount == 1 ? "" : "s"))); LOGGER.error("No chunks created");
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA);
String base58Digest = dataFile.base58Digest(); }
if (base58Digest != null) { LOGGER.info(String.format("Successfully split into %d chunk%s", chunkCount, (chunkCount == 1 ? "" : "s")));
AccountData accountData = repository.getAccountRepository().getAccount(creatorAddress);
if (accountData == null || accountData.getPublicKey() == null) {
dataFile.deleteAll();
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.ADDRESS_UNKNOWN);
}
byte[] creatorPublicKey = accountData.getPublicKey();
byte[] lastReference = accountData.getReference();
BaseTransactionData baseTransactionData = new BaseTransactionData(NTP.getTime(), Group.NO_GROUP,
lastReference, creatorPublicKey, BlockChain.getInstance().getUnitFee(), null);
int size = (int)dataFile.size();
ArbitraryTransactionData.DataType dataType = ArbitraryTransactionData.DataType.DATA_HASH;
byte[] digest = dataFile.digest();
byte[] chunkHashes = dataFile.chunkHashes();
List<PaymentData> payments = new ArrayList<>();
ArbitraryTransactionData transactionData = new ArbitraryTransactionData(baseTransactionData,
5, 2, 0, size, digest, dataType, chunkHashes, payments);
ArbitraryTransaction transaction = (ArbitraryTransaction) Transaction.fromData(repository, transactionData);
transaction.computeNonce();
Transaction.ValidationResult result = transaction.isValidUnconfirmed();
if (result != Transaction.ValidationResult.OK) {
dataFile.deleteAll();
throw TransactionsResource.createTransactionInvalidException(request, result);
}
byte[] bytes = ArbitraryTransactionTransformer.toBytes(transactionData);
return Base58.encode(bytes);
}
// Something went wrong, so delete our copies of the data and chunks
dataFile.deleteAll();
String base58Digest = dataFile.base58Digest();
if (base58Digest == null) {
LOGGER.error("Unable to calculate digest");
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA);
} }
return "false"; AccountData accountData = repository.getAccountRepository().getAccount(creatorAddress);
if (accountData == null || accountData.getPublicKey() == null) {
dataFile.deleteAll();
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.ADDRESS_UNKNOWN);
}
byte[] creatorPublicKey = accountData.getPublicKey();
byte[] lastReference = accountData.getReference();
BaseTransactionData baseTransactionData = new BaseTransactionData(NTP.getTime(), Group.NO_GROUP,
lastReference, creatorPublicKey, BlockChain.getInstance().getUnitFee(), null);
int size = (int)dataFile.size();
ArbitraryTransactionData.DataType dataType = ArbitraryTransactionData.DataType.DATA_HASH;
byte[] digest = dataFile.digest();
byte[] chunkHashes = dataFile.chunkHashes();
List<PaymentData> payments = new ArrayList<>();
ArbitraryTransactionData transactionData = new ArbitraryTransactionData(baseTransactionData,
5, 2, 0, size, digest, dataType, chunkHashes, payments);
ArbitraryTransaction transaction = (ArbitraryTransaction) Transaction.fromData(repository, transactionData);
transaction.computeNonce();
Transaction.ValidationResult result = transaction.isValidUnconfirmed();
if (result != Transaction.ValidationResult.OK) {
dataFile.deleteAll();
throw TransactionsResource.createTransactionInvalidException(request, result);
}
byte[] bytes = ArbitraryTransactionTransformer.toBytes(transactionData);
return Base58.encode(bytes);
} catch (DataException e) { } catch (DataException e) {
LOGGER.error("Repository issue when uploading data", e); LOGGER.error("Repository issue when uploading data", e);

View File

@ -95,52 +95,54 @@ public class WebsiteResource {
} }
DataFile dataFile = this.hostWebsite(path); DataFile dataFile = this.hostWebsite(path);
if (dataFile != null) { if (dataFile == null) {
String base58Digest = dataFile.base58Digest(); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA);
if (base58Digest != null) { }
try (final Repository repository = RepositoryManager.getRepository()) {
String base58Digest = dataFile.base58Digest();
AccountData accountData = repository.getAccountRepository().getAccount(creatorAddress); if (base58Digest != null) {
if (accountData == null || accountData.getPublicKey() == null) { LOGGER.error("Unable to calculate digest");
dataFile.deleteAll(); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA);
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.ADDRESS_UNKNOWN); }
}
byte[] creatorPublicKey = accountData.getPublicKey(); try (final Repository repository = RepositoryManager.getRepository()) {
byte[] lastReference = accountData.getReference();
AccountData accountData = repository.getAccountRepository().getAccount(creatorAddress);
BaseTransactionData baseTransactionData = new BaseTransactionData(NTP.getTime(), Group.NO_GROUP, if (accountData == null || accountData.getPublicKey() == null) {
lastReference, creatorPublicKey, BlockChain.getInstance().getUnitFee(), null); dataFile.deleteAll();
int size = (int)dataFile.size(); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.ADDRESS_UNKNOWN);
ArbitraryTransactionData.DataType dataType = ArbitraryTransactionData.DataType.DATA_HASH; }
byte[] digest = dataFile.digest(); byte[] creatorPublicKey = accountData.getPublicKey();
byte[] chunkHashes = dataFile.chunkHashes(); byte[] lastReference = accountData.getReference();
List<PaymentData> payments = new ArrayList<>();
BaseTransactionData baseTransactionData = new BaseTransactionData(NTP.getTime(), Group.NO_GROUP,
ArbitraryTransactionData transactionData = new ArbitraryTransactionData(baseTransactionData, lastReference, creatorPublicKey, BlockChain.getInstance().getUnitFee(), null);
5, 2, 0, size, digest, dataType, chunkHashes, payments); int size = (int)dataFile.size();
ArbitraryTransactionData.DataType dataType = ArbitraryTransactionData.DataType.DATA_HASH;
ArbitraryTransaction transaction = (ArbitraryTransaction) Transaction.fromData(repository, transactionData); byte[] digest = dataFile.digest();
transaction.computeNonce(); byte[] chunkHashes = dataFile.chunkHashes();
List<PaymentData> payments = new ArrayList<>();
Transaction.ValidationResult result = transaction.isValidUnconfirmed();
if (result != Transaction.ValidationResult.OK) { ArbitraryTransactionData transactionData = new ArbitraryTransactionData(baseTransactionData,
dataFile.deleteAll(); 5, 2, 0, size, digest, dataType, chunkHashes, payments);
throw TransactionsResource.createTransactionInvalidException(request, result);
} ArbitraryTransaction transaction = (ArbitraryTransaction) Transaction.fromData(repository, transactionData);
transaction.computeNonce();
byte[] bytes = ArbitraryTransactionTransformer.toBytes(transactionData);
return Base58.encode(bytes); Transaction.ValidationResult result = transaction.isValidUnconfirmed();
if (result != Transaction.ValidationResult.OK) {
} catch (TransformationException e) { dataFile.deleteAll();
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.TRANSFORMATION_ERROR, e); throw TransactionsResource.createTransactionInvalidException(request, result);
} catch (DataException e) { }
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
} byte[] bytes = ArbitraryTransactionTransformer.toBytes(transactionData);
} return Base58.encode(bytes);
// Something went wrong, so delete our copies of the data and chunks
dataFile.deleteAll(); } catch (TransformationException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.TRANSFORMATION_ERROR, e);
} catch (DataException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
} }
return "false";
} }
@POST @POST