Files
qortal/src/transform/transaction/TransactionTransformer.java
catbref 3d78d5dad9 More refactoring - DOES NOT COMPILE
qora.* packages are business logic/handler/processing
data.* packages are "value objects" or are they "business objects"?

toBytes(), fromBytes() (which used to be called parse()) and toJSON() moved to transform.* packages

new issues:

Lost control of SQL Transactions. Previously, some class "knew" whether to call COMMIT or not.
e.g. simply saving a payment transaction would involve updating Transactions table first, then the PaymentTransactions table after (to satisfy foreign key constraints) then commit.
Processing a block would involve a new transaction, a savepoint, a rollback and then maybe a further commit or rollback.
Not sure how this is going to work with the repository, especially if business logic isn't supposed to be aware of such things.

Growing number of stupid try-catch blocks. Probably best to ditch TransformationException (was ParseException) and throw IllegalStateExceptions instead as they're "unchecked".

What happens if the repository fails to save() to the database? It can't throw SQLException any more as that has no meaning outside the repository. Ditto with delete().
2018-06-08 17:40:27 +01:00

89 lines
2.3 KiB
Java

package transform.transaction;
import java.nio.ByteBuffer;
import org.json.simple.JSONObject;
import data.transaction.Transaction;
import data.transaction.Transaction.TransactionType;
import transform.TransformationException;
import transform.Transformer;
import utils.Base58;
public class TransactionTransformer extends Transformer {
protected static final int TYPE_LENGTH = INT_LENGTH;
public static Transaction fromBytes(byte[] bytes) throws TransformationException {
if (bytes == null)
return null;
if (bytes.length < TYPE_LENGTH)
throw new TransformationException("Byte data too short to determine transaction type");
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
TransactionType type = TransactionType.valueOf(byteBuffer.getInt());
if (type == null)
return null;
switch (type) {
case GENESIS:
return GenesisTransactionTransformer.fromByteBuffer(byteBuffer);
default:
return null;
}
}
public static int getDataLength(Transaction transaction) throws TransformationException {
switch (transaction.getType()) {
case GENESIS:
return GenesisTransactionTransformer.getDataLength(transaction);
default:
throw new TransformationException("Unsupported transaction type");
}
}
public static byte[] toBytes(Transaction transaction) throws TransformationException {
switch (transaction.getType()) {
case GENESIS:
return GenesisTransactionTransformer.toBytes(transaction);
default:
return null;
}
}
public static JSONObject toJSON(Transaction transaction) throws TransformationException {
switch (transaction.getType()) {
case GENESIS:
return GenesisTransactionTransformer.toJSON(transaction);
default:
return null;
}
}
@SuppressWarnings("unchecked")
static JSONObject getBaseJSON(Transaction transaction) {
JSONObject json = new JSONObject();
json.put("type", transaction.getType().value);
json.put("fee", transaction.getFee().toPlainString());
json.put("timestamp", transaction.getTimestamp());
json.put("signature", Base58.encode(transaction.getSignature()));
byte[] reference = transaction.getReference();
if (reference != null)
json.put("reference", Base58.encode(reference));
// XXX Can't do this as it requires database access:
// json.put("confirmations", RepositoryManager.getTransactionRepository.getConfirmations(transaction));
return json;
}
}