3
0
mirror of https://github.com/Qortal/qortal.git synced 2025-02-11 17:55:50 +00:00

more refactoring

This commit is contained in:
Kc 2018-06-09 00:29:21 +02:00
parent 749baf8843
commit 0f16b1588c
13 changed files with 75 additions and 31 deletions

View File

@ -4,7 +4,7 @@ import java.math.BigDecimal;
import qora.account.PublicKeyAccount;
public class Block implements IBlockData {
public class Block implements BlockData {
private int version;
private byte[] reference;
private int transactionCount;

View File

@ -2,7 +2,7 @@ package data.block;
import java.math.BigDecimal;
public interface IBlockData {
public interface BlockData {
public int getVersion();
public byte[] getReference();
public int getTransactionCount();

View File

@ -1,9 +0,0 @@
package data.repository;
import data.block.IBlockData;
public interface IRepository {
// XXX use NoDataFoundException?
IBlockData getBlockBySignature(byte[] signature) throws Exception;
IBlockData getBlockByHeight(int height) throws Exception;
}

View File

@ -11,6 +11,8 @@ import static java.util.stream.Collectors.toMap;
public abstract class Transaction {
// Transaction types
// TODO Transaction types are semantic and should go into the business logic layer.
// No need to know the meaning of the integer value in data layer
public enum TransactionType {
GENESIS(1), PAYMENT(2), REGISTER_NAME(3), UPDATE_NAME(4), SELL_NAME(5), CANCEL_SELL_NAME(6), BUY_NAME(7), CREATE_POLL(8), VOTE_ON_POLL(9), ARBITRARY(
10), ISSUE_ASSET(11), TRANSFER_ASSET(12), CREATE_ASSET_ORDER(13), CANCEL_ASSET_ORDER(14), MULTIPAYMENT(15), DEPLOY_AT(16), MESSAGE(17);
@ -29,7 +31,8 @@ public abstract class Transaction {
}
// Properties shared with all transaction types
protected TransactionType type;
protected TransactionType type;
// TODO PublicKeyAccount is a separate data entity, so here should only be a key to reference it
protected PublicKeyAccount creator;
protected long timestamp;
protected byte[] reference;

View File

@ -6,19 +6,19 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import data.block.IBlockData;
import data.repository.HSQLDBRepository;
import data.repository.IRepository;
import data.block.BlockData;
import database.DB;
import database.NoDataFoundException;
import qora.account.PublicKeyAccount;
import qora.transaction.Transaction;
import qora.transaction.TransactionFactory;
import repository.BlockRepository;
import repository.hsqldb.HSQLDBBlockRepository;
public class BlockFactory {
// XXX repository should be pushed here from the root entry, no need to know the repository type
private static IRepository repository = new HSQLDBRepository();
private static BlockRepository repository = new HSQLDBBlockRepository();
/**
* Load Block from DB using block signature.
@ -52,7 +52,7 @@ public class BlockFactory {
return GenesisBlock.getInstance();
try {
IBlockData data = repository.getBlockByHeight(height);
BlockData data = repository.fromHeight(height);
// TODO fill this list from TransactionFactory
List<Transaction> transactions = new ArrayList<Transaction>();

View File

@ -0,0 +1,8 @@
package repository;
import data.block.BlockData;
public interface BlockRepository {
BlockData fromSignature(byte[] signature) throws DataException;
BlockData fromHeight(int height) throws DataException;
}

View File

@ -0,0 +1,22 @@
package repository;
public class DataException extends Exception {
private static final long serialVersionUID = -3963965667288257605L;
public DataException() {}
public DataException(String message)
{
super(message);
}
public DataException(String message, Throwable cause) {
super(message, cause);
}
public DataException(Throwable cause) {
super(cause);
}
}

View File

@ -3,9 +3,13 @@ package repository;
public abstract class Repository {
protected TransactionRepository transactionRepository;
protected BlockRepository blockRepository;
public TransactionRepository getTransactionRepository() {
return this.transactionRepository;
}
public BlockRepository getBlockRepository() {
return this.blockRepository;
}
}

View File

@ -12,4 +12,8 @@ public abstract class RepositoryManager {
return repository.transactionRepository;
}
public static BlockRepository getBlockRepository() {
return repository.blockRepository;
}
}

View File

@ -1,4 +1,4 @@
package data.repository;
package repository.hsqldb;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
@ -7,11 +7,13 @@ import java.sql.SQLException;
import java.sql.Timestamp;
import data.block.Block;
import data.block.IBlockData;
import data.block.BlockData;
import database.DB;
import qora.account.PublicKeyAccount;
import repository.BlockRepository;
import repository.DataException;
public class HSQLDBRepository implements IRepository
public class HSQLDBBlockRepository implements BlockRepository
{
protected static final int TRANSACTIONS_SIGNATURE_LENGTH = 64;
protected static final int GENERATOR_SIGNATURE_LENGTH = 64;
@ -20,19 +22,29 @@ public class HSQLDBRepository implements IRepository
private static final String BLOCK_DB_COLUMNS = "version, reference, transaction_count, total_fees, "
+ "transactions_signature, height, generation, generating_balance, generator, generator_signature, AT_data, AT_fees";
public IBlockData getBlockBySignature(byte[] signature) throws SQLException
public BlockData fromSignature(byte[] signature) throws DataException
{
ResultSet rs = DB.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE signature = ?", signature);
ResultSet rs;
try {
rs = DB.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE signature = ?", signature);
} catch (SQLException e) {
throw new DataException("Error loading data from DB", e);
}
return getBlockFromResultSet(rs);
}
public IBlockData getBlockByHeight(int height) throws SQLException
public BlockData fromHeight(int height) throws DataException
{
ResultSet rs = DB.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE height = ?", height);
ResultSet rs;
try {
rs = DB.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE height = ?", height);
} catch (SQLException e) {
throw new DataException("Error loading data from DB", e);
}
return getBlockFromResultSet(rs);
}
private IBlockData getBlockFromResultSet(ResultSet rs) throws SQLException {
private BlockData getBlockFromResultSet(ResultSet rs) throws DataException {
int version = rs.getInt(1);
byte[] reference = DB.getResultSetBytes(rs.getBinaryStream(2), REFERENCE_LENGTH);
int transactionCount = rs.getInt(3);

View File

@ -10,7 +10,7 @@ import data.transaction.GenesisTransaction;
import data.transaction.Transaction;
import database.DB;
public class HSQLDBGenesisTransaction extends HSQLDBTransaction {
public class HSQLDBGenesisTransactionRepository extends HSQLDBTransactionRepository {
Transaction fromBase(byte[] signature, byte[] reference, PublicKeyAccount creator, long timestamp, BigDecimal fee) {
try {

View File

@ -5,7 +5,7 @@ import repository.Repository;
public class HSQLDBRepository extends Repository {
public HSQLDBRepository() {
this.transactionRepository = new HSQLDBTransaction();
this.transactionRepository = new HSQLDBTransactionRepository();
}
}

View File

@ -12,12 +12,12 @@ import database.DB;
import qora.block.Block;
import repository.TransactionRepository;
public class HSQLDBTransaction implements TransactionRepository {
public class HSQLDBTransactionRepository implements TransactionRepository {
private HSQLDBGenesisTransaction genesisTransactionRepository;
private HSQLDBGenesisTransactionRepository genesisTransactionRepository;
public HSQLDBTransaction() {
genesisTransactionRepository = new HSQLDBGenesisTransaction();
public HSQLDBTransactionRepository() {
genesisTransactionRepository = new HSQLDBGenesisTransactionRepository();
}
public Transaction fromSignature(byte[] signature) {